[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Object not moving!



Hi, Tanner.

A minor correction: blit(surface, rect) is allowed and has the same effect as blit(surface, rect.topleft). This is explained in the documentation for Surface.blit.

The real culprit, as Al indicated, is the third argument to blit, which essentially causes nothing to blit once the rect's position (e.g. topleft) moves outside the dimensions of the surface area. This can be a bit confusing, and the mistake is not at all obvious when using the same rect for the second and third argument so don't feel bad. :)

Gumm


On Thu, Dec 6, 2012 at 9:54 PM, Al Sweigart <al@xxxxxxxxxxxxxxxxxxxx> wrote:
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



On Thu, Dec 6, 2012 at 7:56 PM, Elias Benevedes <benevedeselias@xxxxxxxxx> wrote:
Explicit, please? I'm not very good at riddles...


On Thu, Dec 6, 2012 at 7:31 PM, Tanner Johnson <tbjohnson42@xxxxxxxxx> wrote:
I'm the 'point me in the right direction' type, so here's a vague description of where to go to fix this bug. If you want a more explicit answer, let me know!

Take a look at the line where you're blitting the image and look at your inputs to that function. There's a problem there. Take a look at the blit documentation and see what you can do to fix it.

Once you resolve that, you'll see a second bug, but the answer to that one should be pretty easy to figure out.

Happy pygaming!


On Thu, Dec 6, 2012 at 6:59 PM, Elias Benevedes <benevedeselias@xxxxxxxxx> wrote:
Hello everyone! I was having a little bug in my program. I HAVE NO IDEA WHY! I put print statements everywhere to check to make sure everything was being executed. Here is my code:

import pygame, sys
from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((640,480))

class goodGuy(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('goodGuy.png')
        self.rect = self.image.get_rect()
    def up(self):
        print 'up'
        self.rect[1] -= 10
    def down(self):
        print 'down'
        self.rect[1] += 10
    def right(self):
        print 'right'
        self.rect[0] += 10
    def left(self):
        print 'left'
        self.rect[0] -= 10
Guy = goodGuy()

while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == K_UP:
                Guy.up()
                print 'Up'
            if event.key == K_DOWN:
                Guy.down()
                print 'Down'
            if event.key == K_RIGHT:
                Guy.right()
                print 'Right'
            if event.key == K_LEFT:
                Guy.left()
                print 'Left'
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
        screen.blit(Guy.image, Guy.rect, Guy.rect)
        pygame.display.update()

All that I want to happen is that for each respective arrow key, I want to move the image 10 pixels in that direction. Any help?


--
"The validity of internet quotes are getting sketchy nowadays"
-Abraham Lincoln





--
"The validity of internet quotes are getting sketchy nowadays"
-Abraham Lincoln