Python外星人入侵游戏编程完整版!孩子开心坏了( 二 )

3.设置子弹 , bullet.py , 代码:import pygamefrom pygame.sprite import Spriteimport timeclass Bullet(Sprite): '''飞船子弹进行管理''' def __init__(self,ai_settings,screen,ship): super(Bullet,self).__init__() self.screen = screen # 创建子弹矩形初始位置(0,0,3,15)分别对应lef , top , 宽 , 高 self.rect = pygame.Rect(0,0, ai_settings.bullet_width, ai_settings.bullet_height) self.rect.centerx = ship.rect.centerx # 设置中心点x轴坐标跟飞船一致 self.rect.top = ship.rect.top# 设置y轴坐标顶部跟飞船一致 # 设置成小数进行计算 self.top = float(self.rect.top) self.color = ai_settings.bullet_color self.speed_factor = ai_settings.bullet_speed_factor def update(self): self.top -=self.speed_factor self.rect.top = self.top print(self.rect.top) def draw_bullet(self): pygame.draw.rect(self.screen,self.color,self.rect)4.设置Play按钮 , button.py , 代码:import pygame.fontclass Button():def __init__(self,ai_settings,screen,msg): """初始化按钮属性""" self.screen = screen self.screen_rect = screen.get_rect()#设置按钮的尺寸和其他属性 self.width,self.height = 200,50 self.button_color = (0,255,0) self.text_color = (255,255,255) self.font = pygame.font.SysFont(None,48)#创建按钮的rect对象 , 并使其居中 self.rect = pygame.Rect(0,0,self.width,self.height) self.rect.center = self.screen_rect.center#按钮的标签只需创建一次 self.prep_msg(msg)def prep_msg(self,msg): """将msg渲染为图像 , 并使其在按钮上居中""" self.msg_image = self.font.render(msg,True,self.text_color,self.button_color) self.msg_image_rect = self.msg_image.get_rect() self.msg_image_rect.center =self.rect.centerdef draw_button(self): #绘制一个用颜色填充的按钮 , 再绘制文本 self.screen.fill(self.button_color,self.rect) self.screen.blit(self.msg_image,self.msg_image_rect)5.设置游戏功能 , game_functions.py , 代码:import sysimport pygamefrom bullet import Bulletfrom alien import Alienfrom time import sleepdef check_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:check_keydown_events(event,ai_settings,screen,ship,bullets) elif event.type == pygame.KEYUP:check_keyup_events(event,ship) elif event.type == pygame.MOUSEBUTTONDOWN:mouse_x, mouse_y = pygame.mouse.get_pos()check_play_button(ai_settings,screen,stats,sb,play_button,ship,aliens,bullets,mouse_x, mouse_y)def check_play_button(ai_settings,screen,stats,sb,play_button,ship,aliens,bullets,mouse_x, mouse_y): """在玩家单击Play按钮时开始游戏""" 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#重置计分牌图像 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) # 设置背景颜色 ship.blitme() # 绘制飞船 aliens.draw(screen) # 循环子弹组里面的元素 , 进行绘制 为空时不执行 for bullet in bullets.sprites(): bullet.draw_bullet() # 绘制子弹 #显示得分 sb.show_score() #如果游戏处于非活跃状态 , 就显示Play按钮 if not stats.game_active: play_button.draw_button() # 显示最新屏幕 , 擦拭旧屏幕 pygame.display.flip() # print('1')def check_keydown_events(event,ai_settings,screen,ship,bullets): if event.key == pygame.K_RIGHT: ship.moving_right = True elif event.key == pygame.K_LEFT: ship.moving_left = True elif event.key == pygame.K_SPACE: fire_bullet(ai_settings,screen,ship,bullets) elif event.key == pygame.K_q: sys.exit()def check_keyup_events(event,ship): if event.key == pygame.K_RIGHT: ship.moving_right = False elif event.key == pygame.K_LEFT: ship.moving_left = Falsedef update_bullets(ai_settings,screen,stats,sb,ship,aliens,bullets): '''更新子弹位置 , 删除子弹''' bullets.update() # 子弹组每个成员执行self.update()操作 for bullet in bullets.sprites(): if bullet.rect.bottom