Python-飛機大戰
阿新 • • 發佈:2018-12-26
# -*- coding: cp936 -*- import pygame import time from pygame.locals import * from threading import Timer import math import random import os #匯入包# pygame.mixer.pre_init(44100,16,2,4096) pygame.init() pygame.mixer.init()#遊戲元素初始化 timers=0#遊戲初始引數 fag=0;#勝負標誌 badtimers=50#敵人重新整理時間 healthvalue=194 healthvalue2=194#我方生命值 width,height=480,640#視窗大小 arrows=[]#武器序列 eamy=[]#敵人序列 score=0#遊戲得分 gametime=0#遊戲時間 attack=3#攻擊次數 healthyadds=[] screen=pygame.display.set_mode((width,height)) pygame.display.set_caption("J-20 fly") keys = [False, False, False, False]#按鍵真值表 keys2 = [False, False, False, False] playlocation2=[332,592] playlocation=[112,592] background=pygame.image.load("backgrounds.png")#遊戲精靈匯入 healthbar = pygame.image.load("healthbar.png") player=pygame.image.load("flys.png") boom=pygame.image.load("boom.png") boommusic=pygame.mixer.Sound("boom.wav") eamys=pygame.image.load("plane1.png") eamyss=pygame.image.load("eamyss.png") health = pygame.image.load("health.png") gameover=pygame.image.load("defeat.png") gamestart=pygame.image.load("gamestart2.png") gamedown = pygame.image.load("gamedown2.png") bosshealthbar=pygame.image.load("bosshealthbar.png") bosshealth = pygame.image.load("bosshealth.png") bigbooms = pygame.image.load("bigboom.png") acks=pygame.image.load("ack.png") stopgame=pygame.image.load("stop.png") first=pygame.Rect(player.get_rect()) arrowlocation=[]#playlocation[0]+(first.left+first.right)/2,playlocation[1] arrowlocation2=[]#playlocation2[0]+(first.left+first.right)/2,playlocation2[1] #彈藥座標 arrow=pygame.image.load("arrow.png") gamewin=pygame.image.load("victory.png") boss=pygame.image.load("boss.png") backgroundmusic=pygame.mixer.Sound("backgroundmusic.wav")#音訊檔案匯入 victory=pygame.mixer.Sound("victory.wav") defeat=pygame.mixer.Sound("defeat.wav") logo=pygame.image.load("logo.png") healthyadd=pygame.image.load("add.png") backgroundmusic.play(-1) backgroundmusic.set_volume(0.25)#音量大小 boommusic.set_volume(0.5) victory.set_volume(0.5) defeat.set_volume(0.5) start=True starttime=time.time() locationx=100#boss初始座標 locationy = -200 d=3 boosheahthvalue=99 bossarrow=[] firstscreen=1#初始頁面 check=0#判定符 y1=0 y2=-640#背景重新整理週期 sbooms=[] num=0 tag=0 while firstscreen:#遊戲主頁面 screen.blit(background, (0, 0)) screen.blit(logo,(20,120)) screen.blit(gamestart, (170, 400)) screen.blit(gamedown,(170, 480))#元素匯入 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit(0) if event.type == pygame.KEYDOWN: if event.key == K_ESCAPE: exit(0) position =pygame.mouse.get_pos() if ((position[0] >= 170 and position[0] < 330) and (position[1] > 400 and position[1] < 441)): gamestart = pygame.image.load("gamestart.png") if event.type==pygame.MOUSEBUTTONDOWN: check=1 else: gamestart = pygame.image.load("gamestart2.png") if ((position[0] >= 170 and position[0] < 330) and (position[1] > 480 and position[1] < 521)): gamedown = pygame.image.load("gamedown.png") if event.type==pygame.MOUSEBUTTONDOWN: pygame.quit() exit(0) else: gamedown = pygame.image.load("gamedown2.png") pygame.display.update() if check: break while start:#遊戲開始介面 y1+=4 screen.blit(background, (0, y1)) screen.blit(background, (0, y2+y1)) if y1==640: y1 = 0 y2 = -640#背景連續重新整理 if score>2000:#boss出現 if locationy<0: screen.blit(boss, (locationx, locationy)) locationy=locationy+5 else : screen.blit(boss,(locationx,locationy)) if locationx>=480-293: d = -3 if locationx<=0: d = 3 locationx=locationx+d if(random.randint(1,30)==20): bossarrow.append([locationx+pygame.Rect(boss.get_rect()).width/2,locationy+pygame.Rect(boss.get_rect()).height]) index2=0 if random.randint(1,1000)==450: sbooms.append([random.randint(20,460),0]) index3=0 for s in sbooms: s[1] +=5 if s[1]>=640: if len(sbooms): sbooms.pop(index3) if s[0]+pygame.Rect(bigbooms.get_rect()).width/2>=playlocation[0] and s[0]+pygame.Rect(acks.get_rect()).width/2<=playlocation[0]+pygame.Rect(player.get_rect()).width and s[1]>=playlocation[1]: sbooms.pop(index3) num+=1 if s[0]+pygame.Rect(bigbooms.get_rect()).width/2>=playlocation2[0] and s[0]+pygame.Rect(acks.get_rect()).width/2<=playlocation2[0]+pygame.Rect(player.get_rect()).width and s[1]>=playlocation2[1]: sbooms.pop(index3) num+=1 screen.blit(bigbooms,s) index3+=1 for ack in bossarrow: ack[1] +=10 if ack[1]>=640: bossarrow.pop(index2) if ack[0]+pygame.Rect(acks.get_rect()).width/2>=playlocation[0] and ack[0]+pygame.Rect(acks.get_rect()).width/2<=playlocation[0]+pygame.Rect(player.get_rect()).width and ack[1]>=playlocation[1]: if len(bossarrow): bossarrow.pop(index2) healthvalue2-=random.randint(30,40) if ack[0]+pygame.Rect(acks.get_rect()).width/2>=playlocation2[0] and ack[0]+pygame.Rect(acks.get_rect()).width/2<=playlocation2[0]+pygame.Rect(player.get_rect()).width and ack[1]>=playlocation2[1]: if len(bossarrow): bossarrow.pop(index2) healthvalue-=random.randint(30,40) if len(bossarrow): screen.blit(acks,(ack[0],ack[1])) index2+=1 if healthvalue > 0:#玩家判定 screen.blit(player,playlocation2) if healthvalue2 > 0: screen.blit(player,playlocation) if timers==0:#子彈填裝 arrowlocation = [playlocation[0] + (first.left + first.right) / 2, playlocation[1]-2] arrowlocation2 = [playlocation2[0] + (first.left + first.right) / 2, playlocation2[1]-2] if healthvalue2 > 0: arrows.append([arrowlocation[0],arrowlocation[1]]) if healthvalue > 0: arrows.append([arrowlocation2[0],arrowlocation2[1]]) timers = 65 else: timers-=5 #pygame.display.flip() index = 0 for bullet in arrows:#敵機重新整理與精靈碰撞 bullet[1] -= 5 if bullet[1] <= 0: arrows.pop(index) index += 1 screen.blit(arrow, (bullet[0], bullet[1])) if badtimers==0: eamy.append([random.randint(10,440),0]) badtimers=50 else: badtimers-=5 index=0 for bad in eamy: bad[1]=bad[1]+10 if bad[1]>640: if index<len(eamy): eamy.pop(index) if ((playlocation[0]>=bad[0] and playlocation[0]<bad[0]+35) and (playlocation[1]>bad[1] and playlocation[1]<bad[1]+35) or (playlocation[0]+48>=bad[0] and playlocation[0]+48<bad[0]+35) and (playlocation[1]>bad[1] and playlocation[1]<bad[1]+35)): if len(eamy): eamy.pop(index) healthvalue2 -= random.randint(20,30) if ((playlocation2[0]>=bad[0] and playlocation2[0]<bad[0]+35) and (playlocation2[1]>bad[1] and playlocation2[1]<bad[1]+35) or (playlocation2[0]+48>=bad[0] and playlocation2[0]+48<bad[0]+35) and (playlocation2[1]>bad[1] and playlocation2[1]<bad[1]+35)): if len(eamy): eamy.pop(index) healthvalue -= random.randint(20,30) index1=0 for bullet in arrows: if score>1000 and bullet[0]>locationx and bullet[0] <locationx+293 and bullet[1] < locationy + 220: if len(arrows): arrows.pop(index1) boosheahthvalue-=1 if (bullet[0]>=bad[0]-4 and bullet[0]<bad[0]+44) and (bullet[1]>bad[1] and bullet[1]<bad[1]+35): if (bullet[0]> 213 and bullet[0] <230) or (bullet[0] > 15 and bullet[0] < 35): if index<len(eamy): if attack==0: eamy.pop(index) arrows.pop(index1) score+=800 attack=3 boommusic.play() pygame.display.flip() time.sleep(0.005) else: arrows.pop(index1) attack-=1 else: if index < len(eamy): eamy.pop(index) arrows.pop(index1) score += 200 screen.blit(boom,(bullet[0],bullet[1]-10)) boommusic.play() pygame.display.flip() time.sleep(0.005) index1+=1 index += 1 if (bad[0]>=212 and bad[0]<230) or (bad[0]>15 and bad[0]<35): screen.blit(eamyss,bad) else: screen.blit(eamys, bad) if random.randint(1,200)==150: healthyadds.append([random.randint(10,440),0]) index=0 for add in healthyadds: add[1]=add[1]+10 if add[1]>640: if len(healthyadds): healthyadds.pop(index) if add[0]+pygame.Rect(healthyadd.get_rect()).width/2>=playlocation[0] and add[0]+pygame.Rect(healthyadd.get_rect()).width/2<=playlocation[0]+pygame.Rect(player.get_rect()).width and add[1]>=playlocation[1]: healthyadds.pop(index) if(healthvalue2<179): healthvalue2 += random.randint(15,15) if add[0]+pygame.Rect(healthyadd.get_rect()).width/2>=playlocation2[0] and add[0]+pygame.Rect(healthyadd.get_rect()).width/2<=playlocation2[0]+pygame.Rect(player.get_rect()).width and add[1]>=playlocation2[1]: if len(healthyadds): healthyadds.pop(index) if(healthvalue<179): healthvalue += random.randint(15,15) screen.blit(healthyadd,add) font = pygame.font.SysFont('arial', 18)#得分文字框 scoretext = font.render("Score :" + str(score), True, (255, 255, 255)) textRect=scoretext.get_rect() textRect.topright=[455,22] screen.blit(scoretext,textRect) scoretext = font.render("Time:"+str(int ((pygame.time.get_ticks()) / 60000)) + ":" +str(int((pygame.time.get_ticks()) / 1000 % 60)).zfill(2), True, (255, 255, 255)) textRect.topright = [455, 50] screen.blit(scoretext, textRect) scoretext = font.render("gameplayer1:", True, (255, 255, 255)) textRect.topright=[110,0] screen.blit(scoretext,textRect) scoretext = font.render("gameplayer2:", True, (255, 255, 255)) textRect.topright=[110,38] screen.blit(scoretext,textRect) scoretext = font.render("Tool * " + str(num), True, (255, 255, 255)) textRect.topright=[455,80] screen.blit(scoretext,textRect) for event in pygame.event.get():#按鍵響應 if event.type==pygame.QUIT: pygame.quit() exit(0) if event.type == pygame.KEYDOWN: if event.key== pygame.K_w: keys[0] = True if event.key == pygame.K_s: keys[1] = True if event.key == pygame.K_a: keys[2] = True if event.key == pygame.K_d: keys[3] = True if event.key== pygame.K_UP: keys2[0] = True if event.key == pygame.K_DOWN: keys2[1] = True if event.key == pygame.K_LEFT: keys2[2] = True if event.key == pygame.K_RIGHT: keys2[3] = True if event.key == pygame.K_o: if num>0: eamy=[] num-=1 if event.key==pygame.K_p: while 1: screen.blit(stopgame,(140,220)) for event in pygame.event.get(): if event.type==QUIT: pygame.quit() exit(0) if event.type==pygame.KEYDOWN: if event.key==pygame.K_p: tag=1 break elif event.key == K_ESCAPE: exit(0) if tag: break pygame.display.update() tag=0 if event.key==K_ESCAPE: exit(0) if event.type == pygame.KEYUP: if event.key== pygame.K_w: keys[0] = False if event.key == pygame.K_s: keys[1] = False if event.key == pygame.K_a: keys[2] = False if event.key == pygame.K_d: keys[3] = False if event.key== pygame.K_UP: keys2[0] = False if event.key == pygame.K_DOWN: keys2[1] = False if event.key == pygame.K_LEFT: keys2[2] = False if event.key == pygame.K_RIGHT: keys2[3] = False if keys[0]: if playlocation[1]>0: playlocation[1]-=5 if keys[1]: if playlocation[1]<620-(first.top+first.bottom)/2: playlocation[1]+=5 if keys[2]: if playlocation[0]>0: playlocation[0]-=5 if keys[3]: if playlocation[0]<455-(first.left+first.right)/2: playlocation[0]+=5 if keys2[0]: if playlocation2[1]>0: playlocation2[1]-=5 if keys2[1]: if playlocation2[1]<620-(first.top+first.bottom)/2: playlocation2[1]+=5 if keys2[2]: if playlocation2[0]>0: playlocation2[0]-=5 if keys2[3]: if playlocation2[0]<455-(first.left+first.right)/2: playlocation2[0]+=5 screen.blit(healthbar, (5, 18)) screen.blit(healthbar, (5, 55)) for health1 in range(healthvalue):#生命條更新 screen.blit(health, (health1+8,21)) for health1 in range(healthvalue2): screen.blit(health, (health1 + 8, 58)) if locationy>=0: screen.blit(bosshealthbar, (locationx + 95, locationy+77)) for health1 in range(boosheahthvalue): screen.blit(bosshealth,(health1+locationx+96,locationy+77)) if healthvalue<=0 and healthvalue2<=0:#勝負判定 fag = 0 start = False endtime=time.time() if boosheahthvalue<=0: fag = 1 start = False endtime = time.time() pygame.display.update() time.sleep(0.02) if fag: backgroundmusic.stop() victory.play() pygame.font.init() font=pygame.font.SysFont('arial',24) text = font.render("Score :" + str(score) + " " + "Time :" + str(int(endtime - starttime)) + "s", True,(255, 255, 255)) textRect=text.get_rect() textRect.centerx=screen.get_rect().centerx textRect.centery = screen.get_rect().centery+36 screen.blit(background, (0, 0)) screen.blit( gamewin ,(90, 90)) screen.blit(text,textRect) file=open("historyscore.txt","a") file.write(str(score)+"\n") file.close() file1 = open("historyscore.txt", "r") result=[] for line in file1: result.append(list(map(int,line.split()))) maxscore=max(result) text1=font.render("The record:"+str(maxscore),True,(255, 255, 255)) textRect1=text1.get_rect() textRect1.centerx=screen.get_rect().centerx textRect1.centery = screen.get_rect().centery+72 screen.blit(text1,textRect1) file.close() else : backgroundmusic.stop() defeat.play() pygame.font.init() font=pygame.font.SysFont('arial',24) text=font.render("Score :"+str(score)+" "+"Time :" + str(int (endtime-starttime)+1)+"s",True,(255,255,255)) textRect=text.get_rect() textRect.centerx=screen.get_rect().centerx textRect.centery = screen.get_rect().centery+36 screen.blit(background, (0, 0)) screen.blit(gameover, (90, 90)) screen.blit(text,textRect) file=open("historyscore.txt","a") file.write(str(score)+"\n") file.close() file1 = open("historyscore.txt", "r") result=[] for line in file1: result.append(list(map(int,line.split()))) maxscore=max(result) text1=font.render("The record:"+str(maxscore),True,(255, 255, 255)) textRect1=text1.get_rect() textRect1.centerx=screen.get_rect().centerx textRect1.centery = screen.get_rect().centery+72 screen.blit(text1,textRect1) file.close() while True:#遊戲結束介面 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit(0) if event.type == pygame.KEYDOWN: if event.key==K_ESCAPE: exit(0) pygame.display.update()