0

我不知道如何设置一个事件,以便当我的乒乓球击中障碍物时,它会被禁用或离开屏幕。有人能帮我吗。我很新,我看那里的 API,但这让我很困惑。帮助将不胜感激。

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()
4

2 回答 2

0

这不是优雅的解决方案 - 我不使用事件。

我添加visible到类块:

class Block(Widget):

    visible = BooleanProperty(True)

    # the rest of the code

然后我block在它碰撞时移除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)
于 2013-11-20T11:20:41.267 回答
0

设置块的 x 或 y 坐标,以便将其绘制到屏幕外(即,根本不绘制)。首先,如果适用,请保存您的 y 坐标,以便稍后检索它,以恢复您的块:

# Save old y setting for later retrieval.
root.saved_y = self.block.y 
# Now set y so the block is moved offscreen.
self.block.y = 5000

(我已经测试过这个解决方案;它有效。)

于 2014-11-19T15:18:17.010 回答