O'Reilly logo

Raspberry Pi For Dummies by Mike Cook, Sean McManus

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Colors in Pygame

If you did the web design project in Chapter 8, your knowledge of colors will come in handy here. In Pygame, colors are expressed according to how much red, green, and blue they have in them, hence the term RGB color code. You provide three values in parentheses, one for each color (red, green, and blue), ranging from 0 for none at all, up to 255 for the maximum. Pure red would be (255,0,0), which is the maximum amount of red and no green or blue. Black is (0,0,0), a complete absence of color.

Using these numbers can get quite clumsy, and it makes the program hard to understand, so instead, it’s a good idea to create variables to store the color numbers. Variables that don’t change their values while the program is running are known as constants, and it’s a good idea to write them with capital letters so you can immediately see they’re constants, wherever they are in the program. Here are my color constants:

SHADOW = (192, 192, 192)

WHITE = (255, 255, 255)

LIGHTGREEN = (0, 255, 0 )

GREEN = (0, 200, 0 )

BLUE = (0, 0, 128)

LIGHTBLUE= (0, 0, 255)

RED= (200, 0, 0 )

LIGHTRED= (255, 100, 100)

PURPLE = (102, 0, 102)

LIGHTPURPLE= (153, 0, 153)

We’ve chosen these colors especially for PiBuster, and we’ve spaced them out to make them easier to understand. The SHADOW color is a light gray, but we’ve given it a more meaningful name because it will be used to create the impression of shade.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required