-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulator.py
More file actions
57 lines (46 loc) · 1.59 KB
/
simulator.py
File metadata and controls
57 lines (46 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# simulator.py
import pygame
import imageio
import numpy as np
from robot import Robot
class Simulator:
def __init__(self, arena_size=500):
pygame.init()
self.arena_size = arena_size
self.window = pygame.display.set_mode((arena_size, arena_size))
pygame.display.set_caption("Brownian Robot Simulation")
self.clock = pygame.time.Clock()
self.robot = Robot(arena_size)
self.path = [self.robot.position.copy()]
self.frames = []
def run(self):
running = True
while running:
self.clock.tick(60) # 60 FPS
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
self.robot.move()
self.path.append(self.robot.position.copy())
self._draw()
pygame.quit()
imageio.mimsave("brownian_motion.gif", self.frames, fps=30)
def _draw(self):
self.window.fill((255, 255, 255)) # white background
# Draw the path
if len(self.path) > 1:
pygame.draw.lines(
self.window, (200, 200, 200), False,
[pos.astype(int) for pos in self.path], 1
)
# Draw the robot
pygame.draw.circle(
self.window, (0, 0, 255),
self.robot.position.astype(int),
self.robot.radius
)
pygame.display.flip()
# Capture frame for GIF
frame = pygame.surfarray.array3d(self.window)
frame = np.transpose(frame, (1, 0, 2)) # Convert to (H, W, C)
self.frames.append(frame)