added possibility to load level file
This commit is contained in:
parent
65f195fecf
commit
e910041c95
|
@ -1,5 +1,4 @@
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import random
|
|
||||||
import pygame
|
import pygame
|
||||||
import os
|
import os
|
||||||
import threading
|
import threading
|
||||||
|
@ -32,18 +31,21 @@ 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, n_coins=2):
|
def __init__(self, dim_x, dim_y, n_coins=2, file=None):
|
||||||
self.tiles = np.zeros((dim_y, dim_x), dtype=tiledt)
|
if file is None:
|
||||||
for x in range(dim_x):
|
self.tiles = np.zeros((dim_y, dim_x), dtype=tiledt)
|
||||||
for y in range(dim_y):
|
for x in range(dim_x):
|
||||||
self.tiles[y, x]['color'] = random.choice(Board.valid_colors)
|
for y in range(dim_y):
|
||||||
|
self.tiles[y, x]['color'] = Board.valid_colors[np.random.randint(len(Board.valid_colors))]
|
||||||
|
|
||||||
coins_distributed = False
|
coins_distributed = n_coins == 0
|
||||||
while not coins_distributed:
|
while not coins_distributed:
|
||||||
coinx = np.random.randint(0, dim_x)
|
coinx = np.random.randint(0, dim_x)
|
||||||
coiny = np.random.randint(0, dim_y)
|
coiny = np.random.randint(0, dim_y)
|
||||||
self.tiles[coiny,coinx]['star'] = True
|
self.tiles[coiny,coinx]['star'] = True
|
||||||
coins_distributed = sum([t['star'] for t in self.tiles.flatten()]) == n_coins
|
coins_distributed = sum([t['star'] for t in self.tiles.flatten()]) == n_coins
|
||||||
|
else:
|
||||||
|
self.tiles = np.load(file)
|
||||||
|
|
||||||
def render(self, scale_fac):
|
def render(self, scale_fac):
|
||||||
dimy, dimx = self.tiles.shape
|
dimy, dimx = self.tiles.shape
|
||||||
|
@ -208,7 +210,7 @@ class Program:
|
||||||
self.prg_counter = 0
|
self.prg_counter = 0
|
||||||
self.screen_rect = None
|
self.screen_rect = None
|
||||||
|
|
||||||
def step(self, state='running'):
|
def step(self, state='running', check_victory=True):
|
||||||
if self.prg_counter >= len(self.cmds):
|
if self.prg_counter >= len(self.cmds):
|
||||||
return 'game_over'
|
return 'game_over'
|
||||||
cmd = self.cmds[self.prg_counter]
|
cmd = self.cmds[self.prg_counter]
|
||||||
|
@ -237,7 +239,8 @@ class Program:
|
||||||
elif cmd.action == 'P0':
|
elif cmd.action == 'P0':
|
||||||
self.prg_counter = 0
|
self.prg_counter = 0
|
||||||
else:
|
else:
|
||||||
print("color not matching -> skipping command")
|
#print("color not matching -> skipping command")
|
||||||
|
pass
|
||||||
|
|
||||||
# update state for new robot position
|
# update state for new robot position
|
||||||
if (not (0 <= self.robot.x < self.board.tiles.shape[1])) or not (0 <= self.robot.y < self.board.tiles.shape[0]):
|
if (not (0 <= self.robot.x < self.board.tiles.shape[1])) or not (0 <= self.robot.y < self.board.tiles.shape[0]):
|
||||||
|
@ -251,7 +254,7 @@ class Program:
|
||||||
if tile['star']:
|
if tile['star']:
|
||||||
tile['star'] = False
|
tile['star'] = False
|
||||||
|
|
||||||
if all([not t['star'] for t in self.board.tiles.flatten()]):
|
if check_victory and all([not t['star'] for t in self.board.tiles.flatten()]):
|
||||||
self.robot.update_pos(self.board.get_xdims(), self.board.get_ydims())
|
self.robot.update_pos(self.board.get_xdims(), self.board.get_ydims())
|
||||||
print("YOU WON")
|
print("YOU WON")
|
||||||
return 'won'
|
return 'won'
|
||||||
|
@ -296,9 +299,10 @@ class Program:
|
||||||
|
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
def __init__(self, dimx, dimy, robotx, roboty, use_real_robot=False):
|
def __init__(self, dimx, dimy, robotx, roboty, orientation, 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=orientation, use_real_robot=use_real_robot)
|
||||||
self.board = Board(dimx, dimy)
|
#self.board = Board(dimx, dimy)
|
||||||
|
self.board = Board(dimx, dimy, file='levels/56.npy')
|
||||||
|
|
||||||
# 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('-')]
|
||||||
|
@ -529,9 +533,8 @@ if __name__ == "__main__":
|
||||||
webserver_thread.start()
|
webserver_thread.start()
|
||||||
|
|
||||||
seed = 4
|
seed = 4
|
||||||
random.seed(seed)
|
|
||||||
np.random.seed(seed)
|
np.random.seed(seed)
|
||||||
game = Game(dimx=7, dimy=4, robotx=3, roboty=1, use_real_robot=False)
|
game = Game(dimx=7, dimy=4, robotx=5, roboty=1, orientation='>', use_real_robot=False)
|
||||||
game.run()
|
game.run()
|
||||||
|
|
||||||
# TODOs
|
# TODOs
|
||||||
|
|
Loading…
Reference in New Issue
Block a user