0
Follow
0
View

How do i move a sprite that's animated

dx0218jay 注册会员
2023-01-25 03:47

To make your animated sprites move, I would recommend you use the built-in functions for pygame rectangles like the move function instead of updating the start_x on each iteration of the game loop:

https://www.pygame.org/docs/ref/rect.html#pygame.Rect.move_ip

Also you should make the Gunner object only once before the game loop and not re-create the same thing every iteration. Eventually your game will start to lag because of this once you add more to the game.

For transitioning between animations, I would highly recommend that you start with making only one instance of your Gunner object, and it contains all of the animations. Instead of two gunner objects where each gunner has only 1 set of images. For example you can have your Gunner.sprites be structured like

self.sprites = {
    "running" : [get_image(bg_run_sheet, i, 48, 48, 4, 'Black') for i in range(bg_run_frames)],
    "idle" :  [get_image(bg_idle_sheet , i, 48, 48, 4, 'Black') for i in range(bg_idle_frames)]
}

Then you can keep track of the gunner's state in a variable, and key into this dict depending on your state. To manage the state, in the gunner's update function if there's a KEYDOWN event, you run, KEYUP, you go idle etc