-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
61 lines (44 loc) · 1.72 KB
/
Copy pathagent.py
File metadata and controls
61 lines (44 loc) · 1.72 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
from collections import deque
import pygame as pg
AGENT_SIZE = 35, 35
TRANSPARENT = 0, 0, 0, 0
class Agent:
def __init__(self, pos):
self.image = self.make_img()
self.rect = self.image.get_rect(center=pos)
self.true_pos = list(self.rect.center)
self.path = None
self.speed = 100
def make_img(self):
img = pg.Surface(AGENT_SIZE).convert_alpha()
img.fill(TRANSPARENT)
rect = img.get_rect()
# Assigning color to the agents start states
pg.draw.ellipse(img, pg.Color('black'), rect.inflate(-1, -1)) # Start state color
pg.draw.ellipse(img, pg.Color('green'), rect.inflate(-10, -10))
return img
def draw(self, surface):
surface.blit(self.image, self.rect)
# Draw path:
if self.path:
for p in self.path:
pg.draw.circle(surface, pg.Color('red'), p, 5)
def update(self, dt):
if self.path:
current = self.path[0]
self.move_to(current, dt)
# Update current when we reached it
dx = current[0] - self.true_pos[0]
dy = current[1] - self.true_pos[1]
if pg.math.Vector2(dx, dy).length() < 1:
self.path.popleft()
def set_path(self, path):
self.path = deque(path)
def move_to(self, pos, dt):
# Calculate distance between current pos and target, and direction
vec = pg.math.Vector2(pos[0] - self.true_pos[0], pos[1] - self.true_pos[1])
direction = vec.normalize()
# Progress towards the target
self.true_pos[0] += direction[0] * self.speed * dt
self.true_pos[1] += direction[1] * self.speed * dt
self.rect.center = self.true_pos