-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomAgent.py
69 lines (63 loc) · 2.27 KB
/
randomAgent.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
from env import *
class randPlayer(pygame.sprite.Sprite):
def __init__(self, randAgentPath, color):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((WALLSIZE, WALLSIZE))
self.image.fill(color)
pygame.draw.rect(
self.image, rand_color(random.randint(0, N)), self.image.get_rect(), 3
)
self.rect = self.image.get_rect() # get image position
self.rect.x = random.randint(0, N - 1) * WALLSIZE
self.rect.y = random.randint(0, N - 1) * WALLSIZE
self.speedx = SPEED
self.speedy = SPEED
self.score = 0
self.steps = 0
self.randAgentPath = randAgentPath
def move(self, direction):
if direction == "r":
self.steps += 1
self.rect.x += self.speedx
if self.is_player_collide_wall():
self.rect.x -= self.speedx
if direction == "l":
self.steps += 1
self.rect.x -= self.speedx
if self.is_player_collide_wall():
self.rect.x += self.speedx
if direction == "u":
self.steps += 1
self.rect.y -= self.speedy
if self.is_player_collide_wall():
self.rect.y += self.speedy
if direction == "d":
self.steps += 1
self.rect.y += self.speedy
if self.is_player_collide_wall():
self.rect.y -= self.speedy
def is_player_collide_wall(self):
for wall in walls:
if self.rect.colliderect(wall):
return True
return False
def update(self):
direction = self.randAgentPath[self.steps] # random.choice(directions)
# print(direction)
if direction == 0: ## left
self.move("l")
if direction == 1: ## right
self.move("r")
if direction == 2: ## up
self.move("u")
if direction == 3: ## down
self.move("d")
# Avoid colliding with wall and go out of edges
if self.rect.right > WIDTH:
self.rect.right = WIDTH
if self.rect.left < 0:
self.rect.left = 0
if self.rect.bottom > HEIGHT:
self.rect.bottom = HEIGHT
if self.rect.top < 0:
self.rect.top = 0