color_variable = Red,Green,Blue
Red,Green,Blue handles standard RGB color settings. For example, the color red is represented
by the RGB numbers 255,0,0, and the color blue is represented by the RGB numbers 0, 0,
255. A few color examples and their RGB settings are shown here:
black=0,0,0
white=255,255,255
blue=0,0,255
red=255,0,0
green=0,255,0
Once you have the screen size set up and the color variables created, you can fill the screen’s
background with the color of your choice. To do so, you need to use the Surface object, which was
created to represent the game screen, and use the fill module of that object, as in the following
example:
GameScreen.fill(blue)
In this example, the defined GameScreen Surface object would have the screen’s background
filled with the color blue. You can make your screen’s background a picture, if you want to.
However, it’s best to start simply as you design your game script and keep the screen’s background a
plain color.
Putting Text on the Game Screen
Putting text on your game screen can be tricky. First, you must determine what font you want to use
and whether that font exists on your system. PyGame provides a default game font that you can use.
The module to use is pygame.font. Within the font module, you can create a Font object by using
the syntax game_font_variable = pygame.font.Font(font, size), like this:
Click here to view code image
GameFont=pygame.font.Font(None, 60)
Notice in this example that the font used is None. This is how to set the font equal to the PyGame
default game font.
To create a text image, you need to use the Font object, which was created to represent the font, and
use the render module of that object.
Click here to view code image
GameTextGraphic=GameFont.render("Hello",True,white)
In this example, the word "Hello" is the text to be displayed. It is displayed in the PyGame default
font with a color of white. (Remember that white’s color definition, defined earlier this hour, equals
255,255,255.)
The second argument in the example, True, is set to make the displayed text characters have smooth
edges. It is a Boolean argument, so if you set it to False, the characters do not have smooth edges.
You’ve done a lot of work, but you still haven’t displayed the text to the screen yet! To put the text on
the screen, you need to use the Surface object created to represent the game’s screen. The module
of the object to use is the .blit module. Earlier, you created the Surface object GameScreen
to represent the game screen. The Python statements below are used to display the text: