2024-04-12 21:50:11 +02:00
|
|
|
import random
|
2024-03-05 14:36:03 +01:00
|
|
|
import pygame
|
|
|
|
import sys
|
2024-03-22 16:59:39 +01:00
|
|
|
from elephant import Elephant
|
2024-03-22 20:11:58 +01:00
|
|
|
from giraffe import Giraffe
|
|
|
|
from penguin import Penguin
|
|
|
|
from parrot import Parrot
|
|
|
|
from bear import Bear
|
2024-03-22 16:59:39 +01:00
|
|
|
from agent import Agent
|
2024-03-24 14:55:49 +01:00
|
|
|
from enclosure import Enclosure
|
2024-03-24 17:33:58 +01:00
|
|
|
from spawner import Spawner
|
2024-04-12 18:17:30 +02:00
|
|
|
from state_space_search import graphsearch
|
2024-03-05 14:36:03 +01:00
|
|
|
|
|
|
|
BLACK = (0, 0, 0)
|
|
|
|
|
2024-04-12 23:11:46 +02:00
|
|
|
GRID_SIZE = 64
|
2024-03-24 14:55:49 +01:00
|
|
|
GRID_WIDTH = 30
|
|
|
|
GRID_HEIGHT = 15
|
2024-03-05 14:36:03 +01:00
|
|
|
|
|
|
|
pygame.init()
|
|
|
|
|
|
|
|
WINDOW_SIZE = (GRID_WIDTH * GRID_SIZE, GRID_HEIGHT * GRID_SIZE)
|
|
|
|
screen = pygame.display.set_mode(WINDOW_SIZE)
|
|
|
|
pygame.display.set_caption("Mini Zoo")
|
|
|
|
|
2024-03-22 16:59:39 +01:00
|
|
|
|
2024-03-05 14:36:03 +01:00
|
|
|
|
2024-03-22 20:11:58 +01:00
|
|
|
background_image = pygame.image.load('images/tło.jpg')
|
2024-03-05 14:36:03 +01:00
|
|
|
background_image = pygame.transform.scale(background_image, WINDOW_SIZE)
|
2024-03-24 14:55:49 +01:00
|
|
|
fenceH = pygame.image.load('images/fenceHor.png')
|
|
|
|
fenceV = pygame.image.load('images/fenceVer.png')
|
2024-03-24 17:33:58 +01:00
|
|
|
gate = pygame.image.load('images/gate.png')
|
2024-03-05 14:36:03 +01:00
|
|
|
|
2024-03-10 17:08:39 +01:00
|
|
|
|
2024-03-24 17:33:58 +01:00
|
|
|
fences = set()
|
2024-03-24 17:46:13 +01:00
|
|
|
animals_position = set()
|
2024-03-23 13:55:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
an1 = Parrot(16, 2)
|
|
|
|
an2 = Penguin(8, 6)
|
|
|
|
an3 = Bear(14, 9)
|
2024-03-22 20:11:58 +01:00
|
|
|
old_an2 = Giraffe(18,4, adult=True)
|
2024-03-23 13:55:16 +01:00
|
|
|
old_an1 = Elephant(4, 7, adult=True)
|
|
|
|
an4 = Elephant(4,3)
|
2024-03-10 17:08:39 +01:00
|
|
|
|
2024-03-23 13:55:16 +01:00
|
|
|
Animals = [an1, an2, an3, an4, old_an1, old_an2]
|
2024-03-10 17:40:26 +01:00
|
|
|
|
2024-04-12 18:17:30 +02:00
|
|
|
# Enclosure (lewy_górny_x, lewy_górny_y, prawy_dolny_x, prawy_dolny_y, brama, klimat, fenceH, fenceV, gate_obrazek)
|
|
|
|
en1 = Enclosure(1,5, 9,11, (9,6),"medium", fenceH, fenceV, gate)
|
|
|
|
en2 = Enclosure(13,1, 29,3, (16,3), 'medium', fenceH, fenceV, gate)
|
2024-03-24 17:33:58 +01:00
|
|
|
en3 = Enclosure(11,5, 16,11, (12,5),'cold', fenceH, fenceV, gate)
|
2024-04-12 18:17:30 +02:00
|
|
|
en4 = Enclosure(19,5, 30,11, (25,5),'hot', fenceH, fenceV, gate)
|
2024-03-24 17:33:58 +01:00
|
|
|
en5 = Enclosure(4,13, 28,15, (16,13),'cold', fenceH, fenceV, gate)
|
2024-03-24 14:55:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
Enclosures = [en1, en2, en3, en4, en5]
|
|
|
|
|
|
|
|
|
2024-03-05 14:36:03 +01:00
|
|
|
def draw_grid():
|
|
|
|
for y in range(0, GRID_HEIGHT * GRID_SIZE, GRID_SIZE):
|
|
|
|
for x in range(0, GRID_WIDTH * GRID_SIZE, GRID_SIZE):
|
|
|
|
rect = pygame.Rect(x, y, GRID_SIZE, GRID_SIZE)
|
|
|
|
pygame.draw.rect(screen, BLACK, rect, 1)
|
|
|
|
|
2024-03-24 17:33:58 +01:00
|
|
|
def draw_enclosures():
|
2024-03-24 14:55:49 +01:00
|
|
|
for enclosure in Enclosures:
|
2024-03-24 17:33:58 +01:00
|
|
|
enclosure.draw(screen, GRID_SIZE, fences)
|
|
|
|
|
|
|
|
def draw_gates():
|
|
|
|
for enclosure in Enclosures:
|
|
|
|
enclosure.gatebuild(screen, GRID_SIZE)
|
|
|
|
|
|
|
|
def opengates():
|
|
|
|
for enclosure in Enclosures:
|
|
|
|
enclosure.gateopen(fences)
|
2024-03-22 16:59:39 +01:00
|
|
|
|
2024-03-22 17:16:34 +01:00
|
|
|
def draw_Animals():
|
|
|
|
for Animal in Animals:
|
|
|
|
Animal.draw(screen, GRID_SIZE)
|
|
|
|
if Animal.feed() == 'True':
|
2024-03-23 13:55:16 +01:00
|
|
|
Animal.draw_exclamation(screen, GRID_SIZE, Animal.x, Animal.y)
|
|
|
|
else:
|
|
|
|
Animal.draw_food(screen,GRID_SIZE,Animal.x,Animal.y)
|
2024-03-05 14:36:03 +01:00
|
|
|
|
2024-03-24 17:33:58 +01:00
|
|
|
def spawn_all_animals():
|
|
|
|
for Animal in Animals:
|
|
|
|
spawner1 = Spawner(Animal, Enclosures)
|
2024-03-24 17:46:13 +01:00
|
|
|
spawner1.spawn_animal(fences, animals_position)
|
2024-03-24 17:33:58 +01:00
|
|
|
|
2024-04-12 18:17:30 +02:00
|
|
|
def generate_obstacles():
|
|
|
|
obstacles = []
|
|
|
|
|
|
|
|
for en in Enclosures:
|
|
|
|
# Pobierz współrzędne bramy
|
|
|
|
gate_x, gate_y = en.gate
|
|
|
|
gate_x -= 1
|
|
|
|
gate_y -= 1
|
|
|
|
|
|
|
|
# Dodaj lewy brzeg prostokąta
|
|
|
|
for y in range(en.y1, en.y2 + 1):
|
|
|
|
if (en.x1, y) != (gate_x, gate_y):
|
|
|
|
obstacles.append((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):
|
|
|
|
obstacles.append((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):
|
|
|
|
obstacles.append((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):
|
|
|
|
obstacles.append((x, en.y2))
|
|
|
|
|
|
|
|
return obstacles
|
|
|
|
|
|
|
|
# region Obstacles Tests
|
|
|
|
# WHITE = (255,255,255)
|
|
|
|
# TRANSPARENT_BLACK = (0, 0, 0, 128) # Półprzezroczysty czarny
|
|
|
|
# def draw_obstacles(obstacles):
|
|
|
|
# for obstacle in obstacles:
|
|
|
|
# x, y = obstacle
|
|
|
|
# pygame.draw.rect(screen, TRANSPARENT_BLACK, (x * GRID_SIZE, y * GRID_SIZE, GRID_SIZE, GRID_SIZE))
|
|
|
|
|
|
|
|
# def main():
|
|
|
|
# obstacles = generate_obstacles() # Załóżmy, że masz funkcję generate_obstacles, która generuje listę przeszkód
|
|
|
|
|
|
|
|
# # Pętla główna
|
|
|
|
# running = True
|
|
|
|
# while running:
|
|
|
|
# # Obsługa zdarzeń
|
|
|
|
# for event in pygame.event.get():
|
|
|
|
# if event.type == pygame.QUIT:
|
|
|
|
# running = False
|
|
|
|
|
|
|
|
# # Czyszczenie ekranu
|
|
|
|
# screen.fill(WHITE)
|
|
|
|
|
|
|
|
# # Rysowanie przeszkód
|
|
|
|
# draw_obstacles(obstacles)
|
|
|
|
|
|
|
|
# # Odświeżenie ekranu
|
|
|
|
# pygame.display.flip()
|
|
|
|
|
|
|
|
# # Wyjście z Pygame
|
|
|
|
# pygame.quit()
|
|
|
|
|
|
|
|
# endregion
|
2024-03-10 17:40:26 +01:00
|
|
|
|
2024-03-05 14:36:03 +01:00
|
|
|
def main():
|
2024-04-12 18:17:30 +02:00
|
|
|
initial_state = (1,1,'W')
|
2024-04-12 22:11:58 +02:00
|
|
|
agent = Agent(initial_state, 'images/agent1.png', GRID_SIZE)
|
2024-04-12 18:17:30 +02:00
|
|
|
|
|
|
|
obstacles = generate_obstacles()
|
|
|
|
|
|
|
|
actions = []
|
2024-03-05 14:36:03 +01:00
|
|
|
clock = pygame.time.Clock()
|
2024-04-12 21:50:11 +02:00
|
|
|
|
|
|
|
spawned = False
|
2024-03-05 14:36:03 +01:00
|
|
|
while True:
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
pygame.quit()
|
|
|
|
sys.exit()
|
2024-04-12 18:17:30 +02:00
|
|
|
agent.handle_event(event, GRID_WIDTH, GRID_HEIGHT, Animals, obstacles)
|
2024-03-05 14:36:03 +01:00
|
|
|
|
|
|
|
screen.blit(background_image,(0,0))
|
|
|
|
draw_grid()
|
2024-03-24 17:33:58 +01:00
|
|
|
draw_enclosures()
|
|
|
|
draw_gates()
|
2024-04-12 21:50:11 +02:00
|
|
|
if not spawned:
|
|
|
|
spawn_all_animals()
|
|
|
|
|
|
|
|
# region Test szukania ścieżki
|
2024-04-12 23:11:46 +02:00
|
|
|
for animal in Animals:
|
|
|
|
animal._feed = 2 # Ustawienie aby zwierzę było głodne
|
2024-04-12 21:50:11 +02:00
|
|
|
animal = random.choice(Animals)
|
2024-04-12 23:11:46 +02:00
|
|
|
# actions = graphsearch(agent.istate, (animal.x, animal.y), GRID_WIDTH, GRID_HEIGHT, obstacles)
|
2024-04-12 21:50:11 +02:00
|
|
|
# endregion
|
|
|
|
|
|
|
|
spawned = True
|
2024-03-24 17:33:58 +01:00
|
|
|
draw_Animals()
|
|
|
|
opengates()
|
2024-04-12 22:11:58 +02:00
|
|
|
agent.draw(screen, GRID_SIZE)
|
2024-03-05 14:36:03 +01:00
|
|
|
pygame.display.flip()
|
|
|
|
clock.tick(10)
|
2024-04-12 18:17:30 +02:00
|
|
|
|
|
|
|
if actions:
|
|
|
|
action = actions.pop(0)
|
2024-04-12 21:50:11 +02:00
|
|
|
agent.move(action, GRID_WIDTH, GRID_HEIGHT, obstacles, Animals)
|
2024-04-12 18:17:30 +02:00
|
|
|
pygame.time.wait(200)
|
|
|
|
|
2024-03-05 14:36:03 +01:00
|
|
|
if __name__ == "__main__":
|
2024-04-12 18:17:30 +02:00
|
|
|
main()
|