def _throw(self, ant, colony): """Animate a leaf thrown at a Bee.""" bee = ant.nearest_bee(colony.hive) # nearest_bee logic from ants.py if bee: start = shift_point(self.place_points[ant.place.name], LEAF_START_OFFSET) end = shift_point(self.place_points[bee.place.name], LEAF_END_OFFSET) animate_leaf(self.canvas, start, end, color=LEAF_COLORS[ant.name])
def _draw_insect(self, insect, place_name, random_offset=False, behind=0): """Draw an insect and store the ID of its image.""" image_file = INSECT_FILES[insect.name] pos = shift_point(self.place_points[place_name], PLACE_PADDING) if random_offset: pos = shift_point(pos, (random.randint(-10, 10), random.randint(-50, 50))) image = self.canvas.draw_image(pos, image_file, behind=behind) self.images[place_name][insect] = image
def _init_places(self, gamestate): """Construct places in the play area.""" self.place_points = dict() # self.images: place_name -> insect instance -> image id self.images = {'Ant Home Base': dict()} place_pos = PLACE_POS width = BEE_IMAGE_WIDTH + 2 * PLACE_PADDING[0] height = ANT_IMAGE_HEIGHT + 2 * PLACE_PADDING[1] rows = 0 for name, place in gamestate.places.items(): if place.name == 'Hive': continue # Handled as a special case later if place.exit.name == 'Ant Home Base': row_offset = (0, rows * (height + PLACE_MARGIN)) place_pos = shift_point(PLACE_POS, row_offset) rows += 1 def on_click(gamestate, frame, name=name): ant_type = self.ant_type_selected existing_ant = gamestate.places[name].ant if ant_type is 'Remover': if existing_ant is not None: print("gamestate.remove_ant('{0}')".format(name)) gamestate.remove_ant(name) self._update_places(gamestate) elif ant_type is not None: try: print("gamestate.deploy_ant('{0}', '{1}')".format( name, ant_type)) gamestate.deploy_ant(name, ant_type) self._update_places(gamestate) except Exception as e: print(e) color = '#99F1F2' if place.name.startswith( 'water') else 'White' # change water color here frame = self.add_click_rect(place_pos, width, height, on_click, color=color) self.canvas.draw_image(place_pos, TUNNEL_FILE) self.place_points[name] = place_pos self.images[name] = dict() place_pos = shift_point(place_pos, (width + PLACE_MARGIN, 0)) # Hive self.images[gamestate.beehive.name] = dict() self.place_points[gamestate.beehive.name] = (place_pos[0] + width, HIVE_HEIGHT) self.laser_end = (BEE_IMAGE_WIDTH + 2 * PLACE_PADDING[0]) * len( gamestate.places) for bee in gamestate.beehive.bees: self._draw_insect(bee, gamestate.beehive.name, True)
def _init_places(self, colony): """Construct places in the play area.""" self.place_points = dict() # self.images: place_name -> insect instance -> image id self.images = {'AntQueen': dict()} place_pos = PLACE_POS width = BEE_IMAGE_WIDTH + 2 * PLACE_PADDING[0] height = ANT_IMAGE_HEIGHT + 2 * PLACE_PADDING[1] rows = 0 for name, place in colony.places.items(): if place.name == 'Hive': continue # Handled as a special case later if place.exit.name == 'AntQueen': row_offset = (0, rows * (height + PLACE_MARGIN)) place_pos = shift_point(PLACE_POS, row_offset) rows += 1 def on_click(colony, frame, name=name): ant_type = self.ant_type_selected existing_ant = colony.places[name].ant if ant_type is 'Remover': if existing_ant is not None: print("colony.remove_ant('{0}')".format(name)) colony.remove_ant(name) self._update_places(colony) elif ant_type is not None: try: print("colony.deploy_ant('{0}', '{1}')".format(name, ant_type)) colony.deploy_ant(name, ant_type) self._update_places(colony) except Exception as e: print(e) color = 'Blue' if place.name.startswith('water') else 'White' frame = self.add_click_rect(place_pos, width, height, on_click, color=color) self.canvas.draw_image(place_pos, TUNNEL_FILE) self.place_points[name] = place_pos self.images[name] = dict() place_pos = shift_point(place_pos, (width + PLACE_MARGIN, 0)) # Hive self.images[colony.hive.name] = dict() self.place_points[colony.hive.name] = (place_pos[0] + width, HIVE_HEIGHT) self.laser_end = (BEE_IMAGE_WIDTH + 2 * PLACE_PADDING[0]) * len(colony.places) for bee in colony.hive.bees: self._draw_insect(bee, colony.hive.name, True)
def _init_places(self, colony): """Construct places in the play area.""" self.place_points = dict() # self.images: place_name -> insect instance -> image id self.images = {"AntQueen": dict()} place_pos = PLACE_POS width = BEE_IMAGE_WIDTH + 2 * PLACE_PADDING[0] height = ANT_IMAGE_HEIGHT + 2 * PLACE_PADDING[1] rows = 0 for name, place in colony.places.items(): if place.name == "Hive": continue # Handled as a special case later if place.exit.name == "AntQueen": row_offset = (0, rows * (height + PLACE_MARGIN)) place_pos = shift_point(PLACE_POS, row_offset) rows += 1 def on_click(colony, frame, name=name): ant_type = self.ant_type_selected existing_ant = colony.places[name].ant if ant_type is "Remover": if existing_ant is not None: print("colony.remove_ant("{0}")".format(name)) colony.remove_ant(name) self._update_places(colony) elif ant_type is not None: try: print("colony.deploy_ant("{0}", "{1}")".format(name, ant_type)) colony.deploy_ant(name, ant_type) self._update_places(colony) except Exception as e: print(e) color = "Blue" if place.name.startswith("water") else "White" frame = self.add_click_rect(place_pos, width, height, on_click, color=color) self.canvas.draw_image(place_pos, TUNNEL_FILE) self.place_points[name] = place_pos self.images[name] = dict() place_pos = shift_point(place_pos, (width + PLACE_MARGIN, 0)) # Hive self.images[colony.hive.name] = dict() self.place_points[colony.hive.name] = (place_pos[0] + width, HIVE_HEIGHT) self.laser_end = (BEE_IMAGE_WIDTH + 2 * PLACE_PADDING[0]) * len(colony.places) for bee in colony.hive.bees: self._draw_insect(bee, colony.hive.name, True)
def _init_control_panel(self, colony): """Construct the control panel of available ant types.""" self.ant_type_selected = None self.ant_type_frames = [] # rectangle ids of frames. panel_pos = PANEL_POS for name, ant_type in colony.ant_types.items(): width = ANT_IMAGE_WIDTH + 2 * PANEL_PADDING[0] height = ANT_IMAGE_HEIGHT + 6 + 2 * PANEL_PADDING[1] def on_click(colony, frame, name=name): self.ant_type_selected = name self._update_control_panel(colony) frame = self.add_click_rect(panel_pos, width, height, on_click) self.ant_type_frames.append((name, frame)) img_pos = shift_point(panel_pos, PANEL_PADDING) self.canvas.draw_image(img_pos, INSECT_FILES[name]) cost_pos = shift_point(panel_pos, (width / 2, ANT_IMAGE_HEIGHT + 4 + PANEL_PADDING[1])) food_str = str(ant_type.food_cost) self.canvas.draw_text(food_str, cost_pos, anchor="center") panel_pos = shift_point(panel_pos, (width + 2, 0))
def _update_places(self, gamestate): """Reflect the game state in the play area. This function handles several aspects of the game: - Adding Ant images for newly placed ants - Moving Bee images for bees that have advanced - Moving insects out of play when they have expired """ for name, place in gamestate.places.items(): if place.name == 'Hive': continue current = self.images[name].keys() # Add/move missing insects if place.ant is not None: if isinstance(place.ant, ants.ContainerAnt) \ and place.ant.contained_ant and place.ant.contained_ant not in current: container = self.images[name][place.ant] self._draw_insect(place.ant.contained_ant, name, behind=container) if place.ant not in current: self._draw_insect(place.ant, name) for bee in place.bees: if bee not in current: other_places = [ p for p, i in self.images.items() if bee in i ] if other_places: other_place = other_places[0] image = self.images[other_place].pop(bee) pos = shift_point(self.place_points[name], PLACE_PADDING) self.canvas.slide_shape(image, pos, STRATEGY_SECONDS) self.images[name][bee] = image else: # Bee not found for some reason... pass # Remove expired insects valid_insects = set(place.bees + [place.ant]) if place.ant is not None and isinstance(place.ant, ants.ContainerAnt): valid_insects.add(place.ant.contained_ant) for insect in current - valid_insects: if not place.exit or insect not in self.images[ place.exit.name] and insect not in place.entrance.bees: image = self.images[name].pop(insect) pos = (self.place_points[name][0], CRYPT) self.canvas.slide_shape(image, pos, STRATEGY_SECONDS)
def _update_places(self, colony): """Reflect the game state in the play area. This function handles several aspects of the game: - Adding Ant images for newly placed ants - Moving Bee images for bees that have advanced - Moving insects out of play when they have expired """ for name, place in colony.places.items(): if place.name == 'Hive': continue current = self.images[name].keys() # Add/move missing insects if place.ant is not None: if hasattr(place.ant, 'container') and place.ant.container \ and place.ant.ant and place.ant.ant not in current: container = self.images[name][place.ant] self._draw_insect(place.ant.ant, name, behind=container) if place.ant not in current: self._draw_insect(place.ant, name) for bee in place.bees: if bee not in current: for other_place in colony.places.values(): if other_place.exit is place: break else: other_place = colony.hive image = self.images[other_place.name].pop(bee) pos = shift_point(self.place_points[name], PLACE_PADDING) self.canvas.slide_shape(image, pos, STRATEGY_SECONDS) self.images[name][bee] = image # Remove expired insects valid_insects = set(place.bees + [place.ant]) if place.ant is not None and hasattr(place.ant, 'container') and \ place.ant.container: valid_insects.add(place.ant.ant) for insect in current - valid_insects: if not place.exit or insect not in self.images[ place.exit.name]: image = self.images[name].pop(insect) pos = (self.place_points[name][0], CRYPT) self.canvas.slide_shape(image, pos, STRATEGY_SECONDS)
def _update_places(self, colony): """Reflect the game state in the play area. This function handles several aspects of the game: - Adding Ant images for newly placed ants - Moving Bee images for bees that have advanced - Moving insects out of play when they have expired """ for name, place in colony.places.items(): if place.name == 'Hive': continue current = self.images[name].keys() # Add/move missing insects if place.ant is not None: if hasattr(place.ant, 'container') and place.ant.container \ and place.ant.ant and place.ant.ant not in current: container = self.images[name][place.ant] self._draw_insect(place.ant.ant, name, behind=container) if place.ant not in current: self._draw_insect(place.ant, name) for bee in place.bees: if bee not in current: for other_place in colony.places.values(): if other_place.exit is place: break else: other_place = colony.hive image = self.images[other_place.name].pop(bee) pos = shift_point(self.place_points[name], PLACE_PADDING) self.canvas.slide_shape(image, pos, STRATEGY_SECONDS) self.images[name][bee] = image # Remove expired insects valid_insects = set(place.bees + [place.ant]) if place.ant is not None and hasattr(place.ant, 'container') and \ place.ant.container: valid_insects.add(place.ant.ant) for insect in current - valid_insects: if not place.exit or insect not in self.images[place.exit.name]: image = self.images[name].pop(insect) pos = (self.place_points[name][0], CRYPT) self.canvas.slide_shape(image, pos, STRATEGY_SECONDS)
def points_fn(frame_count): nonlocal start angle = pi / 8 * frame_count cs = leaf_coords(start, angle, length) start = shift_point(start, increment) return cs