Get a sprites position so that the next sprite can be placed near it

Viewed 47

I am trying to build a simulator inside of Pygame that simulates grass being grown. The purpose of trying to get the position of a current spite (grass) is so that a new sprite (grass) can be added next to it.

First I create a class that gives the pieces of grass a position.

class Grass(pygame.sprite.Sprite):
def __init__(self, width, height, pos_x, pos_y, color):
    super().__init__()
    self.image = pygame.Surface([width, height])
    self.image.fill(color)
    self.rect = self.image.get_rect()
    self.rect.center = [pos_x, pos_y]

Then I add a piece of grass so that we can start the spawning process, I do this by creating a group.

grass_group = pygame.sprite.Group()
grass = Grass(20, 20, random.randrange(50, width - 50), random.randrange(50, height - 50), green)
grass_group.add(grass)
grass_group.draw(screen)

Then I want to create a new piece of grass every second next to the old bits of grass.

    if seconds <= (one_second + 100) and seconds >= (one_second - 100):
    one_second += 1000
    for i in range(len(grass_group)):
        for j in range(len(grass_group)):
            j.x = 
            j.y =
            i.x = random.choice(j.x - 20, j.x, j.x + 20)
            i.y = random.choice(j.y - 20, j.y, j.y + 20)
            i = Grass(20, 20, i.x, i.y, green)
            grass_group.add(i)
            grass_group.draw(screen)
            pygame.display.flip()

So I need to find out what the position of all of the old bits of grass is in order to create the new bit of grass near it.

1 Answers

The peace of grass should be aligned to the grid. Set the step argument when you create a random position:

grass = Grass(20, 20, 
    random.randrange(50, width - 50, 20),
    random.randrange(50, height - 50, 20),
    green)
grass_group.add(grass)

First you have to find all the possible grass positions. Implement the following algorithm:

  • Iterate through the possible grass positions in a nested loop.
  • Test if a grass is at a position by pygame.Rect.collidepoint().
  • If a grass is at the position, then go t o the next position.
  • Test if a grass is next to the position. If a grass is found then add the position to a list.
def isGrass(x, y):
    return any(g for g in grass_group.sprites if g.rect.collidepoint(x, y))

def isNextToGrass(x, y):
    neighbours = [
        (x-20, y-20), (x, y-20), (x+20, y-20), (x-20, y),
        (x+20, y), (x-20, y+20), (x, y+20), (x+20, y+20)]
    return any(pos for pos in neighbours if isGrass(*pos))

def findGrassPositions():
    poslist = []
    for x in range(50, width - 50, 20):
        for y in range(50, height - 50, 20):
            if not isGrass(x, y):
                if isNextToGrass(x, y):
                    poslist.append((x, y))
    return poslist

Use the algorithm to find all the possible position for a grass and take a random.choice from the list:

if seconds <= (one_second + 100) and seconds >= (one_second - 100):
    one_second += 1000
    allposlist = findGrassPositions()
    if allposlist:
        new_x, new_y = random.choice(allposlist)
        new_grass = Grass(20, 20, new_x, new_y, green)
        grass_group.add(new_grass)
        

grass_group.draw(screen)
pygame.display.flip()
Related