Male_zoo_Projekt_SI/main.py

158 lines
5.6 KiB
Python

import random
import pygame
import sys
sys.path.append('./Animals')
from animals import create_animals, draw_Animals
from agent import Agent
from enclosure import create_enclosures, draw_enclosures, draw_gates
from spawner import Spawner
from state_space_search import graphsearch, generate_cost_map
from terrain_obstacle import create_obstacles, draw_Terrain_Obstacles
from constants import Constants, init_pygame
from draw import draw_goal, draw_grid, draw_house
from season import draw_background
from night import change_time
from genetics import genetic_algorithm
const = Constants()
init_pygame(const)
pygame.display.set_caption("Mini Zoo")
obstacles = set()
animals_position = set()
terrain_obstacles_position = set()
Animals = create_animals()
Enclosures = create_enclosures()
Terrain_Obstacles = create_obstacles()
def spawn_all_animals():
for Animal in Animals:
spawner1 = Spawner(Animal)
spawner1.spawn_animal(obstacles, animals_position, Enclosures)
def spawn_obstacles():
for terrain_obstacle in Terrain_Obstacles:
spawner2 = Spawner(terrain_obstacle)
spawner2.spawn_terrain_obstacles(obstacles, animals_position, terrain_obstacles_position, const.GRID_WIDTH, const.GRID_HEIGHT)
def generate_obstacles():
for en in Enclosures:
# Pobierz współrzędne bramy
gate_x, gate_y = en.gate1
gate_x -= 1
gate_y -= 1
gate_x2, gate_y2 = en.gate2
gate_x2 -= 1
gate_y2 -= 1
# Dodaj lewy brzeg prostokąta
for y in range(en.y1, en.y2 + 1):
if (en.x1, y) != (gate_x, gate_y) and (en.x1, y) != (gate_x2, gate_y2):
obstacles.add((en.x1, y))
# Dodaj prawy brzeg prostokąta
for y in range(en.y1, en.y2 + 1):
if (en.x2, y) != (gate_x, gate_y) and (en.x2, y) != (gate_x2, gate_y2):
obstacles.add((en.x2, y))
# Dodaj górny brzeg prostokąta
for x in range(en.x1+1, en.x2):
if (x, en.y1) != (gate_x, gate_y) and (x, en.y1) != (gate_x2, gate_y2):
obstacles.add((x, en.y1))
# Dodaj dolny brzeg prostokąta
for x in range(en.x1+1, en.x2):
if (x, en.y2) != (gate_x, gate_y) and (x, en.y2) != (gate_x2, gate_y2):
obstacles.add((x, en.y2))
return obstacles
def main():
initial_state = (0, 0, 'S')
agent = Agent(initial_state, 'images/agent1.png', const.GRID_SIZE)
obstacles = generate_obstacles()
actions = []
clock = pygame.time.Clock()
spawned = False
route = False
# # Lista zawierająca klatki do odwiedzenia
# enclosures_to_visit = Enclosures.copy()
# current_enclosure_index = -1 # Indeks bieżącej klatki
# actions_to_compare_list = [] # Lista zawierająca ścieżki do porównania
# goals_to_compare_list = list() # Lista zawierająca cele do porównania
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
agent.handle_event(event, const.GRID_WIDTH, const.GRID_HEIGHT, Animals, obstacles,const)
change_time(const)
draw_background(const)
draw_enclosures(Enclosures, const)
draw_gates(Enclosures, const)
draw_house(const)
if not spawned:
spawn_all_animals()
spawn_obstacles()
cost_map = generate_cost_map(Animals, Terrain_Obstacles)
for animal in Animals:
# animal._feed = 0
animal._feed = random.randint(0, 10)
spawned = True
if not route:
animals = [(animal.x, animal.y) for animal in Animals]
best_route = genetic_algorithm(animals)
route = True
draw_Animals(Animals, const)
draw_Terrain_Obstacles(Terrain_Obstacles, const)
agent.draw(const)
pygame.display.flip()
clock.tick(10)
if actions:
action = actions.pop(0)
agent.move(action, const.GRID_WIDTH, const.GRID_HEIGHT, obstacles, Animals, goal,const)
pygame.time.wait(200)
else:
if agent._dryfood > 1 and agent._wetfood > 1 :
# if not goals_to_compare_list:
# current_enclosure_index = (current_enclosure_index + 1) % len(enclosures_to_visit)
# current_enclosure = enclosures_to_visit[current_enclosure_index]
# for animal in current_enclosure.animals:
# goal = (animal.x, animal.y)
# goals_to_compare_list.append(goal)
# actions_to_compare = graphsearch(agent.istate, goal, const.GRID_WIDTH, const.GRID_HEIGHT, obstacles, cost_map)
# actions_to_compare_list.append((actions_to_compare, goal))
# chosen_path_and_goal = min(actions_to_compare_list, key=lambda x: len(x[0]))
# goal = chosen_path_and_goal[1]
# draw_goal(const, goal)
# # Usuń wybrany element z listy
# actions_to_compare_list.remove(chosen_path_and_goal)
# goals_to_compare_list.remove(goal)
goal = best_route.pop(0)
best_route.append(goal)
draw_goal(const, goal)
actions, cost = graphsearch(agent.istate, goal, const.GRID_WIDTH, const.GRID_HEIGHT, obstacles, cost_map)
else:
goal = (3,1)
draw_goal(const, goal)
actions, cost = graphsearch(agent.istate, goal, const.GRID_WIDTH, const.GRID_HEIGHT, obstacles, cost_map)
if __name__ == "__main__":
main()