Python Pygame做飞机大战(修改升级版)

6 篇文章 0 订阅
3 篇文章 0 订阅

Python Pygame做飞机大战(修改升级版)

代码

稍微修改了一下,bug变少了,添加了游戏开始界面等

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
start=False
bomb_supply=[]
bullet_supply=[]
end=False
bd=[]
td=[]
nowhb=0
nowtd=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 and start:
                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
                    start=False
                    end=False
                    bd=[]
                    td=[]
                    nowhb=0
                    nowtd=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()
                elif not start:
                    start=True
    #绘制图片
    screen.blit(img['background.png'],(0,x1))
    screen.blit(img['background.png'],(0,x2))
    if life!=0 and start:
        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 start:
                        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 start:
                        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 start:
                        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 and start:
        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 and not end:
            bulletPos.append([pygame.mouse.get_pos()[0],pygame.mouse.get_pos()[1]-(126/2)])
    if nowT%50==0:
        if gamerun and not end:
            ET=random.randint(1,100)
            if ET>=1 and ET<=88:
                enemy1.append([random.randint(0,380-57),-50])
                e1Hit.append(0)
            elif ET>=88 and ET<=97:
                enemy2.append([random.randint(0,380-69),-99])
                e2Hit.append(0)
            elif ET>=97 and ET<=100:
                enemy3.append([random.randint(0,380-169),-258])
                e3Hit.append(0)
    if nowT%100==0 and start:
        b=random.randint(1,100)
        if b>=0 and b<=10:
            a=random.randint(1,2)
            if a==1:
                td.append(0)
                bomb_supply.append([random.randint(0,480-60),-107])
            else:
                bd.append(0)
                bullet_supply.append([random.randint(0,480-58),-88])         
    #背景移动
    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))
    if not start:        
        color = 255,255,255
        position = 233,340
        radius = 100
        width = 100
        pygame.draw.circle(screen,color,position,radius,width)
        screen.blit(font1.render("全 民",True,(0,0,0)),(180,290))
        screen.blit(font1.render("飞机大战",True,(0,0,0)),(150,350))
        screen.blit(font.render("点击屏幕开始游戏",True,(0,0,0)),(130,450))
    pygame.display.update()

总结

: D自我感觉良好
bug还是有的,不过变少了,增加了游戏开始界面。
代码显得有些乱:(

完结

素材翻我的第一个博文,里面有地址

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值