diff --git a/gauss-turing/webserver/gauss_turing.py b/gauss-turing/webserver/gauss_turing.py index b8c7254..9cb0897 100644 --- a/gauss-turing/webserver/gauss_turing.py +++ b/gauss-turing/webserver/gauss_turing.py @@ -2,23 +2,7 @@ import numpy as np import random import pygame import time -pygame.init() - -pygame.font.init() # you have to call this at the start, - # if you want to use this module. -myfont = pygame.font.SysFont('Comic Sans MS', 55) -P0_text = myfont.render('P0', False, (0, 0, 0)) -game_over_text = myfont.render('GAME OVER', False, (255, 0, 0)) - -random.seed(0) - -dimx = 5 -dimy = 10 - -scale_fac = 50 -screen = pygame.display.set_mode((dimx*scale_fac+100,dimy*scale_fac+200)) - -tiledt = np.dtype([('x', np.uint8), ('y', np.uint8), ('color', np.uint8, 3), ('star', np.bool)]) +import copy BLACK = np.array([0, 0, 0], dtype=np.uint8) WHITE = np.array([255, 255, 255], dtype=np.uint8) @@ -27,6 +11,27 @@ BLUE = np.array([0, 0, 255], dtype=np.uint8) YELLOW = np.array([255, 255, 0], dtype=np.uint8) GREEN = np.array([0, 255, 0], dtype=np.uint8) +pygame.init() + +pygame.font.init() # you have to call this at the start, + # if you want to use this module. +myfont = pygame.font.SysFont('Comic Sans MS', 55) +P0_text = myfont.render('P0', False, (0, 0, 0)) +game_over_text = myfont.render('GAME OVER', False, RED) +run_text = myfont.render('RUN', False, tuple(BLACK)) + +random.seed(0) + +dimx = 5 +dimy = 10 + +scale_fac = 50 +screen = pygame.display.set_mode((dimx*scale_fac+200,dimy*scale_fac+300)) + +tiledt = np.dtype([('x', np.uint8), ('y', np.uint8), ('color', np.uint8, 3), ('star', np.bool)]) + + + class Board: @@ -132,7 +137,7 @@ class Command: cmd_surf.fill(tuple(self.color)) arrow_surf = pygame.Surface((300, 300), pygame.SRCALPHA) - #arrow_surf.fill(tuple(WHITE)) + pygame.draw.polygon(arrow_surf, (0, 0, 0), ((0, 100), (0, 200), (200, 200), (200, 300), (300, 150), (200, 0), (200, 100))) arrow_surf = pygame.transform.scale(arrow_surf, (int(0.9*scale_fac), int(0.9*scale_fac))) @@ -155,9 +160,46 @@ class Program: self.robot = robot self.board = board + self.available_inputs = [Command('forward'), Command('left'), Command('right'), Command('P0'), + Command('-', color=RED), Command('-', color=BLUE), Command('-', color=WHITE)] + def input_program(self): - self.render() - pass + selected_cmd = 0 + self.render(selected_cmd) + + start = False + + while not start: + # get all events + ev = pygame.event.get() + + # proceed events + for event in ev: + # handle MOUSEBUTTONUP + if event.type == pygame.MOUSEBUTTONUP: + pos = pygame.mouse.get_pos() + + if pos[0] >= 50 and pos[0] <= 50 + 250 and pos[1] >= 600 and pos[1] <= 650: + print(f"clicked at pos = {pos}") + selected_cmd = (pos[0] - 50)//50 + if pos[0] >= 50 and pos[0] <= 50 + 350 and pos[1] >= 700 and pos[1] <= 750: + print(f"clicked at pos = {pos}") + chosen_input = (pos[0] - 50)//50 + chosen_input_cmd = self.available_inputs[chosen_input] + if selected_cmd < len(self.cmds): + edited_cmd = self.cmds[selected_cmd] + if chosen_input_cmd.action is not '-': + edited_cmd.action = chosen_input_cmd.action + else: + edited_cmd.color = chosen_input_cmd.color + else: + self.cmds.append(copy.copy(chosen_input_cmd)) + if pos[0] >= 325 and pos[0] <= 400 and pos[1] >= 600 and pos[1] <= 650: + print(f"clicked at pos = {pos}") + return + self.render(selected_cmd) + + def run(self): running = True @@ -211,18 +253,37 @@ class Program: prg_surf = self.render_prg(prg_counter) screen.blit(prg_surf, (dx, board_surf.get_height()+2*dy, prg_surf.get_width(), prg_surf.get_height())) + inp_surf = self.render_inputs() + screen.blit(inp_surf, (dx, board_surf.get_height()+4*dy, inp_surf.get_width(), inp_surf.get_height())) + + run_surf = pygame.Surface((80, 50)) + run_surf.fill(tuple(GREEN)) + run_surf.blit(run_text, (0,10)) + screen.blit(run_surf, (325, 600, run_surf.get_height(), run_surf.get_width())) + pygame.display.update() + def render_inputs(self): + inp_surf = pygame.Surface((len(self.available_inputs) * scale_fac, 1 * scale_fac)) + + for i, inp in enumerate(self.available_inputs): + cmd_surf = inp.render() + inp_surf.blit(cmd_surf, (i * scale_fac, 0, scale_fac, scale_fac)) + return inp_surf + def render_prg(self, prg_counter=None): prg_surf = pygame.Surface((5 * scale_fac, 1 * scale_fac)) - prg_surf.fill(WHITE) for i in range(5): if i < len(self.cmds): cmd = self.cmds[i] cmd_surf = cmd.render() - 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)) + else: + cmd_surf = pygame.Surface((50,50)) + cmd_surf.fill(WHITE) + 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)) + return prg_surf r = Robot(x=1, y=1, orientation='v') @@ -230,6 +291,7 @@ b = Board(dimx,dimy, r) b.tiles[3,4]['star'] = True print(b) + cmds = [Command('forward'), Command('left', color=RED), Command('left', color=BLUE), Command('P0')] prg = Program(r, b, cmds)