-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot.py
More file actions
29 lines (24 loc) · 851 Bytes
/
robot.py
File metadata and controls
29 lines (24 loc) · 851 Bytes
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
# robot.py
import numpy as np
class Robot:
def __init__(self, arena_size):
self.arena_size = arena_size
self.radius = 5
self.position = np.array([arena_size / 2, arena_size / 2], dtype=np.float32)
self.angle = np.random.uniform(0, 2 * np.pi)
self.speed = 2 # pixels per update
def move(self):
dx = self.speed * np.cos(self.angle)
dy = self.speed * np.sin(self.angle)
self.position += np.array([dx, dy])
if self._check_collision():
self._rotate_random()
def _check_collision(self):
x, y = self.position
r = self.radius
return (
x - r <= 0 or x + r >= self.arena_size or
y - r <= 0 or y + r >= self.arena_size
)
def _rotate_random(self):
self.angle = np.random.uniform(0, 2 * np.pi)