首页 > 解决方案 > 外星人入侵高分和分数问题

问题描述

到目前为止,我的项目的高分正在发挥作用,但是,在我的船被击中后,当前的分数不会自行重置。例如,如果我得到 4000 分,而我的船被击中,在下一轮中,最高分和当前分数显示为 4000 分。在这个项目中,我有模块 alien.py、alien_invasion.py、bullet.py、button.py、gamefunctions.py、game_stats.py、scoreboard.py、settings.py 和 ship.py。但是,我只会添加书中告诉您要获得高分的更改。如果您想查看更多代码,请随时询问

game_stats.py:

    class GameStats():
         def __init__(self, ai_settings):
              self.ai_settings = ai_settings
              self.reset_stats()
              self.game_active = False
              self.high_score = 0

         def reset_stats(self):
              self.ships_left = self.ai_settings.ship_limit
              self.score = 0

记分牌.py:

    def show_score(self):
         self.screen.blit(self.score_image, self.score_rect)
         self.screen.blit(self.high_score_image, self.high_score_rect)
    def prep_high_score(self):
         high_score = int(round(self.stats.high_score, -1))
         high_score_str = "{:,}".format(high_score)
         self.high_score_image = self.font.render(high_score_str, True, self.text_color, 
              self.ai_settings.bg_color)
         self.high_score_rect = self.high_score_image.get_rect()
         self.high_score_rect.centerx = self.screen_rect.centerx
         self.high_score_rect.top = self.score_rect.top

游戏功能.py:

    def check_high_score(stats, sb):
         if stats.score > stats.high_score:
             stats.high_score = stats.score
             sb.prep_high_score()
    def check_play_button(ai_settings, screen, stats, play_button, ship, 
    aliens, bullets, mouse_x, mouse_y):
         button_clicked = play_button.rect.collidepoint(mouse_x, mouse_y)
         if button_clicked and not stats.game_active:
              ai_settings.initialize_dynamic_settings()
              pygame.mouse.set_visible(False)
              stats.reset_stats()
              stats.game_active = True
              aliens.empty()
              bullets.empty()
              create_fleet(ai_settings, screen, ship, aliens)
              ship.center_ship()
    def update_aliens(ai_settings, stats, screen, ship, aliens, bullets):
    check_fleet_edges(ai_settings, aliens)
        aliens.update()
        check_aliens_bottom(ai_settings, stats, screen, ship, aliens,                                 
        bullets)
        if pygame.sprite.spritecollideany(ship, aliens):
        print("Ship hit!!!")
        ship_hit(ai_settings, stats, screen, ship, aliens, bullets)

    def ship_hit(ai_settings, stats, screen, ship, aliens, bullets):
        if stats.ships_left > 0:
             stats.ships_left -= 1
             stats.reset_stats()
             aliens.empty()
             bullets.empty()
             create_fleet(ai_settings, screen, ship, aliens)
             ship.center_ship()
             sleep(0.5)
       else:
            stats.game_active = False
            pygame.mouse.set_visible(True)

标签: pythonpython-3.xpygame

解决方案


我有同样的问题 。但后来我尝试了这个:

我在 ship_hit 函数中插入了一个命令“stats.score = 0”,这样分数就完成了


推荐阅读