Male_zoo_Projekt_SI/main.py

116 lines
3.8 KiB
Python

import random
import pygame
import sys
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
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
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.screen.blit(const.background_image, (0, 0))
draw_grid(const)
draw_enclosures(Enclosures, const)
draw_gates(Enclosures, const)
if not spawned:
spawn_all_animals()
spawn_obstacles()
cost_map = generate_cost_map(Animals, Terrain_Obstacles)
for animal in Animals:
animal._feed = 2 # Ustawienie, aby zwierzę było głodne
spawned = True
draw_Animals(Animals, const)
draw_Terrain_Obstacles(Terrain_Obstacles, const)
agent.draw(const.screen, const.GRID_SIZE)
pygame.display.flip()
clock.tick(10)
if actions:
action = actions.pop(0)
agent.move(action, const.GRID_WIDTH, const.GRID_HEIGHT, obstacles, Animals, goal)
pygame.time.wait(200)
else:
animal = random.choice(Animals)
goal = (animal.x, animal.y)
draw_goal(const, goal)
actions = graphsearch(agent.istate, goal, const.GRID_WIDTH, const.GRID_HEIGHT, obstacles, cost_map)
if __name__ == "__main__":
main()