def __crateMaze(self): i = 0 for y, row in enumerate(self.__matrix): for x, cell in enumerate(row): if cell == '#': self.__maze[y, x] = model.Wall(i, (x, y), color=(0, 0, 255)) self.__walls.append(self.__maze[y, x]) elif cell == '-': self.__maze[y, x] = model.Path(i, (x, y), color=(0, 0, 255)) self.__paths.append(self.__maze[y, x]) i += 1 for y, row in enumerate(self.__maze): for x, cell in enumerate(row): if type(cell) is model.Path: if type(self.__maze[y, x + 1]) is model.Path: cell.neighbors["right"] = self.__maze[y, x + 1] if type(self.__maze[y, x - 1]) is model.Path: cell.neighbors["left"] = self.__maze[y, x - 1] if type(self.__maze[y - 1, x]) is model.Path: cell.neighbors["up"] = self.__maze[y - 1, x] if type(self.__maze[y + 1, x]) is model.Path: cell.neighbors["down"] = self.__maze[y + 1, x]
def __init__(self, assets): super(TestView, self).__init__() self.assets = assets # Get events nwidget.events.clear(cocos.director.director.window) self.is_event_handler = True # Ui layer ui = CocosWidget() self.add(ui, z=1) # Load background bg = model.Background(self.assets) self.add(bg.node) # Load marker marker = model.Marker(self.assets) marker.node.position = 50, 50 marker.node.scale = 0.25 self.add(marker.node) # Snake instructions ui.widgets.append( nwidget.Label( text="Press arrow keys to change snake sprite", size=13, color=(255, 255, 255, 255) ).bounds(20, 150, 600, 180) ) # Load snake snake = model.Snake(self.assets) snake.node.position = 50, 100 snake.node.scale = 0.5 self.add(snake.node) self.snake = snake # Path instructions ui.widgets.append( nwidget.Label( text="Press arrow keys to move path in that direction\nL to increase length of path, R to reset path", size=13, color=(255, 255, 255, 255) ).bounds(20, 300, 600, 425) ) # Load a path and draw it self.path = model.Path() self.reset_path() self.path.move(x=-50) self.path.move(y=-30) self.path.move(x=-100) self.path.move(y=-20) self.path.move(y=-20) self.add(self.path)
def cps_value(path=model.Path()): vals = [0] * len(model.buildings) * 2 for i in range(len(model.buildings)): #cost_one, time_needed_one, num_one, excess_one = path.cost_of(model.buildings[i], path.index, False) cost_ten, time_needed_ten, num_ten, excess_ten = path.cost_of( model.buildings[i], path.index, True) vals[i] = model.buildings[i].rate vals[i + len(model.buildings)] = model.buildings[i].rate * num_ten return normalize(vals)
def weigh_functions(val_funcs, weights, path=model.Path()): if len(val_funcs) != len(weights): raise ValueError("Number of Functions and Weights don't match") for w in weights: if abs(w) > 1: raise ValueError("Weight not between -1 and 1") totals = [0] * len(model.buildings) * 2 for i in range(len(val_funcs)): vals = val_funcs[i](path) for j in range(len(model.buildings)): totals[j] += vals[j] * weights[i] return totals
def load_objects(self): assets = self.assets # Clear events from other views nwidget.events.clear(cocos.director.director.window) self.is_event_handler = True # View model & ui self.model = { "score": 0, "dead": False, "updated": False, "play_time": 0, } self.ui = model.Ui("game.py", self.model) self.add(self.ui, z=1) # Above the background # bind events nwidget.listen("GAME_RESTART", self.on_restart) nwidget.listen("GAME_GOTO_MENU", self.on_menu) # Background bg = model.Background(assets) self.add(bg.node) # Add path and snake self.snake = model.Snake(assets) self.path = model.Path() width, height = cocos.director.director.get_window_size() x = width / 2 y = height / 2 self.snake.jump(x, y) self.path.jump(x, y) self.add(self.snake.node) self.add(self.path) self.bounds = (0, 0, width, height) # Direction self.snake.right() self.vector = "RIGHT" self.speed = SNAKE_SPEED # Are we paused because we died? self.dead = False # Start~ self.marker = None self.inc_dt = 0 self.generate_marker()
def rate_value(path=model.Path()): vals = [0] * len(model.buildings) * 2 for i in range(len(model.buildings)): cost_one, time_needed_one, num_one, excess_one = path.cost_of( model.buildings[i], path.index, False) cost_ten, time_needed_ten, num_ten, excess_ten = path.cost_of( model.buildings[i], path.index, True) if path.total_cookies[path.index] + cost_one < model.FINAL_GOAL: vals[i] = (( (model.buildings[i].rate * num_one) / path.rate[path.index]) + 1)**(1 / time_needed_one) if path.total_cookies[path.index] + cost_ten < model.FINAL_GOAL: vals[i + len(model.buildings)] = (( (model.buildings[i].rate * num_ten) / path.rate[path.index]) + 1)**(1 / time_needed_ten) return normalize(vals)
def excess_value(path=model.Path()): vals = [0] * len(model.buildings) * 2 for i in range(len(model.buildings)): cost_one, time_needed_one, num_one, excess_one = path.cost_of( model.buildings[i], path.index, False) cost_ten, time_needed_ten, num_ten, excess_ten = path.cost_of( model.buildings[i], path.index, True) vals[i] += excess_one vals[i + len(model.buildings)] += excess_ten if path.total_cookies[path.index] + cost_one > model.FINAL_GOAL: vals[i] += model.FINAL_GOAL - path.total_cookies[path.index] if path.total_cookies[path.index] + cost_ten > model.FINAL_GOAL: vals[i + len(model.buildings )] += model.FINAL_GOAL - path.total_cookies[path.index] return normalize(vals)
def random_path(f, path=model.Path()): done = False while not done: building_vals = f(path) done = path.buy(vf.choose_weighted(building_vals)) return path
from numpy import loadtxt, array, empty, append matrix = array([[char for char in line if char != '\n'] for line in open(MAP_LEVEL1)]) maze = empty((20, 40), dtype=object) walls = [] paths = [] i = 0 for y, row in enumerate(matrix): for x, cell in enumerate(row): if cell == '#': maze[y, x] = model.Wall(i, (x, y), color=(0, 0, 255)) walls.append(maze[y, x]) elif cell == '-': maze[y, x] = model.Path(i, (x, y), color=(0, 0, 255)) paths.append(maze[y, x]) i += 1 for y, row in enumerate(maze): for x, cell in enumerate(row): if type(cell) is model.Path: if type(maze[y, x + 1]) is model.Path: cell.neighbors["right"] = maze[y, x + 1] if type(maze[y, x - 1]) is model.Path: cell.neighbors["left"] = maze[y, x - 1] if type(maze[y - 1, x]) is model.Path: cell.neighbors["up"] = maze[y - 1, x] if type(maze[y + 1, x]) is model.Path: cell.neighbors["down"] = maze[y + 1, x]
def make_path(f, path=model.Path()): done = False while not done: building_vals = f(path) done = path.buy(vf.choose_best(building_vals)) return path
def equal_value(path=model.Path()): return normalize([1] * len(model.buildings) * 2)