import pygame
from pygame.locals import *
import time
import random
class Biu():
def __init__(self,x,y,windows):
self.x=x
self.y=y
self.windows=windows
self.pic=pygame.image.load('C:\\Users\\Administrator\\Desktop\\img\\bullet.png')#获取我方子弹的图片
def draw(self):
self.windows.blit(self.pic,(self.x,self.y))#画图片
self.move()
def move(self):
self.y-=2
class EnemyBiu():#敌方子弹射击
def __init__(self,x,y,windows):
self.x=x
self.y=y
self.windows=windows
self.pic=pygame.image.load('C:\\Users\\Administrator\\Desktop\\img\\bullet1.png')
def draw(self):
self.windows.blit(self.pic,(self.x,self.y))
self.move()
def move(self):
self.y+=2
windows=pygame.display.set_mode((480,552),0,32)#设置窗口的大小
pygame.display.set_caption('飞机大战')#设置名称
background=pygame.image.load('C:\\Users\\Administrator\\Desktop\\img\\background.png')
icon=pygame.image.load('C:\\Users\\Administrator\\Desktop\\img\\icon72x72.png')
heroPlane1=pygame.image.load('C:\\Users\\Administrator\\Desktop\\img\\hero1.png')
heroPlane2=pygame.image.load('C:\\Users\\Administrator\\Desktop\\img\\hero2.png')
enemyPlane=pygame.image.load('C:\\Users\\Administrator\\Desktop\\img\\enemy1.png')#敌方飞机
enmeyBombPicList=['C:\\Users\\Administrator\\Desktop\\img\\enemy1_down1.png',
'C:\\Users\\Administrator\\Desktop\\img\\enemy1_down2.png',
'C:\\Users\\Administrator\\Desktop\\img\\enemy1_down3.png',
'C:\\Users\\Administrator\\Desktop\\img\\enemy1_down4.png']#敌方飞机爆炸图片1-4
enmeyBombPicIndex=0#敌方飞机爆炸图片索引
heroPlaneBombPicList=['C:\\Users\\Administrator\\Desktop\\img\\hero_blowup_n1.png',
'C:\\Users\\Administrator\\Desktop\\img\\hero_blowup_n2.png',
'C:\\Users\\Administrator\\Desktop\\img\\hero_blowup_n3.png',
'C:\\Users\\Administrator\\Desktop\\img\\hero_blowup_n4.png',
]#我方飞机爆炸图片1-4
heroPlaneBombIndex=0#我方飞机爆炸图片索引
pIndex=0#
playList=[heroPlane1,heroPlane2]
pygame.display.set_icon(icon)#设置图标icon
heroPlaneX=480//2-100//2#计算飞机的左上点位置,起始点x位置
heroPlaneY=552-124#计算飞机的左上点,起始点y的位置
enemyPlaneX=480//2-69//2#计算敌机左上点,起始点x
enemyPlaneY=0#计算敌机左上点,起始点x
pygame.key.set_repeat(30,30)#设置移动速度
direct='左'
zdList=[]
djzdList=[]#
def dealEvent(evls):
for event in evls:
if event.type==QUIT:
exit(0)
elif event.type==KEYDOWN:
global heroPlaneX
global heroPlaneY
if event.key==K_a:
heroPlaneX-=5 if heroPlaneX>=5 else 0
elif event.key==K_d:
heroPlaneX +=5 if heroPlaneX<=380 else 380
elif event.key==K_w:
heroPlaneY-=5 if heroPlaneY>5 else 0
elif event.key==K_s:
heroPlaneY+=5 if heroPlaneY<=428 else 428
elif event.key==K_SPACE:
zd=Biu(heroPlaneX+50-22//2,heroPlaneY-22,windows)
zdList.append(zd)
#敌方飞机被击毁
isBomb=False
PlaneisBomb=False
playerBlow_index=0
while True:
windows.blit(background,(0,0))#设置背景坐标
windows.blit(playList[pIndex],(heroPlaneX,heroPlaneY))
if not isBomb:
windows.blit(enemyPlane,(enemyPlaneX,enemyPlaneY))
if direct=='左':
enemyPlaneX-=3
if enemyPlaneX<=0:
direct='右'
elif direct=='右':
enemyPlaneX+=3
if enemyPlaneX>=480-69:
direct='左'
else:
if enmeyBombPicIndex==len(enmeyBombPicList):#敌方飞机毁灭
time.sleep(0.6)
exit(0)
img=pygame.image.load(enmeyBombPicList[enmeyBombPicIndex])
windows.blit(img,(enemyPlaneX,enemyPlaneY))
enmeyBombPicIndex+=1
time.sleep(0.2)
if not PlaneisBomb:
# windows.blit(background,(0,0))
windows.blit(playList[pIndex],(heroPlaneX,heroPlaneY))
pIndex=(pIndex+1)%len(playList)#
playract=Rect(heroPlaneX,heroPlaneY,100,124)
else:
if heroPlaneBombIndex==len(heroPlaneBombPicList):
time.sleep(0.6)
exit(0)
img=pygame.image.load(heroPlaneBombPicList[heroPlaneBombIndex])
windows.blit(img,(heroPlaneX,heroPlaneY))
heroPlaneBombIndex+=1
time.sleep(0.2)
for zd in zdList:
zd.draw()
zdList.remove(zd) if zd.y<0 else ''
for zd in djzdList:
zd.draw()
djzdList.remove(zd)if zd.y<0 else ''
pIndex=(pIndex+1)%len(playList)
pygame.display.update()
djRect=Rect(enemyPlaneX,enemyPlaneY,69,89)
planeRect=Rect(heroPlaneX,heroPlaneY,100,122)
for zd in zdList:
zdRect=Rect(zd.x,zd.y,22,22)
if zdRect.colliderect(djRect):
isBomb=True
zdList.remove(zd)
for djzd in djzdList:
djzdRect=Rect(djzd.x,djzd.y,9,21)
if djzdRect.colliderect(planeRect):
PlaneisBomb=True
djzdList.remove(zd)
r=random.randint(1,100)
if r==22 or r==77:
zd=EnemyBiu(enemyPlaneX+69//2-9//2,enemyPlaneY+89,windows)
djzdList.append(zd)
dealEvent(pygame.event.get())
07-24