Question

Created first pygame program to move a rectangle around the screen. Can't figure out why the shape is not actually moving. I had it working before with a simple

  shape = shape.move(speed)

But now with keyboard inputs the shape does not move. I used some print statements to make sure that the function checkKeys is rendering my keypresses (and the changes in speed that they make), and it is. However the shape still does not move.

import sys, pygame
pygame.init()

size = width, height = 320, 240
black = (0, 0, 0)
red = (255, 0, 0)
pygame.display.set_caption("Object Move Test")
clock = pygame.time.Clock()

def main():
    screen = pygame.display.set_mode(size)
    shape = pygame.draw.rect(screen, (255, 0, 0), (200, 100, 10, 10,))
    ballrect = pygame.Surface((10,10), 0, shape)

    def checkKeys(speedY, speedX, shape):
        key = pygame.key
        pygame.event.pump()
        if key.get_pressed()[pygame.K_UP] and speedY < 1:
            speedY = speedY - 1
            #print 'UP'
        if key.get_pressed()[pygame.K_DOWN] and speedY > -1:
            speedY = speedY - 1
            #print 'DOWN'
        if key.get_pressed()[pygame.K_LEFT] and speedX > -1:
            speedX = speedX - 1
            #print 'LEFT'
        if key.get_pressed()[pygame.K_RIGHT] and speedX < 1:
            speedX = speedX + 1
            #print speedX
        speed = [speedX, speedY]
        #print speed
        moveShape(speed, shape)

    def moveShape(speed, shape):
        print speed
        shape = shape.move(speed)
        if shape.left < 0 or shape.right > width:
            speed[0] = -speed[0]
        if shape.top < 0 or shape.bottom > height:
            speed[1] = -speed[1]

    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        speedX, speedY = (0, )*2 
        speed = [speedX, speedY]        
        screen.fill((255,255,255))
        clock.tick(20)
        checkKeys(speedX, speedY, shape)

        screen.blit(ballrect, shape)
        pygame.display.flip()

if __name__ == '__main__':
    main()
Was it helpful?

Solution

I'm not very familiar with pygame, so this is just a guess. However, the thing that looks most like a problem to me is this line (inside the definition of moveShape):

shape = shape.move(speed)

The reason this is a problem is that shape is the name of a local variable within moveShape, but I'm guessing that you intend by the equals assignment to update the nonlocal shape object (the one declared at the top of your main() definition. When you call shape.move(speed), the return value is a new pygame Rect object, which gets assigned the name shape. But this assignment happens inside the moveShape function, and so the update is not visible outside that scope.

It looks like you can replace this line with an "inplace" version (http://www.pygame.org/docs/ref/rect.html#pygame.Rect.move_ip) that will change the object without returning a new one:

shape.move_ip(speed)

Maybe that will work for you?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top