No history yet

Pygame Basics

Transcript

Beau

Okay, so, last time we got everything installed. Python is there, Pygame is... somewhere in the machine, doing its thing. But now what? I feel like I've bought all the ingredients for a cake, but I have no idea how to even turn the oven on.

Jo

That is the perfect analogy. Because the very first thing you do is literally turn on the oven. In your Python script, after you import the Pygame library, the absolute first line of code you'll write is `pygame.init()`.

Beau

init as in i-n-i-t? For initialize?

Jo

Exactly. You're telling the Pygame library, 'Hey, wake up. I'm about to need you.' It goes through and gets all of its little modules ready—the sound module, the graphics module, all of it—so they're ready to go when you call them. It's like the pre-flight check for a plane.

Beau

Okay, that makes sense. So I'm not doing anything specific yet, I'm just... flipping the master switch for the whole system.

Jo

You got it. Once the switch is flipped, the next step is to create the stage. You need a window to draw your game in, right? A blank canvas.

Beau

Right. Just... a black box or something that I can put stuff on.

Jo

Exactly. And for that, we use a command, usually something like `screen = pygame.display.set_mode()`. Inside the parentheses, you have to tell it how big you want that window to be.

Beau

Okay, so like, eight hundred by six hundred pixels? That classic monitor size.

Jo

Yep, a perfect starting point. The tricky part for beginners is that you have to pass those numbers in as a single unit, what Python calls a tuple. So it's not `(800, 600)`, it's `((800, 600))`. Two sets of parentheses.

Beau

Oh, that's one of those things that would have taken me an hour to debug. So `set_mode` wants one thing—a tuple—that happens to contain two numbers, the width and the height.

Jo

You've nailed it. Think of it like ordering a canvas. You don't tell the art store 'I need an 18 inch and a 24 inch.' You say, 'I need one 18-by-24-inch canvas.' Same idea.

Beau

Okay, so... `pygame.init()` to turn it on, `pygame.display.set_mode` to create the window. If I run that code, do I see my game?

Jo

You'll see... a black window flash on the screen and then immediately disappear.

Beau

Ah. Not very useful.

Jo

Not yet. Because the program just runs top to bottom and then stops. It did its job—it made a window—and then it finished. To keep that window open, we need the most important concept in all of game development: the game loop.

Beau

Sounds... loopy. Is it just a `while` loop?

Jo

It is! It's fundamentally just a `while True` loop that keeps running as long as your game is open. Think of a movie. A movie is just a series of still pictures, right? Twenty-four of them per second, shown so fast it looks like motion.

Beau

Right, a flipbook.

Jo

Exactly. The game loop is the person flipping the pages. Each time the loop runs—each 'tick'—it draws one frame of your game. It checks for player input, updates the position of everything on screen, and then draws the new picture. Over and over, sixty times a second.

Beau

So... inside this `while` loop, I would... what? Tell it to draw my little spaceship character?

Jo

You would. But first, you'd usually fill the screen with a background color, like black. It's like erasing the whiteboard before you draw the next frame. So, step one in the loop: `screen.fill()` with a color. Step two: draw your spaceship. And the final, crucial step is `pygame.display.flip()` or `pygame.display.update()`.

Beau

What's the difference?

Jo

Flip updates the entire screen. Update can be used to update just a small portion of it that changed, which can be more efficient. But for beginners, `flip` is perfect. It basically says 'Okay, I'm done drawing this frame behind the scenes. Now, take that finished drawing and slap it onto the actual monitor for the player to see.'

Beau

So the loop is like a frantic artist. Erase canvas, draw everything, show it to the audience. Erase, draw, show. Sixty times a second. That's... wild.

Jo

It is! And that's the heart of every single game you've ever played, from Pong to the most complex modern titles. It's all just a loop running incredibly fast, checking what you're doing, and redrawing the world based on your actions.

Beau

So without the loop, the window just closes. With the loop, it stays open, constantly refreshing itself, waiting for me to give it something to do.

Jo

Exactly. You've built the stage, and the stage manager is now in a constant cycle of 'ready for the next scene, ready for the next scene'. Next we just have to tell it what scenes to play.