I am able to press the Enter button in Python, using Pygame. Now, it prints "once" into the console everytime I press the button. How can i detect if the button is pressed more than once and print "more than once"?

  press = False
  if event.key == pygame.K_RETURN:
      press = True

      print("once")
  if press == True:
      print("more than once")

1 Answers

2
sloth On Best Solutions

You're nearly there. Just use an if/else block and set press to True after printing:

import pygame
pygame.init()
screen = pygame.display.set_mode((200, 200))
run = True
press = False

while run:
  for e in pygame.event.get():
    if e.type == pygame.QUIT: 
        run = False
    if e.type == pygame.KEYDOWN:
        if e.key == pygame.K_RETURN:
            if not press:
                print('once')
            else:
                print('more than once')
            press = True

  screen.fill((30, 30, 30))
  pygame.display.flip()