python飞机大战代码
1 import pygame 2 from pygame.locals import * 3 from pygame.sprite import Sprite 4 import random 5 import time 6 pygame.init()#游戏初始化 7 pygame.mixer.init()#混音器初始化 8 #游戏背景音乐 9 pygame.mixer.music.load("./sound/game_music.wav") 10 pygame.mixer.music.set_volume(0.2) 11 #子弹发射音乐 12 bullet_sound = pygame.mixer.Sound("./sound/bullet.wav") 13 bullet_sound.set_volume(0.2) 14 #我方飞机挂了的音乐 15 me_down_sound = pygame.mixer.Sound("./sound/game_over.wav") 16 me_down_sound.set_volume(0.2) 17 #敌方飞机挂了的音乐 18 enemy_down_sound = pygame.mixer.Sound("./sound/enemy1_down.wav") 19 enemy_down_sound.set_volume(0.2) 20 21 #设置定时器事件 22 CREAT_ENEMY = pygame.USEREVENT 23 pygame.time.set_timer(CREAT_ENEMY,1000) 24 25 #创建一个窗口,用来显示内容 26 screen = pygame.display.set_mode((480,800),0,32) 27 28 class Base(object): 29 def __init__(self,screen): 30 self.screen = screen 31 32 class Plan(Base): 33 def __init__(self,screen): 34 super().__init__(screen) 35 36 self.image = pygame.image.load(self.imageName).convert() 37 self.bulletList = [] 38 def display(self): 39 self. screen.blit(self.image,(self.x,self.y)) 40 for bullet in self.bulletList: 41 bullet.display() 42 bullet.move() 43 if bullet.judge(): 44 self.bulletList.remove(bullet) 45 class GamePlan(Plan,pygame.sprite.Sprite): 46 def __init__(self,screen): 47 self.imageName = "./feiji/hero.gif" 48 super().__init__(screen) 49 Sprite.__init__(self) 50 51 self.rect = self.image.get_rect() 52 53 self.rect.x=200 54 self.rect.y=680 55 #加载我机损毁图片 56 self.bomb1 = pygame.image.load("./feiji/hero_blowup_n1.png") 57 self.bomb2 = pygame.image.load("./feiji/hero_blowup_n2.png") 58 self.bomb3 = pygame.image.load("./feiji/hero_blowup_n3.png") 59 self.bombList = [self.bomb1,self.bomb2,self.bomb3] 60 def display(self): 61 self.screen.blit(self.image,(self.rect.x,self.rect.y)) 62 for bullet in self.bulletList: 63 bullet.display() 64 bullet.move() 65 def moveLeft(self): 66 if self.rect.x >=0: 67 self.rect.x-=20 68 def moveRight(self): 69 if self.rect.x <480-100: 70 self.rect.x+=20 71 def moveUp(self): 72 if self.rect.y>0: 73 self.rect.y-=20 74 def moveDown(self): 75 if self.rect.y<860-124: 76 self.rect.y+=20 77 def _del_(self): 78 print("游戏结束") 79 def shootbullet(self): 80 Newbullet =PublicBullet(self.rect.x,self.rect.y,self.screen) 81 self.bulletList.append(Newbullet) 82 bullet_sound.play() 83 class EnemyPlan(Plan,pygame.sprite.Sprite): 84 def __init__(self,screen): 85 self.speed = random.randint(1,3) 86 self.imageName = "./feiji/enemy-1.gif" 87 super().__init__(screen) 88 Sprite.__init__(self) 89 #确定敌机位置 90 self.rect = self.image.get_rect() 91 self.reset() 92 #加载敌机损毁图片 93 self.bomb1 = pygame.image.load("./feiji/enemy0_down1.png") 94 self.bomb2 = pygame.image.load("./feiji/enemy0_down2.png") 95 self.bomb3 = pygame.image.load("./feiji/enemy0_down3.png") 96 self.bombList = [self.bomb1,self.bomb2,self.bomb3] 97 def reset(self): 98 99 self.rect.x = random.randint(0,400) 100 self.rect.y= 0 101 102 def move(self): 103 self.rect.y+=self.speed 104 def update(self): 105 if self.rect.y>860: 106 self.kill() 107 def _del_(self): 108 print("敌机挂了") 109 110 class PublicBullet(Base): 111 def __init__(self,x,y,screen): 112 super().__init__(screen) 113 self.imageName="./feiji/bullet-3.gif" 114 self.x = x+40 115 self.y = y-20 116 self.image = pygame.image.load(self.imageName).convert() 117 def move(self): 118 self.y-=4 119 120 def display(self): 121 self.screen.blit(self.image,(self.x,self.y)) 122 def judge(self): 123 if self.y<0 or self.y>860 : 124 return True 125 else: 126 return False 127 128 #设置敌机精灵组 129 enemy = EnemyPlan(screen) 130 enemy1 = EnemyPlan(screen) 131 enemy2 = EnemyPlan(screen) 132 enemy3 = EnemyPlan(screen) 133 enemy4 = EnemyPlan(screen) 134 enemy5 = EnemyPlan(screen) 135 enemy6 = EnemyPlan(screen) 136 enemy7= EnemyPlan(screen) 137 enemy_group = pygame.sprite.Group(enemy,enemy1,enemy2,enemy3,enemy4,enemy5,enemy6,enemy7) 138 139 gamePlan =GamePlan(screen) 140 def key_control(gamePlan): 141 for event in pygame.event.get(): 142 #退出按钮 143 if event.type == QUIT: 144 print("exit") 145 exit() 146 elif event.type == CREAT_ENEMY: 147 enemy = EnemyPlan(screen) 148 enemy_group.add(enemy) 149 #按键 150 elif event.type == KEYDOWN: 151 if event.key == K_LEFT: 152 gamePlan.moveLeft() 153 elif event.key == K_RIGHT: 154 gamePlan.moveRight() 155 elif event.key == K_UP: 156 gamePlan.moveUp() 157 elif event.key == K_DOWN: 158 gamePlan.moveDown() 159 elif event.key == K_SPACE: 160 gamePlan.shootbullet() 161 def main(): 162 #创建一个窗口,用来显示内容 163 screen = pygame.display.set_mode((480,800),0,32) 164 #创建一个和窗口大小的图片,用来充当背景 165 background = pygame.image.load("./feiji/background.png").convert() 166 # 167 clock = pygame.time.Clock() 168 pygame.mixer.music.play(-1) 169 enemy_index = 0 170 plan_index = 0 171 score = 0 172 while True: 173 #设定需要显示的背景图 174 screen.blit(background,(0,0)) 175 # 刷新帧率 176 clock.tick(60) 177 gamePlan.display() 178 179 #让精灵组中所有精灵更新位置 180 enemy_group.update() 181 enemy_group.draw(screen) 182 183 for enemy in enemy_group: 184 enemy.move() 185 x1 = enemy.rect.x 186 x2 = enemy.rect.x + 51 187 y1 = enemy.rect.y 188 y2 = enemy.rect.y + 39 189 for bullet in gamePlan.bulletList: 190 x = bullet.x 191 y = bullet.y 192 a = x>x1 and x<x2 and y>y1 and y<y2 193 if a: 194 screen.blit(enemy.bombList[enemy_index],enemy.rect) 195 enemy_index = (enemy_index +1)%3 196 time.sleep(0.022) 197 if enemy_index == 0: 198 enemy_down_sound.play() 199 enemy.kill() 200 score +=10 201 c1 = gamePlan.rect.x 202 c2 = gamePlan.rect.x + 100 203 d1 = gamePlan.rect.y 204 b = c1<x2 and c2>x1 and d1<y2 205 if b: 206 screen.blit(enemy.bombList[enemy_index],enemy.rect) 207 screen.blit(gamePlan.bombList[plan_index],gamePlan.rect) 208 plan_index = (plan_index +1)%3 209 time.sleep(0.022) 210 if plan_index == 0: 211 me_down_sound.play() 212 says =("Game Over!") 213 my_font = pygame.font.SysFont("UbuntuMono-Bold1",84) 214 says_suface = my_font.render(says,True,(10,10,10)) 215 pygame.image.save(says_suface,"hello.png") 216 screen.blit(says_suface,(100,300)) 217 say = ("SCORE:%d"%score) 218 my_font = pygame.font.SysFont("UbuntuMono-Bold1",64) 219 say_surface = my_font.render(say,True,(0,0,0)) 220 pygame.image.save(say_surface,"12.png") 221 screen.blit(say_surface,(150,400)) 222 223 key_control(gamePlan) 224 pygame.display.update() 225 if __name__ =="__main__": 226 227 main()