I am currently working on a game where I wish to give the player an option of four characters to play. Here is my current code to do this:
running = 1
charactersChoice = ['char.png', 'char2.png', 'char3.png', 'char4.png']
choice = ''
while choice == '':
screen.fill((47, 79, 79))
screen.blit(pygame.image.load(charactersChoice[0]), (100,100))
screen.blit(pygame.image.load(charactersChoice[1]), (700,100))
screen.blit(pygame.image.load(charactersChoice[2]), (100,600))
screen.blit(pygame.image.load(charactersChoice[3]), (700,600))
keys = pygame.key.get_pressed()
#Choose character
if keys[pygame.K_1]:
choice = charactersChoice[0]
if keys[pygame.K_2]:
choice = charactersChoice[1]
if keys[pygame.K_3]:
choice = charactersChoice[2]
if keys[pygame.K_4]:
choice = charactersChoice[3]
pygame.display.flip()
while running == 1:
#rest of code for game here
As you can see, I blit the four different character profiles onto the screen and then check if the player has pressed the keys 1-4 to select their option. After selecting their option, it should move onto the main loop. Currently, if I press the key '1' while the code is running, it will not change the variable choice to what is intended.
Hope I have explained my problem well enough.
keys. - Micheal O'Dwyerpygame.event.pump()before callingget_pressed()? - ShadowMitia