Home » Python » How can I make moving objects starting locations a certain distance apart? Using python pygame-Exceptionshub

How can I make moving objects starting locations a certain distance apart? Using python pygame-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I am wanting the 3 objects I have created to move across the screen from right to left. Each one of their starting locations spaced out by 90 pixels from one another.

Here is my class I have created and the for loop which creates them onto the screen.

class MovingObj(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((30, 30))
        self.image.fill(RED)
        self.rect = self.image.get_rect()
        self.rect.x = random.randrange(WIDTH)
        self.rect.y = HEIGHT / 2
        self.speedx = -4

    def update(self):
        self.rect.x += self.speedx
        if self.rect.x <= 0:
            self.rect.x = 480
            self.rect.y = HEIGHT / 2
            self.speedx = -4

all_sprites = pygame.sprite.Group()
movingobj = pygame.sprite.Group()

for i in range (3):
    obj = MovingObj()
    all_sprites.add(obj)
    movingobj.add(obj)

running = True
while running:

    clock.tick(FPS)

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            running = False

    all_sprites.update()
    screen.fill(BLACK)
    all_sprites.draw(screen)
    pygame.display.flip()

pygame.quit()
quit()

Right now I have self.rect.x = random.randrange(WIDTH)
I’m trying to figure out how to have the first one spawn at self.rect.x = 45
Then, have the other 2 spawn 90 pixels apart from one another.
Is there any way to do this within the same class and how I have it set up?

How to&Answers:

You have 2 possibilities:

Either add an class attributes next_x = 45, which is used to set the x position of the object. The attribute has to be incremented by 90, after it is used to set the location of an object:

class MovingObj(pygame.sprite.Sprite):

    next_x = 45 

    def __init__(self, x):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((30, 30))
        self.image.fill(RED)
        self.rect = self.image.get_rect()

        self.rect.x = MovingObj.next_x
        MovingObj.next_x += 90 

        self.rect.y = HEIGHT / 2
        self.speedx = -4

Or add an argument to the constructor of the class and compute the position when creating the instances of the objects in the loop.

Add an argument x to the class MovingObj:

class MovingObj(pygame.sprite.Sprite):
    def __init__(self, x):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((30, 30))
        self.image.fill(RED)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = HEIGHT / 2
        self.speedx = -4

Compute the position in the loop (45 + i*90):

for i in range (3):
    obj = MovingObj(45 + i*90)
    all_sprites.add(obj)
    movingobj.add(obj)