[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Object not moving!
Three things:
1) The line should be: screen.blit(Guy.image, Guy.rect.topleft)
If you look at the pygame documentation for the blit() method here: http://www.pygame.org/docs/ref/surface.html#Surface.blit
The first parameter to blit() is the pygame.Surface of the image to draw (you have this right), but the second one should be a tuple of x, y coordinates for the top left on the screen Surface that Guy.image gets drawn. That's why it needs to be Guy.rect.topleft, not Guy.rect. The third parameter is a rect object that shows what section of Guy.image is blitted to screen. You are drawing the entire Guy.image image, so you don't have to pass anything for the third one (it's the entire image by default). Passing Guy.rect is the exact same thing as specifying the full image, so you don't have to do anything.
Basically, it was passing the pygame.Rect object Guy.rect instead of the tuple of two ints Guy.rect.topleft that was causing the problem.
2) Change the indentation of the .blit() and .update() lines. They only need to be inside the while True: loop, not the for loop.
3) Remember to fill the screen image with the background color, or else you will keep drawing the good guy image over and over on the screen. Add:
screen.fill( (0,0,0) ) to the start of the while True loop. (If you don't, you'll see what I mean by "drawing the good guy image over and over on the screen.)
-Al