本文介绍了如何以特定时间间隔连续触发操作?敌人在 pygame 中发射恒定光束而不是子弹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图在 pygame 中制造一个敌人,在 pygame 中以直线发射子弹.我已经设法让敌人开枪,但它会持续发射子弹而不是将它们分开.有什么办法可以隔开子弹吗?

这是敌人的职业

class Boss(pygame.sprite.Sprite):def __init__(self):pygame.sprite.Sprite.__init__(self)self.image = pygame.Surface((100, 70))self.image.fill(白色)self.rect = self.image.get_rect()self.rect.centerx = 宽度/2self.rect.y = (WIDTH/2) - 500self.speedy = 3定义更新(自我):self.rect.y += self.speedy如果 self.rect.y >= 30:self.rect.y = 30def射击(自我):bossbullet = Bossbullet(self.rect.centerx, self.rect.bottom)all_sprites.add(bossbullet)bossbullets.add(bossbullet)类Bossbullet(pygame.sprite.Sprite):def __init__(self, x, y):pygame.sprite.Sprite.__init__(self)self.image = pygame.Surface((10, 20))self.image.fill(白色)self.rect = self.image.get_rect()self.rect.bottom = yself.rect.centerx = xself.speedy = -10定义更新(自我):self.rect.y -= self.speedy如果 self.rect.bottom 

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

running = True在跑步的时候:时钟滴答(FPS)如果 boss.rect.y >= 30:boss.shoot()
解决方案

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

milliseconds_delay = 500 # 0.5 秒bullet_event = pygame.USEREVENT + 1pygame.time.set_timer(bullet_event,毫秒延迟)

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

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

running = True在跑步的时候:时钟滴答(FPS)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:正在运行 = 错误elif event.type == bullet_event:如果 boss.rect.y >= 30:boss.shoot()

有什么办法可以让敌人暂停一段时间后..假设5次射击,然后暂停后再次开始射击

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

delay_time = 500 # 0.5 秒pause_time = 3000 # 3 秒子弹事件 = pygame.USEREVENT + 1pygame.time.set_timer(bullet_event, delay_time)no_shots = 0运行 = 真在跑步的时候:时钟滴答(FPS)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:正在运行 = 错误elif event.type == bullet_event:如果 boss.rect.y >= 30:boss.shoot()# 改变定时器事件时间如果 no_shots == 0:pygame.time.set_timer(bullet_event, delay_time)no_shots += 1如果 no_shots == 5:pygame.time.set_timer(bullet_event, pause_time)no_shots = 0被杀 = # [...] 被杀时设置状态# 停止计时器如果被杀:pygame.time.set_timer(bullet_event, 0)

Trying to make an enemy in pygame that shoots bullets in a straight line in pygame. I've managed to make the enemy shoot, but it shoots a constant beam of bullets instead of spacing them out. Is there any way to space out the bullets?

This is the class for the enemy

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()

this is the loop in which the game runs and the enemy shoots

running = True
while running:

    clock.tick(FPS)
    if boss.rect.y >= 30:
        boss.shoot()
解决方案

I recommend to use a timer event. Use pygame.time.set_timer() to repeatedly create an USEREVENT. e.g.:

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

Note, in pygame customer events can be defined. Each event needs a unique id. The ids for the user events have to start at pygame.USEREVENT. In this case pygame.USEREVENT+1 is the event id for the timer event, which spawns the bullets.

Create a new bullet when the event occurs in the event loop:

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()


The timer event can be stopped by passing 0 to the time parameter. e.g.:

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)

这篇关于如何以特定时间间隔连续触发操作?敌人在 pygame 中发射恒定光束而不是子弹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:54