单人版:
效果:
代码如下:
import pygame,math,random
pygame.mixer.init()
bg=pygame.image.load(r'E:\飞机\飞机大战素材\img\jj.jpg') #背景
hero=pygame.image.load(r'E:\飞机\飞机大战素材\img\hero1.png') #主机
enemy=pygame.image.load(r'E:\飞机\飞机大战素材\img\enemy1.png') #敌机
enemy_2=pygame.image.load(r'E:\飞机\飞机大战素材\img\enemy2.png') #敌机
enemy_boom=pygame.image.load(r'E:\飞机\飞机大战素材\img\enemy1_down3.png') #敌机爆炸
bullet=pygame.image.load(r'E:\飞机\飞机大战素材\img\bullet1.png') #子弹
fuck=pygame.image.load(r'E:\飞机\飞机大战素材\img\bomb.png') #大必杀
pygame.mixer.music.load('E:\飞机\飞机大战素材\music\JJD - Mario.mp3')
heroX=250
heroY=680
stepX=0
stepY=0
bullet_pos=[]
fuck_pos=[]
enemy_speed=1 #敌机速度
enemy_objs=[]
enemy2_objs=[]
pygame.init()
pygame.mixer.music.play(-1,0)
screen=pygame.display.set_mode((600,800))
pygame.display.set_caption("这是一个打飞机游戏")
def screen_border(X,Y):
if X<0:
X=0
elif X>500:
X=500
if Y<0:
Y=0
elif Y>700:
Y=700
return X,Y
def keydown_envent(event,stepX,stepY,hero_pos): #传入参数为事件、主角坐标
bullet_pos=[]
if event.key == pygame.K_RIGHT:
stepX=1
elif event.key == pygame.K_LEFT:
stepX=-1
elif event.key == pygame.K_UP:
stepY=-1
elif event.key == pygame.K_DOWN:
stepY=1
elif event.key == pygame.K_SPACE:
bullet_pos=[hero_pos[0],hero_pos[1]+10]
print('space:',bullet_pos)
return stepX,stepY,bullet_pos
def enemy_show(enemy_objs,startY=-40):
if len(enemy_objs)<3:
while len(enemy_objs)<3: #这两行可以调整敌机数量
enemy_X = random.randint(0,500)
enemy_pos=[enemy_X,startY]
screen.blit(enemy,enemy_pos)
enemy_objs.append(enemy_pos) #如果敌机的数量少于5,就增加
else:
i=0
for pos in enemy_objs:
screen.blit(enemy,pos)
enemy_objs[i]=[pos[0],pos[1]+enemy_speed]
i=i+1 #超过五个就开始移动
return enemy_objs
#----------------------------------------------
def enemy2_show(enemy_objs,startY=-40):
if len(enemy_objs)<2:
while len(enemy_objs)<2: #这两行可以调整敌机数量
enemy_X = random.randint(0,500)
enemy_pos=[enemy_X,startY]
screen.blit(enemy_2,enemy_pos)
enemy_objs.append(enemy_pos) #如果敌机的数量少于5,就增加
else:
i=0
for pos in enemy_objs:
screen.blit(enemy_2,pos)
enemy_objs[i]=[pos[0],pos[1]+enemy_speed]
i=i+1 #超过五个就开始移动
return enemy_objs
#---------------------------------------------
def distance(bx,by,ex,ey):
a=bx-ex
b=by-ey
k=math.sqrt(a*a+b*b)
return k
font1=pygame.font.SysFont('arial', 20, bold=True)
font2=pygame.font.SysFont('arial', 40, bold=True)
font3=pygame.font.SysFont('arial', 50, bold=True)
font_list=pygame.font.get_fonts()#-----------------设置有关的字体------------------------------
life=3
enemy1_beat=0
enemy2_beat=0
score=0
start_time=pygame.time.get_ticks()
now_time=30-(pygame.time.get_ticks()-start_time)/1000
while True:
now_time=30-(pygame.time.get_ticks()-start_time)/1000
text_time=font2.render("Time %d" % now_time,True,[100,100,100])
bullet_pos_=[]
heroX=heroX+stepX
heroY=heroY+stepY
heroX,heroY=screen_border(heroX,heroY)
screen.blit(bg,(0,0))
screen.blit(hero,(heroX,heroY))
enemy_objs=enemy_show(enemy_objs)
enemy2_objs=enemy2_show(enemy2_objs)
print(bullet_pos)
i=0
for v in bullet_pos:
bullet_pos[i]=[v[0],v[1]-10]
screen.blit(bullet,(bullet_pos[i][0]+45,bullet_pos[i][1]))
distance_b=[bullet_pos[i][0]+45,bullet_pos[i][1]]
ei=0
for ep in enemy_objs:
if distance(distance_b[0],distance_b[1],ep[0],ep[1])<30:
screen.blit(enemy_boom,(ep[0],ep[1]))
enemy1_beat+=1
enemy_objs[ei]=[random.randint(0,500),-50]
elif ep[1]>700:
screen.blit(enemy_boom,(ep[0],ep[1]))
enemy_objs[ei]=[random.randint(0,500),-50]
ei=ei+1
ei=0
for ep in enemy2_objs:
if distance(distance_b[0],distance_b[1],ep[0],ep[1])<60:
screen.blit(enemy_boom,(ep[0],ep[1]))
enemy2_beat+=1
enemy2_objs[ei]=[random.randint(0,500),-50]
elif ep[1]>700:
screen.blit(enemy_boom,(ep[0],ep[1]))
enemy2_objs[ei]=[random.randint(0,500),-50]
ei=ei+1
i=i+1
for event in pygame.event.get():
if event.type==pygame.QUIT:
exit()
if event.type==pygame.KEYDOWN:
stepX,stepY,bullet_pos_=keydown_envent(event,stepX,stepY,[heroX,heroY])
if len(bullet_pos_)>0: #如果接收值长度大于一表示已经按下空格
bullet_pos.append(bullet_pos_)
score=enemy1_beat*10+enemy2_beat*20
screen.blit(font1.render('SCORE %d' % score,True,[0,0,0]),[20,20])
screen.blit(text_time,[230,20])
if now_time>0:#--------------------------控制游戏的时间-----防沉迷-------------------
pygame.display.update()
else:
break
while now_time>-3:
screen.blit(font3.render('Last Score %d' % score,True,[200,0,0]),[50,300])
pygame.display.update()
now_time=30-(pygame.time.get_ticks()-start_time)/1000
双人版(在单人版上加了一些重复的代码,还调了帧率)
import pygame,math,random
pygame.mixer.init()
bg=pygame.image.load(r'E:\飞机\飞机大战素材\img\jj.jpg') #背景
hero=pygame.image.load(r'E:\飞机\飞机大战素材\img\hero_yellow.png') #主机1
hero2=pygame.image.load(r'E:\飞机\飞机大战素材\img\hero_blue.png') #主机2
enemy=pygame.image.load(r'E:\飞机\飞机大战素材\img\enemy1.png') #敌机
enemy_2=pygame.image.load(r'E:\飞机\飞机大战素材\img\enemy2.png') #敌机
enemy_boom=pygame.image.load(r'E:\飞机\飞机大战素材\img\enemy1_down3.png') #敌机爆炸
bullet=pygame.image.load(r'E:\飞机\飞机大战素材\img\bullet1.png') #子弹1
bullet2=pygame.image.load(r'E:\飞机\飞机大战素材\img\bullet2.png') #子弹2
fuck=pygame.image.load(r'E:\飞机\飞机大战素材\img\bomb.png') #大必杀
pygame.mixer.music.load('E:\飞机\飞机大战素材\music\JJD - Mario.mp3')
last=pygame.image.load(r'E:\飞机\飞机大战素材\img\last.png') #最后背景
heroX=30
heroY=680
heroX2=370
heroY2=680
stepX=0
stepY=0
stepX2=0
stepY2=0
bullet_pos=[]
bullet2_pos=[]
fuck_pos=[]
enemy_speed=2 #敌机速度
enemy_objs=[]
enemy2_objs=[]
pygame.init()
pygame.mixer.music.play(-1,0)
screen=pygame.display.set_mode((600,800))
pygame.display.set_caption("这是一个打飞机游戏")
def screen_border(X,Y):
if X<0:
X=0
elif X>500:
X=500
if Y<0:
Y=0
elif Y>700:
Y=700
return X,Y
def keydown_envent2(event,stepX,stepY,hero_pos): #传入参数为事件、主角坐标
bullet_pos=[]
if event.key == pygame.K_RIGHT:
stepX=2
elif event.key == pygame.K_LEFT:
stepX=-2
elif event.key == pygame.K_UP:
stepY=-2
elif event.key == pygame.K_DOWN:
stepY=2
#---------------------------------
#=--------------------------------------
elif event.key == pygame.K_SPACE:
bullet_pos=[hero_pos[0],hero_pos[1]+10]
print('space:',bullet_pos)
#-------------------------------------
return stepX,stepY,bullet_pos
def keydown_envent(event,stepX,stepY,hero_pos): #传入参数为事件、主角坐标
bullet_pos=[]
if event.key == pygame.K_d:
stepX=2
elif event.key == pygame.K_a:
stepX=-2
elif event.key == pygame.K_e:
stepY=-2
elif event.key == pygame.K_s:
stepY=2
#=----------------------------------------
elif event.key == pygame.K_w:
bullet_pos=[hero_pos[0],hero_pos[1]+20]
print('space:',bullet_pos)
return stepX,stepY,bullet_pos
def enemy_show(enemy_objs,startY=-40):
if len(enemy_objs)<4:
while len(enemy_objs)<4: #这两行可以调整敌机数量
enemy_X = random.randint(0,500)
enemy_pos=[enemy_X,startY]
screen.blit(enemy,enemy_pos)
enemy_objs.append(enemy_pos) #如果敌机的数量少于5,就增加
else:
i=0
for pos in enemy_objs:
screen.blit(enemy,pos)
enemy_objs[i]=[pos[0],pos[1]+enemy_speed]
i=i+1 #超过五个就开始移动
return enemy_objs
#----------------------------------------------
def enemy2_show(enemy_objs,startY=-40):
if len(enemy_objs)<3:
while len(enemy_objs)<3: #这两行可以调整敌机数量
enemy_X = random.randint(0,500)
enemy_pos=[enemy_X,startY]
screen.blit(enemy_2,enemy_pos)
enemy_objs.append(enemy_pos) #如果敌机的数量少于5,就增加
else:
i=0
for pos in enemy_objs:
screen.blit(enemy_2,pos)
enemy_objs[i]=[pos[0],pos[1]+enemy_speed]
i=i+1 #超过五个就开始移动
return enemy_objs
#---------------------------------------------
def distance(bx,by,ex,ey):
a=bx-ex
b=by-ey
k=math.sqrt(a*a+b*b)
return k
font1=pygame.font.SysFont('arial', 20, bold=True)
font2=pygame.font.SysFont('arial', 40, bold=True)
font3=pygame.font.SysFont('arial', 50, bold=True)
font_list=pygame.font.get_fonts()#-----------------设置有关的字体------------------------------
life=3
enemy1_beat=0
enemy2_beat=0
enemy1_beatx=0
enemy2_beatx=0
score=0
score2=0
start_time=pygame.time.get_ticks()
now_time=30-(pygame.time.get_ticks()-start_time)/1000
clock=pygame.time.Clock()
while True:
clock.tick(100)#帧数
now_time=30-(pygame.time.get_ticks()-start_time)/1000
text_time=font2.render("Time %d" % now_time,True,[100,100,100])
bullet_pos_=[]
bullet2_pos_=[]
heroX=heroX+stepX
heroY=heroY+stepY
heroX2=heroX2+stepX2
heroY2=heroY2+stepY2
heroX,heroY=screen_border(heroX,heroY)
heroX2,heroY2=screen_border(heroX2,heroY2)
screen.blit(bg,(0,0))
screen.blit(hero,(heroX,heroY))
screen.blit(hero2,(heroX2,heroY2))
enemy_objs=enemy_show(enemy_objs)
enemy2_objs=enemy2_show(enemy2_objs)
print(bullet_pos)
i=0
for v in bullet_pos:
bullet_pos[i]=[v[0],v[1]-10]
screen.blit(bullet,(bullet_pos[i][0]+45,bullet_pos[i][1]))
distance_b=[bullet_pos[i][0]+45,bullet_pos[i][1]]
ei=0
for ep in enemy_objs:
if distance(distance_b[0],distance_b[1],ep[0],ep[1])<30:
screen.blit(enemy_boom,(ep[0],ep[1]))
enemy1_beat+=1
enemy_objs[ei]=[random.randint(0,500),-50]
elif ep[1]>700:
screen.blit(enemy_boom,(ep[0],ep[1]))
enemy_objs[ei]=[random.randint(0,500),-50]
ei=ei+1
ei=0
for ep in enemy2_objs:
if distance(distance_b[0],distance_b[1],ep[0],ep[1])<60:
screen.blit(enemy_boom,(ep[0],ep[1]))
enemy2_beat+=1
enemy2_objs[ei]=[random.randint(0,500),-50]
elif ep[1]>700:
screen.blit(enemy_boom,(ep[0],ep[1]))
enemy2_objs[ei]=[random.randint(0,500),-50]
ei=ei+1
i=i+1
#----------------------------------
print(bullet_pos)
j=0
for v in bullet2_pos:
bullet2_pos[j]=[v[0],v[1]-10]
screen.blit(bullet2,(bullet2_pos[j][0]+45,bullet2_pos[j][1]))
distance_b=[bullet2_pos[j][0]+45,bullet2_pos[j][1]]
ei=0
for ep in enemy_objs:
if distance(distance_b[0],distance_b[1],ep[0],ep[1])<30:
screen.blit(enemy_boom,(ep[0],ep[1]))
enemy1_beatx+=1
enemy_objs[ei]=[random.randint(0,500),-50]
elif ep[1]>700:
screen.blit(enemy_boom,(ep[0],ep[1]))
enemy_objs[ei]=[random.randint(0,500),-50]
ei=ei+1
ei=0
for ep in enemy2_objs:
if distance(distance_b[0],distance_b[1],ep[0],ep[1])<60:
screen.blit(enemy_boom,(ep[0],ep[1]))
enemy2_beatx+=1
enemy2_objs[ei]=[random.randint(0,500),-50]
elif ep[1]>700:
screen.blit(enemy_boom,(ep[0],ep[1]))
enemy2_objs[ei]=[random.randint(0,500),-50]
ei=ei+1
j=j+1
#-----------------------------------
for event in pygame.event.get():
if event.type==pygame.QUIT:
exit()
if event.type==pygame.KEYDOWN:
stepX,stepY,bullet_pos_=keydown_envent(event,stepX,stepY,[heroX,heroY])
stepX2,stepY2,bullet2_pos_=keydown_envent2(event,stepX2,stepY2,[heroX2,heroY2])
if len(bullet_pos_)>0: #如果接收值长度大于一表示已经按下空格
bullet_pos.append(bullet_pos_)
if len(bullet2_pos_)>0: #如果接收值长度大于一表示已经按下空格
bullet2_pos.append(bullet2_pos_)
score=enemy1_beat*10+enemy2_beat*20
score2=enemy1_beatx*10+enemy2_beatx*20
screen.blit(font1.render('SCORE %d' % score,True,[200,0,0]),[20,20])
screen.blit(font1.render('SCORE %d' % score2,True,[0,0,200]),[450,20])
screen.blit(text_time,[230,20])
if now_time>0:#--------------------------控制游戏的时间-----防沉迷-------------------
pygame.display.update()
else:
break
while now_time>-3:
screen.blit(last,(0,0))
screen.blit(font3.render('Last Score %d' % score,True,[200,0,0]),[50,300])
screen.blit(font3.render('Last Score %d' % score2,True,[0,0,200]),[50,500])
pygame.display.update()
now_time=30-(pygame.time.get_ticks()-start_time)/1000
效果: