代碼區
作的不是很好,請見諒。python
代碼
有些長,別暈。app
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()
不想加註釋了:Ddom
總結
遊戲裏仍是有一些bug的,過幾周在出修復過的。
整體來說還能夠,就是有的時候會閃退:(
ide
素材
https://pan.baidu.com/s/1-sD4qUC1vKnJaq91q2Z3TA 提取碼:178f
或者直接上網搜也行:D
字體