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.