Handling Keyboards Inputs

Basic steps to handle keyboard input:

  • Import required libraries.
  • Create a display surface object using display.set_mode() method of pygame.
  • Load the image/object.
  • Create a click event i.e., KEYDOWN
  • Define all the events keys and perform task.
  • Create a pause event i.e., KEYUP
  • Copying the Text surface object to the display surface object using blit() method of pygame display surface object.
  • Show the display surface object on the pygame window using the display.update() method of pygame.

Example:

Python3




# importing all the required libraries
import pygame
from pygame.locals import *
from sys import exit
 
# initiating pygame library to use it's
# functions
pygame.init()
 
# declaring windows/surface width and height
size = width, height = 740, 480
screen = pygame.display.set_mode(size)
 
# loads a new image from a file and convert()
# will create a copy of image on surface
img = pygame.image.load("char.png").convert()
 
# declaring value to variables
x, y = 0, 0
move_x, move_y = 0, 0
 
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            # pygame.QUIT deactivates pygame
            exit()
            # exit() is sys function used to
            # kill the program
             
     # KEYDOWN event will be triggered everytime
    # we press a button
    if event.type == KEYDOWN: 
       
        if event.key == K_LEFT: 
            move_x = -0.3  # object moves -0.3 towards x axis
            print("pressed LEFT")
        elif event.key == K_RIGHT: 
            move_x = +0.3  # object moves 0.3 towards x axis
            print("pressed RIGHT")
        elif event.key == K_UP:
            move_y = -0.3  # object moves -0.3 towards y axis
            print("pressed UP")
        elif event.key == K_DOWN:
            move_y = +0.3  # object moves 0.3 towards y axis
            print("pressed DOWN")
             
        # K_LCTRL event will be triggered everytime we
        # press left CTRL button
        elif event.key == K_LCTRL:
           
            # declaring new image file to update image
            # everytime left CTRL is pressed
            img = pygame.image.load("char1.png")
            pygame.display.update()  # update image
        elif event.key == K_BACKSPACE:
           
            # this the default file we declared in start
            # and it will restore it everytime we press
            # backspace
            img = pygame.image.load("char.png")
            pygame.display.update()  # update image
             
     # it will get triggered when left key is released
    if event.type == KEYUP:
        if event.key == K_LEFT:
            move_x = 0  # movement stops
        elif event.key == K_RIGHT: 
            move_x = 0  # movement stops
        elif event.key == K_UP:
            move_y = 0  # movement stops
        elif event.key == K_DOWN: 
            move_y = 0  # movement stops
            """KEYUP event will be triggered when the release the keys
            and x,y coordinates will not change anymore"""
 
    x += move_x
    y += move_y
    # updating coordinate values of x,y
    screen.fill((255, 255, 255))
     
    # the function will fill the background with white color
    screen.blit(img, (x, y))
     
    # blit() function will copy image file to x,y coordinates.
    pygame.display.update()
    # draw the objects on screen


Output:

Pygame – Input Handling

Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language.

The sys module in Python provides various functions and variables that are used to manipulate different parts of the Python runtime environment. It allows operating on the interpreter as it provides access to the variables and functions that interact strongly with the interpreter.

Similar Reads

Handling Keyboards Inputs

Basic steps to handle keyboard input:...

Handling Mouse Inputs:

...

Contact Us