I use kivy and I do not know how to create the event to block the block

advertisements

I don't know how to set up a event so that when my pong ball hits block it will be disabled or be gone off screen. Can someone help me. I'm very new and I look on there API but it just confused me a lot. Help would be much appreciated.

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty
from kivy.vector import Vector
from kivy.clock import Clock

class Block(Widget):

    score = NumericProperty(0)

    def bounce_ball(self, ball):
        if self.collide_widget(ball):
            vx, vy = ball.velocity
            offset = (ball.center_y - self.center_y) / (self.height / 2)
            bounced = Vector(-1 * vx, vy)
            vel = bounced * 1.0
            ball.velocity = vel.y, vel.x + offset
            self.dispatch

class PongPaddle(Widget):
    score = NumericProperty(0)

    def bounce_ball(self, ball):
        if self.collide_widget(ball):
            vx, vy = ball.velocity
            offset = (ball.center_y - self.center_y) / (self.height / 2)
            bounced = Vector(-1 * vx, vy)
            vel = bounced * 1.0
            ball.velocity = vel.y, vel.x + offset

class PongBall(Widget):

    ball = image
    velocity_x = NumericProperty(0)
    velocity_y = NumericProperty(0)
    velocity = ReferenceListProperty(velocity_x, velocity_y)

    def move(self):
        self.pos = Vector(*self.velocity) + self.pos

class PongGame(Widget):

    brick = ObjectProperty(None)
    ball = ObjectProperty(None)
    player1 = ObjectProperty(None)
    block = ObjectProperty(None)

    def serve_ball(self, vel=(0, 4)):
        self.ball.center = self.center
        self.ball.velocity = vel

    def update(self, dt):
        self.ball.move()

        #bounce of paddles
        self.player1.bounce_ball(self.ball)
        if self.block.bounce_ball(self.ball): self.dispatch

        #bounce ball off bottom or top
        if (self.ball.top > self.top):
            self.ball.velocity_y *= -1
             #bounce ball off bottom or top
        if (self.ball.x < 0) or (self.ball.right > self.width):
            self.ball.velocity_x *= -1

    def on_touch_move(self, touch):
        if touch.x > self.width / 12:
            self.player1.center_x = touch.x

class PongApp(App):
    def build(self):
        game = PongGame()
        game.serve_ball()
        Clock.schedule_interval(game.update, 1.0 / 30.0)

        return game

if __name__ == '__main__':
    PongApp().run()


It is not elegant solution - I don't use event.

I add visible to class Block:

class Block(Widget):

    visible = BooleanProperty(True)

    # the rest of the code

and then I remove block when it collide with ball

def update(self, dt):
    self.ball.move()

    #bounce of paddles
    self.player1.bounce_ball(self.ball)

    #bounce ball off bottom or top
    if (self.ball.top > self.top):
        self.ball.velocity_y *= -1

    #bounce ball off bottom or top
    if (self.ball.x < 0) or (self.ball.right > self.width):
        self.ball.velocity_x *= -1

    # remove block when collide with ball
    if self.block.visible and self.block.collide_widget(self.ball):
        self.block.bounce_ball(self.ball)
        self.block.visible = False
        self.remove_widget(self.block)