本文整理汇总了Python中ball.Ball.checkCollision方法的典型用法代码示例。如果您正苦于以下问题:Python Ball.checkCollision方法的具体用法?Python Ball.checkCollision怎么用?Python Ball.checkCollision使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ball.Ball
的用法示例。
在下文中一共展示了Ball.checkCollision方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Pong
# 需要导入模块: from ball import Ball [as 别名]
# 或者: from ball.Ball import checkCollision [as 别名]
class Pong(Engine):
def __init__(self):
Engine.__init__(self, 320, 240, (00, 150, 150))
self.fps_text = None
self.scoreP1_text = None
self.scoreP1 = 0
self.scoreP2_text = None
self.scoreP2 = 0
self.ball = None
self.playerOne = None
self.playerTwo = None
self.fps_update = 0
self.fps_count = 0
self.sfx_beep = None
self.sfx_padOne = None
self.sfx_padTwo = None
self.music_bg = None
self.winner_text = None
self.start_game_text = None
self.max_score = 7
def on_init(self):
Engine.on_init(self)
self.winner_text = Text(self.width/4, 80, str(""), 30)
self.add(self.winner_text)
self.start_game_text = Text(self.width/3, 120, str("Press START"), 30)
self.add(self.start_game_text)
self.scoreP1_text = Text(self.width/4, 30, str(self.scoreP1), 50)
self.scoreP2_text = Text(self.width - self.width/4, 30, str(self.scoreP2), 50)
self.add(self.scoreP1_text)
self.add(self.scoreP2_text)
self.fps_text = Text(280, 220, "30")
self.add(self.fps_text)
self.ball = Ball(self.width/2, self.height/2, "ball.png")
self.add(self.ball)
self.playerOne = Paddle(20, self.height/2, "paddle.png")
self.add(self.playerOne)
self.playerTwo = Paddle(320 - 20, self.height/2, "paddle.png")
self.add(self.playerTwo)
self.sfx_padOne = soundEffect("blip1.wav")
self.sfx_padTwo = soundEffect("blip2.wav")
self.music_bg = musicPlayer("UGH1.wav", -1)
self.music_bg.play()
self.start_game(False)
def start_game(self, active):
self.fps_text.set_active(active)
self.scoreP1_text.set_active(active)
self.scoreP1 = 0
self.scoreP2_text.set_active(active)
self.scoreP2 = 0
self.ball.set_active(active)
self.playerOne.set_active(active)
self.playerTwo.set_active(active)
self.fps_update = 0
self.fps_count = 0
self.start_game_text.set_active(not active)
self.winner_text.set_active(not active)
def show_end_game(self):
self.ball.set_active(False)
self.playerOne.set_active(True)
self.playerTwo.set_active(True)
self.fps_update = 0
self.fps_count = 0
self.winner_text.set_active(True)
self.start_game_text.set_active(True)
if self.scoreP1 > self.scoreP2:
self.winner_text.set_text("Player 1 Wins")
else:
self.winner_text.set_text("Player 2 Wins")
def update(self, delta_time):
self.fps_update += 1
self.fps_count += self.delta_time
if self.fps_update >= 20:
self.fps_count /= 20
self.fps_text.set_text(str(int(1.0/(float(self.fps_count)/1000))))
self.fps_count = 0
self.fps_update = 0
self.handleInput()
if self.scoreP1 == self.max_score or self.scoreP2 == self.max_score:
self.show_end_game()
else:
self.checkCollision()
def checkCollision(self):
if self.ball.checkCollision(self.playerOne.getRect()):
self.sfx_padOne.play_sound()
self.ball.negateXVel()
if self.ball.checkCollision(self.playerTwo.getRect()):
self.sfx_padTwo.play_sound()
#.........这里部分代码省略.........