def cheak_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets):
“”“响应按键和鼠标事件”“”
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
cheak_keydowen_events(event, ai_settings, screen, ship, bullets)
elif event.type == pygame.KEYUP:
cheak_keyup_events(event, ship)
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = pygame.mouse.get_pos()
cheak_play_button(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets, mouse_x, mouse_y)
def cheak_play_button(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets, mouse_x, mouse_y):
“”“在玩家单击play按钮时开始新游戏”“”
bullets_clicked = play_button.rect.collidepoint(mouse_x, mouse_y)
if bullets_clicked and not stats.game_active:
重置游戏设置
ai_settings.initialize_dynamic_settings()
隐藏光标
pygame.mouse.set_visible(False)
重置游戏统计信息
stats.reset_stats()
stats.game_active = True
重置记分牌对象
sb.prep_score()
sb.prep_high_score()
sb.prep_level()
sb.prep_ships()
清空外星人列表和子弹列表
aliens.empty()
bullets.empty()
创建一群新的外星人,并让飞船居中
create_fleet(ai_settings, screen, ship, aliens)
ship.center_ship()
def update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button):
“”“更新屏幕上的图像,并切换到新屏幕”“”
每次循环都重绘屏幕
screen.fill(ai_settings.bg_color)
在飞船和外星人后面重绘所有子弹
for bullet in bullets.sprites():
bullet.draw_bullet()
ship.blitme()
aliens.draw(screen)
显示得分
sb.show_score()
如果游戏处于非活动状态,就绘制play按钮
if not stats.game_active:
play_button.draw_button()
让最近绘制的屏幕可见
pygame.display.flip()
def update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets):
“”“更新子弹的位置,并删除已消失子弹”“”
更新子弹位置
bullets.update()
删除已消失的子弹
for bullet in bullets.copy():
if bullet.rect.bottom <= 0:
bullets.remove(bullet)
cheak_bullets_alien_collisions(ai_settings, screen, stats, sb, ship, aliens, bullets)
def cheak_bullets_alien_collisions(ai_settings, screen, stats, sb, ship, aliens, bullets):
“”“响应子弹和外星人碰撞”“”
删除发生碰撞的子弹和外星人
collections = pygame.sprite.groupcollide(bullets, aliens, True, True)
if collections:
for aliens in collections.values():
stats.score += ai_settings.alien_points * len(aliens)
sb.prep_score()
cheak_high_score(stats, sb)
if len(aliens) == 0:
如果整群外星人都被消灭,就提高一个等级
删除现有的子弹,加快游戏节奏,并创建一群新的外星人
bullets.empty()
ai_settings.increase_speed()
提高等级
stats.level += 1
sb.prep_level()
create_fleet(ai_settings, screen, ship, aliens)
def get_number_alien_x(ai_settings, alien_width):
“”“计算每行可容纳多少外星人”“”
available_space_x = ai_settings.screen_width - 2 * alien_width
number_alien_x = int(available_space_x / (2 * alien_width))
return number_alien_x
def get_number_rows(ai_settings, ship_height, alien_height):
“”“计算屏幕可容纳多少行机器人”“”
available_space_y = (ai_settings.screen_height - (3 * alien_height) - ship_height)
number_rows = int(available_space_y / (2 * alien_height))
return number_rows
def create_alien(ai_settings, screen, aliens, alien_number, row_number):
创建第一行外星人并将其加入当前行
alien = Alien(ai_settings, screen)
alien_width = alien.rect.width
alien.x = alien_width + 2 * alien_width * alien_number
alien.rect.x = alien.x
alien.rect.y = alien.rect.height + 2 * alien.rect.height * row_number
aliens.add(alien)
def create_fleet(ai_settings, screen, ship, aliens):
“”“创建外星人群”“”
创建一个外星人,并计算一行可容纳多少个外星人
外星人间距为外星人宽度
alien = Alien(ai_settings, screen)
number_alien_x = get_number_alien_x(ai_settings, alien.rect.width)
number_rows = get_number_rows(ai_settings, ship.rect.height, alien.rect.height)
创建外星人群
for row_number in range(number_rows):
for alien_number in range(number_alien_x):
create_alien(ai_settings, screen, aliens, alien_number, row_number)
def cheak_fleet_edges(ai_settings, aliens):
“”“有外星人到达屏幕边缘时采取相应的措施”“”
for alien in aliens.sprites():
if alien.cheak_edgs():
change_fleet_direction(ai_settings, aliens)
break
def change_fleet_direction(ai_settings, aliens):
“”“将整群外星人下移,并改变它们的位置”“”
for alien in aliens.sprites():
alien.rect.y += ai_settings.fleet_drop_speed
ai_settings.fleet_direction *= -1
def ship_hit(ai_settings, screen, stats, sb, ship, aliens, bullets):
“”“响应被外星人撞到的飞船”“”
if stats.ships_left > 0:
将ships_left减1
stats.ships_left -= 1
更新记分牌
sb.prep_ships()
清空外星人列表和子弹列表
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)
def cheak_aliens_bottom(ai_settings, screen, stats, sb, ship, aliens, bullets):
“”“检查是否有外星人到达了屏幕底端”“”
screen_rect = screen.get_rect()
for alien in aliens.sprites():
if alien.rect.bottom >= screen_rect.bottom:
像飞船被撞到一样进行处理
ship_hit(ai_settings, screen, stats, sb, ship, aliens, bullets)
break
def update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets):
“”“检查是否有外星人位于屏幕边缘,并更新整群外星人的位置”“”
cheak_fleet_edges(ai_settings, aliens)
aliens.update()
检查外星人和飞船之间的碰撞
if pygame.sprite.spritecollideany(ship, aliens):
ship_hit(ai_settings, screen, stats, sb, ship, aliens, bullets)
检查是否有外星人到达屏幕底端
cheak_aliens_bottom(ai_settings, screen, stats, sb, ship, aliens, bullets)
def cheak_high_score(stats, sb):
“”“检查是否诞生了新的最高得分”“”
if stats.score > stats.high_score:
stats.high_score = stats.score
sb.prep_high_score()
game_stats.py
跟踪统计游戏信息类
class GameSrats():
“”“跟踪游戏的统计信息”“”
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
self.level = 1
scoreboard.py
创建scoerboard类,用来显示当前得分,最高得分,玩家等级,余下的飞船数。
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.prep_high_score()
self.prep_level()
self.prep_ships()
def 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.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.score_rect.right
self.level_rect.top = self.score_rect.bottom + 10
def prep_high_score(self):
“”“将最高得分转换为渲染的图像”“”
high_score = int(round(self.stats.high_score, -1))
high_score_str = 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.screen_rect.top
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 = 20
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)
settings.py
存储游戏所有设置类
class Settings():
“”“存储外星人入侵所有设置的类”“”
def init(self):
“”“初始化游戏静态设置”“”
屏幕设置
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230, 230, 230)
飞船的速度设置
self.ship_speed_factor = 1.5
self.ship_limit = 3
子弹设置
self.bullet_speed_factor = 10
self.bullet_width = 10
self.bullet_height = 15
self.bullet_color = 60, 60, 60
self.bullets_allowed = 3
外星人设置
self.alien_speed_factor = 1
self.fleet_drop_speed = 5
fleet_direction为1表示向右移,为-1表示向左移
self.fleet_direction = 1
加快游戏节奏的速度
self.speedup_scale = 1.1
外星人点数的提高速度
self.score_scale = 1.5
self.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
一、Python所有方向的学习路线
Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
二、学习软件
工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。
三、入门学习视频
我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。
小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Python爬虫全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:python)
img.cn/img_convert/8c4513c1a906b72cbf93031e6781512b.png)
三、入门学习视频
我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。
小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Python爬虫全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:python)
[外链图片转存中…(img-C8VtJtFc-1710973899686)]