diff --git a/code/__pycache__/sprites.cpython-310.pyc b/code/__pycache__/sprites.cpython-310.pyc index 4d785eb..13689d0 100644 Binary files a/code/__pycache__/sprites.cpython-310.pyc and b/code/__pycache__/sprites.cpython-310.pyc differ diff --git a/code/simulation.py b/code/simulation.py index e7d98b0..bfa1805 100644 --- a/code/simulation.py +++ b/code/simulation.py @@ -14,8 +14,32 @@ clock = pygame.time.Clock() player = pygame.sprite.GroupSingle() player.add(sprites.Player()) -top_street_boarder = pygame.Rect(0, 50, SCREEN_WIDTH, 4) +streets = pygame.sprite.Group() +street_1 = sprites.Streets(0, 100, SCREEN_WIDTH, 4) +street_2 = sprites.Streets(0, 200, SCREEN_WIDTH, 4) +streets.add(street_1) +streets.add(street_2) +lanterns = pygame.sprite.Group() +lantern_list = [] +detectors = pygame.sprite.Group() +detectors_list = [] +id = 1 + +for y_pos in range(50, 251, 200): + x_pos = 100 + while x_pos < SCREEN_WIDTH: + lantern = sprites.Lanterns(x_pos, y_pos, 25, 25, id) + lantern_list.append(lantern) + detector = sprites.Detectors(x_pos, y_pos, 133, 300, id) + detectors_list.append(detector) + x_pos += 150 + id += 1 + +for item in lantern_list: + lanterns.add(item) +for item in detectors_list: + detectors.add(item) if __name__ == "__main__": @@ -26,16 +50,27 @@ if __name__ == "__main__": run = False - screen.fill("Grey") + screen.fill("Grey") + detectors.draw(screen) + streets.draw(screen) + lanterns.draw(screen) player.draw(screen) player.update() - - #pygame.draw.rect(screen, (0, 0, 0), line) + for lantern in lanterns: + lantern.reset_light() - #if stickman_rect.colliderect(line): - # stickman_rect.y = 55 + # Check for collision and light up corresponding lantern + collisions = pygame.sprite.spritecollide(player.sprite, detectors, False) + for detector in collisions: + for lantern in lanterns: + if lantern.id == detector.id: + lantern.light_up() + if pygame.sprite.spritecollide(player.sprite, streets, False): + player.sprite.set_back() + + pygame.display.update() clock.tick(60) diff --git a/code/sprites.py b/code/sprites.py index 418e2e5..700af23 100644 --- a/code/sprites.py +++ b/code/sprites.py @@ -10,7 +10,9 @@ class Player(pygame.sprite.Sprite): self.player_direction = [player_right, player_left] self.image = self.player_direction[0] - self.rect = self.image.get_rect(topleft = (0, 55)) + self.rect = self.image.get_rect(center = (0, 150)) + + def player_inputs(self): key = pygame.key.get_pressed() @@ -25,5 +27,42 @@ class Player(pygame.sprite.Sprite): if key[pygame.K_w]: self.rect.y -= 3 + def get_last_position(self): + self.last_position = self.rect.topleft + + def set_back(self): + self.rect.topleft = self.last_position + def update(self): - self.player_inputs() \ No newline at end of file + self.get_last_position() + self.player_inputs() + + +class Streets(pygame.sprite.Sprite): + def __init__(self, left, top, width, height): + super().__init__() + self.image = pygame.Surface([width, height]) + self.image.fill((0, 0, 0)) + self.rect = self.image.get_rect(topleft=(left, top)) + +class Lanterns(pygame.sprite.Sprite): + def __init__(self, left, top, width, height, id): + super().__init__() + self.image = pygame.Surface([width, height]) + self.image.fill((0, 0, 0)) + self.rect = self.image.get_rect(center=(left, top)) + self.id = id + + def light_up(self): + self.image.fill((255, 255, 0)) + + def reset_light(self): + self.image.fill((0, 0, 0)) + +class Detectors(pygame.sprite.Sprite): + def __init__(self, left, top, width, height, id): + super().__init__() + self.image = pygame.Surface([width, height]) + self.image.fill("Grey") + self.rect = self.image.get_rect(center=(left, top)) + self.id = id