问题
I've been suck on my Flappy Bird clone. If you don't know the game, there is an animation that happens when the bird fly up.
Here is a general idea of how I tried to do an animation: self.x and self.y refer to the position of the photo
Here is my code:
def move_up_animation(self):
#list of bird photos to animate
animation_list = ['1.tiff','2.tiff','3.tiff','4.tiff','5.tiff']
for i in range(len(animation_list)):
if self.y - 1 > 0: # checks if the bird is within the frame
self.y = self.y - 1 #changes the bird's, allowing the bird to fly up
self.image = pygame.image.load(animation[i])
self.display_image()
I tried time.sleep(1) but it doesn't work.
I have no idea how this code works:
for i in range(5):
print(i)
time.sleep(1)
回答1:
"I have no idea how this code works":
for i in range(5):
print(i)
time.sleep(1)"
It works like that: for will see if it's a true or false variable I on the range (0, 5), not 5. If it is true will run the print command.
time.sleep I don't know how it works.
To move the bird you need to add and decrease the y axis, for both flying and gravity. I hope you know to do do it (like pressing space it flies, no pressing it falls).
Instead of using for you can use if.
if K_SPACE:
y -= 12
It will make the y-axis of the bird increase by 12. I hope that will be like the original jump game. To make it smooth you can use .tick() module inside the for, but I think it is really unnecessary.
来源:https://stackoverflow.com/questions/34756671/animation-in-with-a-list-of-images-in-pygame