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.
import pygame, sys from pygame.locals import *
400
by 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!')
Hello world!
at coordinates (100
, 100
). The text has font size 19
and a red
color:while True: sysFont = pygame.font.SysFont("None", 19) rendered = sysFont.render('Hello World', 0, (255, 100, 100)) screen.blit(rendered, (100, 100)) for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() pygame.display.update()
We get the following screenshot as an end result:
Following is the complete code for the Hello World! example:
import pygame, sys from pygame.locals import * pygame.init() screen = pygame.display.set_mode((400, 300)) pygame.display.set_caption('Hello World!') while True: sysFont = pygame.font.SysFont("None", 19) rendered = sysFont.render('Hello World', 0, (255, 100, 100)) screen.blit(rendered, (100, 100)) for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() pygame.display.update()
It may not seem like much, but we learned a lot in this section. The functions that passed the review are summarized in the following table: