# 飞机大战迭代

import pygame
import time
from pygame.locals import *
import random
class HeroPlan(object):
def __init__(self,screen_temp):
self.x=205
self.y=500
self.screen=screen_temp
self.bullet_list=[]#
def display(self):
#显示英雄飞机
self.screen.blit(self.image,(self.x,self.y))
#显示子弹
for bulet in self.bullet_list:
bulet.display()
if bulet.judge():   #如果越界删除
self.bullet_list.remove(bulet)  #删除元素
bulet.move()
def move_left(self):
self.x=self.x-5
def move_right(self):
self.x=self.x+5
def plan_fire(self):
self.bullet_list.append(Butten(self.screen,self.x,self.y))

class Butten(object):
def __init__(self,screen_temp,temp_x,temp_y):
self.x=temp_x+40
self.y=temp_y-20
self.screen=screen_temp

def judge(self):
if self.y < 200:
return True
else:
return False

def display(self):
self.screen.blit(self.image, (self.x, self.y))
def move(self):
self.y=self.y-5

class Enemyplan(object):
def __init__(self,screen_temp):
self.x=0
self.y=0
self.screen=screen_temp
self.flag=1
self.bullet_list=[]
def dispaly(self):
self.screen.blit(self.image, (self.x, self.y))
self.move()
for bulet in self.bullet_list:
bulet.display()
if bulet.judge():  # 如果越界删除
self.bullet_list.remove(bulet)  # 删除元素
bulet.move()

def move(self):   #左右移动  碰撞检测
if self.flag==1:
self.x+=5
elif self.flag==2:
self.x-=5
if self.x > 410:
self.flag = 2
elif self.x < 0:
self.flag = 1
#self.plan_fire()
def plan_fire(self):
number=random.randint(1,100)
if number==8 or number==25 or number==75 or number ==100:
self.bullet_list.append(EnemyButten(self.screen, self.x, self.y))

class EnemyButten(object):
def __init__(self,screen_temp,temp_x,temp_y):
self.x=temp_x+25
self.y=temp_y+90
self.screen=screen_temp

def judge(self):
if self.y >852:
return True
else:
return False

def display(self):
self.screen.blit(self.image, (self.x, self.y))
def move(self):
self.y=self.y+5

for event in pygame.event.get():
# 判断是否是点击了退出按钮
if event.type == QUIT:
print("exit")
exit()
# 判断是否是按下了键
elif event.type == KEYDOWN:
# 检测按键是否是a或者left
if event.key == K_a or event.key == K_LEFT:
plan.move_left()
print('left')
# 检测按键是否是d或者right
elif event.key == K_d or event.key == K_RIGHT:
plan.move_right()
print('right')
# 检测按键是否是空格键
elif event.key == K_SPACE:
plan.plan_fire()
print('space')

def main():
#创建窗口
screen=pygame.display.set_mode((480,850),0,32)
#创建背景图片
#创建飞机图片
plan = HeroPlan(screen)
enemyplan=Enemyplan(screen)
while True:
#确定图片 显示的坐标
screen.blit(background,(0,0))
plan.display()
enemyplan.dispaly()
enemyplan.move()
enemyplan.plan_fire()
#显示到屏幕
pygame.display.update()
main()