飞机大战-飞机爆炸(8)

简介: 编写飞机大战,完成飞机爆炸功能。

编写飞机大战,完成飞机爆炸功能。完成后相关代码如下:

1.搭建界面,主要完成窗口和背景图的显示

import pygame
import time
from pygame.locals import *
import random
"""
判定输赢,飞机爆炸
敌方子弹在我方飞机上,我方子弹在敌方飞机上。
我方子弹和敌方子弹碰撞
step1:在飞机基类中添加is_hit=False,用于表示飞机是否被击中
step2:在飞机基类中添加test(),用于检测
step3:在主函数中,显示我机下,调用test(),并增条件语句,如果被击中,替换图片

"""

飞机的基类

class BasePlan:
def init(self, screen, x, y, image):
self.x = x
self.y = y
self.screen = screen
self.image = pygame.image.load(image)
self.bullet_list = [] # 存储发射的子弹
self.is_hit = False # # 此标志用来表示飞机是否被击中了

def test(self, bullets):
    # 检测飞机被击中,子弹处于飞机的上
    for bullet in bullets:
        if self.x < bullet.x <self.x+self.image.get_width() and self.y < bullet.y < self.y + self.image.get_height():
            self.is_hit = True


def display(self):
    self.screen.blit(self.image, (self.x, self.y))

    for bullet in self.bullet_list:
        bullet.display()
        bullet.move()
        # 判断子弹是否越界
        if bullet.judge():
            self.bullet_list.remove(bullet)

class HeroPlan(BasePlan):

def __init__(self, screen, image="./feiji/hero1.png"):
    super().__init__(screen,190, 576, image)

def move_left(self):
    self.x -= 5

def move_right(self):
    self.x += 5

def fire(self):
    self.bullet_list.append(Bullet(self.screen, self.x, self.y))

敌机

class EnemyPlan(BasePlan):

def __init__(self, screen,image="./feiji/enemy0.png"):
    super().__init__(screen, 0, 0, image)
    self.direction = "right"  # 新增一个属性,表示敌机的方向

def move(self):
    # 敌机移动
    if self.direction == "right":
        self.x += 5
    else:
        self.x -= 5

    if self.x > 450:
        self.direction = "left"
    elif self.x < 0:
        self.direction = "right"

def fire(self):
    random_num = random.randint(1, 100)
    if random_num == 10 or random_num == 20:
        self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y))

子弹的基类:

class BaseBullet:

def __init__(self, screen, x, y,image):
    self.x = x
    self.y = y
    self.screen = screen
    self.image = pygame.image.load(image)

def display(self):
    self.screen.blit(self.image, (self.x, self.y))

我机发射的子弹

class Bullet(BaseBullet):

def __init__(self, screen, x, y,image="./feiji/bullet.png"):
    super().__init__(screen, x+40,y-20,image)

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

def judge(self):
    if self.y < 0:
        return True
    else:
        return False

敌机发射的子弹

class EnemyBullet(BaseBullet):

def __init__(self, screen, x, y,image="./feiji/bullet1.png"):
    super().__init__(screen, x + 15, y + 20, image)

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

def judge(self):
    if self.y > 700:
        return True
    else:
        return False

def key_control(hero):

# 获取事件,比如按键等
for event in pygame.event.get():

    # 判断是否是点击了退出按钮
    if event.type == QUIT:
        print("exit")
        exit()
    # 判断是否是按下了键
    elif event.type == KEYDOWN:
        # 检测按键是否是a或者left
        if event.key == K_a or event.key == K_LEFT:
            print('left')
            hero.move_left()

        # 检测按键是否是d或者right
        elif event.key == K_d or event.key == K_RIGHT:
            print('right')
            hero.move_right()

        # 检测按键是否是空格键
        elif event.key == K_SPACE:
            print('space')
            # 发射子弹
            hero.fire()

def main():

# 1.创建一个窗口,用来显示内容,窗口宽480,高800
scree = pygame.display.set_mode((480, 700), 0, 32)

