【问题标题】:PyGame Cooldown in collisionPyGame 碰撞冷却
【发布时间】:2021-03-28 14:49:27
【问题描述】:

我有一个代码,当玩家击中一个矩形时会弹出一个语音气泡。但现在它只是在给讲话泡泡发垃圾邮件。这不好,因为我有随机出现的文本,所以它只是通过它非常快

这是对话泡泡的代码

def draw_speech_bubble(screen, text, text_color, bg_color, pos, size):

    font = pygame.font.SysFont(None, size)
    text_surface = font.render(text, True, text_color)
    text_rect = text_surface.get_rect(midbottom=pos)

    #Background Object
    bg_rect = text_rect.copy()
    bg_rect.inflate_ip(10,10)

    #Border
    border_rect = bg_rect.copy()
    border_rect.inflate_ip(4,4)
    
    pygame.draw.rect(screen, text_color, border_rect)
    pygame.draw.rect(screen, bg_color, bg_rect)
    screen.blit(text_surface, text_rect)

这是我的碰撞代码

if(player.player_rect.colliderect(BarCounter)):
    draw_speech_bubble(screen, str(RandomText()), (255, 255, 255), (0, 0,0),SpeechBubbleAnchor.midtop,25)

我想要某种冷却时间,这样它就不会向语音气泡发送垃圾邮件。我尝试使用刻度进行计算,但我无法做到这一点

【问题讨论】:

    标签: python python-3.x pygame


    【解决方案1】:

    添加一个存储文本的变量 (current_bubble_text) 并在玩家点击矩形时更改文本。使用pygame.time.get_ticks() 获取自调用pygame.init() 以来的毫秒数。计算文本必须再次消失的时间点。时间到了,重置变量:

    current_bubble_text = None
    bubble_text_end_time = 0
    
    #application loop
    while True:
        current_time = pygame.time.get_ticks()
        # [...]
    
        if current_bubble_text == None and player.player_rect.colliderect(BarCounter):
            current_bubble_text = RandomText()
            bubble_text_end_time = current_time + 5000 # 1 second time interval
    
        if current_bubble_text:
             draw_speech_bubble(screen, current_bubble_text,
                 (255, 255, 255), (0, 0, 0), SpeechBubbleAnchor.midtop, 25)
             if current_time > bubble_text_end_time:
                 current_bubble_text = None 
        # [...]
    

    另见Adding a particle effect to my clicker game

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-22
      • 2020-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多