From 945833c8ac94ee8a719450df720206c805ab3a93 Mon Sep 17 00:00:00 2001 From: Simon Pirkelmann Date: Sun, 5 Sep 2021 20:43:22 +0200 Subject: [PATCH] implemented state machine and option to switch display mode for second screen --- gauss-turing/webserver/gauss_turing.py | 106 +++++++++++++++++++------ 1 file changed, 83 insertions(+), 23 deletions(-) diff --git a/gauss-turing/webserver/gauss_turing.py b/gauss-turing/webserver/gauss_turing.py index 9cb0897..8c934e7 100644 --- a/gauss-turing/webserver/gauss_turing.py +++ b/gauss-turing/webserver/gauss_turing.py @@ -3,6 +3,7 @@ import random import pygame import time import copy +import os BLACK = np.array([0, 0, 0], dtype=np.uint8) WHITE = np.array([255, 255, 255], dtype=np.uint8) @@ -13,27 +14,31 @@ GREEN = np.array([0, 255, 0], dtype=np.uint8) pygame.init() +#os.environ['SDL_VIDEO_WINDOW_POS'] = '1920, 280' + + 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) +myfont_small = pygame.font.SysFont('Comic Sans MS', 45) P0_text = myfont.render('P0', False, (0, 0, 0)) game_over_text = myfont.render('GAME OVER', False, RED) +won_text = myfont.render('YOU WON', False, GREEN) run_text = myfont.render('RUN', False, tuple(BLACK)) +stop_text = myfont_small.render('STOP', False, tuple(BLACK)) -random.seed(0) +random.seed(42) -dimx = 5 -dimy = 10 +dimx = 7 +dimy = 4 -scale_fac = 50 -screen = pygame.display.set_mode((dimx*scale_fac+200,dimy*scale_fac+300)) +scale_fac = 180 +screen = pygame.display.set_mode((dimx*scale_fac,dimy*scale_fac)) +#screen = pygame.display.set_mode((dimx*scale_fac+int(0.1*scale_fac),dimy*scale_fac+300)) tiledt = np.dtype([('x', np.uint8), ('y', np.uint8), ('color', np.uint8, 3), ('star', np.bool)]) - - - class Board: valid_colors = [WHITE, RED, BLUE] @@ -160,16 +165,22 @@ class Program: self.robot = robot self.board = board + self.initial_pos = (self.robot.x, self.robot.y, self.robot.orientation) + + self.state = 'input' + self.available_inputs = [Command('forward'), Command('left'), Command('right'), Command('P0'), Command('-', color=RED), Command('-', color=BLUE), Command('-', color=WHITE)] + self.fullscreen = False + def input_program(self): + self.state = 'input' + selected_cmd = 0 self.render(selected_cmd) - start = False - - while not start: + while self.state == 'input': # get all events ev = pygame.event.get() @@ -196,18 +207,34 @@ class Program: 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.state = 'running' + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_x: + if not self.fullscreen: + os.environ['SDL_VIDEO_WINDOW_POS'] = '1920, 280' + screen = pygame.display.set_mode((dimx * scale_fac, dimy * scale_fac), + pygame.NOFRAME) + self.fullscreen = True + else: + os.environ['SDL_VIDEO_WINDOW_POS'] = '0, 0' + screen = pygame.display.set_mode((dimx * scale_fac, dimy * scale_fac)) + self.fullscreen = False self.render(selected_cmd) + pygame.time.wait(100) + + self.run() + def run(self): - running = True prg_counter = 0 self.render(prg_counter) - while running: + self.state = 'running' + + while self.state == 'running': cmd = self.cmds[prg_counter] prg_counter += 1 @@ -231,21 +258,48 @@ class Program: else: print("color not matching -> skipping command") + # 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] >= 325 and pos[0] <= 400 and pos[1] >= 600 and pos[1] <= 650: + print(f"clicked at pos = {pos}") + self.state = 'input' + self.render(prg_counter) if (not (0 <= r.x < self.board.tiles.shape[1])) or not (0 <= r.y < self.board.tiles.shape[0]): print("GAME OVER") screen.blit(game_over_text, (50, 00)) pygame.display.update() - running = False + pygame.time.wait(1500) + self.state = 'input' + else: + tile = self.board.tiles[r.y,r.x] + if tile['star']: + tile['star'] = False - time.sleep(0.1) + if all([not t['star'] for t in self.board.tiles.flatten()]): + print("YOU WON") + screen.blit(won_text, (50, 00)) + pygame.display.update() + pygame.time.wait(1500) + self.state = 'input' + pygame.time.wait(100) + + self.robot.x = self.initial_pos[0] + self.robot.y = self.initial_pos[1] + self.robot.orientation = self.initial_pos[2] + self.input_program() def render(self, prg_counter=None): - dx = 50 - dy = 50 + dx = 0 + dy = 0 screen.fill(tuple(BLACK)) board_surf = self.board.render() screen.blit(board_surf, (dx, dy, dx + dimx * scale_fac, dy + dimy * scale_fac)) @@ -256,10 +310,14 @@ class Program: 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())) + btn_surf = pygame.Surface((80, 50)) + if self.state == 'input': + btn_surf.fill(tuple(GREEN)) + btn_surf.blit(run_text, (0, 10)) + elif self.state == 'running': + btn_surf.fill(tuple(RED)) + btn_surf.blit(stop_text, (0, 10)) + screen.blit(btn_surf, (325, board_surf.get_height()+2*scale_fac, btn_surf.get_height(), btn_surf.get_width())) pygame.display.update() @@ -288,7 +346,9 @@ class Program: r = Robot(x=1, y=1, orientation='v') b = Board(dimx,dimy, r) -b.tiles[3,4]['star'] = True +b.tiles[3,3]['star'] = True +b.tiles[3,2]['star'] = True + print(b)