简体   繁体   English

pygame中的精灵动画

[英]Sprite animation in pygame

I've a little problem. 我有点问题 I'm writting a program in Python with Pygame, and I have a player moving with key of the keyboard (Right, left, jump ...). 我正在用Pygame用Python编写一个程序,并且有一个玩家在用键盘的键(向右,向左,跳转...)移动。 But, I would like to create an animation when my player "walks", because I have 6 image (PNG) to do this, but I can't do it. 但是,我想在播放机“行走”时创建动画,因为我有6张图片(PNG)可以做到这一点,但我做不到。 This is my program : 这是我的程序:

self.image = pygame.image.load(image_perso_droite).convert_alpha()

frame_1 = pygame.image.load("images/GIF/course_6.png").convert_alpha()

frame_2 = pygame.image.load("images/GIF/course_5.png").convert_alpha()

frame_3 = pygame.image.load("images/GIF/course_4.png").convert_alpha()

frame_4 = pygame.image.load("images/GIF/course_3.png").convert_alpha()

frame_5 = pygame.image.load("images/GIF/course_2.png").convert_alpha()

frame_6 = pygame.image.load("images/GIF/course_1.png").convert_alpha()
self.frame_set = [frame_1, frame_2, frame_3, frame_4, frame_5, frame_6]
self.current_frame = 0
self.timer = time.clock()

self.image = self.frame_set[self.current_frame]

if droite:

    self.xvel = 8
    if time.clock() >= self.timer + .05 and self.onGround:
        self.current_frame = 1
        self.image = self.frame_set[self.current_frame]

I try also with pyganim, without success. 我也尝试使用pyganim,但没有成功。 Thank you. 谢谢。

Assign values of clock.tick() for the player to alternate between the walking images. 为播放器分配clock.tick()的值,以在步行图像之间交替。

eg if you have two "walk images", you can use the function: 例如,如果您有两个“步行图像”,则可以使用以下功能:

def changeIMG(img):
    if img = IMG1:
        img = IMG2
    else:
        img = IMG1

... ...

clock.tick(20)
i += 1

if i % 5 == 0:
    changeIMG(currentIMG)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM