diff --git a/gauss-turing/game/gauss_turing.py b/gauss-turing/game/gauss_turing.py index ea44fd7..1e24c9d 100644 --- a/gauss-turing/game/gauss_turing.py +++ b/gauss-turing/game/gauss_turing.py @@ -25,19 +25,26 @@ myfont_small = pygame.font.SysFont('Comic Sans MS', 45) P0_text = myfont.render('P0', False, tuple(BLACK)) -tiledt = np.dtype([('x', np.uint8), ('y', np.uint8), ('color', np.uint8, 3), ('star', np.bool)]) +tiledt = np.dtype([('color', np.uint8, 3), ('star', np.bool)]) class Board: valid_colors = [GRAY, RED, BLUE] - def __init__(self, dim_x, dim_y): + def __init__(self, dim_x, dim_y, n_coins=2): self.tiles = np.zeros((dim_y, dim_x), dtype=tiledt) for x in range(dim_x): for y in range(dim_y): self.tiles[y, x]['color'] = random.choice(Board.valid_colors) + coins_distributed = False + while not coins_distributed: + coinx = np.random.randint(0, dim_x) + coiny = np.random.randint(0, dim_y) + self.tiles[coiny,coinx]['star'] = True + coins_distributed = sum([t['star'] for t in self.tiles.flatten()]) == n_coins + def render(self, scale_fac): dimy, dimx = self.tiles.shape board_surf = pygame.Surface((dimx * scale_fac, dimy * scale_fac)) @@ -292,10 +299,6 @@ class Game: 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) - self.board.tiles[coin1y,coin1x]['star'] = True - self.board.tiles[3,2]['star'] = True # TODO fix number of commands at 5 self.cmds = [Command('forward'), Command('left', color=RED), Command('left', color=BLUE), Command('P0'), Command('-')] @@ -311,7 +314,7 @@ class Game: self.screen = pygame.display.set_mode((int(self.board.tiles.shape[1] * self.scale_fac * 1.1), int((self.board.tiles.shape[0] + 2) * self.scale_fac * 1.2))) - self.game_over_text = myfont.render('GAME OVER', False, BLACK) + self.game_over_text = myfont.render('GAME OVER', False, WHITE) self.won_text = myfont.render('YOU WON', False, BLACK) self.run_text = myfont.render('RUN', False, tuple(BLACK)) self.stop_text = myfont_small.render('STOP', False, tuple(BLACK)) @@ -321,7 +324,7 @@ class Game: # save initial state self.initial_pos = (self.robot.x, self.robot.y, self.robot.orientation) - self.inital_board_tiles = self.board.tiles.copy() + self.initial_board_tiles = self.board.tiles.copy() def render(self): """Render the game screen. @@ -397,7 +400,7 @@ class Game: # render messages if self.state == 'game_over': game_over_surf = pygame.Surface(((self.screen.get_width() - dx) // 2, self.screen.get_height() // 2)) - game_over_surf.fill(tuple(GREEN)) + game_over_surf.fill(tuple(BLACK)) game_over_surf.blit(self.game_over_text, ((game_over_surf.get_width() - self.game_over_text.get_width()) // 2, (game_over_surf.get_height() - self.game_over_text.get_height()) // 2)) self.screen.blit(game_over_surf, (dx + (self.screen.get_width() - dx) // 4, self.screen.get_height() // 4)) @@ -406,7 +409,7 @@ class Game: self.state = 'reset' elif self.state == 'won': won_surf = pygame.Surface(((self.screen.get_width() - dx) // 2, self.screen.get_height() // 2)) - won_surf.fill(tuple(GREEN)) + won_surf.fill(tuple(WHITE)) won_surf.blit(self.won_text, ((won_surf.get_width() - self.won_text.get_width()) // 2, (won_surf.get_height() - self.won_text.get_height()) // 2)) @@ -474,6 +477,9 @@ class Game: self.state = self.prg.step(self.state) elif event.key == pygame.K_r: self.state = 'reset' + elif event.key == pygame.K_n: + self.initial_board_tiles = Board(self.board.get_xdims(), self.board.get_ydims()).tiles.copy() + self.state = 'reset' elif event.type == pygame.USEREVENT: for i, cmd in enumerate(event.cmds): self.cmds[i].action = cmd.action @@ -488,7 +494,7 @@ class Game: self.robot.y = self.initial_pos[1] self.robot.orientation = self.initial_pos[2] self.robot.update_pos(self.board.get_xdims(), self.board.get_ydims()) - self.board.tiles = self.inital_board_tiles.copy() + self.board.tiles = self.initial_board_tiles.copy() return 'input' def run(self): @@ -522,10 +528,10 @@ if __name__ == "__main__": webserver_thread = threading.Thread(target=app.run, kwargs={'host': '0.0.0.0', 'port': 5000}) webserver_thread.start() - seed = 2 + seed = 4 random.seed(seed) np.random.seed(seed) - game = Game(dimx=7, dimy=4, robotx=3, roboty=1, use_real_robot=True) + game = Game(dimx=7, dimy=4, robotx=3, roboty=1, use_real_robot=False) game.run() # TODOs