Overview I am making a game with a spaceship that can move in all 4 directions. In the game while loop my spaceship can be controlled by the keyboard as seen below. For the sake of simplicity I have only shown some part of my code, and only the part where I move my spaceship to the right.
Method As seen, I use a 'for loop' and the pygame module to receive the input from the keyboard. And I am using the flag method to enable continuous moving. I start by setting all the flags to False. Then depending on the keyboard press it moves in the different direction and set to False again when the key goes up. Its all working fine... NB: the variable surf_centerx is the center of my spaceship and the screen.blit(surf, surf_rect), pygame.display.flip() at the end is just displaying the spaceship on the screen and updating the latest changes in position of the spaceship, respectively.
import pygame
# pygame initializing
pygame.init()
#create the screen surface
screen = pygame.display.set_mode((400, 400))
#..snip...
speedfactor = 0.1
move_right = False
#..snip...
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
move_right = True
#..snip...
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
move_right = False
#..snip...
if move_right:
surf_centerx += speedfactor
#..snip...
screen.blit(surf, surf_rect)
pygame.display.flip()
Code refactoring - I thought But because my while loop keeps growing I want to do some code refactoring, I thought :o) So I would put my keyboard stuff in a separate module, with a function I would then call in my while loop. I save the value from k_c.check_key_events() in the variable, 'returned' and below that the value is unpacked by the line 'move_right = returned'. My while loop now look like this (a lot shorter):
import keyboard_control as k_c
while True:
returned = k_c.check_key_events()
move_right = returned
if move_right:
surf_centerx += speedfactor
#..snip...
screen.blit(surf, surf_rect)
pygame.display.flip()
And the side module controlling the keyboard (keyboard_control) is:
import pygame
#necessary pygame initializing
pygame.init()
def check_key_events():
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
move_right = True
#..snip...
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
move_right = False
#..snip...
return move_right
I now call the keyboard_control module within the 'while loop' in the main module and the keyboard_control module should now return the value of the 'move_right' variable. Putting the for loop etc. in its own module is effectively simplifying the main module.
Problem But running my program now gives the error: *return move_right UnboundLocalError: local variable 'move_right' referenced before assignment *
The problem is that the function does not know the variable 'move_right', because it is not defined within the function 'check_key_events()'. And if I defines it at the beginning of the function, something like this:
#..snip...
def check_key_events():
move_right = False
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
move_right = True
#..snip...
Then my problem is that I loose the continuous moving, because in every cycle of the while loop the line 'returned = k_c.check_key_events()' calls the function 'check_key_events()', which sets the variable move_right to False even though I haven't released the 'move right' key. So my question is how do I cleverly come around this problem?? I just can't figure that out.