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:
parent
021036f5f4
commit
1a6ef4f3c4
@ -1,6 +0,0 @@
|
|||||||
#File Name: imports.py
|
|
||||||
|
|
||||||
import pygame
|
|
||||||
import json
|
|
||||||
import random
|
|
||||||
import time
|
|
||||||
19
rendering.py
19
rendering.py
@ -1,16 +1,9 @@
|
|||||||
#File Name: rendering.py
|
#File Name: rendering.py
|
||||||
|
|
||||||
from imports import pygame, json, random
|
from settings import pygame, json, random, particle_properties
|
||||||
|
|
||||||
|
|
||||||
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 Rendering:
|
class Rendering:
|
||||||
|
|
||||||
def __init__(self, width, height):
|
def __init__(self, width, height):
|
||||||
self.screen = pygame.display.set_mode((width, height))
|
self.screen = pygame.display.set_mode((width, height))
|
||||||
self.background = pygame.Surface((width, height))
|
self.background = pygame.Surface((width, height))
|
||||||
@ -48,6 +41,7 @@ class Rendering:
|
|||||||
self.category_buttons = {}
|
self.category_buttons = {}
|
||||||
self.setup_category_menu()
|
self.setup_category_menu()
|
||||||
|
|
||||||
|
|
||||||
def setup_category_menu(self):
|
def setup_category_menu(self):
|
||||||
# Category buttons at the top
|
# Category buttons at the top
|
||||||
x_offset = self.width - 350
|
x_offset = self.width - 350
|
||||||
@ -57,6 +51,7 @@ class Rendering:
|
|||||||
self.category_buttons[category] = button_rect
|
self.category_buttons[category] = button_rect
|
||||||
x_offset += 90
|
x_offset += 90
|
||||||
|
|
||||||
|
|
||||||
def load_buttons(self):
|
def load_buttons(self):
|
||||||
x_offset = 10
|
x_offset = 10
|
||||||
y_offset = 10
|
y_offset = 10
|
||||||
@ -178,7 +173,6 @@ class Rendering:
|
|||||||
|
|
||||||
y_offset += 30 # Stack buttons vertically
|
y_offset += 30 # Stack buttons vertically
|
||||||
|
|
||||||
|
|
||||||
# Draw clear screen button
|
# Draw clear screen button
|
||||||
self.clear_screen_button = pygame.Rect(x_offset, y_offset + 10, 80, 25)
|
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)
|
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))
|
label = font.render("Clear", True, (255, 255, 255))
|
||||||
self.screen.blit(label, (self.clear_screen_button.x + 5, self.clear_screen_button.y + 5))
|
self.screen.blit(label, (self.clear_screen_button.x + 5, self.clear_screen_button.y + 5))
|
||||||
|
|
||||||
|
|
||||||
def render_brush_curser(self, x, y, radius):
|
def render_brush_curser(self, x, y, radius):
|
||||||
# Draw a circle cursor for brushsize
|
# Draw a circle cursor for brushsize
|
||||||
pygame.draw.circle(self.screen, (255, 255, 255), (x, y), radius)
|
pygame.draw.circle(self.screen, (255, 255, 255), (x, y), radius)
|
||||||
|
|
||||||
|
|
||||||
def draw_brush_size_slider(self, brush_size):
|
def draw_brush_size_slider(self, brush_size):
|
||||||
# Draw the slider for brush size
|
# Draw the slider for brush size
|
||||||
pygame.draw.rect(self.screen, (255, 255, 255), (500, 10, 100, 20))
|
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))
|
label = pygame.font.SysFont(None, 24).render(f"Brush Size: {brush_size}", True, (255, 255, 255))
|
||||||
self.screen.blit(label, (500, 10))
|
self.screen.blit(label, (500, 10))
|
||||||
|
|
||||||
|
|
||||||
def clear_screen(self, sim):
|
def clear_screen(self, sim):
|
||||||
# Store current particle type
|
# Store current particle type
|
||||||
current_type = sim.current_particle_type
|
current_type = sim.current_particle_type
|
||||||
|
|||||||
@ -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 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.
|
# This needs further optimizations to core performance sections.
|
||||||
|
|
||||||
from imports import pygame
|
from settings import pygame
|
||||||
from rendering import Rendering
|
from rendering import Rendering
|
||||||
from sim import Simulation
|
from sim import Simulation
|
||||||
|
|
||||||
@ -98,5 +98,6 @@ def main():
|
|||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
15
settings.py
Normal file
15
settings.py
Normal 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
17
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.
|
#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.
|
# 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:
|
class Particle:
|
||||||
def __init__(self, position, velocity, mass, particle_type, properties, temperature=20):
|
def __init__(self, position, velocity, mass, particle_type, properties, temperature=20):
|
||||||
self.position = position # (x, y)
|
self.position = position # (x, y)
|
||||||
@ -109,7 +99,6 @@ class Simulation:
|
|||||||
self.transform_particle(x, y, particle.solidify)
|
self.transform_particle(x, y, particle.solidify)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def handle_particle_interactions(self, dt):
|
def handle_particle_interactions(self, dt):
|
||||||
"""Handle interactions between different particle types"""
|
"""Handle interactions between different particle types"""
|
||||||
for x, y in list(self.active_particles):
|
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.add((new_x, new_y))
|
||||||
self.active_particles.discard((x, y))
|
self.active_particles.discard((x, y))
|
||||||
|
|
||||||
|
|
||||||
def temperature(self, dt):
|
def temperature(self, dt):
|
||||||
"""Handle temperature changes and state transitions"""
|
"""Handle temperature changes and state transitions"""
|
||||||
for x, y in list(self.active_particles):
|
for x, y in list(self.active_particles):
|
||||||
@ -361,6 +351,7 @@ class Simulation:
|
|||||||
self.particles[grid_x][grid_y] = new_particle
|
self.particles[grid_x][grid_y] = new_particle
|
||||||
self.active_particles.add((grid_x, grid_y))
|
self.active_particles.add((grid_x, grid_y))
|
||||||
|
|
||||||
|
|
||||||
def update_spatial_grid(self):
|
def update_spatial_grid(self):
|
||||||
"""Update spatial grid for optimized collision detection"""
|
"""Update spatial grid for optimized collision detection"""
|
||||||
self.spatial_grid.clear()
|
self.spatial_grid.clear()
|
||||||
@ -455,7 +446,6 @@ class Simulation:
|
|||||||
self.active_particles.add((x, y))
|
self.active_particles.add((x, y))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def apply_physics(self, dt):
|
def apply_physics(self, dt):
|
||||||
"""Handle all physics effects"""
|
"""Handle all physics effects"""
|
||||||
new_active_particles = set()
|
new_active_particles = set()
|
||||||
@ -568,6 +558,7 @@ class Simulation:
|
|||||||
|
|
||||||
self.active_particles = new_active_particles
|
self.active_particles = new_active_particles
|
||||||
|
|
||||||
|
|
||||||
def clear_particles_circle(self, center_x, center_y):
|
def clear_particles_circle(self, center_x, center_y):
|
||||||
"""Clear particles in a circle around the given point based on brush size"""
|
"""Clear particles in a circle around the given point based on brush size"""
|
||||||
brush_size = int(self.brush_size)
|
brush_size = int(self.brush_size)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user