-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
79 lines (63 loc) · 2.01 KB
/
code.py
File metadata and controls
79 lines (63 loc) · 2.01 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import pygame
import random
# Initialize Pygame
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Obstacle Avoidance Simulator")
# Colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
# Clock for frame rate control
clock = pygame.time.Clock()
# Robot properties
robot_radius = 20
robot_x, robot_y = WIDTH // 4, HEIGHT // 2
robot_speed = 5
robot_direction = [1, 0] # Initial direction (x, y)
# Obstacle properties
obstacle_width, obstacle_height = 50, 50
num_obstacles = 5
obstacles = [
pygame.Rect(
random.randint(200, WIDTH - 100), random.randint(50, HEIGHT - 50), obstacle_width, obstacle_height
)
for _ in range(num_obstacles)
]
# Function to detect collisions
def detect_collision(robot_x, robot_y, obstacles):
for obstacle in obstacles:
if obstacle.collidepoint(robot_x, robot_y):
return True
return False
# Main simulation loop
def run_simulation():
global robot_x, robot_y, robot_direction
running = True
while running:
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Clear the screen
screen.fill(WHITE)
# Draw the robot and obstacles
pygame.draw.circle(screen, BLUE, (robot_x, robot_y), robot_radius)
for obstacle in obstacles:
pygame.draw.rect(screen, RED, obstacle)
# Update robot position
new_x = robot_x + robot_speed * robot_direction[0]
new_y = robot_y + robot_speed * robot_direction[1]
# Handle collisions
if detect_collision(new_x, new_y, obstacles) or not (0 < new_x < WIDTH and 0 < new_y < HEIGHT):
robot_direction = [random.choice([-1, 1]), random.choice([-1, 1])]
else:
robot_x, robot_y = new_x, new_y
# Update the display
pygame.display.flip()
clock.tick(30)
pygame.quit()
# Run the simulation
run_simulation()