Tried to make it faster but now it doesnt run.

This commit is contained in:
The Wobbler 2024-11-08 16:35:24 +01:00
parent aba7f137fb
commit e0415d31e8

View file

@ -103,6 +103,7 @@ class FallingSand:
setattr(self.settings, "save", lambda: save_dataclass_json(self.settings, "settings.json")) setattr(self.settings, "save", lambda: save_dataclass_json(self.settings, "settings.json"))
self.fps = self.settings.fps self.fps = self.settings.fps
self.sand_particles = []
self.falling_sand_particles = [] self.falling_sand_particles = []
self.rainbow_data = [[255, 40, 40], 0] self.rainbow_data = [[255, 40, 40], 0]
self.rainbow_steps = [(1, True), (0, False), (2, True), (1, False), (0, True), (2, False)] self.rainbow_steps = [(1, True), (0, False), (2, True), (1, False), (0, True), (2, False)]
@ -152,8 +153,7 @@ class FallingSand:
if not self.running: if not self.running:
return return
for particle in self.falling_sand_particles: self.update_sand_particles()
particle.update()
self.matrix.close() self.matrix.close()
@ -197,6 +197,74 @@ class FallingSand:
self.sand_surface.blit(old_sand_surface, (0, 0)) self.sand_surface.blit(old_sand_surface, (0, 0))
self.matrix = pygame.PixelArray(self.sand_surface) self.matrix = pygame.PixelArray(self.sand_surface)
def update_sand_particles(self):
i = 0
for particle in self.falling_sand_particles:
old_pos, color, not_moving = particle
ox, oy = old_pos
if oy >= self.sand_surface.get_height() - 4:
self.falling_sand_particles.remove(particle)
break
print(len(self.falling_sand_particles))
x, y = old_pos
if self.matrix[ox, oy + 1] == self.sand_surface.map_rgb(self.gray):
y += 1
elif (
self.matrix[ox - 1, oy + 2] == self.sand_surface.map_rgb(self.gray)
and self.matrix[ox + 1, oy + 2] == self.sand_surface.map_rgb(self.gray)
):
if true_false_random():
x -= 1
y += 2
else:
x += 1
y += 2
elif self.matrix[ox - 1, oy + 2] == self.sand_surface.map_rgb(self.gray):
x -= 1
y += 2
elif self.matrix[ox + 1, oy + 2] == self.sand_surface.map_rgb(self.gray):
x += 1
y += 2
else:
if not_moving == 32:
if self.mouse_pressed[0]:
not_moving = 0
else:
self.falling_sand_particles.remove(particle)
break
else:
if self.mouse_pressed[0]:
not_moving = 0
else:
not_moving += 1
break
not_moving = 0
pos = (x, y)
self.falling_sand_particles.pop(i)
self.falling_sand_particles.append((pos, color, not_moving))
self.matrix[ox, oy] = self.gray
self.matrix[x, y] = color
i += 1
def spawn_sand(self, position): def spawn_sand(self, position):
for ax in range(-8, 9): for ax in range(-8, 9):
color = self.rainbow() color = self.rainbow()
@ -207,7 +275,7 @@ class FallingSand:
by += ay by += ay
if self.matrix[bx, by] == self.sand_surface.map_rgb(self.gray) and bx % 2 == 0 and by % 2 == 0: 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), color)) self.falling_sand_particles.append(((bx, by), color, 0)) # pos, color, not_moving
def rainbow(self): def rainbow(self):
color = self.rainbow_data[0] color = self.rainbow_data[0]