使用 Pygame 计时器的冷却时间

Cooldowns Using Pygame Timer

我是编码新手,正在尝试弄清楚如何正确使用 Pygame 计时器进行冷却。目前我希望精灵(玩家)(当按下或按住空格键时)在超短时间内提升。我希望他们可以使用的每个提升之间有一个小的冷却时间,这样它就不会被垃圾邮件或持有。到目前为止,它只在开始时工作一次,然后使用 else 语句。我没有看到我的代码中显然存在的问题。

boost_cooldown = pygame.USEREVENT +1

self.attacking = False
        self.cooldown = False

    def update(self):
        self.acc = vec(0,0)

        pressed_keys = pygame.key.get_pressed()
        if pressed_keys[K_a]:
            self.acc.x += -ACC
        if pressed_keys[K_d]:
            self.acc.x += ACC
        if pressed_keys[K_w]:
            self.acc.y += -ACC
        if pressed_keys[K_s]:
            self.acc.y += ACC

        if pressed_keys[K_SPACE]:
            if pressed_keys[K_a]:
                if self.cooldown == False:
                    self.acc.x += -ACC2
                    self.cooldown = True
                    pygame.time.set_timer(boost_cooldown, 1000)
                else:
                    print("error (cooldown)")
            if pressed_keys[K_d]:
                if self.cooldown == False:
                    self.acc.x += ACC2
                    self.cooldown = True
                    pygame.time.set_timer(boost_cooldown, 1000)
                else:
                    print("error (cooldown)")
            if pressed_keys[K_w]:
                if self.cooldown == False:
                    self.acc.y += -ACC2
                    self.cooldown = True
                    pygame.time.set_timer(boost_cooldown, 1000)
                else:
                    print("error (cooldown)")
            if pressed_keys[K_s]:
                if self.cooldown == False:
                    self.acc.y += ACC2
                    self.cooldown = True
                    pygame.time.set_timer(boost_cooldown, 1000)
                else:
                    print("error (cooldown)")

        for event in pygame.event.get():
            if event.type == boost_cooldown:
                self.cooldown = False
                pygame.time.set_timer(boost_cooldown, 0)
    

多亏了 Random Davis 我解决了这个问题,我花了一些时间来学习如何开始循环,但如果有人需要,我发布了固定代码。

ACC = 0.8
ACC2 = 20
start_time = 0
 

class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__() 
        self.surf = pygame.Surface((30, 30))
        self.surf.fill((128,255,40))
        self.rect = self.surf.get_rect()
   
        self.pos = vec((10, 385))
        self.vel = vec(0,0)
        self.acc = vec(0,0)


    def update(self):
        self.acc = vec(0,0)
        global start_time

        pressed_keys = pygame.key.get_pressed()
        if pressed_keys[K_a]:
            self.acc.x += -ACC
        if pressed_keys[K_d]:
            self.acc.x += ACC
        if pressed_keys[K_w]:
            self.acc.y += -ACC
        if pressed_keys[K_s]:
            self.acc.y += ACC

        if pressed_keys[K_SPACE]:
            if pressed_keys[K_a]:
                if (pygame.time.get_ticks() - start_time) >= 1000:
                    start_time = pygame.time.get_ticks()
                    self.acc.x += -ACC2
                else:
                    print("error")
            if pressed_keys[K_d]:
                if (pygame.time.get_ticks() - start_time) >= 1000:
                    start_time = pygame.time.get_ticks()
                    self.acc.x += ACC2
                else:
                    print("error")
            if pressed_keys[K_w]:
                if (pygame.time.get_ticks() - start_time) >= 1000:
                    start_time = pygame.time.get_ticks()
                    self.acc.y += -ACC2
                else:
                    print("error")
            if pressed_keys[K_s]:
                if (pygame.time.get_ticks() - start_time) >= 1000:
                    start_time = pygame.time.get_ticks()
                    self.acc.y += ACC2
                else:
                    print("error")

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
     
    displaysurface.fill((0,0,0))
 
    P1.update()
    for entity in all_sprites:
        displaysurface.blit(entity.surf, entity.rect)
    
    pygame.display.update()
    Clock.tick(FPS)