#Python# Alien Invasion项目(十)

龙俊美
2023-12-01

最高分保存的实现

首先更新game_stats模块,在每次初始化时读取文件中的数据
game_stats.py

class GameStats():
    '''跟踪游戏的统计信息'''

    def __init__(self,ai_settings):
        '''初始化统计信息'''
        self.ai_settings = ai_settings
        self.reset_stats()
        # 初始化最高得分
        with open('D:/Python学习/Alien Invasion/high_score.txt','r') as file_object: # 读取文件
            contents = file_object.read().strip()
            self.high_score = int(contents) # 最高得分
        # 游戏启动时处于非激活状态
        self.game_active = False

    def reset_stats(self):
        '''初始化游戏运行期间可能变化的统计信息'''
        self.ship_left = self.ai_settings.ship_limit # 飞船剩余数量
        self.score = 0 # 当前得分
        self.level = 1 # 当前难度等级

更新scoreboard模块,在每次出现最高分的时候,更新文件中的数据

import pygame.font
from pygame.sprite import Group
from ship import Ship

class ScoreBoard():
    '''显示得分信息的类'''

    def __init__(self,ai_settings,screen,stats):
        '''初始化显示得分涉及的属性'''
        self.screen = screen
        self.screen_rect = screen.get_rect()
        self.ai_settings = ai_settings
        self.stats = stats
        # 显示得分信息时使用的字体
        self.text_color = (30,30,30)
        self.font = pygame.font.SysFont(None,48)
        # 准备包含最高得分,当前得分,难度等级和剩余飞船数的图像
        self.prep_score()
        self.perp_high_score()
        self.prep_level()
        self.prep_ships()

    def prep_ships(self):
        '''显示还余下多少艘飞船'''
        self.ships = Group() # 创建飞船编组
        for ship_number in range(self.stats.ship_left):
            ship = Ship(self.ai_settings,self.screen)
            ship.rect.x = 10 + ship_number * ship.rect.width
            ship.rect.y = 10
            self.ships.add(ship) # 将飞船加入编组

    def prep_level(self):
        '''将难度登记转化为渲染的图像'''
        self.level_image = self.font.render(str(self.stats.level),True,self.text_color,self.ai_settings.bg_color)
        # 将等级放在当前得分显示下方
        self.level_rect = self.level_image.get_rect()
        self.level_rect.right = self.screen_rect.right
        self.level_rect.top = self.score_rect.bottom + 10

    def perp_high_score(self):
        '''将最高得分转化为渲染的图像,并在文件中更新最高得分'''
        high_score = int(round(self.stats.high_score,-1)) 
        high_score_str = "{:,}".format(high_score)
        self.high_score_iamge = self.font.render(high_score_str,True,self.text_color,self.ai_settings.bg_color)
        # 将最高得分放在屏幕顶部中央
        self.high_score_rect = self.high_score_iamge.get_rect()
        self.high_score_rect.centerx = self.screen_rect.centerx
        self.score_rect.top = 20
        # 在文件中更新最高得分
        with open('D:/Python学习/Alien Invasion/high_score.txt','w') as file_object: # 写入文件
            file_object.write(str(self.stats.high_score))

    def prep_score(self):
        '''将当前得分转化为渲染的图像'''
        rounded_score = int(round(self.stats.score,-1)) # 使显示的得分圆整
        score_str = "{:,}".format(rounded_score)
        self.score_image = self.font.render(score_str,True,self.text_color,self.ai_settings.bg_color)
        # 将得分放在屏幕右上角
        self.score_rect = self.score_image.get_rect()
        self.score_rect.right = self.screen_rect.right
        self.score_rect.top = 20

    def show_score(self):
        '''在屏幕上显示最高得分,当前得分,难度等级与飞船剩余数'''
        self.screen.blit(self.score_image,self.score_rect)
        self.screen.blit(self.high_score_iamge,self.high_score_rect)
        self.screen.blit(self.level_image,self.level_rect)
        self.ships.draw(self.screen)

总结

with open('D:/Python学习/Alien Invasion/high_score.txt','w') as file_object

with open() as f
关键字with可以在完成未文件的操作后,自动关闭文件,可以简化代码。同时在每次产生新最高分的时候即更新文件中的数据,可以避免因程序的意外中止而导致的记录错误。

 类似资料: