day_5.17 飞机大战
ps:2018-7-24 20:58:11 重新整理这个飞机大战源码,我虽然这个时候没看源码,但是知道思路的话用其他语言还是可以写出来的!
1 ''' 2 2018-5-13 19:53:46 完善成功 3 一个打飞机的游戏 4 其实就是面向对象那个 5 有个主方法. 然后有飞机然后又子弹,逐步的迭代 6 7 2018-5-14 18:40:15 8 继续开干 9 2018-5-17 17:42:29 10 继续完善 11 2018-5-17 20:00:32 12 写基类(父类) 13 然后写其他的子类 14 ''' 15 import pygame 16 import time 17 from pygame.locals import * 18 import random 19 20 21 class EnemyPlane(object): 22 '''敌机的类''' 23 def __init__(self,screen_temp): 24 self.x=0 25 self.y=0 26 self.screen =screen_temp 27 self.image =pygame.image.load("./feiji/enemy0.png") 28 self.bullet_list = [] #储存发射出去子弹对象引用 29 self.direction ="right" #用来存储飞机默认的显式方向 30 31 def display(self): 32 self.screen.blit(self.image,(self.x,self.y)) 33 for bullet in self.bullet_list: 34 bullet.display() 35 bullet.move() 36 def move(self): 37 if self.direction =="right": 38 self.x+= 10 39 elif self.direction =="left": 40 self.x -= 10 41 if self.x>480-50: 42 self.direction ="left" 43 elif self.x<0: 44 self.direction ="right" 45 46 def fire(self): 47 random_num = random.randint(1,100) 48 if random_num ==8 or random_num ==20: 49 self.bullet_list.append(EnemyBullet(self.screen,self.x,self.y)) 50 51 class HeroPlane(object): 52 '''玩家飞机''' 53 def __init__(self,screen_temp): 54 self.x=210 55 self.y=700 56 self.screen =screen_temp 57 self.image =pygame.image.load("./feiji/hero1.png") 58 self.bullet_list = [] #储存发射出去子弹对象引用 59 def display(self): 60 self.screen.blit(self.image,(self.x,self.y)) 61 for bullet in self.bullet_list: 62 bullet.display() 63 bullet.move() 64 if bullet.judge(): #判断子弹是否越界 65 self.bullet_list.remove(bullet) 66 def move_left(self): 67 self.x -=10 68 def move_right(self): 69 self.x +=10 70 def fire(self): 71 self.bullet_list.append(Bullet(self.screen,self.x,self.y)) 72 73 class Bullet(object): 74 def __init__(self,screen_temp,x,y): 75 self.x=x+40 76 self.y=y-20 77 self.screen =screen_temp 78 self.image =pygame.image.load("./feiji/bullet.png") 79 def display(self): 80 self.screen.blit(self.image,(self.x,self.y)) 81 def move(self): 82 self.y-=20 83 def judge(self): #判断是否越界 84 if self.y <0: 85 return True 86 else: 87 return False 88 89 class EnemyBullet(object): 90 def __init__(self,screen_temp,x,y): 91 self.x=x+25 92 self.y=y+40 93 self.screen =screen_temp 94 self.image =pygame.image.load("./feiji/bullet1.png") 95 def display(self): 96 self.screen.blit(self.image,(self.x,self.y)) 97 def move(self): 98 self.y+=10 99 def judge(self): 100 if self.y >852: 101 return True 102 else: 103 return False 104 105 def key_control(hero_temp): 106 # 获取事件,比如按键等 107 for event in pygame.event.get(): 108 # 判断是否是点击了退出按钮 109 if event.type == QUIT: 110 print("exit") 111 exit() 112 # 判断是否是按下了键 113 elif event.type == KEYDOWN: 114 # 检测按键是否是a或者left 115 if event.key == K_a or event.key == K_LEFT: 116 print('left') 117 hero_temp.move_left() 118 # 检测按键是否是d或者right 119 elif event.key == K_d or event.key == K_RIGHT: 120 print('right') 121 hero_temp.move_right() 122 # 检测按键是否是空格键 123 elif event.key == K_SPACE: 124 print('space') 125 hero_temp.fire() 126 127 def main(): 128 #1.创建一个窗口 129 screen = pygame.display.set_mode((480,852),0,32) 130 #2. 创建一个背景图片 131 background = pygame.image.load("./feiji/background.png") 132 #3 创建一个飞机图片 133 hero = HeroPlane(screen) 134 #4.创建一个敌机 135 enemy = EnemyPlane(screen) 136 while True: 137 screen.blit(background,(0,0)) 138 hero.display() 139 enemy.display() 140 enemy.move()#调用敌机的移动方法 141 enemy.fire()#敌机开火 142 pygame.display.update() 143 key_control(hero) 144 time.sleep(0.1) 145 146 147 if __name__ =="__main__": 148 main()