不知道为什么会报错啊。
main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | import sys import pygame import constants from game.plane import OurPlane, SmallEnemyPlane def main(): ''' 游戏入口,main方法 ''' # 初始化 pygame.init() width, height = 480 , 852 # 屏幕对象 screen = pygame.display.set_mode((width, height)) # 设置窗口标题 pygame.display.set_caption( '飞机大战' ) # 加载背景图片 bg = pygame.image.load(constants.BG_IMG) # 游戏的标题 img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) img_game_title_rect = img_game_title.get_rect() # 宽度和高度 t_width, t_height = img_game_title.get_size() img_game_title_rect.topleft = ( int ((width - t_width) / 2 ), int (height / 2 - t_height)) # 开始按钮 btn_start = pygame.image.load(constants.IMG_GAME_START_BTN) btn_start_rect = btn_start.get_rect() btn_width, btn_height = btn_start.get_size() btn_start_rect.topleft = ( int ((width - btn_width) / 2 ), int (height / 2 + btn_height)) # 加载背景音乐 # pygame.mixer.music.load(constants.BG_MUSIC) # pygame.mixer.music.play(-1) # 无限循环播放 # pygame.mixer.music.set_volume(0.2) # 设置音量 # 游戏状态 status = 0 # 准备中,1 游戏中, 2 游戏结束 our_plane = OurPlane(screen, speed = 20 ) frame = 0 # 播放帧数 clock = pygame.time.Clock() # 一架飞机可以属于多个精灵组 small_enemies = pygame.sprite.Group() enemies = pygame.sprite.Group() # 随机添加6架小型敌机 for i in range ( 6 ): plane = SmallEnemyPlane(screen, 8 ) plane.add(small_enemies, enemies) while True : # 设置帧速率 clock.tick( 60 ) frame + = 1 if frame > = 60 : frame = 0 # 1. 监听事件 for event in pygame.event.get(): # 退出游戏 if event. type = = pygame.QUIT: pygame.quit() sys.exit() elif event. type = = pygame.MOUSEBUTTONDOWN: # 鼠标点击进入游戏 # 游戏正在准备中,点击才能进入游戏 if status = = 0 : status = 1 elif event. type = = pygame.KEYDOWN: # 键盘事件 # 正在游戏中,才需要控制键盘 WASD if status = = 1 : if event.key = = pygame.K_w or event.key = = pygame.K_UP: our_plane.move_up() elif event.key = = pygame.K_s or event.key = = pygame.K_DOWN: our_plane.move_down() elif event.key = = pygame.K_a or event.key = = pygame.K_LEFT: our_plane.move_left() elif event.key = = pygame.K_d or event.key = = pygame.K_RIGHT: our_plane.move_right() elif event.key = = pygame.K_SPACE: # 发射子弹 our_plane.shoot() # 更新游戏的状态 if status = = 0 : # 游戏正在准备中 # 绘制背景 screen.blit(bg, bg.get_rect()) # 游戏标题 screen.blit(img_game_title, img_game_title_rect) # 开始按钮 screen.blit(btn_start, btn_start_rect) elif status = = 1 : # 游戏进行中 # 绘制背景 screen.blit(bg, bg.get_rect()) # 绘制飞机 our_plane.update(frame) # 绘制子弹 our_plane.bullets.update() # 绘制敌方飞机 small_enemies.update() pygame.display.flip() if __name__ = = '__main__' : main() |
plane.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | ''' 飞机的基类 我方的飞机 敌方的小型飞机 敌方的中型飞机 敌方的大型飞机 ''' import random import pygame import constants from game.bullet import Bullet class Plane(pygame.sprite.Sprite): ''' 飞机的基础类 ''' # 飞机的图片 plane_images = [] # 飞机爆炸的图片 destroy_images = [] # 坠毁的音乐地址 down_sound_src = None # 飞机的状态:True,活的, False,死的 active = True # 该飞机发射的子弹精灵组 bullets = pygame.sprite.Group() def __init__( self , screen, speed = None ): super ().__init__() self .screen = screen # 加载静态资源 self .img_list = [] self ._destroy_img_list = [] self .down_sound = None self .load_src() # 飞行的速度 self .speed = speed or 10 # 获取飞机的位置 self .rect = self .img_list[ 0 ].get_rect() # 飞机的宽度和高度 self .plane_w, self .plane_h = self .img_list[ 0 ].get_size() # 游戏窗口的宽度和高度 self .width, self .height = self .screen.get_size() # 改变飞机的初始化位置,放在屏幕的下方 self .rect.left = int (( self .width - self .plane_w) / 2 ) self .rect.top = int ( self .height / 2 ) def load_src( self ): '''加载静态资源''' # 飞机图像 for img in self .plane_images: self .img_list.append(pygame.image.load(img)) # 飞机坠毁的图像 for img in self .destroy_images: self ._destroy_img_list.append(pygame.image.load(img)) # 坠毁的音乐 if self .down_sound_src: self .down_sound = pygame.mixer.Sound( self .down_sound_src) @ property def image( self ): return self .img_list[ 0 ] def blit_me( self ): self .screen.blit( self .image, self .rect) def move_up( self ): '''飞机向上移动''' self .rect.top - = self .speed def move_down( self ): '''飞机向下移动''' self .rect.bottom + = self .speed def move_left( self ): '''飞机向左移动''' self .rect.left - = self .speed def move_right( self ): '''飞机向右移动''' self .rect.right + = self .speed def broken_down( self ): '''飞机坠毁''' # 1. 播放坠毁音乐 if self .down_sound: self .down_sound.play() # 2. 播放坠毁的动画 for img in self ._destroy_img_list: self .screen.blit(img, self .rect) # 3. 坠毁后。。。 self .active = False def shoot( self ): '''飞机发射子弹''' bullet = Bullet( self .screen, self , 15 ) self .bullets.add(bullet) class OurPlane(Plane): ''' 我方的飞机 ''' # 飞机的图片 plane_images = constants.OUR_PLANE_IMG_LIST # 飞机爆炸的图片 destroy_images = constants.OUR_DESTROY_IMG_LIST # 坠毁的音乐地址 down_sound_src = None def update( self , frame): '''更新飞机的动画效果''' if frame % 5 : self .screen.blit( self .img_list[ 0 ], self .rect) else : self .screen.blit( self .img_list[ 1 ], self .rect) def move_up( self ): '''向上移动,超出范围之后,拉回来''' super ().move_up() if self .rect.top < = 0 : self .rect.top = 0 def move_down( self ): '''向下移动,超出范围之后,拉回来''' super ().move_down() if self .rect.top > = self .height - self .plane_h: self .rect.top = self .height - self .plane_h def move_left( self ): '''向左移动,超出范围之后,拉回来''' super ().move_left() if self .rect.left < = 0 : self .rect.left = 0 def move_right( self ): '''向右移动,超出范围之后,拉回来''' super ().move_right() if self .rect.left > = self .width - self .plane_w: self .rect.left = self .width - self .plane_w class SmallEnemyPlane(Plane): '''地方的小型飞机类''' # 飞机的图片 plane_images = constants.SMALL_ENEMY_PLANE_IMG_LIST # 飞机爆炸的图片 destroy_images = constants.SMALL_ENEMY_DESTROY_IMG_LIST # 坠毁的音乐地址 down_sound_src = constants.SMALL_ENEMY_PLANE_DOWN_SOUND def __init__( self , screen, speed): super ().__init__(screen, speed) # 每次生成一架新的小型飞机的时候,随机出现在屏幕中 # 改变飞机的随机位置 self .init_pos() def init_pos( self ): '''改变飞机的随机位置''' # 屏幕的宽度 - 飞机的宽度 self .rect.left = random.randint( 0 , self .width - self .plane_w) # 屏幕之外随机高度 self .rect.top = random.randint( - 5 * self .plane_h, - self .plane_h) def update( self , * args): '''更新飞机的移动''' super ().move_down() # 画在屏幕上 self .blit_me() # 超出范围如何处理 # 1.重用 if self .rect.top > = self .height: self .active = False # self.kill() self .reset() # todo 2.多线程、多进程 def reset( self ): '''重置飞机的状态,达到复用的效果''' self .active = True # 改变飞机的随机位置 self .init_pos() |
14
收起
正在回答 回答被采纳积分+1
1回答
1.Python零基础入门
- 参与学习 人
- 提交作业 2727 份
- 解答问题 8160 个
想要进入Python Web、爬虫、人工智能等高薪领域,你需要掌握本阶段的Python基础知识,课程安排带你高效学习轻松入门,学完你也能听得懂Python工程师的行业梗。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