Male_zoo_Projekt_SI/main.py

149 lines
5.3 KiB
Python
Raw Normal View History

import random
import pygame
import sys
sys.path.append('./Animals')
from animals import create_animals, draw_Animals
2024-03-22 16:59:39 +01:00
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
2024-05-08 21:18:55 +02:00
from draw import draw_goal, draw_grid, draw_house
from season import draw_background
from night import change_time
const = Constants()
init_pygame(const)
pygame.display.set_caption("Mini Zoo")
obstacles = set()
2024-03-24 17:46:13 +01:00
animals_position = set()
2024-04-26 23:45:06 +02:00
terrain_obstacles_position = set()
2024-03-23 13:55:16 +01:00
2024-04-27 19:50:18 +02:00
Animals = create_animals()
Enclosures = create_enclosures()
Terrain_Obstacles = create_obstacles()
def spawn_all_animals():
for Animal in Animals:
2024-04-26 23:45:06 +02:00
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
2024-05-12 23:05:28 +02:00
# 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()
2024-05-12 16:25:09 +02:00
agent.handle_event(event, const.GRID_WIDTH, const.GRID_HEIGHT, Animals, obstacles,const)
change_time(const)
draw_background(const)
draw_grid(const)
draw_enclosures(Enclosures, const)
draw_gates(Enclosures, const)
2024-05-08 21:18:55 +02:00
draw_house(const)
2024-05-12 16:25:09 +02:00
if not spawned:
spawn_all_animals()
2024-04-26 23:45:06 +02:00
spawn_obstacles()
cost_map = generate_cost_map(Animals, Terrain_Obstacles)
for animal in Animals:
2024-05-12 23:05:28 +02:00
# animal._feed = 0
animal._feed = random.randint(0, 10)
spawned = 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)
2024-05-12 16:25:09 +02:00
agent.move(action, const.GRID_WIDTH, const.GRID_HEIGHT, obstacles, Animals, goal,const)
pygame.time.wait(200)
else:
2024-05-12 23:05:28 +02:00
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]
2024-05-08 21:18:55 +02:00
draw_goal(const, goal)
2024-05-12 23:05:28 +02:00
# Usuń wybrany element z listy
actions_to_compare_list.remove(chosen_path_and_goal)
goals_to_compare_list.remove(goal)
2024-05-08 21:18:55 +02:00
actions = graphsearch(agent.istate, goal, const.GRID_WIDTH, const.GRID_HEIGHT, obstacles, cost_map)
2024-05-12 23:05:28 +02:00
2024-05-08 21:18:55 +02:00
else:
goal = (3,1)
draw_goal(const, goal)
actions = graphsearch(agent.istate, goal, const.GRID_WIDTH, const.GRID_HEIGHT, obstacles, cost_map)
2024-04-27 19:50:18 +02:00
if __name__ == "__main__":
main()