sandpypi/sandpypi.py
Stan44 a03ba2b950 purdyfied and added to the commit messages
may of changed a couple of other things
maybe minor tweaks to physics.
2024-12-26 05:44:34 -06:00

104 lines
3.8 KiB
Python

#File Name: sandpypi.py
# Sandpypi by Stanton.
# Project name is a placeholder.
# This has been a multimonth or year project i have time blindness sorta.
# 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 settings import pygame
from rendering import Rendering
from sim import Simulation
def main(): # Main function to run the program
pygame.init()
clock = pygame.time.Clock()
pygame.display.set_mode((1024, 768), pygame.HWSURFACE | pygame.DOUBLEBUF)
sim = Simulation(1024, 768)
rendering = Rendering(1024, 768)
mouse_down_left = False
mouse_down_right = False
mouse_down_middle = False
mouse_down_wheel_up = False
mouse_down_wheel_down = False
over_button = False
running = True
while running:
fps = clock.get_fps()
dt = clock.tick(60) / 1000
# Handle events
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = pygame.mouse.get_pos()
if event.button == 4: # Mouse wheel up
sim.brush_size = min(sim.brush_size + 1, sim.max_brush_size)
elif event.button == 5: # Mouse wheel down
sim.brush_size = max(sim.brush_size - 1, 1)
elif event.button == 1: # Left click
over_button = False
# Check category buttons
for category, button in rendering.category_buttons.items():
if button.collidepoint(mouse_pos):
rendering.current_category = category
over_button = True
break
# Check particle buttons
if not over_button:
for particle_type, button in rendering.buttons.items():
if button.collidepoint(mouse_pos):
sim.current_particle_type = particle_type
over_button = True
break
# Check clear screen button
if rendering.clear_screen_button.collidepoint(mouse_pos):
rendering.clear_screen(sim)
over_button = True
if not over_button:
mouse_down_left = True
elif event.button == 3: # Right click
mouse_down_right = True
elif event.button == 2: # Middle click
mouse_down_middle = True
elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
mouse_down_left = False
elif event.type == pygame.MOUSEBUTTONUP and event.button == 3:
mouse_down_right = False
elif event.type == pygame.MOUSEBUTTONUP and event.button == 2:
mouse_down_middle = False
elif event.type == pygame.QUIT:
running = False
if mouse_down_left and not over_button:
x, y = pygame.mouse.get_pos()
sim.create_particle_circle(x, y)
if mouse_down_right:
x, y = pygame.mouse.get_pos()
sim.clear_particles_circle(x, y)
if mouse_down_middle:
x, y = pygame.mouse.get_pos()
sim.create_particle(x, y)
sim.simulate_step(dt)
rendering.draw_particles(sim.particles, sim.active_particles, sim.particle_size, rendering.particle_colors)
rendering.draw_buttons()
rendering.draw_brush_size_slider(sim.brush_size)
rendering.draw_debug_overlay(fps, sim.particles)
pygame.display.flip()
pygame.quit()
if __name__ == "__main__":
main()