我正在使用kivy,我不知道如何创建事件以使块中断



我不知道如何设置一个事件,这样当我的乒乓球碰到障碍时,它就会被禁用或离开屏幕。有人能帮我吗?我很新,我看了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()

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

我将visible添加到类Block:

class Block(Widget):
    visible = BooleanProperty(True)
    # the rest of the code

然后当CCD_ 2与CCD_

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)

设置块的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

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

相关内容

  • 没有找到相关文章

最新更新