Keyboard input

From TRCCompSci - AQA Computer Science
Revision as of 09:38, 22 February 2018 by Admin (talk | contribs) (Created page with "Within the game loop of your code, we can firstly get the status of the keyboard and store it in a variable called user_input. We can then check this for certain keys: <synta...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Within the game loop of your code, we can firstly get the status of the keyboard and store it in a variable called user_input. We can then check this for certain keys:

user_input = pygame.key.get_pressed()
    
if(user_input[pygame.K_UP]):
    Y=Y-1
elif(user_input[pygame.K_DOWN]):
    Y=Y+1
elif(user_input[pygame.K_LEFT]):
    X=X-1
elif(user_input[pygame.K_RIGHT]):
    X=X+1

If these variables are used to draw something to the screen, then the object will move around the screen.

The alternative way of doing this is within the pygame.events