Time for action – creating a simple game
It's important to notice the so-called main game loop where all the action happens and the usage of the Font
module to render text. In this program we will manipulate a Pygame Surface
object that is used for drawing, and we will handle a quit event. Perform the following steps to create a simple game:
First import the required Pygame modules. If Pygame is installed properly, we should get no errors, otherwise please return to the installation recipe.
import pygame, sys from pygame.locals import *
We will initialize Pygame, create a display of 400 x 300 pixels, and set the window title to Hello World!.
pygame.init() screen = pygame.display.set_mode((400, 300)) pygame.display.set_caption('Hello World!')
Games usually have a game loop, which runs forever until for instance a quit event occurs. In this example we will only set a label with the text Hello World at coordinates
(100, 100)
. The text has font size of19
and a red color.while True: sysFont...