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
This commit is contained in:
Stan44 2024-12-26 05:21:13 -06:00
parent 021036f5f4
commit 1a6ef4f3c4
5 changed files with 29 additions and 31 deletions

View File

@ -1,6 +0,0 @@
#File Name: imports.py
import pygame
import json
import random
import time

View File

@ -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

View File

@ -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()

15
settings.py Normal file
View File

@ -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()

17
sim.py
View File

@ -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)