Particle_Simulator/Python_Stuff/python_particle_simulator.py

63 lines
1.9 KiB
Python
Raw Normal View History

2024-11-23 21:59:28 -05:00
import numpy as np
import time
import sys
import sdl2
import sdl2.ext
import world_objects
import universe
import renderer
sprites = ["./sdl_sprites/green.bmp",
"./sdl_sprites/red.bmp",
"./sdl_sprites/yellow.bmp",
"./sdl_sprites/blue.bmp"]
def generate_particle(my_renderer):
location = np.random.randint(10,750,2)
accel = np.random.randint(0,5,2)
vel = np.random.randint(-10,10,2)
print(len(sprites))
sprite = sprites[np.random.randint(0,(len(sprites)))]
return world_objects.particle(location, vel, accel, 10, (255,0,0), 10.0, "blah", my_renderer, sprite)
def run():
my_renderer = renderer.SdlRender()
running = True
my_universe = universe.universe()
black_hole = world_objects.particle([400,400], [10,0], [0,0], 20, (0,0,255), 2000.0, "red", my_renderer, sprites[1])
my_universe.add_particle(black_hole)
for i in range(100):
my_universe.add_particle(generate_particle(my_renderer))
left = world_objects.line([0,0], [1,0])
top = world_objects.line([0,0], [0,1])
right = world_objects.line([800, 800], [1,0])
bottom = world_objects.line([800, 800], [0,1])
my_universe.add_line(left)
my_universe.add_line(top)
my_universe.add_line(right)
my_universe.add_line(bottom)
print(my_universe)
start_time=0
while running:
start_time = time.time()
events = sdl2.ext.get_events()
for event in events:
if event.type == sdl2.SDL_QUIT:
running = False
break
my_universe.process()
stop_time = time.time()
my_renderer.sdl_world.process()
temp = stop_time - start_time
start_time = time.time()
print(f"Process physics: {(temp)*1000} ms Draw world: {(start_time - stop_time)*1000} ms")
if __name__ == '__main__':
sys.exit(run())