Python外星人入侵游戏编程完整版!孩子开心坏了( 四 )
6.游戏统计信息 , 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 self.level = 1def reset_stats(self): """初始化在游戏运行期间可能变化的统计信息""" self.ships_left = self.ai_settings.ship_limit self.score = 0
7.分数设置 , scoreboard.py , 代码:import pygame.fontfrom pygame.sprite import Groupfrom ship import Shipclass 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.prep_high_score() self.prep_level() self.prep_ships()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 - 20 self.score_rect.top = 5def 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 = 5def 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.score_image.get_rect() self.level_rect.right = self.screen_rect.right self.level_rect.top = self.score_rect.bottomdef prep_ships(self): """显示还剩下多少艘飞船""" self.ships = Group() for ship_number in range(self.stats.ships_left):ship = Ship(self.ai_settings, self.screen)ship.rect.x = 10 + ship_number * ship.rect.widthship.rect.y = 10self.ships.add(ship)def show_score(self): """在屏幕上显示得分和等级""" 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.ships.draw(self.screen)
8.设置 , settings.py , 代码:class Settings(): '''存储外星人入侵中所有的设置''' def __init__(self): '''初始化设置''' #屏幕设置 self.screen_width = 1200 self.screen_height = 600 self.bg_color = (230,230,230) # 设置背景色 灰色#飞船设置 self.ship_limit = 3 self.ship_image_path = 'images/ship.bmp' # 飞船图片路径#子弹设置 self.bullet_width = 3 self.bullet_height = 15 self.bullet_color = 60,60,60 self.bullets_allowed = 3# 允许屏幕中出现子弹的数量#外星人设置 self.fleet_drop_speed = 10#以什么样的速度加快游戏节奏 self.speedup_scale = 1.1 #外星人点数提高速度 self.score_scale = 1.5self.initialize_dynamic_settings()def initialize_dynamic_settings(self): """初始化随游戏进行而变化的设置""" self.ship_speed_factor = 1.5 self.bullet_speed_factor = 3 self.alien_speed_factor = 1#fleet_direction为1表示向右移 , 为-1表示向左移 self.fleet_direction = 1#计分 self.alien_points = 50def increase_speed(self): """提高速度设置,外星人点数""" self.ship_speed_factor *= self.speedup_scale self.bullet_speed_factor *= self.speedup_scale self.alien_speed_factor *= self.speedup_scaleself.alien_points = int(self.alien_points * self.score_scale) print(self.alien_points)
- 想自学Python来开发爬虫,需要按照哪几个阶段制定学习计划
- 未来想进入AI领域,该学习Python还是Java大数据开发
- 2021年Java和Python的应用趋势会有什么变化?
- 非计算机专业的本科生,想利用寒假学习Python,该怎么入手
- 专家介绍如何判断智能手机被入侵:运行速度变慢、电池消耗过快以及卡顿
- 用Python制作图片验证码,这三行代码完事儿
- 历时 1 个月,做了 10 个 Python 可视化动图,用心且精美...
- 为何在人工智能研发领域Python应用比较多
- 对于非计算机专业的同学来说,该选择学习Python还是C
- 学习完Python之后,如何向人工智能领域发展