Python-飞机大战

# -*- 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()

猜你喜欢

转载自blog.csdn.net/m0_37848958/article/details/78916075
今日推荐