Movement of Object When Arrow Keys are Pressed in pygame
In this module, we are going to discuss the movement of the object when the keys are pressed. It is one of the most common tasks when we design games making the spirit character move.
Accept Input Key From Keyboard Using pygame.key
Here, taking input from the keyboard refers to performing an event which is implemented using the following keys :
- K_UP: It is used to perform an action when the up arrow is pressed on the keyboard.
- K_DOWN: It is used to perform an action when the down arrow is pressed on the keyboard.
- K_LEFT: It is used to perform an action when the left arrow is pressed on the keyboard.
- K_RIGHT: It is used to perform an action when the right arrow is pressed on the keyboard.
- K_SPACE: It is used to perform an action when the space button is pressed on the keyboard.
There are different type of keys similar to them in pygame which are handled using pygame as follows
key_object = pygame.key.get_pressed()
The above syntax will read the input from the keyboard.
The following code gives a description of how to move an object when the key is pressed.
import pygame import sys pygame.init() fps=30 fpsclock=pygame.time.Clock() sur_obj=pygame.display.set_mode((400,300)) pygame.display.set_caption("Keyboard_Input") White=(255,255,255) p1=10 p2=10 step=5 while True: sur_obj.fill(White) pygame.draw.rect(sur_obj, (255,0,0), (p1, p2, 70, 65)) for eve in pygame.event.get(): if eve.type==pygame.QUIT: pygame.quit() sys.exit() key_input = pygame.key.get_pressed() if key_input[pygame.K_LEFT]: p1 -= step if key_input[pygame.K_UP]: p2 -= step if key_input[pygame.K_RIGHT]: p1 += step if key_input[pygame.K_DOWN]: p2 += step pygame.display.update() fpsclock.tick(fps)
Output:
The above code gives the output as a window with a red rectangle when a left key is pressed it moves towards left and when the right key is pressed it moves towards the right and when the up key is pressed it moves towards up.
if key_input[pygame.K_LEFT]: p1 -= step
Here, when we press a left key once it should move left as we consider the pygame screen as pixel coordinates when we subtract 5 from x coordinate so the position of rectangle get changed from (10,10) to (5,10) similarly how many times we press left key those many items 5 is subtracted from x coordinate.
if key_input[pygame.K_UP]: p2 -= step
When the up key is pressed we are subtracting 5 from y coordinate so that the position gets changed from (10,10) to (10,5). Which set this as a new position.
if key_input[pygame.K_RIGHT]: p1 += step
When the right key is pressed we are adding 5 to x coordinate so that the position gets changed from (10,10) to (15,10).
if key_input[pygame.K_DOWN]: p2 += step
When the down key is pressed we are adding 5 to y coordinate so that the position gets changed from (10,10) to (10,15). So that rectangle moves to the obtained position.
Like this how many times we press keys those many times the x and y coordinates are changed.
Also read: Display Text in pygame Window using Python
Thanks You Very Much!!! It helped me a lot
Very useful. The book I had made no reference to ‘pygame.quit()’ in the event loop.
It wasn’t quitting.