我如何使图像移动? [英] how do i make an image move?
本文介绍了我如何使图像移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要图像pepega";移动/跟随终点.我不知道为什么这不起作用.我看到了佩佩加,但他没有走向终点.我之前在一个不同的项目中完成了这段代码并且它起作用了.我可能遗漏了一些东西,但我不知道是什么.
i need the image "pepega" to move/follow the finish. i dont know why this isn't working though. i see pepega but he doesn't move towards the finish. I've done this code before in a different project and it worked. i probably left out something but I don't know what.
#make the finish position
finishX = 425
finishY = 200
#make the pepega position
pepegaX = 60
pepegaY = 175
pepega = pygame.transform.smoothscale(pygame.image.load("C://Users//me//Desktop//python//pygame//resources//charactors//Pepega.png"),(50,50))
screen.blit(pepega, (round(pepegaX),round(pepegaY)))
if pepegaX < finishX:
pepegaX += .05
else:
pepegaX -= .05
if pepegaY < finishY:
pepegaY += .05
else:
pepegaY -= .05
pygame.display.update()
推荐答案
如果问题中的代码是主更新循环的内容,那么就是重新设置pepega"的位置;回到 (60,175)
每次迭代.
If the code in the question is the content of the main update loop, then it is re-setting the positions of "pepega" back to (60,175)
every iteration.
#make the pepega position
pepegaX = 60 # <-- HERE
pepegaY = 175
pepega = pygame.transform.smoothscale(pygame.image.load("C://Users//me//Desktop//python//pygame//resources//charactors//Pepega.png"),(50,50))
screen.blit(pepega, (round(pepegaX),round(pepegaY)))
if pepegaX < finishX:
pepegaX += .05
else:
pepegaX -= .05
这个初始化代码应该移到循环之外:
This initialisation code should be moved outside of the loop:
# Make the pepega position
pepegaX = 60
pepegaY = 175
pepega = pygame.transform.smoothscale(pygame.image.load("C://Users//me//Desktop//python//pygame//resources//charactors//Pepega.png"),(50,50))
while running:
# Handle user-input
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
running = False
# Update the screen
screen.blit(pepega, (round(pepegaX),round(pepegaY)))
if pepegaX < finishX:
pepegaX += .05
else:
pepegaX -= .05
# ...
这篇关于我如何使图像移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文