-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgamemap.py
More file actions
215 lines (171 loc) · 7.43 KB
/
gamemap.py
File metadata and controls
215 lines (171 loc) · 7.43 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"""
MASLAB 2024
Constructs a Map object from a map file
"""
import sys
from constants import *
from kinematics import convert_velocities
class Map:
def __init__(self, filename):
"""
Constructs a new map from an input file
"""
# Read input file
with open(filename, "r") as f:
data = f.read()
# Initialize robot angle (zero up, positive clockwise)
self.angle = None
# Initialize robot image
img = image.load("botpic_cropped.png")
imgScale = (float(ROBOT_WIDTH), float(ROBOT_WIDTH))
self.img = transform.scale(img, imgScale)
# Save initial robot position
self.initial = ((0, 0), 0)
# Parse map file
w, x, p, b, a, r = self.parse(data)
# Initialize data lists
self.walls = w
self.platforms = p
self.box = x
self.cubes = b
self.apriltags = a
self.robot = r
# Initialize game clock
self.clock = Clock()
def draw(self):
# Initialize pygame
init()
# Set up pygame screen
width, height = SIZE
self.screen = display.set_mode(SIZE)
# Loop
while True:
self.screen.fill(BLACK)
# Draw grid coordinates
x = XOFF
while x <= width:
y = YOFF
while y <= height:
draw.circle(self.screen, WHITE, (x, y), 1)
y += RESOLUTION
x += RESOLUTION
# Draw walls
for wall in self.walls:
draw.line(self.screen, *wall)
# Draw bounding box
for box in self.box:
draw.line(self.screen, *box)
# Draw cubes
for cube in self.cubes:
draw.rect(self.screen, *cube)
# Draw platforms
for platform in self.platforms:
draw.line(self.screen, *platform)
# Draw AprilTags
for tag in self.apriltags:
draw.rect(self.screen, *tag)
# Get drive velocity
drive_speed = self.get_drive_speed()
linear, angular = convert_velocities(drive_speed)
# Apply velocity to robot & draw robot
if not (self.robot is None):
# Get time since last call (in seconds)
dt = self.clock.tick(50) / 1000
# Compute robot motion (flip rotation direction)
delta_angle = -1 * angular * dt
drive_angle = radians(self.angle) + delta_angle/2
dx = linear * dt * sin(drive_angle)
dy = linear * dt * cos(drive_angle)
self.angle += degrees(delta_angle)
# Compute new robot position
x, y = self.robot
newimg = transform.rotate(self.img, self.angle)
x += dx
y += dy
# Update robot
self.robot = x, y
# Shift robot to account for image padding
padding_angle = radians(self.angle % 90)
new_size = ROBOT_WIDTH * (cos(padding_angle) + sin(padding_angle))
adjustment = (new_size - ROBOT_WIDTH) / 2
x -= adjustment
y -= adjustment
# Draw robot
self.screen.blit(newimg, (x, y))
display.update()
for e in event.get():
if e.type == KEYDOWN:
if e.key == K_r:
self.robot, self.angle = self.initial
if e.type == QUIT:
sys.exit()
def parse(self, data):
"""
Parses map data
"""
# Prepare data file
rows = [r.strip() for r in data.split("\n") if len(r.strip()) > 0 and r[0] != "#"]
# Initialize object lists
walls = []
box = []
platforms = []
cubes = []
apriltags = []
# Initialize robot
robot = None
# Iterate through objects
for row in rows:
values = [v.strip() for v in row.split(",")][1:]
match row[0]:
# Wall
case "W":
x1, y1, x2, y2 = [float(v) for v in values]
walls.append((BLUE, (x1 * RESOLUTION + XOFF, y1 * RESOLUTION + YOFF), (x2 * RESOLUTION + XOFF, y2 * RESOLUTION + YOFF), 2))
# Bounding box
case "B":
xc, yc = [float(v) for v in values]
box.append((GREEN, (xc * RESOLUTION + XOFF - BOX_WIDTH / 2, yc * RESOLUTION + YOFF - BOX_WIDTH / 2), (xc * RESOLUTION + XOFF + BOX_WIDTH / 2, yc * RESOLUTION + YOFF - BOX_WIDTH / 2), 1))
box.append((GREEN, (xc * RESOLUTION + XOFF + BOX_WIDTH / 2, yc * RESOLUTION + YOFF - BOX_WIDTH / 2), (xc * RESOLUTION + XOFF + BOX_WIDTH / 2, yc * RESOLUTION + YOFF + BOX_WIDTH / 2), 1))
box.append((GREEN, (xc * RESOLUTION + XOFF + BOX_WIDTH / 2, yc * RESOLUTION + YOFF + BOX_WIDTH / 2), (xc * RESOLUTION + XOFF - BOX_WIDTH / 2, yc * RESOLUTION + YOFF + BOX_WIDTH / 2), 1))
box.append((GREEN, (xc * RESOLUTION + XOFF - BOX_WIDTH / 2, yc * RESOLUTION + YOFF + BOX_WIDTH / 2), (xc * RESOLUTION + XOFF - BOX_WIDTH / 2, yc * RESOLUTION + YOFF - BOX_WIDTH / 2), 1))
# Robot
case "R":
x1, y1 = [float(v) for v in values[0:2]]
self.angle = 180 - float(values[2]) # negative for clockwise rotation
robot = (x1 * RESOLUTION - ROBOT_WIDTH / 2 + XOFF, y1 * RESOLUTION - ROBOT_WIDTH / 2 + YOFF)
self.initial = (robot, self.angle)
# Cube
case "C":
x1, y1, z1 = [float(v) for v in values[0:3]]
color = RED if values[3].upper() == "R" else GREEN
cubes.append((color, Rect(x1 * RESOLUTION - WIDTH/2 + (z1 - 1) * ZSPACING + XOFF, y1 * RESOLUTION - WIDTH/2 - (z1 - 1) * ZSPACING + YOFF, WIDTH, WIDTH)))
# Platform
case "P":
x1, y1, x2, y2 = [float(v) for v in values]
platforms.append((GRAY, (x1 * RESOLUTION + XOFF, y1 * RESOLUTION + YOFF), (x2 * RESOLUTION + XOFF, y2 * RESOLUTION + YOFF), 10))
# AprilTag
case "A":
x1, y1 = [float(v) for v in values[0:2]]
color = LIGHT_RED if values[2].upper() == "R" else LIGHT_GREEN
apriltags.append((color, Rect(x1 * RESOLUTION - WIDTH/2 + XOFF, y1 * RESOLUTION - WIDTH/2 + YOFF, WIDTH, WIDTH)))
case other:
raise Exception(f"Did not recognize game object: {other}")
return walls, box, platforms, cubes, apriltags, robot
def get_drive_speed(self):
"""
Compute drive speed from keyboard input
"""
# Initialize left & right drive speeds
drive_speed = 0, 0
# Process event handlers
event.pump()
# Get keys pressed
pressed = key.get_pressed()
for keycode in [K_w, K_a, K_s, K_d]:
# Check if this key was pressed
if pressed[keycode]:
drive_speed = (
drive_speed[0] + KEY_SPEEDS[keycode][0],
drive_speed[1] + KEY_SPEEDS[keycode][1],
)
return drive_speed