微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PyGame 碰撞冷却

如何解决PyGame 碰撞冷却

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

这是对话泡泡的代码

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,border_rect)
    pygame.draw.rect(screen,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),SpeechBubbleAnchor.midtop,25)

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

解决方法

添加一个变量来存储文本 (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),SpeechBubbleAnchor.midtop,25)
         if current_time > bubble_text_end_time:
             current_bubble_text = None 
    # [...]

另见Adding a particle effect to my clicker game

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。