From 1a6ef4f3c4c5b28fd2f6fa7aa570c98e41fc9ac5 Mon Sep 17 00:00:00 2001 From: Stan44 Date: Thu, 26 Dec 2024 05:21:13 -0600 Subject: [PATCH] changed imports.py it is now settings.py settings handles loading the json file for the particles and imports, and potentially other things such as actual settings that are saveable and more --- imports.py | 6 ------ rendering.py | 19 ++++++++----------- sandpypi.py | 3 ++- settings.py | 15 +++++++++++++++ sim.py | 17 ++++------------- 5 files changed, 29 insertions(+), 31 deletions(-) delete mode 100644 imports.py create mode 100644 settings.py diff --git a/imports.py b/imports.py deleted file mode 100644 index 317f515..0000000 --- a/imports.py +++ /dev/null @@ -1,6 +0,0 @@ -#File Name: imports.py - -import pygame -import json -import random -import time diff --git a/rendering.py b/rendering.py index a275cfc..4bb2cdc 100644 --- a/rendering.py +++ b/rendering.py @@ -1,16 +1,9 @@ #File Name: rendering.py - -from imports import pygame, json, random - - -try: - with open('sandpypi/particles.json') as f: - particle_properties = json.load(f) -except (FileNotFoundError, json.JSONDecodeError): - print("Error loading particles.json") - particle_properties = {} + +from settings import pygame, json, random, particle_properties class Rendering: + def __init__(self, width, height): self.screen = pygame.display.set_mode((width, height)) self.background = pygame.Surface((width, height)) @@ -48,6 +41,7 @@ class Rendering: self.category_buttons = {} self.setup_category_menu() + def setup_category_menu(self): # Category buttons at the top x_offset = self.width - 350 @@ -57,6 +51,7 @@ class Rendering: self.category_buttons[category] = button_rect x_offset += 90 + def load_buttons(self): x_offset = 10 y_offset = 10 @@ -178,7 +173,6 @@ class Rendering: y_offset += 30 # Stack buttons vertically - # Draw clear screen button self.clear_screen_button = pygame.Rect(x_offset, y_offset + 10, 80, 25) pygame.draw.rect(self.screen, (255, 0, 0), self.clear_screen_button) @@ -186,10 +180,12 @@ class Rendering: label = font.render("Clear", True, (255, 255, 255)) self.screen.blit(label, (self.clear_screen_button.x + 5, self.clear_screen_button.y + 5)) + def render_brush_curser(self, x, y, radius): # Draw a circle cursor for brushsize pygame.draw.circle(self.screen, (255, 255, 255), (x, y), radius) + def draw_brush_size_slider(self, brush_size): # Draw the slider for brush size pygame.draw.rect(self.screen, (255, 255, 255), (500, 10, 100, 20)) @@ -198,6 +194,7 @@ class Rendering: label = pygame.font.SysFont(None, 24).render(f"Brush Size: {brush_size}", True, (255, 255, 255)) self.screen.blit(label, (500, 10)) + def clear_screen(self, sim): # Store current particle type current_type = sim.current_particle_type diff --git a/sandpypi.py b/sandpypi.py index 5eee47e..aaeac65 100644 --- a/sandpypi.py +++ b/sandpypi.py @@ -5,7 +5,7 @@ # This is my most functional system for falling sand in python yet i took some things i learned in JS. # This needs further optimizations to core performance sections. -from imports import pygame +from settings import pygame from rendering import Rendering from sim import Simulation @@ -98,5 +98,6 @@ def main(): pygame.display.flip() pygame.quit() + if __name__ == "__main__": main() diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..ed54030 --- /dev/null +++ b/settings.py @@ -0,0 +1,15 @@ +import pygame +import json +import random +import time + +def load_particle_properties(): + try: + with open('particles.json') as f: + return json.load(f) + except (FileNotFoundError, json.JSONDecodeError): + print("Error loading particles.json") + return {} + +# Load particle properties once when module is imported +particle_properties = load_particle_properties() diff --git a/sim.py b/sim.py index dcd5aef..fcb614c 100644 --- a/sim.py +++ b/sim.py @@ -2,19 +2,9 @@ #Load the imports. Pygame is what makes this even work and so simple may consider other engines for performance depends on learning curve. -from imports import json, random, time +from settings import json, random, time, particle_properties # Load particle properties from json so we know what particles we got and how they should be simulated. - -try: - with open('sandpypi/particles.json') as f: - particle_properties = json.load(f) - -except (FileNotFoundError, json.JSONDecodeError): - print("Error loading particles.json") - particle_properties = {} - - class Particle: def __init__(self, position, velocity, mass, particle_type, properties, temperature=20): self.position = position # (x, y) @@ -109,7 +99,6 @@ class Simulation: self.transform_particle(x, y, particle.solidify) - def handle_particle_interactions(self, dt): """Handle interactions between different particle types""" for x, y in list(self.active_particles): @@ -183,6 +172,7 @@ class Simulation: self.active_particles.add((new_x, new_y)) self.active_particles.discard((x, y)) + def temperature(self, dt): """Handle temperature changes and state transitions""" for x, y in list(self.active_particles): @@ -361,6 +351,7 @@ class Simulation: self.particles[grid_x][grid_y] = new_particle self.active_particles.add((grid_x, grid_y)) + def update_spatial_grid(self): """Update spatial grid for optimized collision detection""" self.spatial_grid.clear() @@ -455,7 +446,6 @@ class Simulation: self.active_particles.add((x, y)) - def apply_physics(self, dt): """Handle all physics effects""" new_active_particles = set() @@ -568,6 +558,7 @@ class Simulation: self.active_particles = new_active_particles + def clear_particles_circle(self, center_x, center_y): """Clear particles in a circle around the given point based on brush size""" brush_size = int(self.brush_size)