Alright, team, you're about to take your first step into a larger world: Reacting to the player by pressing keys inside a running Pygame window!
Today, you’ll learn how to detect key presses (like piloting an X-Wing) and display different heroes from Star Wars IV-VI when you move left and right.
🎯 Your Mission:
Set up a Pygame window and keep it open with a Game Loop.
Check if a key was pressed using KEYDOWN.
Respond to specific keys like pygame.K_LEFT and pygame.K_RIGHT.
Display a different Star Wars character name (and eventually an image!) each time.
Wrap around the character list if you reach the end (infinite scrolling!).
Alright, team, we’re kicking off with Pygame by building your app’s stage—like picking the perfect screen for your superhero HQ! You’ll choose the size from a menu and give it a custom name, then make sure it closes when you’re done. You’re the director now—let’s set the scene!”
Pick Window Size: Select from a console menu with options ranging from 800x600 (smallest) to 1920x1080 (largest).
Set a Caption: Type a custom title for the window (e.g., “Spider-Man’s Lair”).
Launch and Close: Open the window at their chosen size with their caption, and click X to exit.
Remember:
set_mode() sets the screen size
set_caption() puts your custom title at the top!
What are we doing?
while running keeps the game alive.
event.get() checks what the user did.
QUIT closes the window when you click X.
flip() shows the newest changes every frame
Once your window is alive...
You need to listen carefully — like a Jedi listening for disturbances in the Force
These are events which have to do with keys being pressed.
There are more, but for now, we can start with these!
This code shows you how we will detect that some key has been pressed!
Once we see if a key is pressed, we can use a nested if statement to check for which one!
Here's Some Code to help you... It's not all of it, but should get you started. I'm just not telling you where it goes!
Today, you’re creating a small input detector! Just like how Star Wars pilots tap different controls to move their ships, you’ll practice catching Up, Down, Left, Right, and create a secret Q-key exit for emergencies (or lightspeed escapes!)
You need to display a message to the screen when a user presses:
Left Arrow
Right Arrow
Up Arrow
Down Arrow
The Window Needs to QUIT when the user presses the Q key
NOTE:
Right now the print() statement will print the message to the CONSOLE TERMINAL, Not to the screen. You've gotta add the screen print!
This is a refresher from our earlier tasks where we loaded an image instead of just writing text to the screen! It's a full program that you can use for reference!!
load() brings the picture into memory
blit() pastes it onto the screen
flip() shows it!
In most games (think Star Wars: TIE Fighter or Super Mario), players press more than one key at once — like holding UP and RIGHT to move diagonally! In Pygame, you handle this a little differently:
Instead of just looking at KEYDOWN events one by one, you ask Pygame directly which keys are being held down at that exact moment.
This uses something called:
It checks all keys at once.
It returns a giant list (technically a tuple) of True/False values.
Each position matches a key.
Example:
If keys[pygame.K_UP] == True, it means the UP arrow is being held down right now
What’s Different?
"Instead of waiting for a KEYDOWN event, we are asking Pygame every frame which keys are still being held!"
Why is this Important?
"Games don’t just react when you tap keys — they react while you are holding keys down!"
What’s Cool?
"Now you can move diagonally, speed up by holding Shift, or make combos like firing blasters while flying!"
Both of these are checking to see if Shift and S are down!
For this task, you'll load an image and make it slide across the screen when you press keys. It's just like controlling an X-Wing flying across space!
Requirements:
You MUST USE: keys = pygame.key.get_pressed( )
The Falcon must be able to fly diagonally as well as up, down, left, right
Today, you're not just coding... You're taking command of a starship in the Star Wars universe! You’re sitting in the cockpit of a Rebel Alliance cruiser, ready to jump to hyperspace, dodge Imperial fleets, and pull off daring maneuvers across the galaxy.
But here’s the thing: Flying a real starship isn’t just tapping one button — it’s about hitting the right controls at the right time.
To launch the hyperdrive, you need to hold down SHIFT and hit SPACE.
To power up the deflector shields, maybe you have to hold CTRL and press D.
To activate stealth mode, maybe ALT + S is your secret move.
Modifier keys like SHIFT, CTRL, and ALT add extra power to ordinary key presses.
Common Modifier Keys
Meta is a weird one!
On Windows computers, Meta is the Windows Key 🪟.
On Macs, Meta is the Command Key (⌘).
👉 Most games don't usually listen for Meta because it can interfere with the operating system.
We have special constants that we use for these keys.
You'll note that they all start with pygame.KMOD_
There are also LEFT and RIGHT Versions!
In Pygame, modifier keys are handled differently from regular keys like 'A' or 'Space.'
Pygame groups modifier keys into something called a bitmask (like a checklist inside a number!).
You can peek inside that checklist using:
pygame.key.get_mods()
This checks what modifiers are ON at the time.
Imagine you have switches
These ON/OFF states are hidden inside one number.
We can use & (bitwise AND) to ask about one specific switch.
Here's How We Detect if a specific mod is "ON"
You are building the advanced control system for a Rebel Starfighter! 🚀 But this isn't just basic flying — your ship needs combo controls for its special maneuvers.
Use the Arrow keys (or WASD) to fly the ship around the screen.
BUT... when you hold certain modifier keys like Shift, Ctrl, or Alt while pressing specific keys, special powers activate!
✨ REQUIRED Special Moves You Must Program:
For these tasks, you just need to figure out how to detect key combinations with modifiers and print messages to screen
Minimum: Print to Console
Better: Print to the Pygame Window
For these tasks, which are more advanced, you'll have to figure out a way to get the millennium falcon to move on screen differently!
This means you'll add to the already existing code!
ADVANCED TASKS TO ATTEMPT!!! (For WAY MORE POINTS)
Things To Remember...
Use pygame.key.get_pressed() to check regular keys (like Space, A, W, etc).
Use pygame.key.get_mods() to check if Shift, Ctrl, or Alt is held.
Use & to check for specific modifiers (like mods & pygame.KMOD_SHIFT).
Now you’ll move through a list of Star Wars characters with your arrow keys... AND (bonus!) you'll display a picture of the character too!You’re basically building a mini hologram projector for the Rebellion!
First, let’s make a list of characters and track which one is active.
current_index tells your program which hero is currently selected.
This section will just change the current index. We need to always know which character is active!
🧠 Why?
Press Right ➡️ move to the next character.
Press Left ⬅️ move back.
Wrapping around makes it feel like flipping through a star chart.
After you fill your background color each frame, draw the character's name:
🧠 Why?
font.render() turns text into an image (called a Surface).
screen.blit() pastes that Surface onto your game window.
Putting it in the center looks nice—like the Star Wars title crawl!
Now... Let's add something WAY Better to your program! We're not only going to display the character name, we will display their image!! Even cooler, is that we're able to scroll through them with the left and right keys!!
Save or collect small .png images for each character.
Important:
Save them in the same folder as your Python file.
Keep the names simple (lowercase, no spaces).
TASK:
Create a list which has the character images
Add the code to load the image
Display the proper image when you use the L and R arrows!!
Skeleton Code A
Skeleton Code B