Pygame window

The first thing we need to do is make a window where we'll start drawing our graphics. Listing 16.1 shows a very simple program that just makes a Pygame window.

Listing 16.1 Making a Pygame window import pygame pygame.init()

screen = pygame.display.set_mode([64 0, 480])

Try running this program. What did you see? If you were looking closely, you might have seen a window (filled with black) pop on the screen very briefly. What's up with that?

Well, Pygame is meant for making games. Games don't just do things on their own—they have to interact with the player. So Pygame has something called an event loop that constantly checks for the user doing something, like pressing keys or moving the mouse. Pygame programs need to keep the event loop running all the time, and as soon as the event loop stops, the program stops. In our first Pygame program, we didn't start the event loop,

That's just like our\ First program, so the program stopped very soon after it

"although it wasnta* started.

"problem then, because you* could see the output in the vI0L6 window even after the* program ran.

That's right. But in Pygame, the window only stays open while the program is running. So we have to keep it running.

11 IHrrr'-i r-irililFi riN INlTI^M^_

Have you been wondering why Pygame sometimes doesn't^ work with IDLE? It has to do with the event loop. An event loop is a loop that runs constantly in a program, checking for events like a key being pressed or the mouse being clicked or moved. Pygame programs need an event loop.

IDLE also has its own event loop, because it's a program too, and it happens to be a graphical program that needs to keep checking for user input. The two event loops don't always get along-they sometimes bump into each other and cause havoc.

The same is true for IDLE and EasyGui. It's like if someone is on the phone and you pick up an extension and try to make another call. You can't, because the phone is already busy. If you start talking or dialing, that will interfere with the conversation that's already going on.

SPE doesn't have this problem because it has a way to keep its own event loop separate from the event loop of the program that it's running (like your game).

One way to keep the Pygame event loop running is with a while loop, like the one in listing 16.2 (but don't try it yet!).

Listing 16.2 Keeping the Pygame window open import pygame pygame.init()

screen = pygame.display.set_mode([64 0, 480]) while True: pass pass is a Python keyword that means "do nothing." It's just a placeholder, because a while loop needs a block of code, and the block can't be empty. (Perhaps you remember that from chapter 8 when we talked about loops.) So we put something in the while block, but that "something" does nothing.

Remember that a while loop runs as long as the condition is True. So this really says, "While True is True, keep looping." Because True is always True, that means forever (or as long as the program runs).

But if it'll keep going forever, how will we stop it? Do you recall that, back in chapter 8, Carter asked about stopping a program that had a runaway loop? We learned that you can use Ctrl-C to do that. We can use the same method here. However, when running programs in SPE on Windows, you need to use Ctrl-Break instead of Ctrl-C. There's only one trick to this: you need to make the command shell the active window before you type Ctrl-Break. If you try using Ctrl-Break in the Pygame window, nothing will happen.

If you have a runaway loop on a Mac, you should be able to press Ctrl-C to stop it. If that doesn't work, you can try Ctrl-\ to send it a quit signal. Or you can start up the Activity Monitor (located in the Utilities folder in the Applications folder), find the Python or Pygame process, and quit it. If you are using Linux, the easiest way is to kill the process.

I* pygame window e m

Okay, now that you know how to stop it, try running the program in listing 16.2. You can type it into whatever editor you're using, and save it as When you run it, you should see a new window pop up, with a black background. It should have pygame window in the title bar. The window will stay there until you make the command shell the active window and end the program with Ctrl-Break.

If you're running Pygame from SPE, there will be a shell window opened for you. That window will have something like SPE <filename> - Press Ctrl + Break to stop in the title bar. Click in that window to make it active before trying to quit the application.

Was this article helpful?

0 0
100 Bowling Tips

100 Bowling Tips

Playing bowling with your friends can help you decide if it is indeed the hobby that you want to invest your time on today. Aside from that, it can help you get a better feel of the sport. More importantly, when you play with your friends, it would become a more fun activity, which you can look forward to each week.

Get My Free Ebook

Post a comment