Reputation: 375
I randomly share pictures of houses on the screen. Then I drive a car across the screen.When the car hits a house, the image of that house should be replaced with a modified copy of that house. I distribute the images with the following function
while len(land) < anzahl_gegenstaende:
ii = len(land)
img = pygame.image.load(f"Bilder/Gegenstaende/geg{ii}.png").convert_alpha()
zb,zh = img.get_rect().size
scale_hoehe =100
scale_breite = int(100 * zb / zh)
img = pygame.transform.scale(img,(scale_breite,scale_hoehe))
m = Landschaft(img)
if not pygame.sprite.spritecollide(m, land, False):
land.add(m)
I check the collision car with house
treffer = pygame.sprite.spritecollide( auto, land, False, pygame.sprite.collide_mask)
for bumbs in treffer:
.... How do I then get the house number or picture number of the house that collided so that I can replace the picture with a slightly modified copy?
Upvotes: 1
Views: 42
Reputation: 211125
Store the image number in an attribute of the Sprite class:
class Landschaft(pygame.sprite.Sprite):
def __init__(self, img, ii):
super().__init__()
self.img = img
self.ii = ii
# [...]
while len(land) < anzahl_gegenstaende:
ii = len(land)
img = pygame.image.load(f"Bilder/Gegenstaende/geg{ii}.png").convert_alpha()
# [...]
m = Landschaft(img, ii)
if not pygame.sprite.spritecollide(m, land, False):
land.add(m)
treffer = pygame.sprite.spritecollide(auto, land, False, pygame.sprite.collide_mask)
for bumbs in treffer:
ii = bomb.ii
# [...]
Upvotes: 1