Computer Science Grade 5 20 min

Key Press Events: Controlling Movement

Use key press events to control the movement of an object on the screen.

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Explain that a key press is an 'event' that can trigger code. Identify the x and y coordinates of an object on a 2D screen. Use a conditional statement (if-then) to check which specific key was pressed. Write code that changes an object's x-coordinate to move it left and right. Write code that changes an object's y-coordinate to move it up and down. Combine four conditional statements to create movement in all four directions (up, down, left, right). Debug simple movement problems, like a character moving in the wrong direction. Have you ever wondered how you control your character in a video game just by pressing the arrow keys? 🎮 Let's learn the secret! In this lesson, you will learn how to listen for keyboard presses, calle...
2

Key Concepts & Vocabulary

TermDefinitionExample EventAn action that happens in a program, like a mouse click, a timer going off, or a key being pressed. The program can be told to listen for these events.When you press the spacebar to make a character jump, the 'key press' is the event. Event HandlerA special block of code that waits for a specific event to happen and then runs a set of instructions.An event handler block that says 'When the UP ARROW key is pressed...' will only run its code when you actually press the up arrow. Key Press EventThe specific event that is triggered when a user presses any key on the keyboard.Pressing the 'A' key, the 'Enter' key, or the 'left arrow' key are all key press events. Coordinate SystemA grid made of an x-axis (left and rig...
3

Core Syntax & Patterns

The Event Handler Pattern WHEN [some event] HAPPENS: DO [these instructions] This is the basic structure for all event-driven programming. You choose an event to listen for (like a key press) and then put all the code you want to run inside that handler block. The Key-Checking Conditional IF key_pressed == 'up_arrow': [code to move up] Inside your key press event handler, you need to check WHICH key was pressed. You use an 'if' statement to compare the key that was actually pressed to the key you are looking for (like 'up_arrow', 'down_arrow', 'a', 'space', etc.). The Coordinate Change Formula object.x = object.x + 10 (Move Right) object.x = object.x - 10 (Move Left) object.y = object.y - 10 (Move Up) o...

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
A variable `canMove` is set using binary: 1 for true (can move), and 0 for false (cannot move). The code is: `IF ('right arrow' pressed) AND (canMove == 1), THEN change X by 5`. If `canMove` is currently 0, what happens when the right arrow is pressed?
A.The character moves right by 5.
B.The game crashes because of a binary error.
C.Nothing happens; the character does not move.
D.The value of `canMove` changes to 1.
Challenging
You want to prevent a player from moving up and down at the exact same time, which could cause a glitch. Which logic inside a game loop best prevents this?
A.`IF 'W' is pressed, move up. IF 'S' is pressed, move down.`
B.`IF 'W' is pressed, move up. ELSE IF 'S' is pressed, move down.`
C.`IF 'W' is pressed AND 'S' is pressed, stop all movement.`
D.`Create two separate programs, one for up and one for down.`
Challenging
A character's movement is based on a `direction` variable. `WHEN 'up arrow' pressed, set direction to 'north'`. A separate loop constantly checks: `IF direction == 'north', change Y by -1`. What is the main advantage of this design?
A.It uses less memory than other methods.
B.It is the only way to make a character move.
C.It makes the character move much faster.
D.It separates the player's input from the movement action, which makes the code more organized and flexible.

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 Event-Driven Programming: Making Things Happen

Ready to find your learning gaps?

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