本文整理汇总了Python中ball.Ball.respawn方法的典型用法代码示例。如果您正苦于以下问题:Python Ball.respawn方法的具体用法?Python Ball.respawn怎么用?Python Ball.respawn使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ball.Ball
的用法示例。
在下文中一共展示了Ball.respawn方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from ball import Ball [as 别名]
# 或者: from ball.Ball import respawn [as 别名]
class SoccerGame:
"""Define game behavior for the soccer gamemode.
Red team is 0, and blue team is 1.
"""
blue_team_score = 0
red_team_score = 0
win_score = 3
_ball = None
announce_win_timer = -1
winner = -1
def __init__(self):
self._ball = Ball()
Map.current_map.objects.add(self._ball)
self.goal_sound = pygame.mixer.Sound(os.path.join("res", "goal.wav"))
self.blue_team_ships = []
self.red_team_ships = []
def get_spawn_pos(self, ship):
"""Return x, y, angle of the spawn position for a ship given their team."""
if ship.team == 0:
return Map.current_map.rect.w / 2 - 50, \
Map.current_map.rect.h / 3 * (1 + self.blue_team_ships.index(ship)), \
270
elif ship.team == 1:
return Map.current_map.rect.w / 2 + 50, \
Map.current_map.rect.h / 3 * (1 + self.red_team_ships.index(ship)), \
90
else:
raise Exception("Couldn't determine ship team to spawn ship!")
def update(self):
team_scored = self._ball.team_scored
if team_scored > -1:
if team_scored == 0:
self.blue_team_score += 1
elif team_scored == 1:
self.red_team_score += 1
self.goal_sound.play()
self._ball.respawn()
if self.blue_team_score == self.win_score:
self.announce_win(0)
self.winner = 0
elif self.red_team_score == self.win_score:
self.announce_win(1)
self.winner = 1
if self.announce_win_timer > 0:
self.announce_win_timer -= 1
elif self.announce_win_timer == 0:
# End the game
pygame.event.post(pygame.event.Event(events.END_GAME, {
"called_by": self, "outcome": self.winner}))
self.announce_win_timer -= 1
@property
def objective(self):
"""Return the main objective of the game (the ball)."""
return self._ball
def new_hud(self, width, height, camera):
"""Return the HUD elements suggested for the gamemode."""
elements = pygame.sprite.Group()
#elements.add(HUDArrowElement(camera, lambda: self._ball.rect.center, False))
return elements
def new_global_hud(self, width, height):
"""Return the HUD elements for a global HUD
as suggested by the gamemode."""
elements = pygame.sprite.Group()
elements.add(HUDScoreElement((round(width * .33), 35), lambda: self.blue_team_score))
elements.add(HUDScoreElement((round(width * .66), 35), lambda: self.red_team_score))
self.hud_win = HUDWinMessage((width // 2 - 100, height // 2))
elements.add(self.hud_win)
return elements
def respawn_all(self):
for ship in self.red_team_ships + self.blue_team_ships:
ship.respawn()
self._ball.respawn()
def request_change_team(self, ship, requested):
"""Check for team balance, then assign a team to the ship."""
if len(self.red_team_ships) - 1 > len(self.blue_team_ships):
# Too many people in red team. Assign to blue instead.
requested = 0
elif len(self.blue_team_ships) - 1 > len(self.red_team_ships):
# and vice versa.
requested = 1
else:
# If no problems, just fulfill the request.
pass
#.........这里部分代码省略.........