Operation Manual

make sure youre ready!
Figure 11-6: Playing Raspberry Snake on the Raspberry Pi
A full copy of the program listing for Raspberry Snake is included in Appendix A, “Python Recipes, and on the Raspberry Pi
User Guide website at http://www.wiley.com/go/raspberrypiuserguide. Downloading the source code from the
website will save you some typing, but entering the code by hand is a good way of ensuring that you understand what each
section does. In addition to the functions used in Raspberry Snake, pygame provides lots of features not used in this program,
including audio playback, sprite handling for better graphics and mouse control. The best place to learn about pygames more-
advanced functions is on the official website, http://www.pygame.org/wiki/tutorials, where you can download tutorials
and example programs to get a handle on how things work.
Example 4: Python and Networking
So far, you have learned how Python can be used to create standalone programs, but the language can also be used to create
programs that communicate with the outside world over a computer’s network connection. This next example, written by Tom
Hudson, offers a brief glimpse of these possibilities with a tool for monitoring the users connected to an Internet Relay Chat
(IRC) channel.
As usual, create a new project in IDLE or a text editor and enter the shebang line along with a comment describing the purpose
of the program:
#!/usr/bin/env python
# IRC Channel Checker, written for the
Raspberry Pi User Guide by Tom Hudson
Next, import the modules required by the programsys, socket and timewith the following line:
import sys, socket, time
You used the sys and time modules previously in the Raspberry Snake program, but you have not yet used socket. The
socket module provides Python with the ability to open, close, read from and write to network sockets—giving Python
programs rudimentary networking capabilities. Its the socket module that provides this example with its ability to connect to a
remote IRC server.
There are some constants needed for this program to operate. Constants are like variables in that they can have values assigned
to thembut unlike variables, the value in a constant shouldnt change. To help differentiate a constant from a variable, its good