重置电源计时器

huangapple go评论83阅读模式
英文:

A resetting powerup timer

问题

我正在学习如何使用pygame,来自YouTube的教程,现在我已经完成了视频,通过添加新的敌人、改变速度和背景对象,将游戏推向了更远。我还想通过添加一个功能来进一步发展游戏,允许你进行一次“超级跳跃”来避开大量的敌人,但我希望它只能在每5秒钟内使用一次,当你使用它时,5秒的计时器会重置。

我将链接代码如下:

if event.type == pygame.KEYDOWN: #如果按下任意键
    if event.key == pygame.K_w: #检查特定的键
        if player_rect.bottom > 299:
            player_grav = -22.5
    if event.key == pygame.K_e: #检查特定的键
        if player_rect.bottom > 299:
            player_grav -= 30 #这里是跳跃应该发生的地方,但我不知道该怎么做
英文:

Im learning how to use pygame from a youtube and now im done with the video ive taken the game further by adding new enemies altering speeds and background objects .i'd also like to take the game further by adding a feature that allows you to do a 'mega jump' to avoid a large amount of enemies but i want to make it so it can only be used once every 5 seconds and when you use it the 5 second timer resets.

i will link the code below

if event.type == pygame.KEYDOWN: #if any key pressed
    if event.key == pygame.K_w: #checking for specific key
        if player_rect.bottom > 299:
            player_grav = -22.5
    if event.key == pygame.K_e: #checking for specific key
        if player_rect.bottom > 299:
            player_grav -= 30 #here is where the jump should be but i have no idea what to do

答案1

得分: 0

使用pygame.time.get_ticks()来测量时间,单位为毫秒。设置可以执行超级跳跃的时间,并检查当前时间是否大于此时间:

next_mega_jump = 5000  # 5000毫秒 == 5秒
run = True
while run:
    current_time = pygame.time.get_ticks()

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

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                if player_rect.bottom > 299:
                    player_grav = -22.5
                    print("跳跃")

            if event.key == pygame.K_e:
                if player_rect.bottom > 299 and current_time > next_mega_jump:
                    next_mega_jump = current_time + 5000
                    player_grav = -30
                    print("超级跳跃")
英文:

Use pygame.time.get_ticks() to measure the time in milliseconds. Set the time when the mega jump may be performed and check if the current time is greater than this time:

next_mega_jump = 5000 # 5000 milliseconds == 5 seconds
run = True
while run:
    current_time = pygame.time.get_ticks()

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

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                if player_rect.bottom > 299:
                    player_grav = -22.5
                    print("jump")

            if event.key == pygame.K_e:
                if player_rect.bottom > 299 and current_time > next_mega_jump:
                    next_mega_jump = current_time + 5000
                    player_grav = -30 
                    print("mega jump")

huangapple
  • 本文由 发表于 2023年1月9日 00:28:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定