pygame制作飞机大战3——边缘检测、发射子弹

本文介绍了如何使用pygame制作飞机大战游戏,包括飞机的边缘检测以防止超出屏幕,以及子弹的发射机制。边缘检测通过判断飞机坐标防止飞出窗口,子弹则按固定时间间隔发射并向上移动直至屏幕顶部。代码中包含了子弹位置调整和发射间隔的设定,以模拟真实的飞行射击体验。

1、边缘检测

飞机可以自由移动后就需要做边缘检测,避免移动到弹窗之外的坐标,pygame的原点坐标在左上角,而我的窗口大小是(450,550),所以只需要当检测到当飞机要飞出这四个坐标范围(0, y)、(x, 0)、(450,y)、(x,550)时将对应的坐标变为边缘对应的值即可,定义函数为judge,代码如下:

def judge(potx, poty):
    if potx >= 426:
        potx = 426
    elif potx <= -24:
        potx = -24
    elif poty >= 526:
        poty = 526
    elif poty <= -24:
        poty = -24
    return potx, poty

之所以调整到了426、526以及-24是因为在视觉上这个时候飞机中心刚好处于界面边缘,比较符合实际在游戏中的样子(当然,有的游戏飞机不能以半身存在,这一点根据喜好调整即可)

2、子弹发射

对子弹发射的步骤进行拆分可以知道,每过一段时间飞机会生成一颗子弹,随后子弹向正上方移动,直到画面边缘,所以在这里需要完成三个小步:

(1)为子弹发射准备时间间隔;

(2)设定子弹每一次向上移动的步数,保证从飞机上发射能达到弹窗顶端;

(3)将第二步的子弹画到界面中。

    if i == 0:
        button_x1 = x1 + 3
        button_y1 = y1 + 60
    elif i == 90:
        i = -10
    i += 10
    button_y1 -= 65

x1+3和y1+60是为了调整子弹发射的位置,保证是从飞机机翼上的发射口上发出,i==90和i+=10是为了通过从0自增9次,9*代码刷新时间 = 不同两颗子弹发射的时间间隔,而button_y1 -= 65则是子弹每一次向上移动的步数,步数值越大,子弹的速度越快。飞机有两个发射口,因此设置了另一个子弹(当然,x1 + 27是将初始位置设置到了另一个发射口上):

    if j == 0:
        button_x2 = x1 + 27
        button_y2 = y1 + 60
    elif j == 90:
        j = -10
    j += 10
    button_y2 -= 65

子弹都是直接向上移动,而多数飞机大战的子弹数量、方向都可以根据不同的道具捕获来变化,这一点在后面会做设计,感兴趣也可以直接调整试试,比如单次不同位置射出8颗,单次同一个位置先后射出两颗,看看有什么不同的效果,本次的完整代码如下(PS:部分功能实现可能不会使用到太多的函数调用,目的是先跑起来,后续会继续优化):

#参数初始化
background_image_filename = 'background.png'
mouse_image_filename = 'airplane.png'
button_image_filename = 'button1.png'

#导入函数
import pygame
from pygame.locals import *
from sys import exit

#加载并转换图像
background = pygame.image.load(background_image_filename)
mouse_cursor = pygame.image.load(mouse_image_filename)
button_picture1 = pygame.image.load(button_image_filename)
button_picture2 = pygame.image.load(button_image_filename)

#创建窗口及标题
screen = pygame.display.set_mode((450, 550), 0, 32)
pygame.display.set_caption("hello world")

#初始化pygame,为硬件使用作准备
pygame.init()

x1 = 225
y1 = 275
#子弹坐标初始化
button_x1 = 0
button_y1 = 0
button_x2 = 0
button_y2 = 0
v = 4
i = 0
j = 0

# 检测边缘
def judge(potx, poty):
    if potx >= 426:
        potx = 426
    elif potx <= -24:
        potx = -24
    elif poty >= 526:
        poty = 526
    elif poty <= -24:
        poty = -24
    return potx, poty

#游戏主循环
while True:
    # 检测退出事件,判断是否执行退出
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()

    #将背景画上去
    screen.blit(background, (0, 0))

    #获取按键状态并完成移动
    press = pygame.key.get_pressed()

    if press[K_LEFT] == True:
        if press[K_UP] == True:
            x1 = x1 - v
            y1 = y1 - v
        elif press[K_DOWN] == True:
            x1 = x1 - v
            y1 = y1 + v
        else:
            x1 = x1 - v
    elif press[K_RIGHT] == True:
        if press[K_UP] == True:
            x1 = x1 + v
            y1 = y1 - v
        elif press[K_DOWN] == True:
            x1 = x1 + v
            y1 = y1 + v
        else:
            x1 = x1 + v
    elif press[K_UP] == True:
        y1 = y1 - v
    elif press[K_DOWN] == True:
        y1 = y1 + 4
    # 获取鼠标位置
    #x, y = pygame.mouse.get_pos()
    #计算光标在左上角的位置
    #x -= mouse_cursor.get_width() / 2
    #y -= mouse_cursor.get_height() / 2

    #子弹移动
    #子弹1移动
    if i == 0:
        button_x1 = x1 + 3
        button_y1 = y1 + 60
    elif i == 90:
        i = -10
    i += 10
    button_y1 -= 65
    #子弹2移动
    if j == 0:
        button_x2 = x1 + 27
        button_y2 = y1 + 60
    elif j == 90:
        j = -10
    j += 10
    button_y2 -= 65

    #判断边缘
    x1, y1 = judge(potx = x1, poty = y1)

    #随机出现敌人
    pass

    #子弹击中
    pass

    #显示分数
    pass

    #将光标画上去
    screen.blit(mouse_cursor, (x1, y1))
    screen.blit(button_picture1, (button_x1, button_y1))
    screen.blit(button_picture2, (button_x2, button_y2))
    # 在新的位置上画图
    pygame.display.update()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值