Przeszukiwanie-stanów-A_star #2
BIN
images/bush.png
Normal file
BIN
images/bush.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 222 KiB |
BIN
images/puddle.png
Normal file
BIN
images/puddle.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.7 KiB |
42
main.py
42
main.py
@ -10,6 +10,7 @@ from agent import Agent
|
||||
from enclosure import Enclosure
|
||||
from spawner import Spawner
|
||||
from state_space_search import graphsearch
|
||||
from terrain_obstacle import Terrain_Obstacle
|
||||
|
||||
BLACK = (0, 0, 0)
|
||||
|
||||
@ -30,9 +31,11 @@ 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')
|
||||
|
||||
puddle_image = pygame.image.load('images/puddle.png')
|
||||
bush_image = pygame.image.load('images/bush.png')
|
||||
obstacles = set()
|
||||
animals_position = set()
|
||||
terrain_obstacles_position = set()
|
||||
|
||||
# region Define the animals
|
||||
giraffe1 = Giraffe(0, 0, adult=True)
|
||||
@ -73,6 +76,19 @@ en5 = Enclosure(4,13, 28,16, (16,13),'cold', fenceH, fenceV, gate) # Dolna klatk
|
||||
|
||||
Enclosures = [en1, en2, en3, en4, en5]
|
||||
# endregion
|
||||
puddle1 = Terrain_Obstacle(0,0,'puddle', puddle_image)
|
||||
puddle2 = Terrain_Obstacle(0,0,'puddle', puddle_image)
|
||||
puddle3 = Terrain_Obstacle(0,0,'puddle', puddle_image)
|
||||
puddle4 = Terrain_Obstacle(0,0,'puddle', puddle_image)
|
||||
puddle5 = Terrain_Obstacle(0,0,'puddle', puddle_image)
|
||||
puddle6 = Terrain_Obstacle(0,0,'puddle', puddle_image)
|
||||
puddle7 = Terrain_Obstacle(0,0,'puddle', puddle_image)
|
||||
bush1 = Terrain_Obstacle(0,0,'bush', bush_image)
|
||||
bush2 = Terrain_Obstacle(0,0,'bush', bush_image)
|
||||
bush3 = Terrain_Obstacle(0,0,'bush', bush_image)
|
||||
bush4 = Terrain_Obstacle(0,0,'bush', bush_image)
|
||||
bush5 = Terrain_Obstacle(0,0,'bush', bush_image)
|
||||
Terrain_Obstacles = [puddle1, puddle2, puddle3, puddle4, puddle5, puddle6, puddle7, bush1, bush2, bush3, bush4, bush5]
|
||||
|
||||
def draw_grid():
|
||||
for y in range(0, GRID_HEIGHT * GRID_SIZE, GRID_SIZE):
|
||||
@ -98,8 +114,18 @@ def draw_Animals():
|
||||
|
||||
def spawn_all_animals():
|
||||
for Animal in Animals:
|
||||
spawner1 = Spawner(Animal, Enclosures)
|
||||
spawner1.spawn_animal(obstacles, animals_position)
|
||||
spawner1 = Spawner(Animal)
|
||||
spawner1.spawn_animal(obstacles, animals_position, Enclosures)
|
||||
|
||||
def draw_Terrain_Obstacles():
|
||||
for terrain_obstacle in Terrain_Obstacles:
|
||||
terrain_obstacle.draw(screen, GRID_SIZE)
|
||||
|
||||
def spawn_obstacles():
|
||||
for terrain_obstacle in Terrain_Obstacles:
|
||||
spawner2= Spawner(terrain_obstacle)
|
||||
spawner2.spawn_terrain_obstacles(obstacles,animals_position, terrain_obstacles_position, GRID_WIDTH, GRID_HEIGHT)
|
||||
|
||||
|
||||
obstacles = set()
|
||||
def generate_obstacles():
|
||||
@ -178,6 +204,8 @@ cost_map = {}
|
||||
def generate_cost_map():
|
||||
adult_animal_cost = 10
|
||||
baby_animal_cost = 5
|
||||
puddle_cost = 1000
|
||||
bush_cost = 20
|
||||
for animal in Animals:
|
||||
if animal.adult:
|
||||
cost_map[(animal.x + 1, animal.y + 1)] = baby_animal_cost
|
||||
@ -186,7 +214,11 @@ def generate_cost_map():
|
||||
cost_map[(animal.x, animal.y)] = adult_animal_cost
|
||||
else:
|
||||
cost_map[(animal.x, animal.y)] = baby_animal_cost
|
||||
|
||||
for terrain_obstacle in Terrain_Obstacles:
|
||||
if terrain_obstacle.type == 'puddle':
|
||||
cost_map[(terrain_obstacle.x , terrain_obstacle.y )] = puddle_cost
|
||||
else:
|
||||
cost_map[(terrain_obstacle.x , terrain_obstacle.y )] = bush_cost
|
||||
# Inne pola z różnym kosztem
|
||||
# cost_map[(x, y)] = cost_value
|
||||
|
||||
@ -215,11 +247,13 @@ def main():
|
||||
draw_gates()
|
||||
if not spawned:
|
||||
spawn_all_animals()
|
||||
spawn_obstacles()
|
||||
generate_cost_map()
|
||||
for animal in Animals:
|
||||
animal._feed = 2 # Ustawienie aby zwierzę było głodne
|
||||
spawned = True
|
||||
draw_Animals()
|
||||
draw_Terrain_Obstacles()
|
||||
agent.draw(screen, GRID_SIZE)
|
||||
pygame.display.flip()
|
||||
clock.tick(10)
|
||||
|
40
spawner.py
40
spawner.py
@ -1,33 +1,44 @@
|
||||
import random
|
||||
|
||||
class Spawner:
|
||||
def __init__(self, animal, enclosures):
|
||||
self.animal = animal
|
||||
# Wyrażenie listowe filtrujące tylko te wybiegi, które pasują do środowiska zwierzęcia
|
||||
self.enclosures = [enclosure for enclosure in enclosures if enclosure.type == self.animal.environment]
|
||||
def __init__(self, entity):
|
||||
self.entity = entity
|
||||
|
||||
def spawn_animal(self, blocked, taken):
|
||||
|
||||
|
||||
def spawn_animal(self, blocked, taken, enclosures):
|
||||
self.enclosures = [enclosure for enclosure in enclosures if enclosure.type == self.entity.environment]
|
||||
# Wyrażenie listowe filtrujące tylko te wybiegi, które pasują do środowiska zwierzęcia
|
||||
enclosure = random.choice(self.enclosures)
|
||||
|
||||
while True:
|
||||
if self.animal.adult:
|
||||
self.animal.x = random.randint(enclosure.x1+1, enclosure.x2-2)
|
||||
self.animal.y = random.randint(enclosure.y1+1, enclosure.y2-2)
|
||||
if self.entity.adult:
|
||||
self.entity.x = random.randint(enclosure.x1+1, enclosure.x2-2)
|
||||
self.entity.y = random.randint(enclosure.y1+1, enclosure.y2-2)
|
||||
else:
|
||||
self.animal.x = random.randint(enclosure.x1+1, enclosure.x2)
|
||||
self.animal.y = random.randint(enclosure.y1+1, enclosure.y2)
|
||||
self.entity.x = random.randint(enclosure.x1+1, enclosure.x2)
|
||||
self.entity.y = random.randint(enclosure.y1+1, enclosure.y2)
|
||||
|
||||
if self.check(blocked, taken):
|
||||
break
|
||||
def spawn_terrain_obstacles(self, blocked1, blocked2, taken, grid_width, grid_height):
|
||||
while True:
|
||||
self.entity.x = random.randint(0, grid_width - 1)
|
||||
self.entity.y = random.randint(0, grid_height - 1)
|
||||
y = self.entity.y
|
||||
x = self.entity.x
|
||||
if (x, y) not in blocked1 and (x, y) not in blocked2 and (x, y) not in taken:
|
||||
taken.add((self.entity.x, self.entity.y))
|
||||
break
|
||||
|
||||
def check(self, blocked, taken):
|
||||
x = self.animal.x
|
||||
y = self.animal.y
|
||||
x = self.entity.x
|
||||
y = self.entity.y
|
||||
|
||||
if (x,y) in blocked or (x,y) in taken:
|
||||
return False
|
||||
|
||||
if self.animal.adult:
|
||||
if self.entity.adult:
|
||||
|
||||
adult_fields = [(x, y), (x+1,y), (x,y+1), (x+1,y+1)] # Duże zwierze zajmuje 4 pola
|
||||
|
||||
@ -39,4 +50,5 @@ class Spawner:
|
||||
else:
|
||||
taken.add((x,y))
|
||||
|
||||
return True
|
||||
return True
|
||||
|
||||
|
13
terrain_obstacle.py
Normal file
13
terrain_obstacle.py
Normal file
@ -0,0 +1,13 @@
|
||||
import pygame
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
class Terrain_Obstacle:
|
||||
def __init__(self, x, y, type , image):
|
||||
self.x = x - 1
|
||||
self.y = y - 1
|
||||
self.type = type
|
||||
self.image = image
|
||||
|
||||
def draw(self, screen, grid_size):
|
||||
scaled_image = pygame.transform.scale(self.image, (grid_size, grid_size))
|
||||
screen.blit(scaled_image, (self.x * grid_size, self.y * grid_size))
|
Loading…
Reference in New Issue
Block a user