Hello, I’m Yue Chuang. We started with the first update of the 30-day challenge, and now we have the second. The first course video has also been posted on wechat. The previous link: mp.weixin.qq.com/s/Fth21WgO6… , in order to prevent operation is 70%, and the code words to copy code friend can click this link to access: mp.weixin.qq.com/s/T03GJ0F1F…

directory

  1. Load the image and resize it
  2. Picture display rule
  3. Football rebound

1. Load the image and resize it

Case 1: Create a new file, prepare two image files (you can download the image you like by yourself, the complete file name of these two images has been changed to football.png and background.jpg), and put the three files in the same folder. The code is then used to load and resize the image.

For the sake of subsequent reading, the quit() part is written as a function, which I explained in the zero-base one-to-one section, so I won’t go into too much detail here.

Code parsing:

  • The image-related functions in PyGame are loaded and used in imageload()
  • Fill in the full name of the image when loading. The image data is treated as a surface, which can change the coordinates and size.
  • When displaying the image, use the screen created to display it (PS: our stuff must be placed on the screen to display it), so use screen here, display function isblit()It takes two parameters, the surface to display and this coordinate.

2. Picture display rules

  • In PyGame we use the top left corner of the screen as the origin of coordinates, positive y down, positive X to the right.

  • Specifies the image coordinates when displaying the image, which is also the upper left corner of the image. Surface defaults to 0, 0.

  • The image shown first will be overwritten by the image shown later, so the background image should be shown first.

bg = pygame.image.load('background.jpg')
bg = pygame.transform.smoothscale(bg, (600.400))
Copy the code
screen.blit(bg, (0.0)) # Display the background image from the top left corner
Copy the code

In order to prevent small white do not understand, do not know where to put the code, the following code picture:

Note:

After loading the background image, set it to the same size as the window, and then set the coordinates to (0, 0) to fill the background.

3. Football bounces

Case 2: Let the ball move in the field and bounce when it hits the boundary. Make the following changes in the principle code.

Let me give you a hint:

We know that the position of the picture is determined by the coordinates, so the coordinate change means the motion of the picture.

To change the picture coordinates, the written value is obviously not possible, so variables are used to represent the coordinates and the corresponding speed.

Touching the boundary is actually the coordinate beyond the screen size, need to pay attention to the image up, down, left and right coordinates, to consider the size of the image.

Here I’ll just put it in the full code:

Concerned public account: AI Yue Chuang, plus wechat: Jiabcdefh