-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.py
113 lines (87 loc) · 2.7 KB
/
environment.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import pygame
class Environment():
"""
A class of the map where the robot will be moving around.
Attributes
----------
dimensions : tuple
The X and Y window dimensions.
"""
def __init__(self, map_dimensions):
# Colors
self.WHITE = (255, 255, 255)
self.BLACK = (0, 0, 0)
self.RED = (255, 0, 0)
self.GREEN = (0, 255, 0)
self.BLUE = (0, 0, 255)
self.BROWN = (189, 154, 122)
self.YELLOW = (255, 255, 0)
self.GRAY = (105, 105, 105)
# Map dimensions
self.WIDTH, self.HEIGHT = map_dimensions
# Window settings
self.FPS = 120
pygame.display.set_caption('Visibility-based PRM')
self.map = pygame.display.set_mode(size=(self.WIDTH, self.HEIGHT))
self.map.fill(self.WHITE)
self.obstacles = []
# Font and a counter for the number of the node
self.font = pygame.font.SysFont('Comic Sans MS', 30)
def make_obstacles_T(self, initial_point):
"""
Given a initial point, it makes a obstacle with shape of T.
Parameters
----------
initial_point : tuple
X and Y coordinates, starting from the top-left most part where
the obstacle will be placed.
Returns
-------
list
A collection of sides composing the T obstacle.
"""
x, y = initial_point[0], initial_point[1]
width, height = 50, 150
side1 = pygame.Rect(x, y, height, width)
side2 = pygame.Rect((x+height//2) - width//2, y, width, height)
obstacle = [side1, side2]
return obstacle
def make_obstacles_L(self, initial_point):
"""
Given a initial point, it makes a obstacle with shape of L.
Parameters
----------
initial_point : tuple
X and Y coordinates, starting from the top-left most part where
the obstacle will be placed.
Returns
-------
list
A collection of sides composing the L obstacle.
"""
x, y = initial_point[0], initial_point[1]
width, height = 50, 150
side1 = pygame.Rect(x, y, width, height)
side2 = pygame.Rect(x, y+height-width, height, width)
obstacle = [side1, side2]
return obstacle
def make_obstacles(self):
"""Generates the obstacles to be placed on the final map."""
obstacle1 = self.make_obstacles_T(initial_point=(350, 200))
obstacle2 = self.make_obstacles_L(initial_point=(150, 20))
self.obstacles.append(obstacle1)
self.obstacles.append(obstacle2)
return self.obstacles
def draw_obstacles(self):
"""Draws each side of the obstacles."""
obstacles = []
for obstacle in self.obstacles:
for side in obstacle:
pygame.draw.rect(surface=self.map, color=self.GRAY,
rect=side)
obstacles.append(side)
return obstacles
def draw_node_number(self, number, point):
"""Draws a number next to the node."""
text_surface = self.font.render(str(number), False, (0, 0, 0))
self.map.blit(text_surface, point)