-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot_module.py
62 lines (38 loc) · 1.64 KB
/
robot_module.py
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
58
59
60
61
62
# robot_module.py
import pygame
import random
import math
class RobotSimulation:
def __init__(self, arena_size=(640, 480), robot_radius=5, speed=2):
pygame.init()
self.screen_width, self.screen_height = arena_size
self.screen = pygame.display.set_mode((self.screen_width, self.screen_height))
pygame.display.set_caption("Robot Movement in Arena")
self.BLACK = (0, 0, 0)
self.WHITE = (255, 255, 255)
self.robot_radius = robot_radius
self.robot_x = self.screen_width // 2
self.robot_y = self.screen_height // 2
self.speed = speed
self.angle = random.uniform(0, 2*math.pi)
def update(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
self.robot_x += self.speed * math.cos(self.angle)
self.robot_y += self.speed * math.sin(self.angle)
if not (0 <= self.robot_x <= self.screen_width and 0 <= self.robot_y <= self.screen_height):
self.angle = random.uniform(0, 2*math.pi)
self.screen.fill(self.BLACK)
pygame.draw.circle(self.screen, self.WHITE, (int(self.robot_x), int(self.robot_y)), self.robot_radius)
pygame.display.flip()
pygame.time.Clock().tick(60)
return True
def run_simulation(self):
running = True
while running:
running = self.update()
pygame.quit()
if __name__ == "__main__":
simulation = RobotSimulation()
simulation.run_simulation()