飞机大战

面向过程

import pygame
from pygame.locals import *
import random, time


class Biu():
    def __init__(self, x, y, wind):
        self.x = x
        self.y = y
        self.wind = wind
        self.pic = pygame.image.load(r"D:\python\飞机大战\img\img\bullet.png")

    def draw(self):
        self.wind.blit(self.pic, (self.x, self.y))
        self.move()

    def move(self):
        self.y -= 5


class EnemyBiu():
    def __init__(self, x, y, wind):
        self.x = x
        self.y = y
        self.wind = wind
        self.pic = pygame.image.load(r"D:\python\飞机大战\img\img\bullet1.png")

    def draw(self):
        self.wind.blit(self.pic, (self.x, self.y))
        self.move()

    def move(self):
        self.y += 5

wind = pygame.display.set_mode((480, 500), 0, 32)  # 设置窗口
background = pygame.image.load(r"D:\python\飞机大战\img\img\background.png")
icon = pygame.image.load(r"D:\python\飞机大战\img\img\icon72x72.png")
heroIndex=0
heroPlane1=pygame.image.load(r"D:\python\飞机大战\img\img\hero1.png")
heroPlane2=pygame.image.load(r"D:\python\飞机大战\img\img\hero2.png")
heroBomImageList=[r"D:\python\飞机大战\img\img\hero_blowup_n1.png",r"D:\python\飞机大战\img\img\hero_blowup_n2.png",r"D:\python\飞机大战\img\img\hero_blowup_n3.png",r"D:\python\飞机大战\img\img\hero_blowup_n4.png"]
enemyBomImageList=[r"D:\python\飞机大战\img\img\enemy1_down1.png",r"D:\python\飞机大战\img\img\enemy1_down2.png",r"D:\python\飞机大战\img\img\enemy1_down3.png",r"D:\python\飞机大战\img\img\enemy1_down3.png"]
heroBomImageIndex=0
enemyBomImageIndex=0
heroPlaneIsBomb=False
enemyPlaneIsBomb=False

# heroPlane=pygame.image.load(r"D:\python\飞机大战\img\img\hero1.png")
enemyPlane=pygame.image.load(r"D:\python\飞机大战\img\img\enemy1.png")

