代码区
做的不是很好,请见谅。
代码
有些长,别晕。
import pygame,sys,os,random
import time as t
from pygame import *
import tkinter
from tkinter import messagebox
root=tkinter.Tk()
root.withdraw()
# 定义常量
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 700
# 初始化 pygame
pygame.init()
# 设置游戏界面大小
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# 游戏界面标题
pygame.display.set_caption('飞机大战')
path = os.listdir("images") #这里可以按照需求更换路径
#加载所有图片
img={}
for i in path:
img[i]=pygame.image.load("images//"+i)
#定义变量(不要在意变量名)
x1=0
x2=700
nowType=1
bulletPos=[]
nowT=0
enemy1=[]
enemy2=[]
enemy3=[]
e1Hit=[]
e2Hit=[]
e3Hit=[]
score=0
life=3
counte1=0
msme=None
e1DL=[]
e2DL=[]
e3DL=[]
meAlive=True
gamerun=True
LP=None
da=0
dn=0
dc=True
A1T=False
A2T=False
res=False
resu=False
stime=2.0
#字体
font=pygame.font.SysFont("微软雅黑",25)
font2=pygame.font.SysFont("微软雅黑",40)
font1=pygame.font.SysFont("幼圆",40)
#主循环
while True:
for event in pygame.event.get():
if event.type==QUIT:
if tkinter.messagebox.askyesno("飞机大战","确定退出吗?"):
pygame.quit()
sys.exit()
elif event.type==KEYDOWN:
if event.key==K_ESCAPE and life!=0 and meAlive:
if gamerun and life!=0 and meAlive:
gamerun=False
res=True
LP=mems
elif event.type==MOUSEBUTTONDOWN:
if event.button==1:
if res and pygame.mouse.get_pos()[0]>=122 and pygame.mouse.get_pos()[0]<=122+232 and pygame.mouse.get_pos()[1]>=380 and pygame.mouse.get_pos()[1]<=380+47: #122,380 232,47
resu=True
elif meAlive==False and pygame.mouse.get_pos()[0]>=100 and pygame.mouse.get_pos()[0]<=100+300 and pygame.mouse.get_pos()[1]>=550 and pygame.mouse.get_pos()[1]<=550+41:
nowType=1
bulletPos=[]
nowT=0
enemy1=[]
enemy2=[]
enemy3=[]
e1Hit=[]
e2Hit=[]
e3Hit=[]
score=0
life=3
counte1=0
msme=None
e1DL=[]
e2DL=[]
e3DL=[]
meAlive=True
gamerun=True
LP=None
da=0
dn=0
dc=True
A1T=False
A2T=False
res=False
resu=False
stime=2.0
elif meAlive==False and pygame.mouse.get_pos()[0]>=100 and pygame.mouse.get_pos()[0]<=100+300 and pygame.mouse.get_pos()[1]>=600 and pygame.mouse.get_pos()[1]<=600+41:
if tkinter.messagebox.askyesno("飞机大战","确定退出吗?"):
pygame.quit()
sys.exit()
#绘制图片
screen.blit(img['background.png'],(0,x1))
screen.blit(img['background.png'],(0,x2))
if life!=0:
screen.blit(font.render("LIFE:"+str(life),True,(0,0,0)),(10,10))
screen.blit(font.render("SCORE:"+str(score),True,(0,0,0)),(340,10))
counte1=0
for i in range(len(enemy1)):
screen.blit(img['enemy1.png'],(enemy1[i-counte1][0],enemy1[i-counte1][1]))
if gamerun:
try:
enemy1[i-counte1][1]+=2
if pygame.Rect(enemy1[i-counte1][0]+3.5,enemy1[i-counte1][1]+3.5,43-3.5,57-3.5).colliderect(pygame.Rect(mems[0]+5,mems[1]+5,102-5,126-5)):
life-=1
e1DL.append((enemy1[i-counte1][0],enemy1[i-counte1][1]))
del enemy1[i-counte1]
counte1+=1
for j in bulletPos:
if enemy1[i-counte1][1]+43>=j[1] and enemy1[i-counte1][1]<=j[1] and enemy1[i-counte1][0]<=j[0] and enemy1[i-counte1][0]+57>=j[0]:
screen.blit(pygame.transform.scale(img['bullet2.png'],(8,15)),(j[0]-2,j[1]))
bulletPos.remove(j)
e1Hit[i-counte1]+=1
if e1Hit[i-counte1]>=2:
e1DL.append((enemy1[i-counte1][0],enemy1[i-counte1][1]))
del enemy1[i-counte1]
counte1+=1
e1Hit.remove(2)
score+=2
except:
pass
mems=(pygame.mouse.get_pos()[0]-(102/2),pygame.mouse.get_pos()[1]-(126/2))
counte1=0
for i in range(len(enemy2)):
if e2Hit[i-counte1]>=1:
screen.blit(img['enemy2_hit.png'],(enemy2[i-counte1][0],enemy2[i-counte1][1]))
else:
screen.blit(img['enemy2.png'],(enemy2[i-counte1][0],enemy2[i-counte1][1]))
if gamerun:
try:
enemy2[i-counte1][1]+=2
for j in bulletPos:
if enemy2[i-counte1][1]+99>=j[1] and enemy2[i-counte1][1]<=j[1] and enemy2[i-counte1][0]<=j[0] and enemy2[i-counte1][0]+69>=j[0]:
screen.blit(pygame.transform.scale(img['bullet2.png'],(8,15)),(j[0]-2,j[1]))
bulletPos.remove(j)
e2Hit[i-counte1]+=1
if pygame.Rect(enemy2[i-counte1][0]+4,enemy2[i-counte1][1]+4,69-4,99-4).colliderect(pygame.Rect(mems[0]+5,mems[1]+5,102-5,126-5)):
life-=1
e2DL.append((enemy2[i-counte1][0],enemy2[i-counte1][1]))
del enemy2[i-counte1]
counte1+=1
if e2Hit[i-counte1]>=5:
e2DL.append((enemy2[i-counte1][0],enemy2[i-counte1][1]))
del enemy2[i-counte1]
counte1+=1
e2Hit.remove(5)
score+=5
except:
pass
counte1=0
for i in range(len(enemy3)):
if e3Hit[i-counte1]>=1:
screen.blit(img['enemy3_hit.png'],(enemy3[i-counte1][0],enemy3[i-counte1][1]))
else:
if gamerun:
if random.randint(0,1)==0:
screen.blit(img['enemy3_n1.png'],(enemy3[i-counte1][0],enemy3[i-counte1][1]))
else:
screen.blit(img['enemy3_n2.png'],(enemy3[i-counte1][0],enemy3[i-counte1][1]))
else:
screen.blit(img['enemy3_n1.png'],(enemy3[i-counte1][0],enemy3[i-counte1][1]))
if gamerun:
try:
enemy3[i-counte1][1]+=2
for j in bulletPos:
if enemy3[i-counte1][1]+261>=j[1] and enemy3[i-counte1][1]<=j[1] and enemy3[i-counte1][0]<=j[0] and enemy3[i-counte1][0]+165>=j[0]:
screen.blit(pygame.transform.scale(img['bullet2.png'],(8,15)),(j[0]-2,j[1]))
bulletPos.remove(j)
e3Hit[i-counte1]+=1
if pygame.Rect(enemy3[i-counte1][0]+10,enemy3[i-counte1][1]+10,165-10,261-10).colliderect(pygame.Rect(mems[0]+5,mems[1]+5,102-5,126-5)):
life-=1
e3DL.append((enemy3[i-counte1][0],enemy3[i-counte1][1]))
del enemy3[i-counte1]
counte1+=1
if e3Hit[i-counte1]>=10:
e3DL.append((enemy3[i-counte1][0],enemy3[i-counte1][1]))
del enemy3[i-counte1]
e3Hit.remove(10)
score+=10
except:
pass
for i in bulletPos:
screen.blit(pygame.transform.scale(img['bullet1.png'],(8,15)),(i[0]-2,i[1]))
if gamerun:
i[1]-=3
if meAlive and gamerun:
if dc:
if nowT%5==0:
screen.blit(img['me1.png'],mems)
else:
screen.blit(img['me2.png'],mems)
else:
da+=1
if da>=50 and da<=100:
screen.blit(img['me1.png'],mems)
if da>=100:
da=0
dn+=1
if dn>=3:
if life==2:
A2T=True
else:
A1T=True
dc=True
dn=0
if life==2 and not A2T:
dc=False
elif life==1 and not A1T:
dc=False
if nowT%20==0:
bulletPos.append([pygame.mouse.get_pos()[0],pygame.mouse.get_pos()[1]-(126/2)])
if nowT%100==0:
if gamerun:
ET=random.randint(1,100)
if ET>=1 and ET<=88:
enemy1.append([random.randint(0,480-57),-43])
e1Hit.append(0)
elif ET>=88 and ET<=97:
enemy2.append([random.randint(0,480-69),-99])
e2Hit.append(0)
elif ET>=97 and ET<=100:
enemy3.append([random.randint(0,480-169),-258])
e3Hit.append(0)
#背景移动
if gamerun:
x1+=1
x2+=1
if x2>=700:
x2=-700
if x1>=700:
x1=-700
for i in e1DL:
screen.blit(img['enemy1_down1.png'],i)
t.sleep(0.01)
pygame.display.update()
screen.blit(img['enemy1_down2.png'],i)
t.sleep(0.01)
pygame.display.update()
screen.blit(img['enemy1_down3.png'],i)
t.sleep(0.01)
pygame.display.update()
screen.blit(img['enemy1_down4.png'],i)
t.sleep(0.01)
pygame.display.update()
e1DL.remove(i)
for i in e2DL:
screen.blit(img['enemy2_down1.png'],i)
t.sleep(0.01)
pygame.display.update()
screen.blit(img['enemy2_down2.png'],i)
t.sleep(0.01)
pygame.display.update()
screen.blit(img['enemy2_down3.png'],i)
t.sleep(0.01)
pygame.display.update()
screen.blit(img['enemy2_down4.png'],i)
t.sleep(0.01)
pygame.display.update()
e2DL.remove(i)
for i in e3DL:
screen.blit(img['enemy3_down1.png'],i)
t.sleep(0.01)
pygame.display.update()
screen.blit(img['enemy3_down2.png'],i)
t.sleep(0.01)
pygame.display.update()
screen.blit(img['enemy3_down3.png'],i)
t.sleep(0.01)
pygame.display.update()
screen.blit(img['enemy3_down4.png'],i)
t.sleep(0.01)
pygame.display.update()
screen.blit(img['enemy3_down5.png'],i)
t.sleep(0.01)
pygame.display.update()
e3DL.remove(i)
if life<=0 and meAlive:
life=0
meAlive=False
screen.blit(img['me_destroy_1.png'],mems)
t.sleep(0.01)
pygame.display.update()
screen.blit(img['me_destroy_2.png'],mems)
t.sleep(0.01)
pygame.display.update()
screen.blit(img['me_destroy_3.png'],mems)
t.sleep(0.01)
pygame.display.update()
screen.blit(img['me_destroy_4.png'],mems)
t.sleep(0.01)
pygame.display.update()
t.sleep(0.01)
nowT+=1
if life<=0:
life=0
gamerun=False
if res:
screen.blit(img['me1.png'],LP)
screen.blit(font1.render("已暂停",True,(0,0,0)),(180,290))
if pygame.mouse.get_pos()[0]>=122 and pygame.mouse.get_pos()[0]<=122+232 and pygame.mouse.get_pos()[1]>=380 and pygame.mouse.get_pos()[1]<=380+47:
screen.blit(img['run_pressed.png'],(122,380))
else:
screen.blit(img['run_nor.png'],(122,380))
if resu:
res=False
screen.blit(img['me1.png'],LP)
screen.blit(font2.render(str(round(stime,1)),True,(0,0,0)),(210,290))
t.sleep(0.07)
stime-=0.1
if stime<=0.0:
resu=False
gamerun=True
stime=3.0
if not meAlive:
screen.blit(img['gameoverbg.png'],(0,-100))
screen.blit(font1.render("您的得分:"+str(score),True,(0,0,0)),(130,200))
screen.blit(img['again.png'],(100,550))
screen.blit(img['gameover.png'],(100,600))
pygame.display.update()
不想加注释了:D
总结
游戏里还是有一些bug的,过几周在出修复过的。
总体来讲还可以,就是有的时候会闪退:(
素材
https://pan.baidu.com/s/1-sD4qUC1vKnJaq91q2Z3TA 提取码:178f
或者直接上网搜也行:D
来源:oschina
链接:https://my.oschina.net/u/4256357/blog/4326474