使用 pygame.time.get_ticks() 显示剩余时间
Using pygame.time.get_ticks() to display remaining time
我目前正在寻找在我的记分牌上显示能量提升的剩余时间。我正在使用 pygame.time.get_ticks() 来确定剩余的加电时间,它似乎按预期运行。当收到一个能量提升时,它会持续 5 秒(5000 ticks),除非再次获得相同的能量提升,否则时间会再延长 5 秒。这是由我的功率水平决定的;拾取能量提升 1 级能量提升,一旦 5 秒过去,能量等级降低 1。
我无法弄清楚如何让它正确显示在我的记分牌上,因为目前它只显示 -5000,这是我的开始启动时间 (0) 减去每个级别允许的启动时间(5000)。接通电源后,时间根本不会改变,只是保持在 -5000。
main.py
class AlienInvasion:
"""Overall class to manage game assets and behavior."""
def __init__(self):
"""Initlize the game, and create game resources."""
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
self.settings.screen_width = self.screen.get_rect().width
self.settings.screen_height = self.screen.get_rect().height
pygame.display.set_caption("Alien Invasion")
self.stats = GameStats(self)
self.sb = Scoreboard(self)
self.ship = Ship(self)
self._wall_setup()
self._create_multiple_walls(*self.wall_x_positions,
x_start=self.settings.screen_width / 8, y_start=850)
self._create_groups()
self._create_fleet()
self._create_buttons()
def run_game(self):
"""Start main loop for our game."""
while True:
self._check_events()
if self.stats.game_active:
self.ship.update()
self._update_bullets()
self._check_bomb_ship_collisions()
self._update_aliens()
self._check_power_time()
self._update_screen()
def _check_pow_collisions(self):
collisions = pygame.sprite.spritecollide(
self.ship, self.powerups, True
)
for collosion in collisions:
if collosion.type == 'gun':
self.settings.ship_power += 1
self.settings.powerup_time = pygame.time.get_ticks()
self._check_power_time()
if collosion.type == 'shield' and self.stats.ships_left < 3:
self.stats.ships_left += 1
self.sb.prep_ships()
def _check_power_time(self):
if (self.settings.ship_power >= 2 and pygame.time.get_ticks() -
self.settings.powerup_time > self.settings.POWERUP_TIME_ALLOWED):
self.settings.ship_power -= 1
self.settings.powerup_time = pygame.time.get_ticks()
def _update_screen(self):
"""Update images on screen and flip to the new screen."""
# fill our background with our bg_color
self.screen.fill(self.settings.bg_color)
# draw scoreboard to screen
self.sb.show_score()
# draw ship to screen
self.ship.blitme()
for bullet in self.bullets.sprites():
bullet.draw_bullet()
self.alien_bombs.update()
self.alien_bombs.draw(self.screen)
self.aliens.draw(self.screen)
self.powerups.draw(self.screen)
self.powerups.update()
self._check_pow_collisions()
self.blocks.draw(self.screen)
self._check_wall_collisions()
# draw play button if game is inactive
if not self.stats.game_active:
if self.stats.level == 1:
self.play_button.draw_button()
elif not self.stats.ships_left:
self.game_over_button.draw_button()
pygame.mouse.set_visible(True)
elif self.stats.ships_left != 0:
self.continue_button.draw_button()
# Make the most recently drawn screen visible.
# this clears our previous screen and updates it to a new one
# this gives our programe smooth movemnt
pygame.display.flip()
if __name__ == '__main__':
# Make a game instance, and run the game
ai = AlienInvasion()
settings = Settings()
ALIENBOMB = pygame.USEREVENT + 1
pygame.time.set_timer(ALIENBOMB, settings.alien_bomb_speed)
ai.run_game()
scoreboard.py
class Scoreboard:
"""a class to report score information"""
def __init__(self, ai_game):
self.ai_game = ai_game
self.screen = ai_game.screen
self.screen_rect = self.screen.get_rect()
self.settings = ai_game.settings
self.stats = ai_game.stats
# font settings for scoreboard
self.text_color = (255, 255, 255)
self.font = pygame.font.Font('fonts/font.ttf', 20)
self.prep_score()
self.prep_high_score()
self.prep_level()
self.prep_remaining_pow_time()
self.prep_empty_hearts()
self.prep_ships()
def prep_remaining_pow_time(self):
"""turn remainging pow time into an image"""
pow_time_left = self.settings.powerup_time - self.settings.POWERUP_TIME_ALLOWED
pow_time_str = "{}".format(pow_time_left)
self.powtime_image = self.font.render(pow_time_str, True,
self.text_color, self.settings.bg_color)
# place image below score and level
self.powtime_rect = self.powtime_image.get_rect()
self.powtime_rect.right = self.screen_rect.right - 20
self.powtime_rect.top = self.level_rect.bottom + 10
def show_score(self):
"""draw score to screen"""
self.screen.blit(self.score_image, self.score_rect)
self.screen.blit(self.high_score_image, self.high_score_rect)
self.screen.blit(self.level_image, self.level_rect)
self.screen.blit(self.powtime_image, self.powtime_rect)
self.empty_hearts.draw(self.screen)
self.ships.draw(self.screen)
settings.py
class Settings:
"""A class to store our settings for Alien Invasion game."""
def __init__(self):
"""Initlize the games settings."""
# screen settings
self.screen_width = 1600
self.screen_height = 900
self.bg_color = (0, 0, 0)
# ship settings
self.ship_limit = 3
self.ship_power = 1
self.POWERUP_TIME_ALLOWED = 5000
# bullet settings
self.bullet_width = 3
self.bullet_height = 15
self.bullet_color = (255, 51, 51)
self.bullets_allowed = 3
self.upgraded_bullets_allowed = 6
# alien settings
self.fleet_drop_speed = 10
self.increse_bomb_rate = 75
# how quickly the game speeds up
self.speedup_scale = 1.1
# how muct alien poits value increses
self.score_scale = 1.5
self.initialize_dynamic_settings()
def initialize_dynamic_settings(self):
"""settings that change through the game"""
self.ship_speed = 1.5
self.bullet_speed = 1.5
self.alien_speed = 0.5
self.alien_bomb_speed = 999
self.wall_speed = 0.5
self.fleet_direction = 1
# scoring
self.alien_points = 50
self.powerup_time = 0
def increse_speed(self):
"""increse speed settings"""
self.ship_speed *= self.speedup_scale
self.bullet_speed *= self.speedup_scale
self.alien_speed *= self.speedup_scale
self.wall_speed *= self.speedup_scale
self.alien_bomb_speed -= self.increse_bomb_rate
self.alien_points = int(self.alien_points * self.score_scale)
由于在 init 中调用了 prep_remaining_pow_time
,因此您只需执行以下两件事。
pow_time_str = "{}".format(pow_time_left)
self.powtime_image = self.font.render(pow_time_str, True,
self.text_color, self.settings.bg_color)
您需要每帧执行此操作,以便 self.powtime_image
更新为最新的 pow_time_left
值。
我目前正在寻找在我的记分牌上显示能量提升的剩余时间。我正在使用 pygame.time.get_ticks() 来确定剩余的加电时间,它似乎按预期运行。当收到一个能量提升时,它会持续 5 秒(5000 ticks),除非再次获得相同的能量提升,否则时间会再延长 5 秒。这是由我的功率水平决定的;拾取能量提升 1 级能量提升,一旦 5 秒过去,能量等级降低 1。
我无法弄清楚如何让它正确显示在我的记分牌上,因为目前它只显示 -5000,这是我的开始启动时间 (0) 减去每个级别允许的启动时间(5000)。接通电源后,时间根本不会改变,只是保持在 -5000。
main.py
class AlienInvasion:
"""Overall class to manage game assets and behavior."""
def __init__(self):
"""Initlize the game, and create game resources."""
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
self.settings.screen_width = self.screen.get_rect().width
self.settings.screen_height = self.screen.get_rect().height
pygame.display.set_caption("Alien Invasion")
self.stats = GameStats(self)
self.sb = Scoreboard(self)
self.ship = Ship(self)
self._wall_setup()
self._create_multiple_walls(*self.wall_x_positions,
x_start=self.settings.screen_width / 8, y_start=850)
self._create_groups()
self._create_fleet()
self._create_buttons()
def run_game(self):
"""Start main loop for our game."""
while True:
self._check_events()
if self.stats.game_active:
self.ship.update()
self._update_bullets()
self._check_bomb_ship_collisions()
self._update_aliens()
self._check_power_time()
self._update_screen()
def _check_pow_collisions(self):
collisions = pygame.sprite.spritecollide(
self.ship, self.powerups, True
)
for collosion in collisions:
if collosion.type == 'gun':
self.settings.ship_power += 1
self.settings.powerup_time = pygame.time.get_ticks()
self._check_power_time()
if collosion.type == 'shield' and self.stats.ships_left < 3:
self.stats.ships_left += 1
self.sb.prep_ships()
def _check_power_time(self):
if (self.settings.ship_power >= 2 and pygame.time.get_ticks() -
self.settings.powerup_time > self.settings.POWERUP_TIME_ALLOWED):
self.settings.ship_power -= 1
self.settings.powerup_time = pygame.time.get_ticks()
def _update_screen(self):
"""Update images on screen and flip to the new screen."""
# fill our background with our bg_color
self.screen.fill(self.settings.bg_color)
# draw scoreboard to screen
self.sb.show_score()
# draw ship to screen
self.ship.blitme()
for bullet in self.bullets.sprites():
bullet.draw_bullet()
self.alien_bombs.update()
self.alien_bombs.draw(self.screen)
self.aliens.draw(self.screen)
self.powerups.draw(self.screen)
self.powerups.update()
self._check_pow_collisions()
self.blocks.draw(self.screen)
self._check_wall_collisions()
# draw play button if game is inactive
if not self.stats.game_active:
if self.stats.level == 1:
self.play_button.draw_button()
elif not self.stats.ships_left:
self.game_over_button.draw_button()
pygame.mouse.set_visible(True)
elif self.stats.ships_left != 0:
self.continue_button.draw_button()
# Make the most recently drawn screen visible.
# this clears our previous screen and updates it to a new one
# this gives our programe smooth movemnt
pygame.display.flip()
if __name__ == '__main__':
# Make a game instance, and run the game
ai = AlienInvasion()
settings = Settings()
ALIENBOMB = pygame.USEREVENT + 1
pygame.time.set_timer(ALIENBOMB, settings.alien_bomb_speed)
ai.run_game()
scoreboard.py
class Scoreboard:
"""a class to report score information"""
def __init__(self, ai_game):
self.ai_game = ai_game
self.screen = ai_game.screen
self.screen_rect = self.screen.get_rect()
self.settings = ai_game.settings
self.stats = ai_game.stats
# font settings for scoreboard
self.text_color = (255, 255, 255)
self.font = pygame.font.Font('fonts/font.ttf', 20)
self.prep_score()
self.prep_high_score()
self.prep_level()
self.prep_remaining_pow_time()
self.prep_empty_hearts()
self.prep_ships()
def prep_remaining_pow_time(self):
"""turn remainging pow time into an image"""
pow_time_left = self.settings.powerup_time - self.settings.POWERUP_TIME_ALLOWED
pow_time_str = "{}".format(pow_time_left)
self.powtime_image = self.font.render(pow_time_str, True,
self.text_color, self.settings.bg_color)
# place image below score and level
self.powtime_rect = self.powtime_image.get_rect()
self.powtime_rect.right = self.screen_rect.right - 20
self.powtime_rect.top = self.level_rect.bottom + 10
def show_score(self):
"""draw score to screen"""
self.screen.blit(self.score_image, self.score_rect)
self.screen.blit(self.high_score_image, self.high_score_rect)
self.screen.blit(self.level_image, self.level_rect)
self.screen.blit(self.powtime_image, self.powtime_rect)
self.empty_hearts.draw(self.screen)
self.ships.draw(self.screen)
settings.py
class Settings:
"""A class to store our settings for Alien Invasion game."""
def __init__(self):
"""Initlize the games settings."""
# screen settings
self.screen_width = 1600
self.screen_height = 900
self.bg_color = (0, 0, 0)
# ship settings
self.ship_limit = 3
self.ship_power = 1
self.POWERUP_TIME_ALLOWED = 5000
# bullet settings
self.bullet_width = 3
self.bullet_height = 15
self.bullet_color = (255, 51, 51)
self.bullets_allowed = 3
self.upgraded_bullets_allowed = 6
# alien settings
self.fleet_drop_speed = 10
self.increse_bomb_rate = 75
# how quickly the game speeds up
self.speedup_scale = 1.1
# how muct alien poits value increses
self.score_scale = 1.5
self.initialize_dynamic_settings()
def initialize_dynamic_settings(self):
"""settings that change through the game"""
self.ship_speed = 1.5
self.bullet_speed = 1.5
self.alien_speed = 0.5
self.alien_bomb_speed = 999
self.wall_speed = 0.5
self.fleet_direction = 1
# scoring
self.alien_points = 50
self.powerup_time = 0
def increse_speed(self):
"""increse speed settings"""
self.ship_speed *= self.speedup_scale
self.bullet_speed *= self.speedup_scale
self.alien_speed *= self.speedup_scale
self.wall_speed *= self.speedup_scale
self.alien_bomb_speed -= self.increse_bomb_rate
self.alien_points = int(self.alien_points * self.score_scale)
由于在 init 中调用了 prep_remaining_pow_time
,因此您只需执行以下两件事。
pow_time_str = "{}".format(pow_time_left)
self.powtime_image = self.font.render(pow_time_str, True,
self.text_color, self.settings.bg_color)
您需要每帧执行此操作,以便 self.powtime_image
更新为最新的 pow_time_left
值。