首页 > 解决方案 > 闪烁 2 个圆圈

问题描述

我必须交替使用 pygame 闪烁(打开和关闭)2 个圆圈。如何使用 pygame 让它闪烁。

for event in pygame.event.get():
    blueball = pygame.draw.circle(screen, b, (175,100),20,3)
    redball = pygame.draw.circle(screen, r, (675,350),20,3)
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_UP:
            bluebally += 5
            redballx +=5
        if event.key == pygame.K_DOWN:
            bluebally += 5
            redballx +=5
        if event.key == pygame.K_RIGHT:
            blueballx += 5
            redbally +=5
        if event.key == pygame.K_RIGHT:
            blueballx += 5
            redbally +=5
    if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()
    screen.blit(blueball,(blueballx,bluebally))
    screen.blit(redball,(redballx,redbally))

我希望蓝球和红球交替闪烁

标签: pythonpython-3.xpygame

解决方案


我不知道您的完整代码,但如果这对您有帮助,请告诉我:

绘图功能之前的某处

color_cycle_index = 0

然后在你的绘图功能中

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_UP:
        bluebally += 5
        redballx +=5
    if event.key == pygame.K_DOWN:
        bluebally += 5
        redballx +=5
    if event.key == pygame.K_RIGHT:
        blueballx += 5
        redbally +=5
    if event.key == pygame.K_RIGHT:
        blueballx += 5
        redbally +=5
if event.type == pygame.QUIT:
    pygame.quit()
    sys.exit()

if color_cycle_index % 2 == 0:
    # draw red
    pygame.draw.circle(screen, r, (redballx, redbally), 20, 3)
else:
    # draw blue
    pygame.draw.circle(screen, b, (blueballx, bluebally), 20, 3)

color_cycle_index += 1  # <-- it's better to increase this every n seconds instead of every frame

推荐阅读