In this day and age, touchscreen devices are everywhere. It’s no surprise that game developers are increasingly using touch input in their games, and frameworks are making this easier and easier.
PyGame makes it easy to create games and applications that can take advantage of touch inputs.
PyGame’s Touch Support
PyGame has built-in support for working with touch inputs. This includes support for mouse and finger touch inputs.
To detect a mouse, you can use the pygame.MOUSEBUTTONDOWN and pygame.MOUSEBUTTONUP events. For example:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
print("Mouse button pressed") To detect finger touch inputs, you can use the pygame.FINGERDOWN and pygame.FINGERUP events. For example:
for event in pygame.event.get():
if event.type == pygame.FINGERDOWN:
print("Finger touched the screen") Creating a Simple Game
You can find all the code in this GitHub Repo.
Start by creating a simple game. This game will consist of a player character that you can move around the screen using touch inputs. To do this, you will need to create a game loop and a player character.
Before you begin, make sure you have pip installed on your device, then use the following command to install the PyGame module:
pip install pygame Now, import the PyGame module in your game code:
import pygame
pygame.init() After that, create the game window and a game object:
# Set up the display
size = (400, 400)
screen = pygame.display.set_mode(size)
# Create a player object
player = pygame.Surface((50, 50))
player.fill((255, 0, 0))
# Set the initial position of the player
player_pos = [175, 175] Finally, create the game loop:
# The game loop
running = True
while running:
# Check for events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Update the player position
player_pos[0] += 5
player_pos[1] += 5
# Draw the player
screen.blit(player, player_pos)
# Update the display
pygame.display.update() Note that the above code only creates a simple game where the player character moves around the screen. To make the game more interesting, you can add physics and collisions to create obstacles for the player to overcome.
Mouse Touch Inputs for Player Movement
Now that you have a game with a player character, you can start adding touch inputs. To do this, you will need to add an event handler for the mouse inputs. Add the pygame.MOUSEBUTTONDOWN and pygame.MOUSEBUTTONUP events to the game loop.
Create an event handler for the mouse inputs. When a player presses the mouse button, update the character’s position to the current mouse position. The program will ignore the release of the mouse button, since it doesn't need to take any action in that case.
# The game loop
running = True
while running:
# Check for events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Check for mouse inputs
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = event.pos
player_pos[0] = mouse_x
player_pos[1] = mouse_y
elif event.type == pygame.MOUSEBUTTONUP:
pass
screen.fill((0, 0, 0))
# Draw the player
screen.blit(player, player_pos)
# Update the display
pygame.display.update() You can also add extra logic to the event handler to make the player move in response to the mouse input.
Finger Touch Inputs for Player Movement
In addition to mouse inputs, you can also add finger touch inputs. To do this, you will need to add an event handler for the finger-touch inputs.
Add the pygame.FINGERDOWN and pygame.FINGERUP events to the game loop:
# The game loop
running = True
while running:
# Check for events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Check for mouse inputs
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = event.pos
player_pos[0] = mouse_x
player_pos[1] = mouse_y
elif event.type == pygame.MOUSEBUTTONUP:
pass
# Check for finger inputs
elif event.type == pygame.FINGERDOWN:
finger_x, finger_y = event.pos
player_pos[0] = finger_x
player_pos[1] = finger_y
elif event.type == pygame.FINGERUP:
pass
screen.fill((0, 0, 0))
# Draw the player
screen.blit(player, player_pos)
# Update the display
pygame.display.update() Notice how similar this is to the mouse input event handler. This event handler updates the character’s position when the player presses their finger on the screen. When they release their finger, nothing happens. This allows you to create a game that you can control using both mouse and finger touch inputs. Keep in mind that you can also use other events such as pygame.FINGERMOTION to respond to finger movement.
Pygame’s Additional Touch Features
With the basic touch features in place, you can start adding more advanced features. PyGame has a few built-in features that can help you add more touch features to your game.
The first feature is the pygame.mouse.set_visible() function. This function allows you to hide the mouse cursor. This can be useful if you want to create a game that only uses touch inputs and not the mouse.
Here’s an example of how to use the set_visible() function:
pygame.mouse.set_visible(False) The pygame.mouse.set_pos() function sets the mouse cursor to a specific position on the screen. This is useful if you want to move the mouse to a specific location without using the mouse inputs.
Below is an example of how to use the set_pos() function:
pygame.mouse.set_pos(200, 200) You can use the pygame.mouse.get_rel() function to get the relative movement of the mouse. You can use this to detect how far the mouse has moved since the last mouse event.
This is how you can use the get_rel() function:
dx, dy = pygame.mouse.get_rel() Finally, you can use the pygame.mouse.get_pressed() function to check if the player presses any mouse button. This can be useful when creating games with mouse/touch controls.
Below is an example of how to use the get_pressed() function:
mouse_buttons = pygame.mouse.get_pressed() PyGame also provides a MOUSEWHEEL event type which you can use to detect mouse wheel scrolls. It supports both vertical and horizontal scrolls.
Here’s an example:
for event in pygame.event.get():
if event.type == pygame.MOUSEWHEEL:
if event.y > 0:
print("Mouse wheel scrolled up")
elif event.y < 0:
print("Mouse wheel scrolled down") Create Interactive Games With Touch Inputs
With the touch inputs in place, you can now create interactive games. For example, you can create a game where the player can move around the screen using touch inputs. You can also create gesture-based games, where the player can perform different gestures to trigger actions in the game.
The possibilities are endless when it comes to creating games with touch inputs. With the help of PyGame, you can create games that are both fun and interactive. So get out there and start creating!