Movemen has been moved to agent, functions are all there ready to be used. Also new functions in project_constants in Direction Class

This commit is contained in:
Jakub Danilewicz 2021-04-17 18:03:16 +02:00
parent 1efe763937
commit 1d9e45c631
5 changed files with 42 additions and 25 deletions

View File

@ -13,13 +13,35 @@ class Agent:
data = json.load(json_data)
self.row, self.column = data['agent_starting_position'].split(",")
self.position = [int(self.row), int(self.column)]
#self.direction = const.Direction()
self.direction = 0
def rotate_left (self):
self.direction = self.direction.previous()
def rotate_right(self):
self.direction = self.direction.next()
def go(self):
if (self.direction == const.Direction.RIGHT ):
temp = go_right()
self.position[1] = temp[1]
elif (self.direction == const.Direction.LEFT ):
temp = go_left()
self.position[1] = temp[1]
elif (self.direction == const.Direction.UP):
temp = go_up()
self.position[0] = temp[0]
elif (self.direction == const.Direction.DOWN):
temp = temp = go_down()
self.position[0] = temp[0]
def go_right(self):
return self.position[0], self.position[1] + 1
def go_left(self):
return self.position[0], self.position[1] - 1
def go_up(self):
@ -28,4 +50,5 @@ class Agent:
def go_down(self):
return self.position[0] + 1, self.position[1]
return self.position[0] + 1, self.position[1]

View File

@ -18,9 +18,11 @@ def _get_random_attribute_values(self, attr_type, grid_dimensions):
class JsonGenerator:
grid = dict()
def __init__(self, agent_starting_position):
def __init__(self, agent_starting_position, agent_direction):
self.agent_starting_position = agent_starting_position
starting_row, starting_column = agent_starting_position
self.agent_direction = agent_direction
starting_direction = agent_direction
self.grid["agent_starting_position"] = str(starting_row) + ',' + str(starting_column)
# sets agent's starting position

22
main.py
View File

@ -64,26 +64,10 @@ def main():
running = False
# Assigning all input from keyboard as variables into an array
keys = pygame.key.get_pressed()
# Depending on what key we press, the agent will move in that direction
# DISCRETION : The only keys that are available are arrow keys
# DISCRETION : is_valid_move is a new brand function that now plays a critical role in movement of our Agent (It is NOT just the "check up" function anymore)
if keys[pygame.K_RIGHT]:
target_row, target_column = minefield.agent.go_right()
minefield.is_valid_move(target_row, target_column)
elif keys[pygame.K_LEFT]:
target_row, target_column = minefield.agent.go_left()
minefield.is_valid_move(target_row, target_column)
elif keys[pygame.K_UP]:
target_row, target_column = minefield.agent.go_up()
minefield.is_valid_move(target_row, target_column)
elif keys[pygame.K_DOWN]:
target_row, target_column = minefield.agent.go_down()
minefield.is_valid_move(target_row, target_column)
if __name__ == "__main__":

View File

@ -41,6 +41,13 @@ class Direction(Enum):
RIGHT = 1
DOWN = 2
LEFT = 3
def next(self):
v = (self.value + 1 ) % 4
return Direction(v)
def previous (self):
v = (self.value - 1) % 4
return Direction(v)
class Action(Enum):

View File

@ -495,5 +495,6 @@
"color": "RED",
"mine": null
},
"agent_starting_position": "0,0"
"agent_starting_position": "0,0"
}