This article will tell you how to create a Pygame application that displays the text Hello World on the Pygame app window center. It also tells you how to create the Pygame app window, set the window title, set the window background color, how to create the text with provided text content, text color, text background color. And how to capture the system quit event when user click the window close button and quits the Pygame application.
1. Create A Pygame Application Steps.
- First, you need to import the Pygame library with the below source code.
import pygame
- Then you should initialize the Pygame application by invoking the Pygame module’s init() method.
pygame.init()
- Next, you should create a Pygame surface or screen object with the code pygame.display.set_mode(), the surface object is just the Pygame app’s main window, you can draw text, graph, and image on it.
# define a tuple to store screen size. screen_size = (600, 600) # set the pygame window screen size. screen = pygame.display.set_mode(screen_size)
- You can call the pygame.font.SysFont() method to create a pygame.font.Font object.
f = pygame.font.SysFont(name = 'SF', size = 50, bold = True, italic = True)
- Then call the render() method of the above pygame.font.Font object to get the text object that will be drawn on the window surface.
text = f.render("Hello World", False, pygame.Color('red'), pygame.Color('green'))
- Get the text’s rectangle area by invoking the get_rect() method.
textRect = text.get_rect()
- Call the window surface’s blit() method to draw the text on the screen.
screen.blit(text,textRect)
- You also need to define an event process function to monitor and handle user or system-triggered events such as close the window event, you can see this example’s main_loop() function to learn more.
2. Pygame Hello World Example.
- Below is the Pygame hello world example’s source code in this article, the python file name is PygameHelloWorld.py.
''' Created on Jan 30, 2022 @author: songzhao ''' # import the sys and pygame module that we needs in this example. import sys import pygame # this function will create a pygame window with Hello World text in it. def pygame_hello_world(): # initialize the pygame app. pygame.init() # define a window title. pygame_window_title = 'Pygame Hello World' # set window title to the pygame app window. pygame.display.set_caption(pygame_window_title) # define a tuple to store screen size. screen_size = (600, 600) # set the pygame window screen size. screen = pygame.display.set_mode(screen_size) # set the pygame window background color. screen.fill(pygame.Color('blue')) # create a pygame.font.Font object to represent the text Font. f = pygame.font.SysFont(name = 'SF', size = 50, bold = True, italic = True) # generate text, the first parameter specifies the text content. # the second parameter specifies whether the text font is antialias or not. # the third parameter specifies the text color. # the fourth parameter specifies the text background color. text = f.render("Hello World", False, pygame.Color('red'), pygame.Color('green')) # get the rectangle area coordinates of the display text object. textRect = text.get_rect() # define the text rectangle center point coordinates. center_coordinates = (300, 300) textRect.center = center_coordinates # draw the prepared text on the main screen. screen.blit(text,textRect) main_loop() # the main_loop function is used the monitor and handle user triggered event. def main_loop(): # implement the feature that when clicking the pygame window close button ("X") on window title bar then it will exit the window. # Almost all pygame application will use this kinds of code. while True: # Loop to get events and listen for event status. for event in pygame.event.get(): # if user click the window close button. if event.type == pygame.QUIT: # quit pygame. pygame.quit() # quit the application. sys.exit() pygame.display.flip() # refresh the screen. if __name__ == '__main__': pygame_hello_world()
- When you run the above example source code, you can see the below picture.
3. How To Fix Error TypeError: render() takes no keyword arguments.
3.1 Question1.
- When I call the pygame.font.Font class’s render() function like below, it shows the error message TypeError: render() takes no keyword arguments.
text = f.render(text = "Hello World", color = pygame.Color.r, background = pygame.Color.g)
3.2 Answer1.
- The error detail message said render() method takes no keyword arguments, but you pass keyword text, color, background to it.
- So you should call the render() method like below, do not pass keyword argument, just pass the parameters as the method defined arguments order.
text = f.render("Hello World", True, pygame.Color('red'), pygame.Color('green'))
- You can press the Command (macOS) / Ctrl (Windows) button + click the render() method with the mouse key to go to the method definition to see the method arguments and their pass in order.