Game Development Reference
In-Depth Information
you could call
exit()
instead of
sys.exit()
in your code. (But most of the time it
is better to use the full function name so that you know which module the
exit()
is in.)
The
pygame.init()
Function
4. # set up pygame
5. pygame.init()
The Pygame software library has some initial code that needs to be run before we can
use it. All Pygame programs must run this code by calling the
pygame.init()
after
importing the
pygame
module but before calling any other Pygame functions.
The
pygame.display.set_mode()
and
pygame.display.set_caption()
Functions
7. # set up the window
8. windowSurface = pygame.display.set_mode((500, 400), 0,
32)
9. pygame.display.set_caption('Hello world!')
Line 8 creates a GUI window for our program by calling the
set_mode()
method in
the
pygame.display
module. (The
display
module is a module inside the
pygame
module. Pygame is so advanced that even the pygame module has its own modules!)
Just to avoid confusion, you should know the difference between the window that is
created is different and the Windows operating system. The graphical user interface is
printed as "window" (lower case and singular) and the Microsoft operating system is
"Windows" (upper case and plural).
There are three parameters to the
set_mode()
method. The first parameter is a tuple of
two integers for the width and height of the window, in pixels. A
pixel
is the tiniest dot on
your computer screen. A single pixel on your screen can turn into any color. All the pixels
on your screen work together to display all the pictures you see. To see how tiny a pixel is,
look at the bottom right corner of the "Hello World!" window. This program sets just one
pixel as white.
We want the window to be 500 pixels wide and 400 pixels high, so we use the tuple
(500, 400)
for the first parameter. To get the total number of pixels in our window,
multiply the width and the height. Our window is made up of 20,000 pixels, and it doesn't
even take up the entire computer screen!
The second parameter is for advanced GUI window options. You won't really need this
for your games, so you can always just pass
0
for this parameter. The third parameter is