我不知道如何设置一个事件,以便当我的乒乓球击中障碍物时,它会被禁用或离开屏幕。有人能帮我吗。我很新,我看那里的 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()