Commit
·
5b0f63c
1
Parent(s):
896eec6
Shift the robot centre point to its body's center. Allow offset around it body when moving
Browse files
app.py
CHANGED
|
@@ -207,7 +207,7 @@ class Robot:
|
|
| 207 |
if self.png.shape[-1] != 4:
|
| 208 |
raise ValueError("Sprite image must have 4 channels (RGBA)")
|
| 209 |
self.png = np.array(Image.open(sprite).convert("RGBA").resize((40,40)))
|
| 210 |
-
self.pos = [
|
| 211 |
def step(self, path):
|
| 212 |
while path:
|
| 213 |
dx, dy = path[0][0] - self.pos[0], path[0][1] - self.pos[1]
|
|
@@ -482,26 +482,26 @@ def _pipeline(uid,img_path):
|
|
| 482 |
cv2.circle(frame, (x, y), 6, color, -1)
|
| 483 |
# Robot displacement
|
| 484 |
robot.step(path)
|
| 485 |
-
rx, ry = robot.pos
|
| 486 |
sp = robot.png
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
|
|
|
|
|
|
|
|
|
| 496 |
alpha = np.stack([alpha] * 3, axis=-1)
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
# Update frame with blended result
|
| 501 |
-
frame[ry:end_y, rx:end_x] = blended
|
| 502 |
# collection check
|
| 503 |
for o in objs:
|
| 504 |
-
if not o["col"] and np.hypot(o["pos"][0]-
|
| 505 |
o["col"]=True
|
| 506 |
vw.write(frame)
|
| 507 |
if all(o["col"] for o in objs): break
|
|
|
|
| 207 |
if self.png.shape[-1] != 4:
|
| 208 |
raise ValueError("Sprite image must have 4 channels (RGBA)")
|
| 209 |
self.png = np.array(Image.open(sprite).convert("RGBA").resize((40,40)))
|
| 210 |
+
self.pos = [20,20]; self.speed=speed
|
| 211 |
def step(self, path):
|
| 212 |
while path:
|
| 213 |
dx, dy = path[0][0] - self.pos[0], path[0][1] - self.pos[1]
|
|
|
|
| 482 |
cv2.circle(frame, (x, y), 6, color, -1)
|
| 483 |
# Robot displacement
|
| 484 |
robot.step(path)
|
|
|
|
| 485 |
sp = robot.png
|
| 486 |
+
sprite_h, sprite_w = sp.shape[:2]
|
| 487 |
+
rx, ry = robot.pos
|
| 488 |
+
x1, y1 = rx - sprite_w // 2, ry - sprite_h // 2
|
| 489 |
+
x2, y2 = x1 + sprite_w, y1 + sprite_h
|
| 490 |
+
# Clip boundaries to image size
|
| 491 |
+
x1_clip, x2_clip = max(0, x1), min(frame.shape[1], x2)
|
| 492 |
+
y1_clip, y2_clip = max(0, y1), min(frame.shape[0], y2)
|
| 493 |
+
# Adjust sprite crop accordingly
|
| 494 |
+
sx1, sy1 = x1_clip - x1, y1_clip - y1
|
| 495 |
+
sx2, sy2 = sprite_w - (x2 - x2_clip), sprite_h - (y2 - y2_clip)
|
| 496 |
+
sprite_crop = sp[sy1:sy2, sx1:sx2]
|
| 497 |
+
alpha = sprite_crop[:, :, 3] / 255.0
|
| 498 |
alpha = np.stack([alpha] * 3, axis=-1)
|
| 499 |
+
bgroi = frame[y1_clip:y2_clip, x1_clip:x2_clip]
|
| 500 |
+
blended = (alpha * sprite_crop[:, :, :3] + (1 - alpha) * bgroi).astype(np.uint8)
|
| 501 |
+
frame[y1_clip:y2_clip, x1_clip:x2_clip] = blended
|
|
|
|
|
|
|
| 502 |
# collection check
|
| 503 |
for o in objs:
|
| 504 |
+
if not o["col"] and np.hypot(o["pos"][0]-robot.pos[0], o["pos"][1]-robot.pos[1]) <= 20:
|
| 505 |
o["col"]=True
|
| 506 |
vw.write(frame)
|
| 507 |
if all(o["col"] for o in objs): break
|