Last Updated on 2021-11-27 by Clay
Pygame is a multimedia game engine written in Python, but most of the time we often use Coco2d, Unity 3D, Unreal... and other famous game engines to develop a game.
That is because most of the editor functions of this type of game engine are quite complete, which means that there is a reliable enough visual editing interface to allow us to complete many game scenes; in comparison, Pygame is too simple. At the same time, you know those are very different.
However, this does not mean that Pygame cannot be used to develop a game. In fact, the syntax of Pygame is simple and quite intuitive. Many times it is used to test whether the idea of a game is feasible enough, that is, use Pygame to build a prototype.
In addition, because Python's syntax is quite elegant and easy to learn, many people will choose to use Pygame to make their first game. After all, many people who learn to write programs have a game dream.
The article introduce how to download Pygame and how to run the first Pygame program.
Download Pygame
Pygame is a famous package in Python, you just need to use the following command in terminal:
pip3 install pygame
And you can install it in your environment.
If you use some IDE such as PyCharm, you may need to find out how to install the package in your current environment.
By the way, the version I used is pygame 2.0.1.
Run the first program (Hello World)
Every time we learn a new programming language or framework, the first thing to do is to print out Hello World. This means that you have learned how to execute this program/framework, and how to display the text you want to print where you want it to be displayed.
The following is a sample code:
# coding: utf-8
import pygame
def main():
# Settings
width = 600
height = 400
color_background = (30, 30, 30)
color_font = (200, 200, 200)
# Init
pygame.init()
window_size = (width, height)
screen = pygame.display.set_mode(window_size)
# Text
font = pygame.font.SysFont("Arial", 35)
text = font.render("Hello World", True, color_font)
text_rect = text.get_rect(center=(width/2, height/2))
# Run
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
# Fills
screen.fill(color_background)
screen.blit(text, text_rect)
# Updates
pygame.display.update()
if __name__ == "__main__":
main()
Output:
This program is not long and does less than 40 lines. Let's talk about how the code works from the beginning.
Import pygame package
# coding: utf-8
import pygame
The very beginning is the most important. We imported the pygame
package. If it is not imported, then we can't use the functions that pygame package. If it is not imported, then we can't use the functions that pygame has packaged for us, and we can't use the functions that pygame has packaged for us, and we can't talk about making games.
def main()
Here is the definition of the main function, just the definition of the program will not be executed. We also need the bottom if __name__ == "__main__":
to execute this function.
We still need to talk about what exactly is executed in the main()
function.
Settings
# Settings
width = 600
height = 400
color_background = (30, 30, 30)
color_font = (200, 200, 200)
Here are the basic settings, including the height and width of the window, as well as the background color and text color. The colors are all represented by RGB.
Initialize
# Init
pygame.init()
pygame.display.set_caption("Hello Wolrd Demo")
window_size = (width, height)
screen = pygame.display.set_mode(window_size)
Here, pygame is initialized, and the pygame framework really starts to work. In addition, the window title and window size are set.
Text
# Text
font = pygame.font.SysFont("Arial", 35)
text = font.render("Hello World", True, color_font)
text_rect = text.get_rect(center=(width/2, height/2))
Set the object with the Hello World string, and set the font, size, and position (center)
Execution
# Run
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
# Fills
screen.fill(color_background)
screen.blit(text, text_rect)
# Updates
pygame.display.update()
Here we need to use a while loop to keep the screen updated; in addition, we also need to set the event of pygame to close the window and screen.blit(text, text_rect)
to continuously print text.
Finally, use pygame.display.update()
to update the screen, then the while loop will continue to perform the next iteration, refreshing the screen all the time.