1
头图

Disclaimer: It is not easy to be original, and may not be reproduced without permission

xa

0. Preface

hello, friends! I'm still the cat lady who took you guys to write games.
Today is the 6th episode of this game! In the last episode, we changed the main character of the game into a vest, turning the little cube into a little boy! Today we want to make the little boy's feet move flexibly, the effect is like this!

1. The principle of animation

After we made the little boy's feet move, visually, it felt like the little boy was walking!
The basic principle of GIFs is that multiple pictures keep playing!
Here we use three pictures. In the player.png player0.png image directory, I have prepared three pictures of the little boy. , the last two pictures are player1.png , player2.png .
Let's come back and write the code.
Here, we still modify the Player class, we use for循环 , put all three pictures load in, we will load the three pictures The pictures are all placed in the list self.plays . Here I write the code first, and then explain it line by line.

 self.plays = []
for i in range(3):
    image_filename = "./images/player" + str(i) + ".png"
    image = pygame.image.load(image_filename)
    self.plays.append(image)

via for i in range(3):
We splice out the path of the boy's picture and assign it to image_filename ,
Then use the pygame.image.load function to import this image, and the path to the image is passed in parentheses.
Have you found this pygame.image.load function I have used many times, I hope you will remember this function for importing pictures.
Finally, use the self.plays的append function to append the images to the list.

Next, we need to use the update function in the blit function to draw the pictures of the three little boys in turn. We only need to replace the self.image self.plays[self.image_index] It's ok!

 def update(self):
        screen.blit(self.plays[self.image_index], self.rect)

A new variable is used here self.image_index , so we need to assign it to 0 in the init function.

 self.image_index = 0

Let's run the program and see the effect. It can be seen that there is no change in the little boy's feet!

What's going on? Because image_index is always 0, the display is always player0.png this picture.

We also need to let the variable image_index keep taking the three values of 0, 1, and 2.
So, we need to write a function that updates the image_index variable:

 def update_image_index(self):
    self.image_change_time += 1
    if self.image_change_time > 2:
        self.image_change_time = 0
        self.image_index = (self.image_index + 1) % 3

A new variable is used above, self.image_change_time , so in the init function, we assign image_change_time to 0:

 self.image_change_time = 0

Finally, we add a line of code to the ---ab97b85c3e80ed0343624fe61b548905 update function to call the update_image_index function.

 def update(self):
    self.update_image_index()
    screen.blit(self.plays[self.image_index], self.rect)

Well, let's run the game and see the effect.

It can be seen that the little boy's feet have moved.
Interesting, what a sense of achievement!

Pay attention to cat sister, in the next episode, we will make mushrooms that fall from the sky!


猫姐_游戏编程
56 声望17 粉丝

😸猫姐,211硕士,定居成都的荆州人,5年IT生涯,教大家做益智小游戏,快乐学编程