基于python的《飞机大战》游戏

一、需求

设计一个简单的《飞机大战》游戏,实现我方飞机和敌方飞机均可左右移动,并且可以射击子弹,而且被击中后会退出游戏

二、效果图

在这里插入图片描述
被击中后:
在这里插入图片描述

三、代码

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

def getPath(path):# 创建一个传入路径的函数,返回路径,然后避免以后传东西的时候路径太长
    return os.path.join("C:\\Users\\Administrator\Desktop\IT研究院-Python\\New_Stydy\\img\\", path)
    
#飞机和子弹共同类
class Father():
    def __init__(self, x, y, windows):
        self.x = x
        self.y = y
        self.windows = windows
        
#子弹父类
class BiuFather(Father):
     def draw(self):
        self.windows.blit(self.image, (self.x, self.y))
        self.move()
        
#飞机父类
class Plane(Father):
    def __init__(self, x, y, windows):
        super().__init__(x,y,windows)
        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 Biu(BiuFather):
    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(BiuFather):
    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 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()

四、图片素材包

https://download.csdn.net/download/qq_40558166/11474415?spm=1001.2014.3001.5503

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张烫麻辣亮。

谢谢老板支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值