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