Male_zoo_Projekt_SI/main.py
karolajoj a8814a763b -Uproszczenie kodu
-Dodanie wizualizacji dostępnych pól
-Zwiększenie rozmiaru klatek
-Zwiększenie ilości zwierząt
-Poprawienie spawnowania zwierząt
2024-04-16 14:50:06 +02:00

226 lines
6.6 KiB
Python

import random
import pygame
import sys
from elephant import Elephant
from giraffe import Giraffe
from penguin import Penguin
from parrot import Parrot
from bear import Bear
from agent import Agent
from enclosure import Enclosure
from spawner import Spawner
from state_space_search import graphsearch
BLACK = (0, 0, 0)
GRID_SIZE = 64
GRID_WIDTH = 30
GRID_HEIGHT = 15
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")
background_image = pygame.image.load('images/tło.jpg')
background_image = pygame.transform.scale(background_image, WINDOW_SIZE)
fenceH = pygame.image.load('images/fenceHor.png')
fenceV = pygame.image.load('images/fenceVer.png')
gate = pygame.image.load('images/gate.png')
obstacles = set()
animals_position = set()
# region Define the animals
giraffe1 = Giraffe(0, 0, adult=True)
giraffe2 = Giraffe(0, 0, adult=True)
giraffe3 = Giraffe(0, 0, adult=True)
giraffe4 = Giraffe(0, 0)
giraffe5 = Giraffe(0, 0)
bear1 = Bear(0, 0, adult=True)
bear2 = Bear(0, 0, adult=True)
bear3 = Bear(0, 0)
bear4 = Bear(0, 0)
bear5 = Bear(0, 0)
penguin1 = Penguin(0, 0)
penguin2 = Penguin(0, 0)
penguin3 = Penguin(0, 0)
penguin4 = Penguin(0, 0)
elephant1 = Elephant(0, 0, adult=True)
elephant2 = Elephant(0, 0, adult=True)
elephant3 = Elephant(0, 0)
elephant4 = Elephant(0, 0)
elephant5 = Elephant(0, 0)
parrot1 = Parrot(0, 0)
parrot2 = Parrot(0, 0)
parrot3 = Parrot(0, 0)
parrot4 = Parrot(0, 0)
parrot5 = Parrot(0, 0)
Animals = [giraffe1, giraffe2, giraffe3, giraffe4, giraffe5, bear1, bear2, bear3, bear4, bear5, elephant1, elephant2, elephant3, elephant4, elephant5, penguin1, penguin2, penguin3, penguin4, parrot1, parrot2, parrot3, parrot4, parrot5]
# endregion
# region Define Enclosures
# Enclosure (lewy_górny_x, lewy_górny_y, prawy_dolny_x, prawy_dolny_y, brama, klimat, fenceH, fenceV, gate_obrazek)
en1 = Enclosure(0,5, 9,11, (9,6),"hot", fenceH, fenceV, gate) # Lewa klatka
en2 = Enclosure(13,0, 29,3, (16,3), 'medium', fenceH, fenceV, gate) # Górna klatka
en3 = Enclosure(11,5, 16,11, (12,5),'cold', fenceH, fenceV, gate) # Środkowa klatka
en4 = Enclosure(19,5, 31,11, (25,5),'hot', fenceH, fenceV, gate) # Prawa klatka
en5 = Enclosure(4,13, 28,16, (16,13),'cold', fenceH, fenceV, gate) # Dolna klatka
Enclosures = [en1, en2, en3, en4, en5]
# endregion
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)
def draw_enclosures():
for enclosure in Enclosures:
enclosure.draw(screen, GRID_SIZE)
def draw_gates():
for enclosure in Enclosures:
enclosure.gatebuild(screen, GRID_SIZE)
def draw_Animals():
for Animal in Animals:
Animal.draw(screen, GRID_SIZE)
if Animal.feed() == 'True':
Animal.draw_exclamation(screen, GRID_SIZE, Animal.x, Animal.y)
else:
Animal.draw_food(screen,GRID_SIZE,Animal.x,Animal.y)
def spawn_all_animals():
for Animal in Animals:
spawner1 = Spawner(Animal, Enclosures)
spawner1.spawn_animal(obstacles, animals_position)
obstacles = set()
def generate_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.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):
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):
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):
obstacles.add((x, en.y2))
return obstacles
available_fields_small = set()
available_fields_large = set()
def generate_available_fields():
for enclosure in Enclosures:
for x in range(enclosure.x1 + 1, enclosure.x2):
for y in range(enclosure.y1 + 1, enclosure.y2):
field = (x, y)
if field not in obstacles:
available_fields_small.add(field)
if x < enclosure.x2 - 1 and y < enclosure.y2 - 1:
available_fields_large.add(field)
# region Fields Tests
WHITE = (255,255,255)
GREEN = (0, 255, 0)
YELLOW = (255, 255, 0)
BLACK = (0, 0, 0)
def draw_fields(fields, color):
for field in fields:
x, y = field
pygame.draw.rect(screen, color, (x * GRID_SIZE, y * GRID_SIZE, GRID_SIZE, GRID_SIZE))
def main_fields_tests():
obstacles = generate_obstacles()
while True:
screen.fill(WHITE)
draw_grid()
draw_fields(obstacles, BLACK)
generate_available_fields()
draw_fields(available_fields_small, GREEN)
draw_fields(available_fields_large, YELLOW)
# Odświeżenie ekranu
pygame.display.flip()
# endregion
# region Main Code
def main():
initial_state = (0,0,'S')
agent = Agent(initial_state, 'images/agent1.png', GRID_SIZE)
obstacles = generate_obstacles()
actions = []
clock = pygame.time.Clock()
spawned = False
while True:
# Manualne poruszanie agentem
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
agent.handle_event(event, GRID_WIDTH, GRID_HEIGHT, Animals, obstacles)
screen.blit(background_image,(0,0))
draw_grid()
draw_enclosures()
draw_gates()
if not spawned:
spawn_all_animals()
for animal in Animals:
animal._feed = 2 # Ustawienie aby zwierzę było głodne
spawned = True
draw_Animals()
agent.draw(screen, GRID_SIZE)
pygame.display.flip()
clock.tick(10)
if actions:
action = actions.pop(0)
agent.move(action, GRID_WIDTH, GRID_HEIGHT, obstacles, Animals)
pygame.time.wait(200)
else:
animal = random.choice(Animals)
actions = graphsearch(agent.istate, (animal.x, animal.y), GRID_WIDTH, GRID_HEIGHT, obstacles)
# endregion
if __name__ == "__main__":
debug_mode = False # Jeśli True to pokazuje dostępne pola
if debug_mode:
main_fields_tests()
else:
main()