|
import pygame,sys,random,time,easygui
from pygame.locals import *
#初始化pygame环境
pygame.init()
c_q='welcome the airplane fight'
c_w=['STARTGAME','Game play']
starT=easygui.buttonbox(c_q,choices=c_w,title='startgame')
if starT=='Game play':
easygui.msgbox('''
Basic operation:
Use the mouse to control the movement of your plane, and it will automatically fire bullets. Press "f" to fire three bullets and deduct 30 points of energy. Every enemy plane needs a certain amount of bullets to defeat.
Detail settings:
Bullet firing interval:0.2second /a bullet
Energy increase: 2 energy / second
Number of bullets needed to defeat enemy aircraft and score table:
---------------------------------------------
type bullets needed score
---------------------------------------------
small 1 1
big 3 5
biggest 10 15
---------------------------------------------''',title='gameplay')
c_a='choose a gameMode'
c_c=['Easy','Normal','Difficulties','Classic']
gameMode=easygui.buttonbox(c_a, choices=c_c,title=c_a)
if gameMode=='Difficulties':
q=easygui.enterbox('Enter difficulties level(1-5)',title='Enter difficulties level(1-5)')
difficultiesLevel=int(q)
if difficultiesLevel<=2:
gameMode=='Difficulties'
if difficultiesLevel==3:
gameMode=='Difficulties+'
if difficultiesLevel==4:
gameMode=='Difficulties++'
if difficultiesLevel==5:
gameMode=='Difficulties+++'
if gameMode==None:
easygui.msgbox('''<Response[404]>
gamemodenotchoose!''',title='<Response[404]>')
pygame.quit()
sys.exit()
#创建一个长宽分别为480/650窗口
canvas = pygame.display.set_mode((480, 650))
canvas.fill((255,255,255))
music=pygame.mixer.Sound('images/bgmusic.wav')
Energy=100
#设置窗口标题
pygame.display.set_caption("airplane fight")
bg1=pygame.image.load("images/bg1.png")
bg2=pygame.image.load("images/bg2.png")
bg3=pygame.image.load("images/bg3.png")
bg4=pygame.image.load("images/bg4.png")
enemy1 = pygame.image.load("images/enemy1.png")
enemy2 = pygame.image.load("images/enemy2.png")
enemy3 = pygame.image.load("images/enemy3.png")
b=pygame.image.load("images/bullet1.png")
h=pygame.image.load("images/hero.png")
hd1=pygame.image.load("images/hero_down1.png")
hd2=pygame.image.load("images/hero_down2.png")
hd3=pygame.image.load("images/hero_down3.png")
hd4=pygame.image.load("images/hero_down4.png")
e1d1=pygame.image.load("images/enemy1_down1.png")
e1d2=pygame.image.load("images/enemy1_down2.png")
e1d3=pygame.image.load("images/enemy1_down3.png")
e1d4=pygame.image.load("images/enemy1_down4.png")
e1d5=pygame.image.load("images/enemy1_down5.png")
e2d1=pygame.image.load("images/enemy2_down1.png")
e2d2=pygame.image.load("images/enemy2_down2.png")
e2d3=pygame.image.load("images/enemy2_down3.png")
e2d4=pygame.image.load("images/enemy2_down4.png")
e2d5=pygame.image.load("images/enemy2_down5.png")
e3d1=pygame.image.load("images/enemy3_down1.png")
e3d2=pygame.image.load("images/enemy3_down2.png")
e3d3=pygame.image.load("images/enemy3_down3.png")
e3d4=pygame.image.load("images/enemy3_down4.png")
e3d5=pygame.image.load("images/enemy3_down5.png")
e3d6=pygame.image.load("images/enemy3_down6.png")
e3d7=pygame.image.load("images/enemy3_down7.png")
a=pygame.image.load("images/herolife.png")
hdil=[hd1,hd2,hd3,hd4]
e1dil=[e1d1,e1d2,e1d3,e1d4,e1d5]
e2dil=[e2d1,e2d2,e2d3,e2d4,e2d5]
e3dil=[e3d1,e3d2,e3d3,e3d4,e3d5,e3d6,e3d7]
#添加时间间隔的方法
def isActionTime(lastTime, interval):
if lastTime == 0:
return True
lastTime=time.time()
currentTime = time.time()
return currentTime - lastTime >= interval
lastTime=time.time()
#定义Sky类
class Sky():
def __init__(self):
self.width = 480
self.height = 852
self.x1 = 0
self.y1 = 0
self.x2 = 0
self.y2 = -self.height
#创建paint方法
def paint(self):
if gameMode=='Easy':
canvas.blit(bg2,(self.x1,self.y1))
canvas.blit(bg2,(self.x2,self.y2))
if gameMode=='Normal':
canvas.blit(bg1,(self.x1,self.y1))
canvas.blit(bg1,(self.x2,self.y2))
if gameMode=='Difficulties':
canvas.blit(bg3,(self.x2,self.y2))
canvas.blit(bg3,(self.x1,self.y1))
if gameMode=='Classic':
canvas.blit(bg4,(self.x1,self.y1))
canvas.blit(bg4,(self.x2,self.y2))
if gameMode=='Difficulties+':
canvas.blit(bg3,(self.x2,self.y2))
canvas.blit(bg3,(self.x1,self.y1))
if gameMode=='Difficulties++':
canvas.blit(bg3,(self.x2,self.y2))
canvas.blit(bg3,(self.x1,self.y1))
if gameMode=='Difficulties+++':
canvas.blit(bg3,(self.x2,self.y2))
canvas.blit(bg3,(self.x1,self.y1))
#创建step方法
def step(self):
self.y1 = self.y1 + 3
self.y2 = self.y2 + 3
if self.y1 > self.height:
self.y1 = -self.height
if self.y2 > self.height:
self.y2 = -self.height
#定义父类FlyingObject
class FlyingObject(object):
def __init__ (self,x,y,width,height,life,img):
self.x = x
self.y = y
self.width = width
self.height = height
self.life = life
self.img = img
#敌飞机移动的时间间隔
self.lastTime = 0
self.interval = 0.01
#添加删除属性
self.canDelete = False
#定义paint方法
def paint(self):
canvas.blit(self.img,(self.x,self.y))
#定义step方法
def step(self):
#判断是否到了移动的时间间隔
if not isActionTime(self.lastTime,self.interval):
return
self.lastTime = time.time()
#控制移动速度
self.y = self.y + 10
#定义hit方法判断两个对象之间是否发生碰撞
def hit(self,component):
c = component
return c.x>self.x-c.width and c.x<self.x+self.width and \
c.y>self.y-c.height and c.y<self.y+self.height
#定义bang方法处理对象之间碰撞后的处理
def bang(self,bangsign):
#敌机和英雄机碰撞之后的处理
if bangsign:
if hasattr(self,'score'):
GameVar.score += self.score
if bangsign == 2:
self.life -= 1
#设置删除属性为True
self.canDelete = True
#敌机和子弹碰撞之后的处理
else:
self.life -= 1
if self.life == 0:
#设置删除属性为True
self.canDelete = True
if hasattr(self,'score'):
GameVar.score += self.score
#定义outOfBounds方法判断对象是否越界
def outOfBounds(self):
return self.y > 650
#重构Enemy类
class Enemy(FlyingObject):
def __init__(self,x,y,width,height,type,life,score,img):
FlyingObject.__init__(self,x,y,width,height,life,img)
self.type = type
self.score = score
shootLastTime=0
self.shootinterval=1.5
#重构Hero类
class Hero(FlyingObject):
def __init__(self,x,y,width,height,life,img):
FlyingObject.__init__(self,x,y,width,height,life,img)
self.x = 480/2-self.width/2
self.y = 650-self.height-30
self.shootLastTime = 0
self.shootInterval = 0.15
def shoot(self):
if not isActionTime(self.shootLastTime,self.shootInterval):
return
else:
self.shootLastTime = time.time()
GameVar.bullets.append(Bullet(self.x+self.width/2-5,self.y-10,10,10,1,b))
#重构Bullet类
class Bullet(FlyingObject):
def __init__(self,x,y,width,height,life,img):
FlyingObject.__init__(self,x,y,width,height,life,img)
def step(self):
self.y = self.y -10
#重写outOfBounds方法判断子弹是否越界
def outOfBounds(self):
return self.y < -self.height
def componentDown(list,x,y):
for i in range(len(list)):
canvas.blit(list,(x,y))
pygame.time.delay(30)
|
|