Python_pygame library study notes (2): create a window, set the background color, import and place pictures

Hello everyone, I am Imanuel, and this is my original study notes. I use the Feynman learning method to ask questions by myself, research and explain to netizens. I hope that I can learn and progress together with you.
Since the purpose of the pygame library is to make games, let's use games as an example to start the second lesson.

As we said in the previous section, game development is divided into the development of the rendering layer and the logic layer. For relatively simple games, the corresponding rendering and logic layers can be divided into screen display, and element attributes and relationships in the screen display.

The basic elements of "Super Mario": the basic elements of the game screen

Screenshot of Super Mario game
This is a screenshot of the game "Super Mario", showing the following basic elements of the game:
0, window
1, blue sky
2, white cloud
3, green grass
4, ground
5, bricks floating in midair
6, Question mark bricks floating in mid-air
Let's implement these elements step by step through pygame! As the second study, based on the principle of step-by-step, this programming does not involve the operation of elements, but only talks about the creation of windows and the display of the three elements of blue sky, white clouds, and green grass.

0. Create a new window (window settings)

To play a game, you first need a window. The window includes size, title and close key. The code is as follows.

insert image description here

1. The display of the blue sky (background setting)

In pygame, blue sky appears as background color. For the convenience of reading, in the following explanations, the newly added codes will appear under '''↓@#¥%%...&''', the codes are as follows:

Alt

The result of the operation is as follows:
Alt

2. Display of Baiyun (import and place pictures)

I regard Baiyun as a picture file. Therefore, the implementation of Baiyun is divided into two steps, one is the import of Baiyun, and the other is the placement of Baiyun. The code is as follows:
Alt

The result of the operation is as follows:
Alt

Here is a knowledge point that needs to be explained, which is the pixel coordinate positioning method in pygame.

Pixel coordinate positioning method in pygame

Let's take the screen as the coordinate system as an example. The point at the upper left corner of the screen is defined by pygame as (0,0), and the unit is a pixel.
Positioning the cloud at (0,0) is to place the pixel coordinates of the upper left corner of the cloud at (0,0) on the (0,0) of the screen. We can try it out by positioning the cloud at the top right of screen (1200,0) and the code is as follows:
Alt

The result of the operation is as follows:
Alt

Obviously, because the upper left corner of the cloud (0,0) is placed on the upper right corner of the screen (1200,0), the cloud disappears beyond the screen.

For your convenience, here is a picture of Baiyun:

Alt

3. Realization of green grass (Picture import and placement practice)

The implementation of green grass and white cloud is similar, but there are some small details in the coordinate positioning, so it is arranged as an exercise. The next note will tell you my answer and provide you with a picture of green grass.
alt
You can also get pictures by taking screenshots in the main picture. I just came to the community and I don’t know how to post pictures without watermarks.
See you next time!

Guess you like

Origin blog.csdn.net/weixin_45980989/article/details/125752473