飞机大战综合性知识的应用
我方和敌方:
我方限定范围通过按键控制飞机发射子弹
敌方限定范围通过随机发射子弹
子弹和飞机通过矩形碰撞检测,发生爆炸,显示爆炸效果,退出游戏
from pygame.locals import *
import pygame,os,time,random
def getImage(image):
return os.path.join('E:\\Python\\IT研究院-Python\\New_Stydy\\img',image )
def getImages(images):
return os.path.join('E:\\Python\\IT研究院-Python\\New_Stydy\\img',images )
class Biu():#设置战机子弹类
def __init__(self,x,y,windows):
self.x=x
self.y=y
self.windows=windows
self.image=pygame.image.load('E:\\Python\\IT研究院-Python\\New_Stydy\\img\\bullet2.png')
def draw(self):
self.windows.blit(self.image,(self.x,self.y))
self.move()
def move(self):
self.y-=4#子弹移动方向
class DjBiu():#设置敌机子弹类
def __init__(self,x,y,windows):
self.x=x
self.y=y
self.windows=windows
self.image=pygame.image.load('E:\\Python\\IT研究院-Python\\New_Stydy\\img\\biu.gif')
def draw(self):
self.windows.blit(self.image,(self.x,self.y))
self.move()
def move(self):#子弹移动方向
self.y+=5
windows=pygame.display.set_mode((480,520),0,32)
backGround=pygame.image.load('E:\\Python\\IT研究院-Python\\New_Stydy\\img\\background.png')
pygame.display.set_caption('灰机大战')
cap=pygame.image.load('E:\\Python\\IT研究院-Python\\New_Stydy\\img\\enemy-1.gif')
pygame.display.set_icon(cap)
pygame.key.set_repeat(30,30)
#设置键盘的灵敏度,第一个参数代表30毫秒后开始反应,第二个参数代表30毫秒没开始,继续下一次按键
feiji1=pygame.image.load('E:\\Python\\IT研究院-Python\\New_Stydy\\img\\hero1.png')
feiji2=pygame.image.load('E:\\Python\\IT研究院-Python\\New_Stydy\\img\\hero2.png')
djBombImageList=['enemy0_down1.png','enemy0_down2.png','enemy0_down3.png','enemy0_down4.png']
wfBombImageList=['hero_blowup_n1.png','hero_blowup_n2.png','hero_blowup_n3.png','hero_blowup_n4.png']
index_fj=0
fjx=480//2-100//2#设置英雄飞机x坐标
fjy=520-124#设置英雄飞机y坐标
diji=pygame.image.load('E:\\Python\\IT研究院-Python\\New_Stydy\\img\\enemy0.png')
djx=480//2-51//2
djy=0
direct='左'
djIsBomb=False
djBombImageIndex=0
wfIsBomb=False
wfBombImageIndex=0
zdList=[]
djzdList=[]
#敌机坐标
while True:
windows.blit(backGround,(0,0))
if index_fj==0:
windows.blit(feiji1,(fjx,fjy))
index_fj=1
else:
windows.blit(feiji2, (fjx, fjy))
index_fj = 0
if djIsBomb==False:
windows.blit(diji, (djx, djy))
else:
if djBombImageIndex==len(djBombImageList):
time.sleep(0.8)
exit(0)
image=pygame.image.load(getImage(djBombImageList[djBombImageIndex]))
windows.blit(image,(djx,djy))
djBombImageIndex+=1
time.sleep(0.5)
if wfIsBomb==False:#子弹坐标
windows.blit(feiji1, (fjx, fjy))
windows.blit(feiji2, (fjx, fjy))
else:
if wfBombImageIndex==len(wfBombImageList):
time.sleep(0.8)
exit(0)
images=pygame.image.load(getImage(wfBombImageList[wfBombImageIndex]))#敌方子弹放到列表
windows.blit(images,(fjx,fjy))
wfBombImageIndex+=1
time.sleep(0.5)
for zd in zdList:#画我方子弹
zd.draw()
zdList.remove(zd) if zd.y<0 else None
for djzd in djzdList:#画敌方子弹
djzd.draw()
djzdList.remove(djzd) if djzd.y>520 else None
if direct=='左':#如果向左走那么,横坐标-2
djx-=2
if djx<=0:#如果坐标小于0了,必须向右
direct='右'
elif direct=='右':#如果向右走,那么横坐标-2
djx+=2
if djx>=480-51:
direct='左'
for event in pygame.event.get():#遍历消息
if event.type==QUIT:#退出
exit(0)
elif event.type==KEYDOWN:#按键
if event.key==K_LEFT :#左键
fjx=fjx-5 if fjx>=5 else 0
if event.key==K_RIGHT:
fjx=fjx+5 if fjx<=480-100-5 else 480-100
if event.key==K_SPACE:#发射子弹键盘设置
zd=Biu(fjx+50-22/2,fjy-22,windows)
zdList.append(zd)
x=random.randint(0,100)
if x==3 :
djzd = DjBiu(djx + 51/2 - 22 / 2, djy+39, windows)
djzdList.append(djzd)
pygame.display.update()
wfRect=Rect(fjx,fjy,51,39)#我方机碰撞检测
for djzd in djzdList:
djzdRect=Rect(djzd.x,djzd.y,22,22)
if djzdRect.colliderect(wfRect):
wfIsBomb=True
djzdList.remove(djzd)
djRect=Rect(djx,djy,100,124)#敌机碰撞检测
for zd in zdList:
zdRect=Rect(zd.x,zd.y,22,22)
if zdRect.colliderect(djRect):
djIsBomb=True
zdList.remove(zd)
pass