Game Development Reference
In-Depth Information
We will use a constant variable to determine how fast the blocks should move. A value
of 4 here means that each block will move 4 pixels on each iteration through the game loop.
Setting Up Constant Variables for Color
21. # set up the colors
22. BLACK = (0, 0, 0)
23. RED = (255, 0, 0)
24. GREEN = (0, 255, 0)
25. BLUE = (0, 0, 255)
We set up constant variables for the colors we will use. Remember, Pygame uses a tuple
of three int values for the amounts of red, green, and blue called an RGB value. The
integers are from 0 to 255 . Unlike our "Hello World" program, this program doesn't use
the white color, so we left it out.
Again, the use of constant variables is for readability. The computer doesn't care if we
use a variable named GREEN for the color green. But if we later look at this program, it is
easier to know that GREEN stands for the color green rather than a bunch of int values in a
tuple.
Setting Up The Block Data Structures
27. # set up the block data structure
28. b1 = {'rect':pygame.Rect(300, 80, 50, 100), 'color':RED,
'dir':UPRIGHT}
We will set up a dictionary to be the data structure that represents each block.
(Dictionaries were introduced at the end of the Hangman chapter.) The dictionary will have
the keys of 'rect' (with a Rect object for a value), 'color' (with a tuple of three ints
for a value), and 'dir' (with one of our direction constant variables for a value).
We will store one of these data structures in a variable named r1 . This block will have
its top left corner located at an X-coordinate of 300 and Y-coordinate of 80. It will have a
width of 50 pixels and a height of 100 pixels. Its color will be red (so we'll use our RED
constant variable, which has the tuple (255, 0, 0) stored in it). And its direction will
be set to UPRIGHT .
29. b2 = {'rect':pygame.Rect(200, 200, 20, 20),
'color':GREEN, 'dir':UPLEFT}
30. b3 = {'rect':pygame.Rect(100, 150, 60, 60), 'color':BLUE,
'dir':DOWNLEFT}
Here we create two more similar data structures for blocks that will be different sizes,
Search WWH ::




Custom Search