0

I want to create a few figures which only have difference in coordinates, but same in other properties. But my code allows me to control and move only last added to array figure, so what shoud I do to be able to move every figure, can you explain?

class car(pygame.sprite.Sprite):
def __init__(self,x,y):
    pygame.sprite.Sprite.__init__(self)
    self.image = pygame.image.load(os.path.join(IMG, "car.png")).convert()
    self.rect = self.image.get_rect()
    self.rect.x = x
    self.rect.y = y
    self.image.set_colorkey(BLACK)  # made bg transparent
    self.image = pygame.transform.scale(self.image, (70, 70))

x = 30
for i in range(8):
    cars = car(x, 35)
    figures.add(cars)
    x += 75
if (cars.rect.y//75)+1 == y and cars.rect.x//75 == x:
       cars.rect.y += 75
This4fun
  • 15
  • 6

2 Answers2

0

you have to keep track of new cars you are creating using an array then you can use the array to control every car you have created .

Ankur Jyoti Phukan
  • 785
  • 3
  • 12
  • 20
0

Class-Names should be uppercase!

class Car(pygame.sprite.Sprite):
    ...

Store every car in the cars list. Why is figures a array? Maybe a pygame.sprite.Group would be better! So you can call the .update() and .draw() method for every sprite in figures.

x = 30
cars = []
for i in range(8):
    cars.append(Car(x, 35))
    x += 75
figures.add(*cars)

So you can move your cars

for car in cars:
    if ...:  # your conditions
        car.rect.y += 75
Jan Meisel
  • 259
  • 3
  • 11
  • Thank you a lot, but what means asterix before "cars", when I removed it from, all figure began to move as I want, if asterix stay so figures don't move, but copy only for one step. – This4fun Mar 09 '17 at 20:04
  • I don't know the type of figures. So I assume it is a pygame.sprite.Group. If you add a list of sprites to a pygame.sprite.Group you have to "unpack" the list. This is what the * means. For other containers this is not necessary. – Jan Meisel Mar 10 '17 at 06:51
  • Here an example: http://stackoverflow.com/questions/3480184/unpack-a-list-in-python – Jan Meisel Mar 10 '17 at 06:57