# 2.创建一个和窗口大小的图片,用来当背景图
background = pygame.image.load("feiji/background.png")

# 3.把背景图放到窗口中显示

# 4.创建一个飞机图片
hero = HeroPlan(scree)

# 5.创建一个敌机
enemy = EnemyPlan(scree)

hero_nums = 0
enemy_nums = 0
while True:
    # 设定需要显示的背景图
    scree.blit(background, (0, 0))  # 背景图的左上角,和窗口左上角重合
    hero.display()
    # 添加检测我机是否被炸
    hero.test(enemy.bullet_list)
    if hero.is_hit:
        hero_nums += 1
        if hero_nums == 10:
            hero.image = pygame.image.load("./feiji/hero_blowup_n1.png")
        elif hero_nums == 20:
            hero.image = pygame.image.load("./feiji/hero_blowup_n2.png")
        elif hero_nums == 30:
            hero.image = pygame.image.load("./feiji/hero_blowup_n3.png")
        elif hero_nums == 40:
            hero.image = pygame.image.load("./feiji/hero_blowup_n4.png")
        elif hero_nums > 50:
            break

    enemy.display()
    enemy.test(hero.bullet_list)
    if enemy.is_hit:
        enemy_nums += 1
        if enemy_nums == 10:
            enemy.image = pygame.image.load("./feiji/enemy0_down1.png")
        elif enemy_nums == 20:
            enemy.image = pygame.image.load("./feiji/enemy0_down2.png")
        elif enemy_nums == 30:
            enemy.image = pygame.image.load("./feiji/enemy0_down3.png")
        elif enemy_nums == 40:
            enemy.image = pygame.image.load("./feiji/enemy0_down4.png")
        elif enemy_nums > 50:
            enemy = EnemyPlan(scree)
            enemy_nums = 0
    enemy.move()  # 调用敌机的移动方法
    enemy.fire()  # 敌机发火
    # 更新需要显示的内容
    pygame.display.update()

    # 键盘事件
    key_control(hero)

    time.sleep(0.05)

if name == "main":
main()

相关文章
Threejs实现下雨,下雪,阴天,晴天,火焰
Threejs实现下雨,下雪,阴天,晴天,火焰
1906 0
|
4月前
|
缓存 数据可视化 Serverless
微信小游戏 案例一 像素飞机
微信小游戏 案例一 像素飞机
40 2
|
8月前
小鸟飞行游戏【附源码】
小鸟飞行游戏【附源码】
93 2
小鸟飞行游戏【附源码】
|
8月前
|
图形学
【unity小技巧】实现FPS射击游戏枪武器随镜头手臂摇摆效果
【unity小技巧】实现FPS射击游戏枪武器随镜头手臂摇摆效果
82 0
|
8月前
|
定位技术 图形学
【用unity实现100个游戏之1】制作类元气骑士、挺进地牢——俯视角射击游戏多种射击效果(一)(附源码)
【用unity实现100个游戏之1】制作类元气骑士、挺进地牢——俯视角射击游戏多种射击效果(一)(附源码)
248 0
|
8月前
|
人工智能 BI
技术心得:国王游戏&保护花朵
技术心得:国王游戏&保护花朵
44 0
Egret学习笔记 (Egret打飞机-8.敌机和主角飞机发射子弹)
Egret学习笔记 (Egret打飞机-8.敌机和主角飞机发射子弹)
88 0
|
JavaScript 容器
Egret学习笔记 (Egret打飞机-4.添加主角飞机和实现飞行效果)
Egret学习笔记 (Egret打飞机-4.添加主角飞机和实现飞行效果)
110 0
|
前端开发
2、CSS动画之行走的米兔、奔跑的小人
2、CSS动画之行走的米兔、奔跑的小人
297 0
|
前端开发 JavaScript 程序员
「《奇迹再现》专属音乐播放器🎵」致以跃动的心与阳光
「《奇迹再现》专属音乐播放器🎵」致以跃动的心与阳光
274 0