Pygame - Input Handling Last Updated : 03 Mar, 2022 Comments Improve Suggest changes Like Article Like Report 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. 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., KEYDOWNDefine all the events keys and perform task.Create a pause event i.e., KEYUPCopying 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: Handling Mouse Inputs: Basic steps to handle mouse 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., MOUSEBUTTONDOWN.Define all the events keys and perform task.Create a pause event i.e., MOUSEBUTTONUP.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 clicking = False right_clicking = False middle_click = False while True: mx, my = pygame.mouse.get_pos() # gets mouse x,y coordinates location = [mx, my] 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 # MOUSEBUTTONDOWN event is triggered when a button is pressed if event.type == MOUSEBUTTONDOWN: # returns true when mouse left button is clicked if event.button == 1: clicking = True # declaring new image file to update image # everytime left button clicking is true img = pygame.image.load("char1.png") pygame.display.update() # update image # returns true when mouse right button is clicked if event.button == 3: right_clicking = True # declaring new image file to update image # everytime right button is clicked img = pygame.image.load("char.png") pygame.display.update() # update image # returns true when mouse middle button is clicked if event.button == 2: middle_click = middle_click # rescale image when middle button clicking is true img = pygame.transform.scale(img, (100, 100)) pygame.display.update() # update image # MOUSEBUTTONUP is triggered when mouse button # is released(not clicked) if event.type == MOUSEBUTTONUP: if event.button == 1: clicking = False screen.fill((255, 255, 255)) # the function will fill the background # with white color screen.blit(img, (location[0], location[1])) # blit() function will copy image file # to x,y coordinates. pygame.display.update() # draw the objects on screen Output: Comment More infoAdvertise with us Next Article Pygame - Input Handling vinamrayadav Follow Improve Article Tags : Python Geeks Premier League Geeks-Premier-League-2022 Python-PyGame Practice Tags : python Similar Reads Install Pygame in Linux In this article, we will discuss how to install PyGame in the Linux system. We are using Ubuntu 20 LTS you can use any other one. To successfully install PyGame in your Linux system, follow the below procedure: First Check you are using python's latest version or not. Because the PyGame is only supp 2 min read Python Arcade - Handling Mouse Inputs In this article, we will learn how we can handle mouse inputs in the arcade module in Python. In Arcade, you can easily handle the mouse inputs using these functions: on_mouse_motion(): Syntax: on_mouse_motion(x, y, dx, dy) Parameters: x : x coordinatey : y coordinatedx : change in x coordinatedy : 3 min read Python Arcade - Handling Keyboard Input In this article, we will discuss how to handle keyboard inputs in Python arcade module. In Arcade, you can easily check which keyboard button is pressed and perform tasks according to that. For this, we are going to use these functions: on_key_press()on_key_release() Syntax: on_key_press(symbol,mod 4 min read How to get keyboard input in PyGame ? While using pygame module of Python, we sometimes need to use the keyboard input for various operations such as moving a character in a certain direction. To achieve this, we have to see all the events happening. Pygame keeps track of events that occur, which we can see with the events.get() functio 3 min read Python input() Function Python input() function is used to take user input. By default, it returns the user input in form of a string.input() Function Syntax: input(prompt)prompt [optional]: any string value to display as input messageEx: input("What is your name? ")Returns: Return a string value as input by the user.By de 4 min read Like