def load_from_file(file_path, scene_speed, statistics_panel_width): with open(file_path) as f: line_number = 1 for line in f: words = line.split() # skip empty lines if len(words) == 0: line_number += 1 continue # skip comments in file if words[0][0] == '#': line_number += 1 continue if words[0] == 'Scene': width = int(words[1]) height = int(words[2]) scene = Scene(width, height, scene_speed, statistics_panel_width) # elif words[0] == 'SensorDrivenRobot': # x = float(words[1]) # y = float(words[2]) # robot = SensorDrivenRobot(x, y, ROBOT_SIZE, ROBOT_WHEEL_RADIUS) # robot.label = line_number # scene.put(robot) elif words[0] == 'Box': x = int(words[1]) y = int(words[2]) size = int(words[3]) box = Box(x, y, size, Color.random_bright()) box.label = line_number scene.put(box) elif words[0] == 'Wall': x1 = int(words[1]) y1 = int(words[2]) x2 = int(words[3]) y2 = int(words[4]) point1 = Point(x1, y1) point2 = Point(x2, y2) wall = Wall(point1, point2, Color.random_bright()) wall.label = line_number scene.put(wall) elif words[0] == 'Light': x = int(words[1]) y = int(words[2]) emitting_power = int(words[3]) light = Light(x, y, emitting_power, Color.YELLOW, Color.BLACK) light.label = line_number scene.put(light) line_number += 1 f.closed return scene
def create_boxes(self, number_to_add=1): for i in range(number_to_add): x = random.randint(0, self.scene.width) y = random.randint(0, self.scene.height) size = random.randint(self.BOX_SIZE_MIN, self.BOX_SIZE_INTERVAL) box = self.build_box(x, y, size, Color.random_bright()) self.scene.put(box)
def create_box(self): x = random.randint(0, self.scene.width) y = random.randint(0, self.scene.height) size = random.randint(self.BOX_MIN_SIZE, self.BOX_MAX_SIZE) return Box(x, y, size, Color.random_bright())
def add_box_at_cursor(self): x, y = pygame.mouse.get_pos() box = self.build_box(x, y, self.BOX_SIZE, Color.random_bright()) self.scene.put(box)