Male_zoo_Projekt_SI/main.py

124 lines
4.1 KiB
Python
Raw Normal View History

import random
import pygame
import sys
sys.path.append('./Animals')
2024-05-08 20:19:48 +02:00
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
2024-05-08 20:19:48 +02:00
from season import draw_background, draw_season
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-08 20:19:48 +02:00
season = draw_season()
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)
2024-05-08 20:19:48 +02:00
draw_background(const, season)
draw_grid(const)
draw_enclosures(Enclosures, const)
draw_gates(Enclosures, const)
2024-05-08 21:18:55 +02:00
draw_house(const)
if not spawned:
2024-05-08 20:19:48 +02:00
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-10 01:56:12 +02:00
animal._feed = 3
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:
2024-05-08 21:18:55 +02:00
if agent._food != 0:
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)
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()