O'Reilly logo

Raspberry Pi For Dummies by Mike Cook, Sean McManus

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Setting Up the Game Window

Before you can use Pygame in a program, you need to set it up, or initialize it, by calling the pygame.init() function:

pygame.init()

After you’ve done that, you can use Pygame to open a new window. Here are the instructions to open a window with a width of 440 pixels and a height of 480 pixels, and with PiBuster in the window’s title bar:

gameSurface=pygame.display.set_mode((440,480))

pygame.display.set_caption(‘PiBuster’)

pygame.mouse.set_visible(0)

tip.eps Take care with the number of parentheses in the first line: You need two opening and closing parentheses around the window width and height. That’s because they are a tuple — a type of list that you can’t change the values in — and they’re inside the parentheses for a function. Tuples always use parentheses and you’ll come across them lots in this chapter. As long as you get the parentheses in the right place, they won’t cause you any trouble!

technicalstuff.eps Because you can’t change the items in a tuple, it’s much less flexible than a list. At times, it’s the ideal solution, however. You might want to ensure data can’t be changed, and a tuple not only enforces permanence but also helps to communicate it when someone looks at your program code. To use a tuple instead of a list, you use parentheses instead of square brackets ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required