Skip to content

Commit

Permalink
Add VisualDebugger class for simulation visualization
Browse files Browse the repository at this point in the history
- Implement Pygame-based visualization for universe and objects
- Add methods for drawing objects, borders, and running the simulation
- Include collision detection and resolution in the main loop
  • Loading branch information
csmangum committed Aug 23, 2024
1 parent f5b941b commit 3979adb
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions fizicks/visual.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from typing import Any, List

import pygame

from fizicks.collision import Collision


class VisualDebugger:
"""
A class for visualizing the universe and objects for debugging purposes.
Uses Pygame to visualize the universe and objects.
Parameters
----------
universe: Universe
The universe to visualize.
objects: List[Matter]
The objects to visualize.
"""

def __init__(self, universe: "Universe", objects: List["Matter"]):
self.universe = universe
self.objects = objects

# Pygame initialization
pygame.init()
self.screen = pygame.display.set_mode(
(int(universe.dimensions.x), int(universe.dimensions.y))
)
pygame.display.set_caption("Fizicks Visual Debugger")
self.clock = pygame.time.Clock()

def draw_object(self, obj: "Matter") -> None:
"""
Draws an object on the screen.
Parameters
----------
obj: Matter
The object to draw.
"""
pygame.draw.circle(
self.screen,
obj.color,
(int(obj.position.x), int(obj.position.y)),
int(obj.radius),
)

def draw_border(self) -> None:
"""
Draws the border of the universe on the screen.
"""
pygame.draw.rect(
self.screen,
(255, 255, 255),
pygame.Rect(
0, 0, int(self.universe.dimensions.x), int(self.universe.dimensions.y)
),
1,
)

def run(self) -> None:
"""
Runs the simulation and visualizes the universe and objects.
"""
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

self.screen.fill((0, 0, 0)) # Clear screen with black

self.draw_border()

# Identify collisions
for i, obj in enumerate(self.objects):
for other_obj in self.objects[i + 1 :]:
if obj.collides_with(other_obj):
Collision.resolve(obj, other_obj)

for obj in self.objects:
obj.update(self.universe)
self.draw_object(obj)

self.universe.time += 1

pygame.display.flip()
self.clock.tick(60) # Cap the frame rate at 60 FPS

pygame.quit()

0 comments on commit 3979adb

Please sign in to comment.