pygame.display.set_caption("飞机大战")#设置窗口名
pygame.display.set_icon(icon)#设置窗口图标
pygame.key.set_repeat(30,30)#第一个参数是,按下键30毫秒后开始反应,第二个是键盘按下30毫秒未抬起触发按键
hero_x=(480-100)//2
hero_y=500-124
enemy_x=(480-69)//2
direct="左"
heroBiuList=[]
enemyBiuList=[]
zd=None
while True:
    wind.blit(background, (0, 0))  # 贴图片,从0,0窗口位置
    #我方飞机处于正常状态,或者被击落
    if heroPlaneIsBomb==False:
        if heroIndex==0:
            wind.blit(heroPlane1,(hero_x,hero_y))
            heroIndex=1
        else:
            wind.blit(heroPlane2,(hero_x, hero_y))
            heroIndex = 0
    else:
        if heroBomImageIndex==len(heroBomImageList):
            time.sleep(1)
            exit(0)
        pic=pygame.image.load(heroBomImageList[heroBomImageIndex])
        wind.blit(pic,(hero_x,hero_y))
        heroBomImageIndex+=1
        time.sleep(0.5)
        # 敌方飞机处于正常状态,或者被击落
    if enemyPlaneIsBomb==False:
        wind.blit(enemyPlane, (enemy_x, 0))
    else:
        if enemyBomImageIndex==len(enemyBomImageList):
            time.sleep(2)
            exit(0)
        pic = pygame.image.load(enemyBomImageList[enemyBomImageIndex])
        wind.blit(pic, (enemy_x, 0))
        enemyBomImageIndex += 1
        time.sleep(1)

    # wind.blit(heroPlane,(hero_x,hero_y))
    # wind.blit(enemyPlane,(enemy_x,0))
    #我方子弹与敌机相遇
    enemy_Rect=Rect(enemy_x,0,69,69)
    for zd in heroBiuList:
        zd.draw()
        zdRect=Rect(zd.x,zd.y,22,22)
        if enemy_Rect.colliderect(zdRect):
            print("击败敌机")
            enemyPlaneIsBomb = True
            heroBiuList.remove(zd)
        else:
            heroBiuList.remove(zd) if zd.y<0 else ""
    #敌方子弹与我相遇Rect()是两矩形相遇触发函数
    hero_Rect=Rect(hero_x,hero_y,100,124)
    for zd in enemyBiuList:
        zd.draw()
        zdRect=Rect(zd.x,zd.y,9,21)
        if hero_Rect.colliderect(zdRect):
            print("被击中")
            heroPlaneIsBomb=True
            enemyBiuList.remove(zd)
        else:
            enemyBiuList.remove(zd) if zd.y >500 else ""

    #键盘触发事件
    for event in pygame.event.get():#循环获取事件(尤其是鼠标,键盘事件)
        if event.type==QUIT: #如果事件的类型是退出(点击退出按钮触发)
            print("退出")
            exit(0)#退出wind
        elif event.type==KEYDOWN:
            if event.key==K_LEFT:
                hero_x=hero_x-5 if hero_x>0 else 0
            elif event.key==K_RIGHT:
                hero_x = hero_x + 5 if hero_x<480-100 else 480-100
            elif event.key==K_UP:
                hero_y = hero_y-5 if hero_y>0 else 0
            elif event.key == K_DOWN:
                hero_y = hero_y + 5 if hero_y < 500 - 124 else 500-124
            elif event.key==K_SPACE:
                zd=Biu(hero_x+100//2 -22//2,hero_y-22,wind)
                heroBiuList.append(zd)
    #敌机左右移动
    if direct=="左":
        enemy_x-=1
        if enemy_x<=0:
            direct="右"
    else:
        enemy_x+=1
        if enemy_x>=480-69:
            direct="左"

    #敌方子弹随机射出
    h=random.randint(0,100)
    if h==3 :
        zd=EnemyBiu(enemy_x+69//2-9//2,89,wind)
        enemyBiuList.append(zd)
        pass

    pygame.display.update()  # 更新,相当于把图片前的帘子去掉
    pass

面向对象

import pygame
from pygame.locals import *
import os
import random
import time


def getPath(path):
    return os.path.join("D:\\python\\飞机大战\\img\\img", path)

class ZhuangBei():
    def __init__(self, x, y, windows):
        self.x = x
        self.y = y
        self.windows = windows

class Zd(ZhuangBei):
    # def __init__(self, x, y, windows):
    #     super().__init__(x,y,windows)#都一样就不用写了
    def draw(self):
        self.windows.blit(self.image, (self.x, self.y))
        self.move()  # 如果这个类不实例化,子类找不到方法会在自己的类中找。

class Biu(Zd):
    def __init__(self, x, y, windows):
        super().__init__(x, y, windows)
        self.image = pygame.image.load(getPath("bomb.png"))

    def move(self):
        self.y -= 5


class EnemyBiu(Zd):
    def __init__(self, x, y, windows):
        super().__init__(x, y, windows)
        self.image = pygame.image.load(getPath("bullet1.png"))

    def move(self):
        self.y += 5


class Plane(ZhuangBei):
    def __init__(self, x, y, windows):
        super().__init__(x, y, windows)
        self.normalImageIndex = 0
        self.bomImageIndex = 0
        self.isBomb = False
        self.biuList = []

    def draw(self):
        if not self.isBomb:
            image = pygame.image.load(getPath(self.normalImageList[self.normalImageIndex]))
            self.normalImageIndex = (self.normalImageIndex + 1) % len(self.normalImageList)  # 切换飞机显示图片
            self.windows.blit(image, (self.x, self.y))
        else:
            if len(self.bomImageList) == self.bomImageIndex:
                time.sleep(0.5)
                exit(0)
            image = pygame.image.load(getPath(self.bomImageList[self.bomImageIndex]))
            self.bomImageIndex += 1
            self.windows.blit(image, (self.x, self.y))
            time.sleep(0.3)


class EnemyPlane(Plane):
    def __init__(self, x, y, windows):
        self.normalImageList = ['enemy1.png']
        self.bomImageList = ['enemy1_down1.png', 'enemy1_down2.png', 'enemy1_down3.png', 'enemy1_down4.png']
        super().__init__(x, y, windows)
        self.direct = "左"

    def draw(self):
        super().draw()
        if not self.isBomb:
            self.move()
            self.fire()
        for zd in self.biuList:
            zd.draw()
            self.biuList.remove(zd) if zd.y > 625 else ""

    def move(self):
        if self.direct == "左":
            self.x -= 2
            if self.x <= 0:
                self.direct = "右"
        else:
            self.x += 2
            if self.x >= 480 - 69:
                self.direct = "左"

    def fire(self):
        h = random.randint(0, 100)
        if h == 3 or h == 67:
            zd = EnemyBiu(self.x + 69 // 2 - 9 // 2, 89, windows)
            self.biuList.append(zd)

    def pzjc(self, bList):
        eRect = Rect(self.x, self.y, 69, 89)
        for zd in bList:
            zdRect = Rect(zd.x, zd.y, 22, 22)
            if zdRect.colliderect(eRect):
                self.isBomb = True
                bList.remove(zd)


class HeroPlane(Plane):
    def __init__(self, x, y, windows):
        self.normalImageList = ['hero1.png', 'hero2.png']
        self.bomImageList = ['hero_blowup_n1.png', 'hero_blowup_n2.png', 'hero_blowup_n3.png', 'hero_blowup_n4.png']
        super().__init__(x, y, windows)

    def pzjc(self, bList):
        zRect = Rect(self.x, self.y, 100, 124)
        for zd in bList:
            edRect = Rect(zd.x, zd.y, 9, 21)
            if edRect.colliderect(zRect):
                self.isBomb = True
                bList.remove(zd)

    def draw(self):
        super().draw()
        for zd in self.biuList:
            zd.draw()
            self.biuList.remove(zd) if zd.y < 0 else ""

    def dealEvent(self, eventList):
        for event in eventList:
            if event.type == QUIT:
                exit(0)
            elif event.type == KEYDOWN:
                if event.key == K_LEFT:
                    self.x = self.x - 5 if self.x > 5 else 0
                elif event.key == K_RIGHT:
                    self.x = self.x + 5 if self.x < 480 - 100 - 5 else 480 - 100
                elif event.key == K_UP:
                    self.y = self.y - 5 if self.y > 0 else 0
                elif event.key == K_DOWN:
                    self.y = self.y + 5 if self.y < 625 - 124 else 625 - 124
                elif event.key == K_SPACE:
                    zd = Biu(self.x + 100 // 2 - 22 // 2, self.y - 22, self.windows)
                    self.biuList.append(zd)


windows = pygame.display.set_mode((480, 625), 0, 32)
pygame.display.set_caption("飞机大战")
backGround = pygame.image.load(getPath("background.png"))
icon = pygame.image.load(getPath('icon72x72.png'))
pygame.display.set_icon(icon)
pygame.key.set_repeat(30, 30)  # 第一个参数是,按下键30毫秒后开始反应,第二个是键盘按下30毫秒未抬起触发按键
heroPlane = HeroPlane(480 // 2 - 100 // 2, 625 - 124, windows)
enemyPlane = EnemyPlane((480 - 69) // 2, 0, windows)

while True:
    windows.blit(backGround, (0, 0))
    heroPlane.draw()
    heroPlane.dealEvent(pygame.event.get())
    enemyPlane.draw()
    enemyPlane.pzjc(heroPlane.biuList)
    heroPlane.pzjc(enemyPlane.biuList)
    pygame.display.update()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值