Computer Science Grade 9 20 min

6. Handling User Input: Keyboard and Mouse

Learn how to handle user input from the keyboard and mouse in Pygame.

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Explain the purpose and structure of the Pygame event loop. Detect and respond to specific key presses and releases (KEYDOWN and KEYUP). Get the current (x, y) coordinates of the mouse cursor. Detect and respond to mouse button clicks (MOUSEBUTTONDOWN). Write code to move a game object on the screen using keyboard input. Write code that makes a game object react when the user clicks on it with the mouse. Ever wondered how your character in a game knows exactly when you press the spacebar to jump or the mouse to build? 🎮 Let's learn how to give our programs ears to listen to our commands! This tutorial will teach you how to make your Pygame applications interactive. We will explore the event loop, the heart of any game, and learn how to capture and...
2

Key Concepts & Vocabulary

TermDefinitionExample EventAn action or occurrence that the program can detect and respond to. In Pygame, an event can be a key press, a mouse click, or even the user trying to close the window.When you press the 'A' key on your keyboard, Pygame generates a `KEYDOWN` event. Event LoopA core part of every game's code. It's a loop that constantly runs, checking for any new events that have occurred and then running the code to handle them.A `while` loop that contains `for event in pygame.event.get():` is the standard Pygame event loop. Event QueueAn internal list where Pygame stores all the events that have happened since the last time they were checked. The event loop processes events from this queue one by one.If you click the mouse and then press a key very quickly, b...
3

Core Syntax & Patterns

The Event Loop Pattern for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # ... other event checks go here This is the fundamental structure for handling all user input. It must be placed inside your main game loop (the `while` loop). It iterates through every event Pygame has detected and allows you to respond to them. Checking for Specific Key Presses if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: player_x -= 5 To react to a specific key, you first check if the event `type` is a key press (`KEYDOWN`). Then, you have a nested `if` statement to check if the event `key` matches the specific key you're looking for (e.g., `K_LEFT`, `K_w`, `K_SPACE`). Checking for Mouse Clicks if event.type == py...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
In the 'Clicking a Target' example, a blue circle turns green when clicked. How would you modify the logic so that clicking the circle again toggles its color back to blue?
A.You cannot change the color back; it can only be set once.
B.Inside the click check, add an `if` statement: `if color == blue: color = green` and an `else` to set it back to blue.
C.Create a separate event handler for `MOUSEBUTTONUP` to change the color back.
D.After changing the color to green, immediately call `pygame.draw.circle()` with the blue color.
Challenging
A game is running slowly on an old computer. The user presses and holds the right arrow key. Because the main loop is slow, the event queue fills up with many `KEYDOWN` events. What is the likely effect on the player character when the program finally processes this backlog?
A.The character will not move at all because the queue is full.
B.The character will move a small, fixed distance regardless of how long the key was held.
C.The character will jump suddenly a large distance to the right as the loop processes all the queued events at once.
D.The game will crash with an 'Event Queue Overflow' error.
Challenging
A game character should only be able to jump (e.g., `player_y -= 50`) if they are on the ground (e.g., `player_y == 400`). Which is the correct way to structure this logic within the event loop?
A.if player_y == 400: if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: player_y -= 50
B.if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: if player_y == 400: player_y -= 50
C.if event.type == pygame.KEYDOWN and player_y == 400: if event.key == pygame.K_SPACE: player_y -= 50
D.if event.key == pygame.K_SPACE: if player_y == 400: player_y -= 50

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from VI. Game Development Basics with Pygame

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.