Skip to content

Commit

Permalink
Added robot drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthijs den Toom authored and Matthijs den Toom committed Jan 9, 2019
1 parent 4f73a8e commit 723c001
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 70 deletions.
5 changes: 1 addition & 4 deletions gym_multi_robot/envs/tiling_pattern_env.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from enum import Enum

import gym
import numpy as np
import operator
from gym.utils import seeding

from gym_multi_robot.envs.tiling_pattern_view_2d import TilingPatternView2D
Expand Down Expand Up @@ -48,7 +45,7 @@ def step(self, action):
return observation, reward, done, info

def reset(self):
self.game_view.reset_robot()
self.game_view.reset_robots()
self.state = np.zeros(2)
self.steps_beyond_done = None
self.done = False
Expand Down
11 changes: 7 additions & 4 deletions gym_multi_robot/envs/tiling_pattern_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ class GripperRobot:
# This variable stores the locations of the robot relative to (0, 0)
relative_locations = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def __init__(self, heading=Heading.NORTH, location=(0, 0)):
def __init__(self, identifier, heading=Heading.NORTH, location=(0, 0)):
self.hold_object = False
self.heading = heading
self.location = location
self.identifier = identifier

def pickup(self, grid):
"""This method picks up a object if it is not already holding one."""
Expand Down Expand Up @@ -144,16 +145,18 @@ def generate_observed_locations(self):
class TilingPatternGame:
""" This class represents a grid used for the tiling pattern problem."""

def __init__(self, grid_size, lattice_size):
def __init__(self, grid_size, lattice_size, num_robots = 5):
self.grid_size = grid_size
self.lattice_size = lattice_size
self.grid = [[0 for _ in range(self.GRID_H)] for _ in range(self.GRID_W)]
self.robots = []
self.reset_grid()

for i in range(num_robots):
self.robots.append(GripperRobot(i, location=(0, i)))

def reset_grid(self):

# Create the grid.
for x in range(0, self.GRID_W, self.lattice_size):
for y in range(0, self.GRID_H, self.lattice_size):
self.grid[x][y] = 1
Expand All @@ -180,4 +183,4 @@ def GRID_W(self):

@property
def GRID_H(self):
return int(self.grid_size[1])
return int(self.grid_size[1])
79 changes: 17 additions & 62 deletions gym_multi_robot/envs/tiling_pattern_view_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,8 @@ def __init__(self, maze_name="TilingPattern2D", lattice_size=2,

self.__draw_tiles()

# # show the portals
# self.__draw_portals()
#
# # show the robot
# self.__draw_robots()
#
# # show the entrance
# self.__draw_entrance()
#
# # show the goal
# self.__draw_goal()
# show the robot
self.__draw_robots()

def update(self, mode="human"):
try:
Expand Down Expand Up @@ -85,17 +76,19 @@ def move_robot(self, dir):
# move the robot
self.__robots += np.array(self.__game.COMPASS[dir])
# if it's in a portal afterward
if self.maze.is_portal(self.robot):
self.__robots = np.array(self.maze.get_portal(tuple(self.robot)).teleport(tuple(self.robot)))
if self.game.is_portal(self.robot):
self.__robots = np.array(self.game.get_portal(tuple(self.robot)).teleport(tuple(self.robot)))
self.__draw_robot(transparency=255)

def reset_robot(self):
def reset_robots(self):

# TODO: implement
self.__draw_robots(transparency=0)
self.__robots = np.zeros(2, dtype=int)
self.__draw_robots(transparency=255)

def __controller_update(self):
# TODO, implement
if not self.__game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
Expand Down Expand Up @@ -139,57 +132,19 @@ def __draw_tiles(self, colour=(0, 0, 150), transparency=235):
if self.__game.grid[i][j]:
self.__colour_cell((i, j), colour=colour, transparency=transparency)

def __cover_walls(self, x, y, dirs, colour=(0, 0, 255, 15)):
def __draw_robots(self, colour=(150, 0, 0), object_colour=(0, 0, 150), transparency=255):

dx = x * self.CELL_W
dy = y * self.CELL_H
for robot in self.__game.robots:
x = int(robot.location[0] * self.CELL_W + self.CELL_W * 0.5 + 0.5)
y = int(robot.location[1] * self.CELL_H + self.CELL_H * 0.5 + 0.5)
r = int(min(self.CELL_W, self.CELL_H) / 2)

if not isinstance(dirs, str):
raise TypeError("dirs must be a str.")
pygame.draw.circle(self.maze_layer, colour + (transparency,), (x, y), r)

for dir in dirs:
if dir == "S":
line_head = (dx + 1, dy + self.CELL_H)
line_tail = (dx + self.CELL_W - 1, dy + self.CELL_H)
elif dir == "N":
line_head = (dx + 1, dy)
line_tail = (dx + self.CELL_W - 1, dy)
elif dir == "W":
line_head = (dx, dy + 1)
line_tail = (dx, dy + self.CELL_H - 1)
elif dir == "E":
line_head = (dx + self.CELL_W, dy + 1)
line_tail = (dx + self.CELL_W, dy + self.CELL_H - 1)
else:
raise ValueError("The only valid directions are (N, S, E, W).")
if robot.hold_object:
r_object = int(min(self.CELL_W, self.CELL_H) / 4 + 0.5)
pygame.draw.circle(self.maze_layer, object_colour + (transparency,), (x, y), r_object)

pygame.draw.line(self.maze_layer, colour, line_head, line_tail)

def __draw_robots(self, colour=(0, 0, 150), transparency=255):
pass
# x = int(self.__robots[0] * self.CELL_W + self.CELL_W * 0.5 + 0.5)
# y = int(self.__robots[1] * self.CELL_H + self.CELL_H * 0.5 + 0.5)
# r = int(min(self.CELL_W, self.CELL_H)/5 + 0.5)
#
# pygame.draw.circle(self.maze_layer, colour + (transparency,), (x, y), r)

def __draw_entrance(self, colour=(0, 0, 150), transparency=235):

self.__colour_cell(self.entrance, colour=colour, transparency=transparency)

def __draw_goal(self, colour=(150, 0, 0), transparency=235):

self.__colour_cell(self.goal, colour=colour, transparency=transparency)

def __draw_portals(self, transparency=160):

colour_range = np.linspace(0, 255, len(self.maze.portals), dtype=int)
colour_i = 0
for portal in self.maze.portals:
colour = ((100 - colour_range[colour_i])% 255, colour_range[colour_i], 0)
colour_i += 1
for location in portal.locations:
self.__colour_cell(location, colour=colour, transparency=transparency)

def __colour_cell(self, cell, colour, transparency):

Expand All @@ -203,7 +158,7 @@ def __colour_cell(self, cell, colour, transparency):
pygame.draw.rect(self.maze_layer, colour + (transparency,), (x, y, w, h))

@property
def maze(self):
def game(self):
return self.__game

@property
Expand Down

0 comments on commit 723c001

Please sign in to comment.