forked from Telos4/RoboRally
implemented option to switch between randomly generated levels
This commit is contained in:
parent
e374198fef
commit
794c987899
|
@ -25,19 +25,26 @@ myfont_small = pygame.font.SysFont('Comic Sans MS', 45)
|
||||||
|
|
||||||
P0_text = myfont.render('P0', False, tuple(BLACK))
|
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:
|
class Board:
|
||||||
valid_colors = [GRAY, RED, BLUE]
|
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)
|
self.tiles = np.zeros((dim_y, dim_x), dtype=tiledt)
|
||||||
for x in range(dim_x):
|
for x in range(dim_x):
|
||||||
for y in range(dim_y):
|
for y in range(dim_y):
|
||||||
self.tiles[y, x]['color'] = random.choice(Board.valid_colors)
|
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):
|
def render(self, scale_fac):
|
||||||
dimy, dimx = self.tiles.shape
|
dimy, dimx = self.tiles.shape
|
||||||
board_surf = pygame.Surface((dimx * scale_fac, dimy * scale_fac))
|
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):
|
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.robot = Robot(x=robotx, y=roboty, orientation='v', use_real_robot=use_real_robot)
|
||||||
self.board = Board(dimx, dimy)
|
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
|
# TODO fix number of commands at 5
|
||||||
self.cmds = [Command('forward'), Command('left', color=RED), Command('left', color=BLUE), Command('P0'), Command('-')]
|
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),
|
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)))
|
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.won_text = myfont.render('YOU WON', False, BLACK)
|
||||||
self.run_text = myfont.render('RUN', False, tuple(BLACK))
|
self.run_text = myfont.render('RUN', False, tuple(BLACK))
|
||||||
self.stop_text = myfont_small.render('STOP', False, tuple(BLACK))
|
self.stop_text = myfont_small.render('STOP', False, tuple(BLACK))
|
||||||
|
@ -321,7 +324,7 @@ class Game:
|
||||||
|
|
||||||
# save initial state
|
# save initial state
|
||||||
self.initial_pos = (self.robot.x, self.robot.y, self.robot.orientation)
|
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):
|
def render(self):
|
||||||
"""Render the game screen.
|
"""Render the game screen.
|
||||||
|
@ -397,7 +400,7 @@ class Game:
|
||||||
# render messages
|
# render messages
|
||||||
if self.state == 'game_over':
|
if self.state == 'game_over':
|
||||||
game_over_surf = pygame.Surface(((self.screen.get_width() - dx) // 2, self.screen.get_height() // 2))
|
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.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))
|
(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))
|
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'
|
self.state = 'reset'
|
||||||
elif self.state == 'won':
|
elif self.state == 'won':
|
||||||
won_surf = pygame.Surface(((self.screen.get_width() - dx) // 2, self.screen.get_height() // 2))
|
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.blit(self.won_text,
|
||||||
((won_surf.get_width() - self.won_text.get_width()) // 2,
|
((won_surf.get_width() - self.won_text.get_width()) // 2,
|
||||||
(won_surf.get_height() - self.won_text.get_height()) // 2))
|
(won_surf.get_height() - self.won_text.get_height()) // 2))
|
||||||
|
@ -474,6 +477,9 @@ class Game:
|
||||||
self.state = self.prg.step(self.state)
|
self.state = self.prg.step(self.state)
|
||||||
elif event.key == pygame.K_r:
|
elif event.key == pygame.K_r:
|
||||||
self.state = 'reset'
|
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:
|
elif event.type == pygame.USEREVENT:
|
||||||
for i, cmd in enumerate(event.cmds):
|
for i, cmd in enumerate(event.cmds):
|
||||||
self.cmds[i].action = cmd.action
|
self.cmds[i].action = cmd.action
|
||||||
|
@ -488,7 +494,7 @@ class Game:
|
||||||
self.robot.y = self.initial_pos[1]
|
self.robot.y = self.initial_pos[1]
|
||||||
self.robot.orientation = self.initial_pos[2]
|
self.robot.orientation = self.initial_pos[2]
|
||||||
self.robot.update_pos(self.board.get_xdims(), self.board.get_ydims())
|
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'
|
return 'input'
|
||||||
|
|
||||||
def run(self):
|
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 = threading.Thread(target=app.run, kwargs={'host': '0.0.0.0', 'port': 5000})
|
||||||
webserver_thread.start()
|
webserver_thread.start()
|
||||||
|
|
||||||
seed = 2
|
seed = 4
|
||||||
random.seed(seed)
|
random.seed(seed)
|
||||||
np.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()
|
game.run()
|
||||||
|
|
||||||
# TODOs
|
# TODOs
|
||||||
|
|
Loading…
Reference in New Issue
Block a user