Game Development Reference
In-Depth Information
If nothing appears after you hit the Enter key, then you know Pygame has successfully
been installed. If the error ImportError: No module named pygame appears,
then try to install Pygame again (and make sure you typed import pygame correctly).
This chapter has five small programs that demonstrate how to use the different features
that Pygame provides. In the last chapter, you will use these features for a complete game
written in Python with Pygame.
A video tutorial of how to install Pygame is available from this topic's website at
http://inventwithpython.com/videos/.
Hello World in Pygame
We are going to create a new "Hello World!" program, just like you created at the
beginning of the topic. This time, we will use Pygame to make "Hello world!" appear in a
graphical user interface (GUI, which is pronounced "gooey") window. A graphical
user interface gives you a window that color, shapes, and images can be drawn on by your
program, as well as accepting mouse input (and not just keyboard input). The basic shapes
that we draw on the screen are called drawing primitives . GUI windows are used instead
of the text window (also called a console window or a terminal window ) that we used
for all our previous games.
Pygame does not work well with the interactive shell because it relies on a game loop
(we will describe game loops later). Because of this, you can only write Pygame programs
and cannot send commands to Pygame one at a time through the interactive shell.
Pygame programs also do not use the input() function. There is no text input and
output. Instead, the program displays output in a window by drawing graphics and text to
the window. Pygame program's input comes from the keyboard and the mouse through
things called events, which we will go over in the next chapter. However, if our program
has bugs that cause Python to display an error message, the error message will show up in
the console window.
You can also use print() calls to display text in the console window, however in
Pygame the print() function is only used for temporarily printing messages to help you
find bugs in your program. It can be useful to print out the values of variables while your
program is running so that you can make sure it is working correctly.
You can also look up information about how to use the Pygame library by visiting the
web site http://pygame.org/docs/ref/.
Hello World's Source Code
Type in the following code into the file editor, and save it as pygameHelloWorld.py . Or
you can download this source code by going to this topic's website at
http://inventwithpython.com/chapter17
Search WWH ::




Custom Search