diff --git a/falling_sand.py b/falling_sand.py index 1af02eb..65342c3 100644 --- a/falling_sand.py +++ b/falling_sand.py @@ -18,10 +18,10 @@ class Settings: class FallingSandParticle: - def __init__(self, app, start_pos: tuple): + def __init__(self, app, start_pos: tuple, color: tuple=(50, 50, 200)): self.app = app self.pos = start_pos - self.color = (50, 50, 200) + self.color = color self.not_moving = 0 x, y = start_pos @@ -62,11 +62,21 @@ class FallingSandParticle: else: if self.not_moving == 32: - self.app.falling_sand_particles.remove(self) + if self.app.mouse_pressed[0]: + self.not_moving = 0 + + else: + self.app.falling_sand_particles.remove(self) + return else: - self.not_moving += 1 + if self.app.mouse_pressed[0]: + self.not_moving = 0 + + else: + self.not_moving += 1 + return self.not_moving = 0 @@ -93,8 +103,9 @@ class FallingSand: setattr(self.settings, "save", lambda: save_dataclass_json(self.settings, "settings.json")) self.fps = self.settings.fps - self.falling_sand_particles = [] + self.rainbow_data = [[255, 40, 40], 0] + self.rainbow_steps = [(1, True), (0, False), (2, True), (1, False), (0, True), (2, False)] # colors self.gray = (20, 20, 20) @@ -102,6 +113,8 @@ class FallingSand: # pygame objects self.clock = pygame.time.Clock() self.mouse = pygame.mouse + self.mouse_pressed = self.mouse.get_pressed() + self.mouse_pos = self.mouse.get_pos() self.sand_surface = pygame.Surface(self.screen.get_size()) self.sand_surface.fill(self.gray) self.matrix = pygame.PixelArray(self.sand_surface) @@ -129,10 +142,11 @@ class FallingSand: self.matrix = pygame.PixelArray(self.sand_surface) - mouse_pressed = self.mouse.get_pressed() - mouse_pos = self.mouse.get_pos() - if mouse_pressed[0]: - self.spawn_sand(mouse_pos) + self.mouse_pressed = self.mouse.get_pressed() + self.mouse_pos = self.mouse.get_pos() + + if self.mouse_pressed[0]: + self.spawn_sand(self.mouse_pos) self.get_events() if not self.running: @@ -175,13 +189,46 @@ class FallingSand: x, y = position for ax in range(-8, 9): + color = self.rainbow() + for ay in range(-8, 9): bx, by = position bx += ax by += ay if self.matrix[bx, by] == self.sand_surface.map_rgb(self.gray) and bx % 2 == 0 and by % 2 == 0: - self.falling_sand_particles.append(FallingSandParticle(self, (bx, by))) + self.falling_sand_particles.append(FallingSandParticle(self, (bx, by), color)) + + def rainbow(self): + color = self.rainbow_data[0] + step_index = self.rainbow_data[1] + step = self.rainbow_steps[step_index] + + if step[1]: + if color[step[0]] == 255: + if step_index == 5: + step_index = 0 + + else: + step_index += 1 + + else: + color[step[0]] += 1 + + else: + if color[step[0]] == 40: + if step_index == 5: + step_index = 0 + + else: + step_index += 1 + + else: + color[step[0]] -= 1 + + self.rainbow_data = [color, step_index] + + return tuple(color) if __name__ == "__main__":