In this article, we will learn how to download and install the Pygame, a game development library, and we will run a simple sample project to get acquainted with Pygame.
Project code download link: https://github.com/la-vie-est-belle/pygame_codes
Installing Pygame
Installing Pygame on Windows
Open a command line window and type the command pip install pygame
followed by pressing Enter. If you see the message Successfully installed pygame
at the end, it means that Pygame has been successfully installed.
Note: The version of Pygame used in this tutorial is 2.6.0.
Of course, we also need to verify if Pygame is working correctly. Open the command line window, type python
, and press Enter to enter the Python command line interface. Then type import pygame
. If the Pygame welcome message appears, it means the installation is successful and Pygame can be used normally.
Installing Pygame on MacOS
Similarly, open a terminal, type pip3 install pygame
, and press Enter to install Pygame. The verification method is the same as above, and will not be repeated here.
Running a Pygame Sample Project
Open a command line window and run the command python -m pygame.examples.aliens
to start the built-in alien game that comes with Pygame. The controls are as follows:
- Arrow keys to move the player
- Spacebar to shoot
In the upcoming practical articles, we will develop and implement this alien game together. For now, letβs take a look at this simple Pygame example code 1-1.
import sys
import pygame
pygame.init() # 1
screen = pygame.display.set_mode((1100, 600)) # 2
pygame.display.set_caption('Dino Runner') # 3
icon = pygame.image.load('icon.png') # 4
pygame.display.set_icon(icon)
dino = pygame.image.load('dino_start.png') # 5
dino_rect = dino.get_rect()
dino_rect.topleft = (80, 300)
while True: # 6
for event in pygame.event.get(): # 7
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill((255, 255, 255)) # 8
screen.blit(dino, dino_rect) # 9
pygame.display.flip() # 10
Code Explanation:
#1 pygame.init()
initializes all the modules in the Pygame library. This line must be included before using any Pygame functions or classes.
#2 Call the pygame.display.set_mode()
function to set the size of the game window (size is passed as a tuple). This function returns a window object, which is stored in the variable screen
. This object allows you to draw graphics, add images, and text to the window. The pygame.display
module is dedicated to window management and screen display.
#3 Use the pygame.display.set_caption()
function to set the window title.
#4 Use the pygame.image.load()
function to load an image, in this case, the window icon. This function returns an image object, which is stored in the icon
variable. Next, use the pygame.display.set_icon()
function to set the window icon.
#5 Load the main character image, and call get_rect()
to obtain the rectangle area of the character image (a Rect
object), and set the top-left corner topleft
of this rectangle to the screen position.
Note: The origin of the screen coordinates is at the top-left corner, with the X-axis extending to the right and the Y-axis extending downward. The coordinate system in Pygame will be explained in detail in later chapters.
#6 Enter a game loop where Pygame will continuously detect and handle user input, update game states, or update screen contents.
#7 Use pygame.event.get()
to get an event queue. In the for loop, we read and handle each event. If the event type
event.type is pygame.QUIT
(i.e., closing the window), call pygame.quit()
to exit the game. sys.exit()
terminates the current Python program, cleans up, and exits the thread running the Pygame program.
#8 Call the fill()
function of the window object screen
to fill the window with a color. You pass a tuple representing the color's RGB values, in this case, white.
#9 Call the blit()
function of the window object screen
to display the character image on the screen, with the position defined by the previously set rectangle dino_rect
. You can also pass a coordinate tuple (x, y)
to blit() to set the character's position on the screen, such as:
screen.blit(dino, (80, 300))
#10 Call the pygame.display.flip()
function to refresh the screen content. You can also use pygame.display.update()
to achieve the same effect. The latter can also accept a rectangle area to update only that portion of the screen. For example, the following line will update only a rectangle with the top-left corner at (0, 0)
and a width and height of 350
pixels.
pygame.display.update((0, 0, 350, 350))
Summary
In this article, we learned how to install Pygame on Windows and MacOS, and explored a simple example code to understand the basic structure, operation principles, and some common functions of Pygame. If there are any parts that you do not fully understand, you can leave them for now. Later chapters may help clarify these concepts.
Buy author a cup of coffee if you enjoyed this tutorial. :)
Top comments (0)