问题
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