0

I have to blink(on and off) 2 circles alternatively using pygame. How to make it blink using 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))

I expect blue ball and redball blink alternatively

help-ukraine-now
  • 3,850
  • 4
  • 19
  • 36
  • What part of this code should make it blink? `blit` means `draw`, not `blink`. You can use some index (`color_cycle_index`) and increase it every second (`color_cycle_index += 1`) and blit/draw blue if even (`color_cycle_index % 2 == 0`) and draw red otherwise. – Laurens Koppenol Aug 12 '19 at 09:16
  • Thank you, I am new to programming and pygame. I just asked the question with whatever I tried using the tutorials. However, i didnt get how to blink a circle. My output should be red and blue ball blinking alternatively. – kalingabhat Aug 12 '19 at 09:27
  • With blinking you mean 1 circle which alternates between red / blue or 2 circles which both blink but not simultanuously? – Laurens Koppenol Aug 12 '19 at 09:40

1 Answers1

0

I don't know your full code, but let me know if this helps you:

Somewhere before the drawing function

color_cycle_index = 0

Then in your drawing function

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
Laurens Koppenol
  • 2,946
  • 2
  • 20
  • 33