赞
踩
alien_invasion.py
# -*- encoding:utf-8 -*- """ @作者:ZYH @文件名:alien_invasion.py @文档说明: 创建游戏窗口和用户游戏输入 """ import sys import pygame from AlienInvasion.settings import Settings from ship import Ship from AlienInvasion.bullet import Bullet class AlienInvasion: """管理游戏资源和行为的类""" def __init__(self): """初始化游戏并创建游戏资源""" pygame.init() self.settings = Settings() # 支持全屏模式 self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) self.settings.screen_width = self.screen.get_rect().width self.settings.screen_height = self.screen.get_rect().height # # 创建一个窗口 并设置宽 和 高 # self.screen = pygame.display.set_mode( # (self.settings.screen_width, self.settings.screen_height)) pygame.display.set_caption("Alien Invasion") self.ship = Ship(self) self.bullets = pygame.sprite.Group() def run_game(self): """开始游戏的主循环""" while True: self._check_events() self.ship.update() # self.bullets.update() self._update_bullets() self._update_screen() def _check_events(self): # 监视键盘和鼠标事件 for event in pygame.event.get(): if event.type == pygame.QUIT: #检测到鼠标点击关闭窗口 sys.exit() # 鼠标按着一直右移 elif event.type == pygame.KEYDOWN: self._check_keydown_events(event) # 抬起停止移动 elif event.type == pygame.KEYUP: self._check_keyup_events(event) # 代码重构 处理keydown事件 def _check_keydown_events(self, event): """响应按键""" if event.key == pygame.K_RIGHT: # 向右移动飞船 self.ship.moving_right = True elif event.key == pygame.K_LEFT: # 向左移动 self.ship.moving_left = True # 按q键退出游戏 elif event.key == pygame.K_UP: # 向上移动 self.ship.moving_up = True elif event.key == pygame.K_DOWN: # 向下移动 self.ship.moving_down = True elif event.key == pygame.K_q: sys.exit() # 按空格发射子弹 elif event.key == pygame.K_SPACE: self._fire_bullet() def _check_keyup_events(self, event): """响应松开""" if event.key == pygame.K_RIGHT: self.ship.moving_right = False elif event.key == pygame.K_LEFT: self.ship.moving_left = False elif event.key == pygame.K_UP: self.ship.moving_up = False elif event.key == pygame.K_DOWN: self.ship.moving_down = False def _fire_bullet(self): """创建一颗子弹,并将其加入编组bullets中""" if len(self.bullets) < self.settings.bullets_allowed: new_bullet = Bullet(self) self.bullets.add(new_bullet) def _update_bullets(self): """更新子弹的位置并删除消失的子弹""" # 更新子弹的位置 self.bullets.update() # 删除消失的子弹 for bullet in self.bullets.copy(): if bullet.rect.bottom <= 0: self.bullets.remove(bullet) def _update_screen(self): # 每次循环时都会重新画屏幕 self.screen.fill(self.settings.bg_color) for bullet in self.bullets.sprites(): bullet.draw_bullet() # 让最近绘制的屏幕可见 不断更新屏幕 self.ship.blitme() pygame.display.flip() if __name__ == '__main__': # 创建游戏实例并运行游戏 ai = AlienInvasion() ai.run_game()
ship.py
# -*- encoding:utf-8 -*- """ @作者:ZYH @文件名:ship.py @文档说明: 飞船类 """ import pygame class Ship: """管理飞船的类""" def __init__(self, ai_game): """初试化飞船并设置其初试值""" self.screen = ai_game.screen self.screen_rect = ai_game.screen.get_rect() self.settings = ai_game.settings # 加载飞船的图像并获取其外接矩形 self.image = pygame.image.load('H:\\py_project\\AlienInvasion\\images\\ship.bmp') self.rect = self.image.get_rect() # 对于每艘新飞船,都将其放在屏幕的底部中央 self.rect.midbottom = self.screen_rect.midbottom # 在飞船的属性x中存储小数值 self.x = float(self.rect.x) # 因为rect只存储整数部分,所以转换为小数 self.y = float(self.rect.y) # 移动标志 上下左右 self.moving_right = False self.moving_left = False self.moving_up = False self.moving_down = False def update(self): """根据移动标志调整飞船的位置""" # 更新飞船而不是rect对象的x值 # 防止飞船移动超出屏幕 (左上角 0,0) if self.moving_right and self.rect.right < self.screen_rect.right: # 向右移动ship_speed个像素 self.x += self.settings.ship_speed if self.moving_left and self.rect.left > 0: # 向左移动ship_speed像素 self.x -= self.settings.ship_speed if self.moving_up and self.rect.top > 0: # 向上移动 self.y -= self.settings.ship_speed if self.moving_down and self.rect.bottom < self.screen_rect.bottom: # 向下移动 self.y += self.settings.ship_speed # 根据self.x更新rect对象 self.rect.x = self.x self.rect.y = self.y def blitme(self): """在指定的位置绘制飞船""" self.screen.blit(self.image, self.rect)
settings.py
# -*- encoding:utf-8 -*- """ @作者:ZYH @文件名:settings.py @文档说明:飞船设置 """ class Settings: """存储游戏《外星人入侵》中所有设置类""" def __init__(self): """初始化游戏的设置""" # 屏幕设置 self.screen_width = 1200 self.screen_height = 800 self.bg_color = (230, 230, 230) # 子弹设置 self.bullet_speed = 1.0 self.bullet_width = 3 self.bullet_height = 15 self.bullet_color = 60, 60, 60 self.bullets_allowed = 3 # 飞船设置 self.ship_speed = 1.5
bullet.py
# -*- encoding:utf-8 -*- """ @作者:ZYH @文件名:bullet.py @文档说明: 子弹类 """ import pygame from pygame.sprite import Sprite class Bullet(Sprite): """管理飞船所发射子弹的类""" def __init__(self, ai_game): """在飞船当前位置创建一个子弹对象""" super().__init__() self.screen = ai_game.screen self.settings = ai_game.settings self.color = self.settings.bullet_color # 在(0, 0) 处创建一个表示子弹的矩形,再设置正确的位置 self.rect = pygame.Rect(0, 0, self.settings.bullet_width, self.settings.bullet_height) self.rect.midtop = ai_game.ship.rect.midtop # 存储用小数表示的子弹位置 self.y = float(self.rect.y) def update(self): """向上移动子弹""" # 更新表示子弹的位置的小数值 self.y -= self.settings.bullet_speed # 更新表示子弹的rect位置 self.rect.y = self.y def draw_bullet(self): """画出子弹""" pygame.draw.rect(self.screen, self.color, self.rect)
飞船图片
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。