# Wormy (a Nibbles clone) # By Al Sweigart firstname.lastname@example.org # http://inventwithpython.com/pygame # Released under a "Simplified BSD" license import random, pygame, sys from pygame.locals import * FPS = 15 WINDOWWIDTH = 640 WINDOWHEIGHT = 480 CELLSIZE = 20 assert WINDOWWIDTH % CELLSIZE == 0, "Window width must be a multiple of cell size." assert WINDOWHEIGHT % CELLSIZE == 0, "Window height must be a multiple of cell size." CELLWIDTH = int(WINDOWWIDTH / CELLSIZE) CELLHEIGHT = int(WINDOWHEIGHT / CELLSIZE) # R G B WHITE = (255, 255, 255) BLACK = ( 0, 0, 0) RED = (255, 0, 0) GREEN = ( 0, 255, 0) DARKGREEN = ( 0, 155, 0) DARKGRAY = ( 40, 40, 40) BGCOLOR = BLACK UP = 'up' DOWN = 'down' LEFT = 'left' RIGHT = 'right' HEAD = 0 # syntactic sugar: index of the worm's head
The code at the start of the program just sets up some constant variables used in the game. The width and height of the cells are stored in
assert statements on lines 13 and 14 ensure that the cells fit perfectly in the window. For example, if the
10 and the
WINDOWHEIGHT constants were set to
15, then only 1.5 cells could fit. The
assert statements make sure that only a whole integer number of cells fits in the window.
Some more constants are set on lines 19 to 32. The
HEAD constant will be explained later in this chapter.