**第一步骤:**首先上网下载飞机大战所需要用到的图包,在导入图片的过程中注意图片的标识名字要
与程序内相同。
**第二步骤:**当飞机大战所需要的素材包准备完成后,下面我们开始对玩家操控的飞机的程序进行编写
程序如下:
import pygame
import random
import time
class Regou:
def init(self,plane,screen):
self.screen = screen
self.image = pygame.image.load(r"imgs\rg.png") # 加载热狗图片并获取外接矩形
self.rect = self.image.get_rect() # 热狗矩形
self.rect.centerx =plane.rect.centerx # 把热狗放在飞机中间顶部的位置
self.rect.bottom =plane.rect.top
left=self.rect.left
def printRegou(self):
self.screen.blit(self.image,self.rect)
def move(self):
self.rect.bottom=self.rect.bottom-26
return self.rect.centerx
class Plane():
def init(self,screen):
self.screen = screen
self.image = pygame.image.load(r"imgs\plane3.png") # 加载飞机图片并获取外接矩形
self.rect=self.image.get_rect() #飞机矩形
self.screen_rect=screen.get_rect() #屏幕矩形
self.rect.centerx=self.screen_rect.centerx #把飞机放在屏幕的底部中间
self.rect.bottom=self.screen_rect.bottom
#将飞机图片画到背景图里的指定位置
def printPlane(self):
self.screen.blit(self.image,self.rect)
class Wsc:
def init(self,wsc_location,screen):
self.screen=screen
self.wsc_location=wsc_location
self.image = pygame.image.load(r"imgs\wsc.png") # 加载王思聪图片并获取外接矩形
self.rect=self.image.get_rect() # 王思聪矩形
self.screen_rect = screen.get_rect() # 屏幕矩形
self.rect.centerx = wsc_location # 把王思聪放在顶部的位置
self.rect.bottom = self.screen_rect.top+70
# 王思聪画到背景图里
def printWsc(self):
self.screen.blit(self.image,self.rect)
def move(self):
self.rect.bottom=8+self.rect.bottom
return self.rect.centerx
**第三步骤:**进行生成敌机以及敌机击杀判定相关程序的编写。
程序如下:
#创建飞机对象,一个储存敌人的列表,一个储存子弹的列表,一个判断飞机是否死了的变量isDie
plane=Plane(window)
wscs=[]
regous=[]
begingame=[-1]
#一个在随机的位置产生敌人的函数
def createWscs():
wsc_location1 = 45
wsc_location2 = 70
wsc_location3 = 100
list = []
list1 = []
list2 = []
list3 = []
#将敌人的位置放在三个列表中
for i in range(1, 9):
list1.append(wsc_location1)
wsc_location1 = wsc_location1 + 100
for i in range(1, 9):
list2.append(wsc_location2)
wsc_location2 = wsc_location2 + 100
for i in range(1, 9):
list3.append(wsc_location3)
wsc_location3 = wsc_location3 + 100
list.append(list1)
list.append(list2)
list.append(list3)
# 取一个随机数,随机得到敌人的位置,实现在随机位置产生敌人的效果
for i in range(1, random.randint(2,8)):
wsc = Wsc(list[random.randint(0,2)][random.randint(0,7)], window)
wscs.append(wsc)
wsc_location1 = wsc_location1 + 100
#将出界的敌人和子弹,相互碰撞的敌人和子弹删除掉
def delwscregou():
for i in regous:
for j in wscs:
#如果子弹的上边在敌人里面,并且子弹的左边或右边在敌人里面,表示子弹命中了敌人,则删除子弹对象和敌人对象
if j.rect.bottom>=i.rect.top>=j.rect.top and (j.rect.left<=i.rect.left<=j.rect.right or j.rect.left<=i.rect.right<=j.rect.right):
#我也不知道为什么要加这个判断,不加的话会报错 list.remove(x): x not in list
if i in regous:
regous.remove(i)
wscs.remove(j)
scorebuf[0]=scorebuf[0]+10
scorebuf[1]=scorebuf[1]+2
#如果子弹或敌人出了边界,删除敌人
for i in regous:
if i.rect.top<0:
if i in regous:
regous.remove(i)
for j in wscs:
if j.rect.bottom>600:
wscs.remove(j)
**第四步骤:**最后进行游戏主界面、外设操作事件监听、得分显示及其他功能的程序编写。
程序如下:
游戏主界面相关:
#设置窗口高和宽,窗口名字
window=pygame.display.set_mode([800,600])
pygame.display.set_caption(“飞机大战”)
#加载背景图和人物 1920x1080
background=pygame.image.load(r"imgs\bg.jpg").convert()
#将游戏的分数、buf值、生命值、最高分都存放在一个列表中
scorebuf=[]
score=0
buf=100
life=100
heightestscore=0
scorebuf.append(score)
scorebuf.append(buf)
scorebuf.append(life)
scorebuf.append(heightestscore)
#开始界面
def begininterface():
background = pygame.image.load(r"imgs\bg.jpg").convert() # 加载背景图和人物 1920x1080
window.blit(background, (0, 0))
font1 = pygame.freetype.Font(“c://windows//Fonts//msyh.ttc”, 30)
fontrect1 = font1.render_to(window, (330, 200), “1、开始游戏”, (255, 255, 0))
font2 = pygame.freetype.Font(“c://windows//Fonts//msyh.ttc”, 30)
fontrect2 = font2.render_to(window, (330, 250), “2、游戏玩法”, (255, 255, 0))
font3 = pygame.freetype.Font(“c://windows//Fonts//msyh.ttc”, 30)
fontrect3 = font3.render_to(window, (330, 300), “3、最高得分”, (255, 255, 0))
font4 = pygame.freetype.Font(“c://windows//Fonts//msyh.ttc”, 30)
fontrect4 = font4.render_to(window, (330, 350), “4、关于游戏”, (255, 255, 0))
def howtoplay():
background = pygame.image.load(r"imgs\howtoplay.jpg").convert()
window.blit(background, (0, 0))
def aboutgame():
background = pygame.image.load(r"imgs\aboutgame.jpg").convert()
window.blit(background, (0, 0))
def gethightest():
background = pygame.image.load(r"imgs\aboutgame.jpg").convert() # 加载背景图和人物 1920x1080
font1 = pygame.freetype.Font(“c://windows//Fonts//msyh.ttc”, 50)
fontrect1 = font1.render_to(window, (200, 250), “最高分记录为:”+str(scorebuf[3]), (255, 0, 0))
在屏幕上方写下得分、血量、buff值
font = pygame.freetype.Font("c://windows//Fonts//msyh.ttc", 30)
fontrect = font.render_to(window, (20,0), "score:"+" "+str(scorebuf[0]), (255,255,0))
font = pygame.freetype.Font("c://windows//Fonts//msyh.ttc", 30)
fontrect = font.render_to(window, (320, 0), "life:" + " " + str(scorebuf[2]),(255,255,0))
font = pygame.freetype.Font("c://windows//Fonts//msyh.ttc", 30)
fontrect = font.render_to(window, (620, 0), "buff:" + " " + str(scorebuf[1]),(255,255,0))
pygame.display.flip()
外设操作监听事件相关:
#把事件监听都封装到一个函数里面
def keyboradcontrol():
for even in pygame.event.get():
if even.typeQUIT: # 事件监听机制,监听鼠标,点击屏幕右上角x关闭游戏
exit()
if even.type == pygame.KEYDOWN: # 事件监听机制,监听键盘,按下上下左右键分别改变飞机的位置,并且发射子弹
if even.key == pygame.K_a:
wscs=[]
if even.keypygame.K_LEFT:
plane.rect.centerx = plane.rect.centerx-20
if even.keypygame.K_UP:
plane.rect.bottom = plane.rect.bottom-20
if even.keypygame.K_DOWN:
plane.rect.bottom = plane.rect.bottom+20
if even.key==pygame.K_RIGHT:
plane.rect.centerx=plane.rect.centerx+20
if even.key == pygame.K_SPACE and scorebuf[2]>0:
regou = Regou(plane, window)
regous.append(regou)
if even.key == pygame.K_KP1:
begingame[0]=1
pygame.mixer.music.load(r"imgs\music.mp3") # 加载背景音乐
pygame.mixer.music.play(-1, 0)
scorebuf[0] = 0
scorebuf[1] = 100
scorebuf[2] = 100
plane.image = pygame.image.load(r"imgs\plane3.png")
scorebuf[0]=0
if even.key == pygame.K_KP2:
begingame[0] = 2
if even.key == pygame.K_KP3:
begingame[0]=3
if even.key == pygame.K_KP4:
begingame[0]=4
if even.key == pygame.K_RETURN:
begininterface()
得分显示相关:
#飞机死亡之后在屏幕中间写下最后得分
if scorebuf[2] < 0:
font = pygame.freetype.Font(“c://windows//Fonts//msyh.ttc”, 70)
fontrect = font.render_to(window, (70, 280), “本局最后得分:” + " " + str(scorebuf[0]), (255, 0, 0))
#死亡后将最后得分与以前的最高得分相比,去较大值存入列表中
if scorebuf[2]<0:
if scorebuf[0] > scorebuf[3]:
scorebuf[3] = scorebuf[0]
plane.image = pygame.image.load(r"imgs\bg.jpg")
regous=[]
wscs=[]
“必杀技”功能相关:
def dazhao():
zidan=Zidan(10,window)
zidan.printZidan()
createWscs()
#begininterface()
while True:
keyboradcontrol()
begininterface()
if begingame[0] == 2:
howtoplay()
keyboradcontrol()
if begingame[0] == 3:
gethightest()
if begingame[0] == 4:
aboutgame()
if begingame[0]==1:
if wscs.len()<20 and scorebuf[2]>0: #如果敌人人数少于20个并且飞机没死,则创建敌人
createWscs()
window.blit(background,(0,0)) # 将背景图传入到窗口中
# 画出飞机、所有的敌人和子弹
plane.printPlane()
for i in wscs:
i.printWsc()
buttom=i.move()
for i in regous:
i.printRegou()
top=i.move()
#将出界的敌人和子弹,相互碰撞的敌人和子弹删除掉
delwscregou()
#keyboradcontrol()中的代码
for even in pygame.event.get():
if even.type == QUIT: # 事件监听机制,监听鼠标,点击屏幕右上角x关闭游戏
exit()
if even.type == pygame.KEYDOWN: # 事件监听机制,监听键盘,按下上下左右键分别改变飞机的位置,并且发射子弹
if even.key == pygame.K_a and scorebuf[1]>=100:
wscs = []
scorebuf[1]=scorebuf[1]-100
scorebuf[0] = scorebuf[0]+200
if even.key == pygame.K_LEFT:
plane.rect.centerx = plane.rect.centerx - 20
if even.key == pygame.K_UP:
plane.rect.bottom = plane.rect.bottom - 20
if even.key == pygame.K_DOWN:
plane.rect.bottom = plane.rect.bottom + 20
if even.key == pygame.K_RIGHT:
plane.rect.centerx = plane.rect.centerx + 20
if even.key == pygame.K_SPACE and scorebuf[2] > 0:
regou = Regou(plane, window)
regous.append(regou)
if even.key == pygame.K_KP1:
begingame[0] = 1
pygame.mixer.music.load(r"imgs\music.mp3") # 加载背景音乐
pygame.mixer.music.play(-1, 0)
scorebuf[0] = 0
scorebuf[1] = 100
scorebuf[2] = 100
plane.image = pygame.image.load(r"imgs\plane3.png")
scorebuf[0] = 0
if even.key == pygame.K_KP2:
begingame[0] = 2
if even.key == pygame.K_KP3:
begingame[0] = 3
if even.key == pygame.K_KP4:
begingame[0] = 4
if even.key == pygame.K_RETURN:
begininterface()
for j in wscs:
if j.rect.bottom>=plane.rect.top>=j.rect.top and (j.rect.left<=plane.rect.left<=j.rect.right or j.rect.left<=plane.rect.right<=j.rect.right):
scorebuf[2] = scorebuf[2] - 40
但不知道为什么用封装的函数无法调用keyboradcontrol(),只能将keyboradcontrol()函数中的代码复制下来。
**第五步骤:**完成以上4个步骤之后,飞机大战小游戏的编写基本完成,最后通过试运行检查,进行最后的
调试。