[英]Character Animation in python not working
I am busy making a game in python using tkinter, time and PIL, and I have a character that I want to animate very simply, but when I try this code, it doesn't do anything then jumps to the last image, can someone please tell me why this is?我正忙于使用 tkinter、时间和 PIL 在 python 中制作游戏,并且我有一个我想非常简单地制作动画的角色,但是当我尝试这段代码时,它什么也没做,然后跳到最后一张图像,有人可以吗请告诉我这是为什么?
def move_char(event):
if event.keysym == "w":
character = ch1
c.delete(character)
character = ch2_1
c.create_image(725, 450, image = character)
sleep(0.2)
c.delete(character)
character = ch2
c.create_image(725, 450, image = character)
sleep(0.2)
c.delete(character)
character = ch2_2
c.create_image(725, 450, image = character)
sleep(0.2)
c.delete(character)
character = ch2
c.create_image(725, 450, image = character)
Appreciate any help感谢任何帮助
Since character
is a reference to PhotoImage
, c.delete(character)
will not delete the image in the canvas as you wish.由于character
是对PhotoImage
的引用,因此c.delete(character)
不会如您所愿删除 canvas 中的图像。 You can use tag
option of c.create_image(...)
to identify the image and use the tag in c.delete()
.您可以使用c.create_image(...)
的tag
选项来识别图像并使用c.delete()
中的标签。 Also in order to see the animation effect, you need to call c.update()
after c.create_image(...)
.另外为了看到 animation 效果,您需要在c.create_image(...)
之后调用c.update()
) 。
Below is an updated move_char(...)
:以下是更新后的move_char(...)
:
def move_char(event):
if event.keysym == "w":
# loop through the images
for ch in (ch2_1, ch2, ch2_2, ch2):
c.delete('char-image') # remove current image
c.create_image(725, 450, image=ch, tag='char-image') # use tag to identify image
c.update() # make the change effective
sleep(0.2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.