Square not moving

44 Views Asked by At

I'm trying to make a square move on the screen. I planned on doing this by adding int 50 to the x-coordinate variable of my square object:

while run:
    pygame.time.delay(500)
    square = Square("crimson",x,200)
    x+=50

However, it only elongates the square horizontally in every iteration. Below is the full code:

import pygame

pygame.init()
screen = pygame.display.set_mode((500,500))

class Square(pygame.sprite.Sprite):
    def __init__(self,col,x,y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50,50))
        self.image.fill(col)
        self.rect = self.image.get_rect()
        self.rect.center = (x,y)

x = 200

squares = pygame.sprite.Group()
run = True
while run:
    pygame.time.delay(500)
    square = Square("crimson",x,200)
    x+=50
    screen.fill("cyan")
    squares.add(square)
    squares.update()
    squares.draw(screen)
    pygame.display.flip()

I tried switching up the orders of .fill() and .draw() but the outcome is consistently the same. Should not screen.fill("cyan") erase everything on the screen and drawing afterwards make the illusion of a square moving?

2

There are 2 best solutions below

0
Jeff Booth On

On each iteration, your main loop adds a sprite to your sprite group squares that is 50 pixels to the right of the last sprite.

Therefore, you don't have a single elongated rectangle, but rather multiple overlapping squares. Each square is 50 pixels to the right of its neighbor.

I think you meant to move your square instead. That means you need to:

  1. Add an update() method to class Square that adds 50 pixels to the x-coordinate of a Square object's rectangle when called.
  2. Add your square to the sprite group once.

Here is an example, tested with Python 3.10.5 and Pygame 2.5.2 on Windows 10:

import pygame

pygame.init()
screen = pygame.display.set_mode((500,500))

class Square(pygame.sprite.Sprite):
    def __init__(self,col,x,y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50,50))
        self.image.fill(col)
        self.rect = self.image.get_rect()
        self.rect.center = (x,y)

    def update(self):
        # move the rectangle in place (ip) by adding
        # 50 pixels to its x-coordinate.
        self.rect.move_ip(50, 0)

x = 200

squares = pygame.sprite.Group()
run = True

# Initialize your square *once*
square = Square("crimson",x,200)

# Add your square to the list *once*.
squares.add(square)

while run:
    # Make sure we can exit the game
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    pygame.time.delay(500)
    screen.fill("cyan")    
    squares.update() # calls update() on all sprites in the group
    squares.draw(screen)
    pygame.display.flip()
0
Dan Nagle On

The pygame.Rect.move_ip method is used to move an instance of pygame.Rect. Your code is creating a sequence of squares and not moving a single square.

import pygame

pygame.init()
screen = pygame.display.set_mode((500, 500))

class Square(pygame.sprite.Sprite):
    def __init__(self, col, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50, 50))
        self.image.fill(col)
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)


squares = pygame.sprite.Group()
x = 0
# Create a square
square = Square("crimson", x, 200)
squares.add(square)
run = True
while run:
    pygame.time.delay(500)
    x += 50
    # Move the square
    square.rect.move_ip(x, 0)
    screen.fill("cyan")
    squares.update()
    squares.draw(screen)
    pygame.display.flip()