如何解决外星人入侵得分高分问题
到目前为止,我项目的最高分正在发挥作用,但是,当我的飞船被击中后,当前得分不会重置。例如,如果我得到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
scoreboard.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
game_functions.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,aliens)
ship.center_ship()
def update_aliens(ai_settings,bullets):
check_fleet_edges(ai_settings,aliens)
aliens.update()
check_aliens_bottom(ai_settings,bullets)
if pygame.sprite.spritecollideany(ship,aliens):
print("Ship hit!!!")
ship_hit(ai_settings,bullets)
def ship_hit(ai_settings,bullets):
if stats.ships_left > 0:
stats.ships_left -= 1
stats.reset_stats()
aliens.empty()
bullets.empty()
create_fleet(ai_settings,aliens)
ship.center_ship()
sleep(0.5)
else:
stats.game_active = False
pygame.mouse.set_visible(True)
解决方法
我遇到了同样的问题。但后来我尝试了这个:
我在 ship_hit 函数中插入了一个命令“stats.score = 0”,这样分数就完成了
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。