飞机大战

飞机大战程序

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


class Biu():
    def __init__(self, x, y, wind):
        self.x = x
        self.y = y
        self.wind = wind
        self.pic = pygame.image.load(r'C:\Users\Administrator\Desktop\素材\bullet.png')

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

    def move(self):
        self.y -= 6  # 我方子弹速度


class EnemyBiu():
    def __init__(self, x, y, wind):
        self.x = x
        self.y = y
        self.wind = wind
        self.pic = pygame.image.load(r'C:\Users\Administrator\Desktop\素材\bullet1.png')

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

    def move(self):
        self.y += 3  # 敌机子弹速度


heroIndex = 0
wind = pygame.display.set_mode((480, 625), 0, 32)  # 窗口大小
background = pygame.image.load(r'C:\Users\Administrator\Desktop\素材\background.png')  # 导入背景图片
icon = pygame.image.load(r'C:\Users\Administrator\Desktop\素材\game_loading1.png')  # 导入左上角图标
heroPlane1 = pygame.image.load(r'C:\Users\Administrator\Desktop\素材\hero1.png')  # 导入我方战机图片1
heroPlane2 = pygame.image.load(r'C:\Users\Administrator\Desktop\素材\hero2.png')  # 导入我方战机图片2
heroBombImageList = [r'C:\Users\Administrator\Desktop\素材\hero_blowup_n1.png',
                     r'C:\Users\Administrator\Desktop\素材\hero_blowup_n2.png',
                     r'C:\Users\Administrator\Desktop\素材\hero_blowup_n3.png',
                     r'C:\Users\Administrator\Desktop\素材\hero_blowup_n4.png']
enemyPlane = pygame.image.load(r'C:\Users\Administrator\Desktop\素材\enemy1.png')  # 导入敌机机图片

heroBombImageIndex = 0
heroPlaneIsBomb = False
pygame.display.set_caption('飞机大战')
pygame.display.set_icon(icon)  # 图标
pygame.key.set_repeat(50, 10)  # 第一个参数是键盘按下30ms后开始反应,第二个参数是按下30ms为抬起再触发一次新的按键事件
hero_x = (480 - 100) // 2
hero_y = 620 - 124
enemy_x = (480 - 69) // 2
direct = '左'
heroBiulist = []
enemyBiulist = []

while True:
    wind.blit(background, (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 heroBombImageIndex == len(heroBombImageList):
            time.sleep(0.1)
            exit(0)
        pic = pygame.image.load(heroBombImageList[heroBombImageIndex])
        wind.blit(pic, (hero_x, hero_y))
        heroBombImageIndex += 1
        time.sleep(0.1)

    wind.blit(enemyPlane, (enemy_x, 0))  # 贴上敌方战机图片
    enemy_Rect = Rect(enemy_x, 0, 69, 89)
    pygame.display.update()
    for zd in heroBiulist:
        zd.draw()
        zdRect = Rect(zd.x, zd.y, 22, 22)
        if enemy_Rect.colliderect(zdRect):
            print("敌方被击中")
            heroBiulist.remove(zd)
        else:
            heroBiulist.remove(zd) if zd.y < 0 else ''

    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 > 625 else ''

    for event in pygame.event.get():  # 循环获取事件(尤其是鼠标、键盘事件)
        if event.type == QUIT:  # 如果事件类型是退出(点击退出按钮触发)
            print("退出了")
            exit(0)
        elif event.type == KEYDOWN:
            if event.key == K_LEFT:
                hero_x = hero_x - 5 if hero_x > 5 else 0
            elif event.key == K_RIGHT:
                hero_x = hero_x + 5 if hero_x < 480 - 100 - 5 else 480 - 100
            elif event.key == K_SPACE:
                zd = Biu(hero_x + 100 // 2 - 22 // 2, hero_y - 22, wind)
                heroBiulist.append(zd)

    if direct == '左':
        enemy_x -= 2
        if enemy_x <= 0:
            direct = '右'
    else:
        enemy_x += 2
        if enemy_x > + 480 - 69:
            direct = '左'

    h = random.randint(0, 200)
    if h == 3:
        zd = EnemyBiu(enemy_x + 69 // 2 - 9 // 2, 89, wind)
        enemyBiulist.append(zd)

    pygame.display.update()
    pass


面向对象版飞机大战

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


def getPath(path):
    return os.path.join("C:\\Users\\Administrator\Desktop\IT研究院-Python\\New_Stydy\\img\\", path)


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


class Zd(ZhuangBei):

    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("bullet.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.windows = windows  # 设置窗口 x y 坐标
        self.normalImageListIndex = 0
        self.bombImageIndex = 0
        self.isBomb = False
        self.biuList = []

    def draw(self):
        if not self.isBomb:
            image = pygame.image.load(getPath(self.normalImageList[self.normalImageListIndex]))
            self.normalImageListIndex = (self.normalImageListIndex + 1) % len(self.normalImageList)  # 切换飞机显示图片
            self.windows.blit(image, (self.x, self.y))

        else:
            # 敌方飞机爆炸
            if len(self.bombImageList) == self.bombImageIndex:
                time.sleep(0.5)
                exit(0)
            image = pygame.image.load(getPath(self.bombImageList[self.bombImageIndex]))
            self.bombImageIndex += 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.bombImageList = ['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()

    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):
        x = random.randint(1, 100)
        if x == 3:
            zd = EnemyBiu(self.x + 69 // 2 - 9 // 2, self.y + 89, self.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.bombImageList = ['hero_blowup_n1.png', 'hero_blowup_n2.png', 'hero_blowup_n3.png',
                              'hero_blowup_n4.png']  # 我方飞机机爆炸图片
        super().__init__(x, y, windows)

    def draw(self):
        super().draw()

        for zd in self.biuList:
            zd.draw()
            self.biuList.remove(zd) if zd.y < 0 else ''  # 子弹越界删除子弹

    # 我方飞机碰撞检测
    def pzjc(self, bList):
        eRect = Rect(self.x, self.y, 100, 124)
        for zd in bList:
            zdRect = Rect(zd.x, zd.y, 9, 21)
            if zdRect.colliderect(eRect):
                self.isBomb = True
                bList.remove(zd)

    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_SPACE:
                    zd = Biu(self.x + 100 // 2 - 22 // 2, self.y - 22, self.windows)
                    self.biuList.append(zd)


windows = pygame.display.set_mode((480, 652), 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(50, 10)  # 第一个参数是键盘按下30ms后开始反应,第二个参数是按下30ms为抬起再触发一次新的按键事件
heroPlane = HeroPlane(480 // 2 - 100 // 2, 652 - 124, windows)  # 贴上我方飞机图片
enemyPlane = EnemyPlane(480 // 2 - 69 // 2, 0, windows)  # 贴上敌方飞机图片

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值