def __init__(self, grid_size, amount_of_players=1): self.grid_size = grid_size self.is_multi = (amount_of_players > 1) self.amount_of_players = amount_of_players self.snakes = [] snake_pos = Vector(random.randrange(grid_size.x), random.randrange(grid_size.y)) self.snakes.append(Snake(snake_pos)) self.snakes[0].grow_pending = True self.snakes[0].move() self.handle_border_cross(self.snakes[0]) for i in range(1, amount_of_players): occupied_axis = [snake.head.position.y for snake in self.snakes] snake_pos = Vector(random.randrange(grid_size.x), random.randrange(grid_size.y)) while snake_pos.y in occupied_axis: snake_pos.y = random.randrange(grid_size.y) self.snakes.append(Snake(snake_pos, num=i)) self.snakes[i].grow_pending = True self.snakes[i].move() self.handle_border_cross(self.snakes[i]) apple_pos = Vector(random.randrange(grid_size.x), random.randrange(grid_size.y)) while apple_pos in [s.head.position for s in self.snakes]: apple_pos = Vector(random.randrange(grid_size.x), random.randrange(grid_size.y)) self.apple = Apple(apple_pos) score_tracker = ScoreTracker(Vector(0, 0), grid_size.x) time_tracker = TimeTracker(Vector(0, 0)) self.info_tracker = InfoTracker(Vector(0, 0), score_tracker, time_tracker) self.action_frame = None self.popup = None
def reset(self): new_snakes = [] snake_pos = Vector(random.randrange(self.grid_size.x), random.randrange(self.grid_size.y)) self.snakes[0].reset(snake_pos) new_snakes.append(self.snakes[0]) new_snakes[0].grow_pending = True new_snakes[0].move() self.handle_border_cross(new_snakes[0]) for snake in self.snakes[1:]: snake_pos = Vector(random.randrange(self.grid_size.x), random.randrange(self.grid_size.y)) occupied_axis = [snake.head.position.y for snake in new_snakes] while snake_pos.y in occupied_axis: snake_pos.y = random.randrange(self.grid_size.y) snake.reset(snake_pos) new_snakes.append(snake) new_snakes[-1].grow_pending = True new_snakes[-1].move() self.handle_border_cross(new_snakes[-1]) self.snakes = new_snakes apple_pos = Vector(random.randrange(self.grid_size.x), random.randrange(self.grid_size.y)) while apple_pos in [s.head.position for s in self.snakes]: apple_pos = Vector(random.randrange(self.grid_size.x), random.randrange(self.grid_size.y)) self.apple.position = apple_pos self.info_tracker.reset() self.action_frame.remove_rendering_component(self.popup) self.popup = None