Hey class, you’ve mastered dictionaries—now let’s make them shine on screen with Pygame! Imagine you’re building a superhero roster app (like from your Marvel Rivals tasks). Today, you’ll create a window, give it a cool name, and make it show off your dictionary heroes. Plus, you’ll add a key press to wake it up! This is your first step into making real windowed apps with Python—no crazy stuff, just the basics to get you started. Let’s unleash your inner game dev!
Create a Pygame window with a set size and caption, and close it properly.
Display dictionary keys and values on the screen (e.g., hero names and stats).
Handle a key press (like spacebar) to show extra text.
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.
Make sure to import pygame and INITIALIZE IT!!!!
Remember, we did this with audio, but this time, we're initializing EVERYTHING! Not just the mixer!
This code should be no problem... Write a function called get_window_size( ), and return the HEIGHT and WIDTH to use! Lots of different ways to handle it!
Make sure you ERROR CHECK!!!
Additionally, write a function called get_caption() which returns the caption that the user wants the window to have
Now, let's set some of the window parameters by using:
pygame.display.set_mode( )
pygame.display.set_caption( )
“Here’s where Pygame builds your stage! screen is like a blank TV, and the caption is its label. Your choices make it happen!”
What Do These Do?
set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)):
Opens a window with the size they picked, storing it in screen (like a canvas to draw on).
set_caption(caption):
Puts their custom name at the top of the window.
Why It’s There:
Without the loop, the window would flash and close instantly
QUIT lets it close cleanly;
flip() keeps it visible.
What It Does:
running = True:
Starts a loop that keeps the window open.
while running:
Runs forever until something stops it.
for event in pygame.event.get():
Checks for actions (like clicking the X).
if event.type == pygame.QUIT:
If they click X, running becomes False, ending the loop.
pygame.display.flip():
Updates the screen every loop (like refreshing a TV picture).
This is going to be outside of your loop!!!
Actually calls the quit method and let's things end nicely!
Instead of displaying boring preset text, let’s give YOU the power! You’ll write a message in the terminal (before the game window pops up), and your message will appear inside the game window. You’re now the announcer of your own game!
This is your first time drawing something in a Pygame window, so we’ll break it down into small chunks so you understand how text works in a game.
This is still regular Python—just a good ol’ input().
Before we launch our game window, prompt the user for a custom message.
We're setting up the same window as we did in Part I. This just simplifies it a lot, and makes a default user height and width!
🧠 Why?
pygame.init() starts Pygame's systems (like the screen, sounds, fonts).
set_mode() creates the screen your game will live in.
set_caption() gives your game window a name—like the title of a YouTube tab.
We're setting up the same window as we did in Part I. This just simplifies it a lot, and makes a default user height and width!
🧠 Why Use a Font?
In Pygame, text is NOT automatic like with print().
You have to:
Choose a font style and size
Render it (like making a picture of the text)
Draw (blit) that picture onto the screen
Using None means "just use the default font," and 48 means "make it big enough to see!"
🧠 Why a Loop?
This keeps your game window open and running! It checks for events, like when someone clicks the X to close the game.
🧠 Why This Way?
screen.fill()
erases old drawings so you can redraw fresh.
font.render()
creates a picture of the text.
blit()
places it on the screen like sticking a label onto a box.
flip()
tells Pygame: "We’re ready—show it!"
A Surface is like a digital sticker or cutout you can paste anywhere on your screen.
When you call font.render(...), you're making a sticker with your message.
When you call screen.blit(...), you paste that sticker onto the window.
That’s the “showtime” button.
Pygame keeps drawing behind the scenes.
When you're done drawing for this frame (text, images, etc.), you use: pygame.display.flip()
🧠 Why??
This shuts Pygame down properly and closes your game without crashing.
Can you figure out a way to let a user select the font before the window opens??
If you want a very specific font (like a Pokémon or Zelda themed font), download a .ttf file and place it in the same folder as your Python script:
✅ Gives you total control over style
❌ You must include the font file with your project
You can even combine fonts and colors!!
Remember... use SysFont if it is not a custom font you downloaded!!! System fonts like comicsans, arial, etc need SysFont
You’ll create a Pygame window that:
Sets a cool title
Displays one line of text
Shows that same line on multiple lines, each using a different system font
All the lines will be centered on the screen horizontally
Here's a snippet of code you can use to center a line of text in Pygame!!