I want to get keyboard input of 5 numbers and add them to a list and print the list. my code just prints an empty list without letting the user type in anything. how do i append the items into the list given the keyboard input?
def get_pygame_events():
pygame_events = pygame.event.get()
return pygame_events
counter_list = []
key = get_pygame_events()
for num in range(0,5):
for event in key:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
counter_list.append(1)
if event.key == pygame.K_2:
counter_list.append(2)
if event.key == pygame.K_3:
counter_list.append(3)
if event.key == pygame.K_4:
counter_list.append(4)
if event.key == pygame.K_5:
counter_list.append(5)
if event.key == pygame.K_6:
counter_list.append(6)
if event.key == pygame.K_7:
counter_list.append(7)
if event.key == pygame.K_8:
counter_list.append(8)
if event.key == pygame.K_9:
counter_list.append(9)
if event.key == pygame.K_0:
counter_list.append(0)
print(counter_list)
CodePudding user response:
event.get()
doesn't wait for user keypresses (it is not input()
) and code run much, much faster then user can press even single key - and this is why you may get empty list. You should run event.get()
inside for
-loop.
Because sometimes user may press wrong key so better use while len(counter_list) < 5:
instead of for num in range(5):
I didn't test this code:
counter_list = []
while len(counter_list) < 5:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
counter_list.append(1)
if event.key == pygame.K_2:
counter_list.append(2)
if event.key == pygame.K_3:
counter_list.append(3)
if event.key == pygame.K_4:
counter_list.append(4)
if event.key == pygame.K_5:
counter_list.append(5)
if event.key == pygame.K_6:
counter_list.append(6)
if event.key == pygame.K_7:
counter_list.append(7)
if event.key == pygame.K_8:
counter_list.append(8)
if event.key == pygame.K_9:
counter_list.append(9)
if event.key == pygame.K_0:
counter_list.append(0)
print(counter_list)
This code my have other problem - if you will type keys with big delay then it will block rest of code for long time and it will not update other elements screen - so it will looks like it freeze.
It may need different organization
# -- at the beginning of program
counter_list = []
# --- main loop ---
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
counter_list.append(1)
if event.key == pygame.K_2:
counter_list.append(2)
if event.key == pygame.K_3:
counter_list.append(3)
if event.key == pygame.K_4:
counter_list.append(4)
if event.key == pygame.K_5:
counter_list.append(5)
if event.key == pygame.K_6:
counter_list.append(6)
if event.key == pygame.K_7:
counter_list.append(7)
if event.key == pygame.K_8:
counter_list.append(8)
if event.key == pygame.K_9:
counter_list.append(9)
if event.key == pygame.K_0:
counter_list.append(0)
if len(counter_list) == 5:
print(counter_list)
# ... update objects ...
# ... draw objecte ...
# ... flip surfaces ...
CodePudding user response:
I just installed pygame and printed pygame.K_1
it turns out to be 49
which happens to be the ASCII for 1
.
This means that you can actually do this in a very short manner as such:
if event.type == pygame.KEYDOWN and event.key in range(48,58):
counter_list.append(int(chr(event.key)))
or you can write a list comprehension as such:
counter_list = [int(chr(event.key)) for event in get_pygame_events() if event.type == pygame.KEYDOWN and event.key in range(48,58)]
This does all the work you are trying to do. As @furas said, the game goes much faster than your inputs, if you want to print all the inputs you can do something like this:
def get_pygame_events():
pygame_events = pygame.event.get()
return pygame_events
counter_list = []
while len(counter_list)<5:
counter_list = [int(chr(event.key)) for event in get_pygame_events() if event.type == pygame.KEYDOWN and event.key in range(48,58)]
print(counter_list)
This does everything and it's fairly short, I would add a simple if counter_list before the print statement to avoid the annoying printing of empty counter_list or similar counter_list and remove the pygame_events assignment, that isn't useful at all:
def get_pygame_events():
return pygame.event.get()
old_counter_list = []
counter_list = []
SIZE = 5
while len(counter_list)<SIZE:
counter_list = [int(chr(event.key)) for event in get_pygame_events() if event.type == pygame.KEYDOWN and event.key in range(48,58)]
if old_counter_list != counter_list and len(counter_list)!=SIZE:
# This avoids printing your list infinite times if it isn't the size we want our list to be
print(counter_list)
old_counter_list = counter_list[:]
If you want infinite lists of 5 numbers, you can write something like this:
def get_pygame_events():
return pygame.event.get()
old_counter_list = []
counter_list = []
SIZE = 5
while True:
counter_list = [int(chr(event.key)) for event in get_pygame_events() if event.type == pygame.KEYDOWN and event.key in range(48,58)]
if old_counter_list != counter_list:
# This avoids printing your list infinite times if it isn't the size we want our list to be
old_counter_list = counter_list[:]
if len(counter_list)==SIZE:
# This prints the list of 5 elements and resets the list
print(counter_list)
counter_list = []