如何以特定的时间间隔连续触发动作?敌人在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(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()


波斯汪
浏览 130回答 1
1回答

当年话下

我建议使用计时器事件。用于pygame.time.set_timer()重复创建USEREVENT. 例如:milliseconds_delay = 500 # 0.5 secondsbullet_event = pygame.USEREVENT + 1pygame.time.set_timer(bullet_event, milliseconds_delay)注意,在 pygame 中可以定义客户事件。每个事件都需要一个唯一的 ID。用户事件的 ID 必须从 开始pygame.USEREVENT。在本例pygame.USEREVENT+1中是生成子弹的计时器事件的事件 ID。在事件循环中发生事件时创建一个新项目符号:running = Truewhile running:&nbsp; &nbsp; clock.tick(FPS)&nbsp; &nbsp; for event in pygame.event.get():&nbsp; &nbsp; &nbsp; &nbsp; if event.type == pygame.QUIT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; running = False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;elif event.type == bullet_event:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if boss.rect.y >= 30:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;boss.shoot()有什么办法可以让敌人在之后暂停一段时间......比如说5次射击,然后在暂停后再次开始射击可以通过将 0 传递给 time 参数来停止计时器事件。例如:delay_time = 500&nbsp; # 0.5 secondspause_time = 3000 # 3 secondsbullet_event = pygame.USEREVENT + 1pygame.time.set_timer(bullet_event, delay_time)no_shots = 0running = Truewhile running:&nbsp; &nbsp; clock.tick(FPS)&nbsp; &nbsp; for event in pygame.event.get():&nbsp; &nbsp; &nbsp; &nbsp; if event.type == pygame.QUIT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; running = False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;elif event.type == bullet_event:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if boss.rect.y >= 30:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;boss.shoot()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# change the timer event time&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if no_shots == 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pygame.time.set_timer(bullet_event, delay_time)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;no_shots += 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if no_shots == 5:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pygame.time.set_timer(bullet_event, pause_time)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;no_shots = 0&nbsp; &nbsp; killed = # [...] set state when killed&nbsp; &nbsp; # stop timer&nbsp; &nbsp; if killed:&nbsp; &nbsp; &nbsp; &nbsp; pygame.time.set_timer(bullet_event, 0)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python