python - 如何在特定时间间隔连续触发 Action ?敌人在pygame中发射恒定光束而不是子弹

标签 python pygame projectile

<分区>

试图在 pygame 中创建一个在 pygame 中沿直线发射子弹的敌人。我设法让敌人开枪,但它射出的是一束恒定的子弹,而不是将它们隔开。有什么办法可以隔开子弹吗?

这是敌人的类(class)

class Boss(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((100, 70))
        self.image.fill(white)
        self.rect = self.image.get_rect()
        self.rect.centerx = WIDTH / 2
        self.rect.y = (WIDTH / 2) - 500
        self.speedy = 3

    def update(self):
        self.rect.y += self.speedy
        if self.rect.y >= 30:
            self.rect.y = 30

    def shoot(self):
        bossbullet = Bossbullet(self.rect.centerx, self.rect.bottom)
        all_sprites.add(bossbullet)
        bossbullets.add(bossbullet)


class Bossbullet(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((10, 20))
        self.image.fill(white)
        self.rect = self.image.get_rect()
        self.rect.bottom = y
        self.rect.centerx = x
        self.speedy = -10

    def update(self):
        self.rect.y -= self.speedy

        if self.rect.bottom < 0:
            self.kill()

all_sprites = pygame.sprite.Group()
boss = Boss()
all_sprites.add(boss)
bossbullets = pygame.sprite.Group()

这是游戏运行和敌人射击的循环

running = True
while running:

    clock.tick(FPS)
    if boss.rect.y >= 30:
        boss.shoot()

最佳答案

我建议使用计时器事件。使用 pygame.time.set_timer()重复创建 USEREVENT .例如:

milliseconds_delay = 500 # 0.5 seconds
bullet_event = pygame.USEREVENT + 1
pygame.time.set_timer(bullet_event, milliseconds_delay)

请注意,在 pygame 中可以定义客户事件。每个事件都需要一个唯一的 ID。用户事件的 ID 必须从 pygame.USEREVENT 开始。在这种情况下,pygame.USEREVENT+1 是生成子弹的计时器事件的事件 ID。

当事件在事件循环中发生时创建一个新的项目符号:

running = True
while running:

    clock.tick(FPS)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

         elif event.type == bullet_event:
             if boss.rect.y >= 30:
                 boss.shoot()

is there any way to make it so the enemy pauses for a while after..let's say 5 shots, then starts shooting again after the pause

可以通过将 0 传递给时间参数来停止定时器事件。例如:

delay_time = 500  # 0.5 seconds
pause_time = 3000 # 3 seconds
bullet_event = pygame.USEREVENT + 1
pygame.time.set_timer(bullet_event, delay_time)

no_shots = 0

running = True
while running:

    clock.tick(FPS)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

         elif event.type == bullet_event:
             if boss.rect.y >= 30:
                 boss.shoot()

                 # change the timer event time
                 if no_shots == 0:
                     pygame.time.set_timer(bullet_event, delay_time)
                 no_shots += 1
                 if no_shots == 5:
                     pygame.time.set_timer(bullet_event, pause_time)
                     no_shots = 0

    killed = # [...] set state when killed

    # stop timer
    if killed:
        pygame.time.set_timer(bullet_event, 0)

关于python - 如何在特定时间间隔连续触发 Action ?敌人在pygame中发射恒定光束而不是子弹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58224668/

相关文章:

python - 为什么我在 pygame 中的玩家角色有时会消失在瓷砖后面,但仍然会移动?

python - 通过欧拉方法预测抛射运动的问题

python - Pandas 箱线图 x 轴设置

python - 如何修复机器学习中恒定的验证准确性?

python - 惯用标准输出作为替代

python - "PortMidi: ` 关闭 mido 端口时错误指针 '"

带有 pydoc 的 django.views.generic 的 Python 文档

python - pygame 不显示我的图像

C++ vector 删除崩溃程序

android - LibGdx弹射器轨迹