how do i make an image move?

送分小仙女□ 提交于 2021-01-29 09:53:55

问题


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()

回答1:


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
    # ...


来源:https://stackoverflow.com/questions/62785495/how-do-i-make-an-image-move

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!