diff --git a/gauss-turing/game/gauss_turing.py b/gauss-turing/game/gauss_turing.py index 0b8940d..038da06 100644 --- a/gauss-turing/game/gauss_turing.py +++ b/gauss-turing/game/gauss_turing.py @@ -2,11 +2,15 @@ import numpy as np import random import pygame import os +import threading + +from webapp import app from event_server_comm import move_grid BLACK = np.array([0, 0, 0], dtype=np.uint8) WHITE = np.array([255, 255, 255], dtype=np.uint8) +GRAY = np.array([200, 200, 200], dtype=np.uint8) RED = np.array([255, 0, 0], dtype=np.uint8) BLUE = np.array([0, 0, 255], dtype=np.uint8) YELLOW = np.array([255, 255, 0], dtype=np.uint8) @@ -26,7 +30,7 @@ tiledt = np.dtype([('x', np.uint8), ('y', np.uint8), ('color', np.uint8, 3), ('s class Board: - valid_colors = [WHITE, RED, BLUE] + valid_colors = [GRAY, RED, BLUE] def __init__(self, dim_x, dim_y): self.tiles = np.zeros((dim_y, dim_x), dtype=tiledt) @@ -80,11 +84,12 @@ class Robot: } - def __init__(self, x, y, orientation): + def __init__(self, x, y, orientation, use_real_robot=False): self.x = x self.y = y self.orientation = orientation self.position_changed = False + self.use_real_robot = use_real_robot def get_forward_coordinates(self): # get the coordinates of the neighboring tile in the given direction @@ -112,7 +117,8 @@ class Robot: return robot_surf def update_pos(self, dimx, dimy): - move_grid(self.x, self.y, self.orientation, dimx, dimy) + if self.use_real_robot: + move_grid(self.x, self.y, self.orientation, dimx, dimy) self.position_changed = False def __repr__(self): @@ -122,7 +128,7 @@ class Robot: class Command: valid_actions = {'forward', 'left', 'right', 'P0', '-'} - def __init__(self, action=None, color=WHITE): + def __init__(self, action=None, color=GRAY): if not (action in Command.valid_actions and any([np.all(color == c) for c in Board.valid_colors])): raise ValueError("invalid values for command") self.action = action @@ -157,7 +163,7 @@ class Programmer: def __init__(self, prg): self.prg = prg self.available_inputs = [Command('forward'), Command('left'), Command('right'), Command('P0'), - Command('-', color=RED), Command('-', color=BLUE), Command('-', color=WHITE)] + Command('-', color=RED), Command('-', color=BLUE), Command('-', color=GRAY)] self.command_to_edit = 0 self.screen_rect = None @@ -196,6 +202,8 @@ class Program: self.screen_rect = None def step(self, state='running'): + if self.prg_counter >= len(self.cmds): + return 'game_over' cmd = self.cmds[self.prg_counter] self.prg_counter += 1 @@ -207,7 +215,7 @@ class Program: tile = self.board.tiles[y, x] # apply next instruction of the program - if np.all(cmd.color == WHITE) or np.all(cmd.color == tile['color']): + if np.all(cmd.color == GRAY) or np.all(cmd.color == tile['color']): # matching color -> execute command if cmd.action == 'forward': ynew, xnew = self.robot.get_forward_coordinates() @@ -264,7 +272,7 @@ class Program: cmd_surf = cmd.render(scale_fac) else: cmd_surf = pygame.Surface((scale_fac,scale_fac)) - cmd_surf.fill(WHITE) + cmd_surf.fill(GRAY) if prg_counter is not None and i == prg_counter: pygame.draw.rect(cmd_surf, tuple(GREEN), (0, 0, scale_fac, scale_fac), 5) prg_surf.blit(cmd_surf, (i * scale_fac, 0, scale_fac, scale_fac)) @@ -280,8 +288,8 @@ class Program: class Game: - def __init__(self, dimx, dimy, robotx, roboty): - self.robot = Robot(x=robotx, y=roboty, orientation='v') + def __init__(self, dimx, dimy, robotx, roboty, use_real_robot=False): + self.robot = Robot(x=robotx, y=roboty, orientation='v', use_real_robot=use_real_robot) self.board = Board(dimx, dimy) coin1x = np.random.randint(0, dimx) coin1y = np.random.randint(0, dimy) @@ -290,7 +298,7 @@ class Game: # TODO fix number of commands at 5 self.cmds = [Command('forward'), Command('left', color=RED), Command('left', color=BLUE), Command('P0'), Command('-')] - self.state = 'won' + self.state = 'reset' self.prg = Program(self.robot, self.board, self.cmds) @@ -450,6 +458,12 @@ class Game: self.state = self.prg.step(self.state) elif event.key == pygame.K_r: self.state = 'reset' + elif event.type == pygame.USEREVENT: + for i, cmd in enumerate(event.cmds): + self.cmds[i].action = cmd.action + self.cmds[i].color = np.array(cmd.color, dtype=np.uint8) + self.reset() + self.state = 'running' return self.state def reset(self): @@ -488,10 +502,14 @@ class Game: pygame.time.wait(100) if __name__ == "__main__": + # launch webapp in thread + webserver_thread = threading.Thread(target=app.run, kwargs={'host': '0.0.0.0', 'port': 5000}) + webserver_thread.start() + seed = 2 random.seed(seed) np.random.seed(seed) - game = Game(dimx=7, dimy=4, robotx=3, roboty=1) + game = Game(dimx=7, dimy=4, robotx=3, roboty=1, use_real_robot=False) game.run() # TODOs diff --git a/gauss-turing/game/static/-.png b/gauss-turing/game/static/-.png new file mode 100644 index 0000000..493483d Binary files /dev/null and b/gauss-turing/game/static/-.png differ diff --git a/gauss-turing/game/static/P0.png b/gauss-turing/game/static/P0.png new file mode 100644 index 0000000..62536c9 Binary files /dev/null and b/gauss-turing/game/static/P0.png differ diff --git a/gauss-turing/game/static/forward.png b/gauss-turing/game/static/forward.png new file mode 100644 index 0000000..101422c Binary files /dev/null and b/gauss-turing/game/static/forward.png differ diff --git a/gauss-turing/game/static/left.png b/gauss-turing/game/static/left.png new file mode 100644 index 0000000..66ef652 Binary files /dev/null and b/gauss-turing/game/static/left.png differ diff --git a/gauss-turing/game/static/right.png b/gauss-turing/game/static/right.png new file mode 100644 index 0000000..8ade14b Binary files /dev/null and b/gauss-turing/game/static/right.png differ diff --git a/gauss-turing/game/static/style.css b/gauss-turing/game/static/style.css new file mode 100644 index 0000000..e597446 --- /dev/null +++ b/gauss-turing/game/static/style.css @@ -0,0 +1,105 @@ +.main-canvas { + outline: 1px solid #dddddd;; + width: 500px; + height: 220px; + margin: 50px auto 0; + position: relative; +} + +.register { + outline: 3px dashed #dddddd;; + width: 120px; + height: 240px; + margin: 0px auto 0; + position: relative; +} + +.box{ + width: 62px; + height: 100px; + position: absolute !important; + top: 100px; + font-size: 15px; + color: #ffffff; + line-height: 25px; + text-align: center; + cursor: move; +} + +.robotOrientation { + width: 75px; + height: 75px; +} + +.robotOrientation90 { + width: 75px; + height: 75px; + -webkit-transform: rotate(90deg); + -moz-transform: rotate(90deg); + -o-transform: rotate(90deg); + -ms-transform: rotate(90deg); + transform: rotate(90deg); +} + +.robotOrientation180 { + width: 75px; + height: 75px; + -webkit-transform: rotate(180deg); + -moz-transform: rotate(180deg); + -o-transform: rotate(180deg); + -ms-transform: rotate(180deg); + transform: rotate(180deg); +} + +.robotOrientation270 { + width: 75px; + height: 75px; + -webkit-transform: rotate(270deg); + -moz-transform: rotate(270deg); + -o-transform: rotate(270deg); + -ms-transform: rotate(270deg); + transform: rotate(270deg); +} + +.rotate90 { + width: 100px; + height: 100px; + -webkit-transform: rotate(90deg); + -moz-transform: rotate(90deg); + -o-transform: rotate(90deg); + -ms-transform: rotate(90deg); + transform: rotate(90deg); +} + +.rotate180 { + -webkit-transform: rotate(180deg); + -moz-transform: rotate(180deg); + -o-transform: rotate(180deg); + -ms-transform: rotate(180deg); + transform: rotate(180deg); +} + +.rotate270 { + -webkit-transform: rotate(270deg); + -moz-transform: rotate(270deg); + -o-transform: rotate(270deg); + -ms-transform: rotate(270deg); + transform: rotate(270deg); +} + +.top-buffer { margin-top:20px; } + + +.card0 { left: 0; top: 0; background-color: #E74C3C; position: absolute !important; +} +.card1 { left: 100px; top: 0; background-color: #8E44AD; position: absolute !important; +} +.card2 { left: 200px; top: 0; background-color: #5DADE2; } +.card3 { left: 300px; top: 0; background-color: #1ABC9C; } +.card4 { left: 400px; top: 0; background-color: #F1C40F; } +.card5 { left: 50px; top: 120px; background-color: #F39C12; } +.card6 { left: 150px; top: 120px; background-color: #34495E; } +.card7 { left: 250px; top: 120px; background-color: #FF00FF; } +.card8 { left: 350px; top: 120px; background-color: #008080; } + +.card_hidden { background-color: #dddddd; } \ No newline at end of file diff --git a/gauss-turing/game/templates/drag_example.html b/gauss-turing/game/templates/drag_example.html new file mode 100644 index 0000000..0b4d623 --- /dev/null +++ b/gauss-turing/game/templates/drag_example.html @@ -0,0 +1,287 @@ + + +
+ + +