模块化拆分飞机小对决
一,类的创建
1,基类创建
2,子弹类创建
3,我方飞机类与敌方飞机类的创建
二,各个对象之间的互相影响的逻辑函数
1,击败函数
2,子弹回收函数
3,随机持续出现敌机函数
三,三个界面的搭建
1,开始介绍界面
2,战斗界面
3,结束结算界面
四,键盘事件,音乐,对象等硬件的搭建函数
1,键盘事件的获取与设置
2,音乐创建加入
3,对象创建
五,主函数
六,打包为exe
一(1):
import pygame
import sys
import time
import random
from pygame.locals import *
ii=0
score=0
class Base(object):
'''
所有的基类
'''
def __init__(self,filepath):
'''
基本属性属性
:param filepath: 文件位置
'''
self.x=0
self.y=0
self.rect=pygame.Rect(0,0,350,500)
self.image=pygame.image.load(filepath)
self.bulletlist=[]
pass
pass
ii变量用于界面转换,score用于计分 pygame基础的了解 Rect获取对象的矩形,方便击败逻辑的判断 一(2),
class Bullet(Base):
'''
子弹类
'''
def __init__(self):
super().__init__(filepath='./feitu/bullet.png')
self.rect=pygame.Rect(self.x,self.y,10,10)
self.pw=0
self.fa=0
pass
def move(self):
if self.fa==0:
self.y-=5
pass
if self.fa==1:
self.y+=5
pass
pass
def judge(self):
if self.y<-10 or self.y>500:
return True
else:
return False
pass
pass
judge函数判断子弹是否出界,此处也可以用矩形方法判断(自由发挥,@我评论,谢谢 ) 一(3): 我方飞机类:
class Woplane(Base):
'''创造我方飞机类'''
def __init__(self):
super(Woplane, self).__init__(filepath='./plane1.1/wo.png')
self.x=150
self.y=450
self.rect=pygame.Rect(self.x,self.y,50,50)
self.fail=0
pass
def mvr(self):
'''
右移
'''
if self.x<300:
self.x+=12.5
pass
pass
def mvl(self):
'''
left
'''
if self.x>0:
self.x-=12.5
pass
pass
def mvu(self):
'''
up
'''
if self.y>0:
self.y-=12.5
pass
pass
def mvd(self):
'''
down
'''
if self.y<450:
self.y+=12.5
pass
pass
def keydown(self):
pass
def keyup(self):
pass
def presskey(self):
pass
def fire(self):
bullet=Bullet()
bullet.x=self.x+20
bullet.y=self.y-11
self.bulletlist.append(bullet)
pass
pass
ps: 由于使用持续按压函数时键盘事件不好操纵,会无法使用单点键盘移动。与时间的调节有关。(哪个小伙伴试出来合适的了记得@我评论,谢谢) 敌方飞机类
class Diplane(Base):
'''创造敌方飞机类'''
def __init__(self):
super().__init__(filepath='./plane1.1/di.png')
self.x=150
self.y=0
self.rect=pygame.Rect(self.x,self.y,50,50)
self.fail=0
self.direction='r'
pass
def __del__(self):
pass
def move(self):
'''
敌机随机左右移动
'''
if self.direction=='r':
if self.x<300:
self.x+=25
pass
elif self.direction == 'l':
if self.x>0:
self.x-=25
pass
pass
pass
def fire(self):
'''
敌机随机开火
'''
num=random.randint(1,30)
if num==10:
self.direction='r'
pass
elif num==20:
self.direction='l'
pass
else:
self.direction='s'
if num==8 or num==22:
bullet = Bullet()
bullet.x=self.x+20
bullet.y=self.y+50
bullet.fa=1
self.bulletlist.append(bullet)
pass
pass
ps:可以分离random操作,更方便控制 二(1):击败函数
def ji(screen,planefa,plane_fail):
'''
判断是否击中,并展示击中特效
:param planefa: 发射子弹的飞机
:param plane_fail: 被击中的飞机
:return:
'''
for bullet in planefa.bulletlist:
bullet.rect[0]=bullet.x
bullet.rect[1]=bullet.y
if pygame.Rect.colliderect(bullet.rect,plane_fail.rect):
del bullet
screen.blit(bao.image,(plane_fail.x,plane_fail.y))
plane_fail.fail=1
pass
pass
ps : bao.image为击败爆炸特效图片 这里我想用while循环使得特效多停留一会,但是加入后总是出错 :Rect.colliderect等拓展函数链接 二(2):子弹回收
def zihui(screen,self):
'''
管理子弹的显示
:param screen: 显示窗口
:param self: 飞机
:return:
'''
bulletlist1 = []
for bullet1 in self.bulletlist:
screen.blit(bullet1.image,(bullet1.x,bullet1.y))
bullet1.move()
if bullet1.judge():
bulletlist1.append(bullet1)
pass
pass
for bullet2 in bulletlist1:
self.bulletlist.remove(bullet2)
del bullet2
pass
pass
ps: 不要直接在bulletlist里面进行del操作,容易报错 二(3):随机持续出现敌机
def suiji():
'''持续出现敌机'''
global score
if dilist[0].fail == 1:
di = Diplane()
dilist.append(di)
del dilist[0]
num = random.randint(0,250)
dilist[0].x = num
dilist[0].fail = 0
score += 1
pass
ps: 用dilist[0]代表当前敌机
三(1):开始介绍页面 开始介绍页面:
def kai():
'''开始画面的展示'''
t1='Welcome to plane'
t2='asdw control move;' \
' e control attack'
t3='If you want to start; ' \
'please enter'
t1_font=pygame.font.SysFont('Arial',33)
t2_font = pygame.font.SysFont('Arial',20)
t3_font = pygame.font.SysFont('Arial',20)
t1_surf=t1_font.render(t1,1,(255,255,0))
t2_surf = t2_font.render(t2, 1, (150,36,5))
t3_surf = t3_font.render(t3, 1, (100,50,90))
screen.blit(kai_background.image,(0,0))
screen.blit(t1_surf,(20,50))
screen.blit(t2_surf,(20,120))
screen.blit(t3_surf, (20, 200))
pass
字体简化设置函数:
def font(content,style,height,r,g,b,x,y):
'''
字体创建函数
:param content: 字体内容
:param style: 字体样式
:param height: 字体高度
:param r: 字体RGB
:param g:
:param b:
:param x: 字体位置
:param y:
:return:
'''
s0=content
s0_font=pygame.font.SysFont(style,height)
s0_surf=s0_font.render(s0,1,(r,g,b))
screen.blit(s0_surf,(x,y))
pass
》》》字体详解(pygame.font)
三(2):战斗界面
def zhan():
'''战斗画面的展示'''
global ii
global score
screen.blit(zhan_background.image,(0,0))
screen.blit(wo.image,(wo.x,wo.y))
screen.blit(dilist[0].image,(dilist[0].x,dilist[0].y))
wo.rect[0]=wo.x
wo.rect[1]=wo.y
dilist[0].rect[0]=dilist[0].x
dilist[0].rect[1] = dilist[0].y
zihui(screen,wo)
zihui(screen,dilist[0])
ji(screen,wo,dilist[0])
ji(screen,dilist[0],wo)
dilist[0].move()
dilist[0].fire()
if wo.fail==1:
ii+=1
pass
suiji()
font('score'+str(score),'corbel',30,200,30,50,0,470)
pass
ps: 此处用到的wo,dilist[0],screen,等会在下面的模块创建 三(3):结束画面的展示
def jiesuan():
'''结算画面的展示'''
screen.blit(jie_background.image,(0,0))
font('HAPPY GAME','calibri',40,240,90,80,10,50)
font('You score is >>'+str(score),'calibri',40,240,90,80,10,100)
font('If you want again>>>press <shift>','calibri',20,255,80,40,10,160)
lotime=time.strftime('%Y/%M/%d',time.localtime())
font('Time is>>>'+lotime, 'calibri',20, 255, 80, 40, 10, 190)
wo.bulletlist.clear()
dilist[0].bulletlist.clear()
wo.x=150
wo.y=450
pass
ps:此处可以增添历史记录功能(感兴趣的小伙伴可以自己发挥,记得@我评论,谢谢) 四(1):键盘事件
def event_control(plane):
global ii
global score
eventlist = pygame.event.get()
for i in eventlist:
if i.type == QUIT:
print('退出')
sys.exit()
pass
elif i.type == KEYDOWN:
if i.key == K_a or i.type == K_LEFT:
print('左')
plane.mvl()
pass
elif i.key == K_d or i.type == K_RIGHT:
print('右')
plane.mvr()
pass
elif i.key == K_w or i.type == K_UP:
print('上')
print(i.key)
plane.mvu()
pass
elif i.key == K_s or i.type == K_DOWN :
print('下')
print(type(K_s))
plane.mvd()
pass
elif i.key == K_e:
print('e')
print(i.type)
plane.fire()
pass
elif i.key==K_RETURN and ii==0:
ii+=1
print(ii)
pass
elif i.key==K_RSHIFT or i.key==K_LSHIFT:
ii=1
wo.fail=0
score=0
pass
pass
elif i.type==KEYUP :
plane.keyup()
pass
pass
》》》》键盘事件详解 四(2):音乐创建加入
def music():
pygame.mixer.init()
pygame.mixer.music.load('./feitu/b.mp3')
pygame.mixer.music.set_volume(0.04)
pygame.mixer.music.play(-1)
pass
ps:音乐(music模块)的详解(致谢来自江南的你) 四(3):对象创建
kai_background=Base('./plane1.1/kai.png')
zhan_background=Base('./plane1.1/zhan.png')
jie_background=Base('./plane1.1/ji.png')
bao=Base('./plane1.1/bao.png')
wo=Woplane()
di=Diplane()
dilist=[]
dilist.append(di)
五:主函数
if __name__=='__main__':
pygame.init()
pygame.font.init()
clock=pygame.time.Clock()
screen=pygame.display.set_mode((350,500))
pygame.display.set_caption('飞机小对决')
music()
while True:
clock.tick(20)
event_control(wo)
if ii==0:
kai()
pass
elif ii==1:
zhan()
pass
else:
jiesuan()
pass
pygame.display.update()
pygame.event.get()
pass
pass
六,打包为exe 使用PyInstaller生成exe文件
关于pygame模块不了解的地方可以去看 来自江南的你 他的文章将的很好
[成品下载]链接: https://pan.baidu.com/s/1UV4UCg2LZfHGThlitCaNRw 提取码: kvfm 复制这段内容后打开百度网盘手机App,操作更方便哦
觉得不错留个赞哦,谢谢
|