class Game(arcade.Window): """ Main application class. """ def __init__(self, width, height): super().__init__(width, height, 'Ecosystem Simulation') self.sprite_list = None self.ecosystem = None arcade.set_background_color(arcade.color.BLACK) def setup(self): self.sprite_list = arcade.SpriteList() self.ecosystem = Ecosystem(int(SCREEN_WIDTH / CELL_WIDTH), int(SCREEN_HEIGHT / CELL_HEIGHT)) def on_draw(self): """ Render the screen. """ arcade.start_render() self.sprite_list.draw() def update(self, delta_time): """ All the logic to move, and the game logic goes here. """ self.sprite_list = arcade.SpriteList() ecosystem_organisms = self.ecosystem.run() for organism in ecosystem_organisms: sprite = arcade.Sprite(organism.get_image(), 1) sprite.center_x = organism.x * CELL_WIDTH + CELL_WIDTH / 2 sprite.center_y = organism.y * CELL_HEIGHT + CELL_HEIGHT / 2 self.sprite_list.append(sprite)
def plot(steps): populations = { 'rabbit': [], 'bee': [], 'fox': [], 'flower': [], 'grass': [] } genetics_factors = {'rabbit': [], 'fox': []} ecosystem = Ecosystem(int(SCREEN_WIDTH / CELL_WIDTH), int(SCREEN_HEIGHT / CELL_HEIGHT)) # Iterate over time actual_steps = steps for i in range(steps): ecosystem_organisms = ecosystem.run() rabbits = 0 foxes = 0 bees = 0 flowers = 0 grass = 0 rabbit_genetics_factor = 0 fox_genetics_factor = 0 for organism in ecosystem_organisms: # Observe animal populations if organism.type == Type.RABBIT: rabbits += 1 rabbit_genetics_factor += organism.genetics_factor elif organism.type == Type.FOX: foxes += 1 fox_genetics_factor += organism.genetics_factor elif organism.type == Type.BEE: bees += 1 elif organism.type == Type.FLOWER: flowers += 1 elif organism.type == Type.GRASS: grass += 1 populations['rabbit'].append(rabbits) populations['fox'].append(foxes) populations['bee'].append(bees) populations['flower'].append(flowers) populations['grass'].append(grass) if not i % 100: print('Iteration ' + str(i) + ':') print(' ' + str(foxes) + ' foxes') print(' ' + str(rabbits) + ' rabbits') print(' ' + str(bees) + ' bees') print(' ' + str(flowers) + ' flowers') if rabbits != 0: genetics_factors['rabbit'].append(rabbit_genetics_factor / rabbits) else: actual_steps = i break if foxes != 0: genetics_factors['fox'].append(fox_genetics_factor / foxes) else: actual_steps = i break if actual_steps >= 25000: # Plot the results plt.plot(populations['rabbit'], label='Rabbits') plt.plot(populations['fox'], label='Foxes') plt.plot(populations['bee'], label='Bees') plt.plot(populations['flower'], label='Flowers') plt.plot(populations['grass'], label='Grass') plt.xlabel('Time') plt.legend(loc='upper right') plt.ylabel('Population amount') plt.show() plt.plot(genetics_factors['rabbit'], label='Rabbits') plt.plot(genetics_factors['fox'], label='Foxes') plt.xlabel('Time') plt.legend(loc='upper right') plt.ylabel('Genetics factor') plt.show() return True, actual_steps else: return False, actual_steps