【问题标题】:How to make a circular countdown timer in Pygame?如何在 Pygame 中制作循环倒数计时器?
【发布时间】:2021-04-19 20:17:55
【问题描述】:

如何在 Pygame 中进行这种倒计时? (我正在寻找如何使圆圈的周长减小,这就是问题所在,因为显示时间并不难) 请记住,圆的周长和显示的时间应该成比例。

【问题讨论】:

    标签: python pygame countdown


    【解决方案1】:

    只需使用 pygame.draw.arc 并根据计数器指定 stop_angle 参数:

    percentage = counter/100
    end_angle = 2 * math.pi * percentage
    pygame.draw.arc(window, (255, 0, 0), arc_rect, 0, end_angle, 10)
    

    小例子:

    import pygame
    import math
    
    pygame.init()
    window = pygame.display.set_mode((200, 200))
    clock = pygame.time.Clock()
    font = pygame.font.SysFont(None, 100)
    counter = 100
    text = font.render(str(counter), True, (0, 128, 0))
    
    timer_event = pygame.USEREVENT+1
    pygame.time.set_timer(timer_event, 1000)
    
    def drawArc(surf, color, center, radius, width, end_angle):
        arc_rect = pygame.Rect(0, 0, radius*2, radius*2)
        arc_rect.center = center
        pygame.draw.arc(surf, color, arc_rect, 0, end_angle, width)
    
    run = True
    while run:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            elif event.type == timer_event:
                counter -= 1
                text = font.render(str(counter), True, (0, 128, 0))
                if counter == 0:
                    pygame.time.set_timer(timer_event, 0)                
    
        window.fill((255, 255, 255))
        text_rect = text.get_rect(center = window.get_rect().center)
        window.blit(text, text_rect)
        drawArc(window, (255, 0, 0), (100, 100), 90, 10, 2*math.pi*counter/100)
        pygame.display.flip()
    
    pygame.quit()
    exit()
    

    遗憾的是,width > 1 的 pygame.draw.arc 的质量很差。然而,这可以改进,使用cv2cv2.ellipse

    import pygame
    import cv2
    import numpy
    
    pygame.init()
    window = pygame.display.set_mode((200, 200))
    clock = pygame.time.Clock()
    font = pygame.font.SysFont(None, 100)
    counter = 100
    text = font.render(str(counter), True, (0, 128, 0))
    
    timer_event = pygame.USEREVENT+1
    pygame.time.set_timer(timer_event, 1000)
    
    def drawArcCv2(surf, color, center, radius, width, end_angle):
        circle_image = numpy.zeros((radius*2+4, radius*2+4, 4), dtype = numpy.uint8)
        circle_image = cv2.ellipse(circle_image, (radius+2, radius+2),
            (radius-width//2, radius-width//2), 0, 0, end_angle, (*color, 255), width, lineType=cv2.LINE_AA) 
        circle_surface = pygame.image.frombuffer(circle_image.flatten(), (radius*2+4, radius*2+4), 'RGBA')
        surf.blit(circle_surface, circle_surface.get_rect(center = center))
    
    run = True
    while run:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            elif event.type == timer_event:
                counter -= 1
                text = font.render(str(counter), True, (0, 128, 0))
                if counter == 0:
                    pygame.time.set_timer(timer_event, 0)                
    
        window.fill((255, 255, 255))
        text_rect = text.get_rect(center = window.get_rect().center)
        window.blit(text, text_rect)
        drawArcCv2(window, (255, 0, 0), (100, 100), 90, 10, 360*counter/100)
        pygame.display.flip()
    
    pygame.quit()
    exit()
    

    【讨论】:

    • 嗯,谢谢!我不知道draw arc方法
    • @SalemJebnoun 基本上,您的问题是如何绘制弧线。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-31
    • 2015-08-23
    • 2013-08-03
    • 2021-10-14
    • 1970-01-01
    相关资源
    最近更新 更多