您的位置:首页 > 编程语言 > Python开发

python飞机大战,初步实现代码

2019-03-24 16:10 453 查看
[code]import pygame,os,time,random
from pygame.locals import*
class EnemyBiu():
def __init__(self,x,y):
self.x = x
self.y = y
# 导入英雄机子弹
self.image = pygame.image.load(getImage("bullet1.png"))
def draw(self):
windows.blit(self.image,(self.x,self.y))
self.move()  # 移动
# 英雄机子弹发射
def move(self):
self.y += 5
class EnemyPlane():
def __init__(self,x,y):
self.x = x
self.y = y
# 正常图片列表
self.normalImageList = ["enemy1.png"]
# 索引号,第一次显示第一张0下标,下一次1下标第二张,下次0下标
self.normalImageIndex = 0
# 子弹列表
self.HeroBiuList = []
self.direct = "左"
def draw(self):
# 导入图片
image = pygame.image.load(getImage(self.normalImageList[self.normalImageIndex]))
# 切换图片
self.normalImageIndex = (self.normalImageIndex+1)%len(self.normalImageList)
# 画图
windows.blit(image,(self.x,self.y))
# 从子弹列表中取出子弹,一个个的画,并且删除超出屏幕的子弹
for zd in self.HeroBiuList:
zd.draw()
self.HeroBiuList.remove(zd) if zd.y > 520 else""
self.move()
self.fire()
def move(self):
if self.direct == "左":
self.x -= 5
if self.x <= 0:
self.direct = "右"
else:
self.x += 5
if self.x >= 480-69:
self.direct = "左"
def fire(self):
x = random.randint(0,100)
if x == 3 or x == 61:
zd = EnemyBiu(self.x + 69/2-9/2,self.y + 89)
self.HeroBiuList.append(zd)
class HeroBiu():
def __init__(self,x,y):
self.x = x
self.y = y
# 导入英雄机子弹
self.image = pygame.image.load(getImage("bullet.png"))
def draw(self):
windows.blit(self.image,(self.x,self.y))
self.move()  # 移动
# 英雄机子弹发射
def move(self):
self.y -= 5
class HeroPlane():
def __init__(self,x,y):
self.x = x
self.y = y
# 正常图片列表
self.normalImageList = ["hero1.png","hero2.png"]
# 索引号,第一次显示第一张0下标,下一次1下标第二张,下次0下标
self.normalImageIndex = 0
# 英雄机爆炸图片列表
self.bombImageList = ["hero_blowup_n1.png","hero_blowup_n2.png","hero_blowup_n3.png","hero_blowup_n4.png",]
# 英雄机爆炸索引
self.bombImageIndex = 0
# 子弹列表
self.HeroBiuList = []
self.isBomb = False
def draw(self):
if self.isBomb == False:
# 导入图片
image = pygame.image.load(getImage(self.normalImageList[self.normalImageIndex]))
# 切换图片
self.normalImageIndex = (self.normalImageIndex+1)%len(self.normalImageList)
windows.blit(image,(self.x,self.y)) # 画图
else:
if self.bombImageIndex == len(self.bombImageList):
time.sleep(0.6)
exit()
image = pygame.image.load(getImage(self.bombImageList[self.bombImageIndex]))
self.bombImageIndex+=1
windows.blit(image,(self.x,self.y))
time.sleep(0.8)
# 导入图片
# image = pygame.image.load(getImage(self.normalImageList[self.normalImageIndex]))
# 切换图片
# self.normalImageIndex = (self.normalImageIndex+1)%len(self.normalImageList)
# 画图
windows.blit(image,(self.x,self.y))
# 从子弹列表中取出子弹,一个个的画,并且删除超出屏幕的子弹
for zd in self.HeroBiuList:
zd.draw()
self.HeroBiuList.remove(zd) if zd.y < 0 else""
# 处理事件
def dealEvent(self,eventList):
# 遍历事件列表
for event in eventList:
# 如果是 退出
if event.type == QUIT:
exit()
# 如果是键盘按键
elif event.type == KEYDOWN:
# 如果是左键
if event.key == K_LEFT:
self.x = self.x-10 if self.x >= 5 else 0
# 如果是右键
elif event.key == K_RIGHT:
self.x = self.x+10 if self.x<480-100-5 else 480-100
# 发射子弹
elif event.key == K_SPACE:
zd = HeroBiu(self.x+50-22/2,self.y-22)
self.HeroBiuList.append(zd)
def pzjc(self,dfZdList):
jtRect = Rect(self.x+36,self.y,28,42)
jsRect = Rect(self.x,self.y+42,100,124-42)
for zd in dfZdList:
zdRect = Rect(zd.x,zd.y,9,21)
if zdRect.colliderect(jtRect) or zdRect.colliderect(jsRect):
self.isBomb = True
# 传入图片的文件名
def getImage(image):
# 下面第一个参数是图片的目录,这样把目录和文件名合成一个完整的绝对路径
return os.path.join("./img",image)
# 创建窗口
windows = pygame.display.set_mode((480,520),0,32)
# 设置标题
pygame.display.set_caption("灰灰")
# 导入图标
icon = pygame.image.load(getImage("icon72x72.png"))
# 把图标画到窗口左上角
pygame.display.set_icon(icon)
# 创建背景
bg = pygame.image.load(getImage("background.png"))
wofang = HeroPlane(480/2-100/2,520-124)
difang = EnemyPlane(480/2-69/2,0)
pygame.key.set_repeat(30,30)
while True:
windows.blit(bg,(0,0))
wofang.draw()
difang.draw()
wofang.pzjc(difang.HeroBiuList)
wofang.dealEvent(pygame.event.get())
pygame.display.update()
pass

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: