Python 飞机大战游戏

#崔,2019.03.08
设定己方飞机可以同时发射三发导弹且相互独立,轰击敌方血量不同(两侧小子弹为1伤,主导弹为3伤),己方飞机血量设为50。
设定敌方飞机可以左右循环移动,子弹为1伤,飞机血量设为100。
小飞机无血量,不会发射导弹,撞向己方飞机会产生3伤的伤害

import pygame,os
from pygame.locals import *
import random
import time
class HeroBulleto():
    def __init__(self,x1,y1,windows):
        self.x1=x1
        self.y1=y1
        self.windows=windows
        self.pic1=pygame.image.load('feiji\\bullet.png')
        self.pic2= pygame.image.load('feiji\\bullet.png')
        self.pic3 = pygame.image.load('feiji\\bomb2.png')
    def draw(self):
        self.windows.blit(self.pic1,(self.x1,self.y1))
        self.move()
    def move(self):
        self.x1-=1
        self.y1-=3


class HeroBullettr():
    def __init__(self, x3, y3, windows):
        self.x3 = x3
        self.y3 = y3
        self.windows = windows
        self.pic3 = pygame.image.load('feiji\\bomb2.png')

    def draw(self):
        self.windows.blit(self.pic3, (self.x3, self.y3))
        self.move()

    def move(self):
        self.y3 -= 3

class HeroBullettw():
    def __init__(self,  x2, y2, windows):
        self.x2 = x2
        self.y2 = y2
        self.windows = windows
        self.pic2 = pygame.image.load('feiji\\bullet.png')

    def draw(self):
        self.windows.blit(self.pic2, (self.x2, self.y2))
        self.move()
    def move(self):
        self.x2 += 1
        self.y2 -= 3

class EnemyBullet():
    def __init__(self, x, y, windows):
        self.x = x
        self.y = y
        self.windows = windows
        self.pic = pygame.image.load(getpath('enemybiu.png'))
        # self.diji= pygame.image.load(getpath('091534_5027005851268.png'))
    def draw(self):
        self.windows.blit(self.pic, (self.x, self.y))
        self.move()
    def move(self):
        self.y += 1
class Enemyfeiji():
    def __init__(self, x, y, windows):
        self.x = x
        self.y = y
        self.windows = windows
        self.diji= pygame.image.load(getpath('091534_5027005851268.png'))
    def draw(self):
        self.windows.blit(self.diji, (self.x, self.y))
        self.move()
    def move(self):
        x=random.randint(-100,100)
        if x==-2 or x==2:
            self.x+=x
        self.y += 1

def getpath(path):
    return os.path.join('...\workpy\\feiji',path)#获得素材库路径

class Heroplan():
    tima=11
    def __init__(self,x,y,windows):
        self.x=x
        self.y=y
        self.windows=windows
        self.nomalImagelist=['091535_6378052234649.png','091535_6378052234640.png']
        self.namalIndex=0
        self.boomImagelist=['hero_blowup_n2.png','hero_blowup_n3.png','hero_blowup_n4.png']
        self.boomIndex=0
        self.isboom=False
        self.biulist=[]
        self.biulist1=[]
        self.biulist2=[]
        self.live=50

    def draw(self):
        if self.isboom==False:
            pic=pygame.image.load(getpath(self.nomalImagelist[self.namalIndex]))
            self.windows.blit(pic,(self.x-(self.namalIndex),self.y))

            self.namalIndex=(self.namalIndex+1)%len(self.nomalImagelist)
        else:
            if self.boomIndex == len(self.boomImagelist):
                time.sleep(1)
                exit(0)
            pic = pygame.image.load(getpath(self.boomImagelist[self.boomIndex]))
            windows.blit(pic, (self.x, self.y))
            self.boomIndex = (self.boomIndex + 1)
            time.sleep(0.5)
        for biu in self.biulist:
            biu.draw()
            self.biulist.remove(biu) if  biu.y1 < 0 else ''
        for biu1 in self.biulist1:
            biu1.draw()
            self.biulist1.remove(biu1) if biu1.y2 < 0 else ''
        for biu2 in self.biulist2:
            biu2.draw()
            self.biulist2.remove(biu2) if biu2.y3 < 0 else ''
    def dealevent(self,eventlist):
        for event in eventlist:
            if event.type==QUIT:
                exit(0)
            key_pressed = pygame.key.get_pressed()
            if key_pressed[K_w] or key_pressed[K_UP]:
                self.y = self.y - 5 if self.y > 5 else 0
            if key_pressed[K_s] or key_pressed[K_DOWN]:
                self.y = self.y + 5 if self.y < 565 else 570
            if key_pressed[K_a] or key_pressed[K_LEFT]:
                self.x = self.x - 5 if self.x > 5 else 0
            if key_pressed[K_d] or key_pressed[K_RIGHT]:
                self.x = self.x + 5 if self.x < 375 else 380
            if key_pressed[K_SPACE]:
                if Heroplan.tima>10:
                    onebullet  = HeroBulleto (self.x - 2, self.y + 23,windows)
                    onebullet3 = HeroBullettw(self.x + 81, self.y + 23,windows)
                    onebullet2 = HeroBullettr(self.x + 40, self.y - 40,windows)
                    self.biulist.append(onebullet)
                    self.biulist1.append(onebullet3)
                    self.biulist2.append(onebullet2)
                    Heroplan.tima=0
                else:
                    Heroplan.tima+=1
    def pzjc(self, bList,bList1):
        heroRect = Rect(self.x, self.y, 117, 80)
        for biu in bList:
            biuRect1 = Rect(biu.x, biu.y, 22, 22)
            if biuRect1.colliderect(heroRect):
                self.live -= 1
                print("Hero:",self.live)
                if self.live <= 0:
                    self.isboom = True
                enemyplan.biulist.remove(biu)
        for biu1 in bList1:
            biuRect2 = Rect(biu1.x, biu1.y, 50, 38)
            if biuRect2.colliderect(heroRect):
                self.live -= 3
                print("Hero",self.live)
                if self.live <= 0:
                    self.isboom = True
                enemyplan.plan.remove(biu1)

class Enemyplan():
    t=10
    count=1
    def __init__(self, x, y, windows,dic):
        self.x = x
        self.y = y
        self.dic=dic
        self.windows = windows
        self.nomalImagelist = ['091534_3025160074234.png',]
        self.namalIndex = 0
        self.boomImagelist = ['enemy0_down2.png','enemy0_down3.png','enemy0_down4.png']
        self.boomIndex = 0
        self.isboom = False
        self.biulist = []
        self.plan=[]
        self.live=100
        self.m= 0
    def draw(self):
        if self.isboom == False:
            pic = pygame.image.load(getpath(self.nomalImagelist[self.namalIndex]))
            self.windows.blit(pic, (self.x - ( self.namalIndex), self.y))
            self.namalIndex = (self.namalIndex + 1) % len(self.nomalImagelist)
            self.move()
            self.fire()
            self.m=random.randint(0, 440)
            if Enemyplan.t>5:
                self.feiji()
                Enemyplan.t = 0
            else:
                Enemyplan.t += 1
            for biu in self.biulist:
                biu.draw()
                self.biulist.remove(biu) if biu.y < 0  else ''
            for biu1 in self.plan:
                biu1.draw()
                self.plan.remove(biu1) if biu1.y < 0 else ''


        else:
            if self.boomIndex == len(self.boomImagelist):
                time.sleep(1)
                exit(0)
            pic = pygame.image.load(getpath(self.boomImagelist[self.boomIndex]))
            windows.blit(pic, (self.x, self.y))
            self.boomIndex = (self.boomIndex + 1)
            time.sleep(0.5)

    def move(self):
        if self.x <= 0:
            self.dic = '→'
        if self.x >=363:
            self.dic = '←'
        if self.dic == '←':
            self.x -=1
        if self.dic == '→':
            self.x +=1

    def fire(self):
        x = random.randint(0, 120)
        if x == 14 or x == 60:
            onebullet=EnemyBullet(self.x+69//2,self.y+70,windows)
            self.biulist.append(onebullet)
    def feiji(self):
        x=random.randint(0,120)
        if x==30 or x==90:
            twobullet=Enemyfeiji(self.m,0,windows)
            self.plan.append(twobullet)


    def pzjc(self, bList,bList1,bList2):
        dijiRect = Rect(self.x, self.y, 117, 80)
        for biu in bList:
            biuRect1 = Rect(biu.x1, biu.y1, 22, 22)
            if biuRect1.colliderect(dijiRect):
                self.live-= 1
                print(self.live)
                if self.live<= 0:
                    self.isboom = True
                heroplan.biulist.remove(biu)
        for biu1 in bList1:
            biuRect2 = Rect(biu1.x2, biu1.y2, 22, 22)
            if biuRect2.colliderect(dijiRect):
                self.live-= 1
                print(self.live)
                if self.live<= 0:
                    self.isboom = True
                heroplan.biulist1.remove(biu1)
        for biu2 in bList2:
            biuRect3 = Rect(biu2.x3, biu2.y3, 22, 48)
            if biuRect3.colliderect(dijiRect):
                self.live-= 3
                print(self.live)
                if self.live<= 0:
                    self.isboom = True
                heroplan.biulist2.remove(biu2)

windows=pygame.display.set_mode((480,650),0,32)
pygame.display.set_caption('打飞机')
icon=pygame.image.load(getpath('icon72x72.png'))
pygame.display.set_icon(icon)
bj=pygame.image.load(getpath('579b1fd07753b.png'))
heroplan=Heroplan(480//2-100//2,680-110,windows)
enemyplan=Enemyplan(480//2-117//2,0,windows,'←')
pygame.key.set_repeat(20, 30)
while True:
    windows.blit(bj,(0,0))
    heroplan.draw()
    enemyplan.draw()
    enemyplan.pzjc(heroplan.biulist,heroplan.biulist1,heroplan.biulist2)
    heroplan.pzjc(enemyplan.biulist,enemyplan.plan)
    heroplan.dealevent(pygame.event.get())
    pygame.display.update()

己方飞机在下方,敌机boss水平运动,小飞机为敢死队飞机

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值