Male_zoo_Projekt_SI/agent.py

110 lines
4.7 KiB
Python
Raw Normal View History

2024-03-22 16:59:39 +01:00
import pygame
2024-05-12 16:25:09 +02:00
import random
from constants import Constants
from state_space_search import is_border, is_obstacle
from night import draw_night
2024-05-12 21:07:31 +02:00
from decision_tree import feed_decision
2024-05-12 16:25:09 +02:00
from constants import Constants
2024-03-23 13:55:16 +01:00
2024-03-22 16:59:39 +01:00
class Agent:
def __init__(self, istate, image_path, grid_size):
self.istate = istate
self.x, self.y, self.direction = istate
2024-03-22 16:59:39 +01:00
self.grid_size = grid_size
2024-04-12 22:11:58 +02:00
self.image= pygame.image.load(image_path)
self.image = pygame.transform.scale(self.image, (grid_size, grid_size))
2024-05-12 16:25:09 +02:00
self._dryfood = 0
self._wetfood = 0
2024-03-22 16:59:39 +01:00
def draw(self, const):
# Obróć obrazek zgodnie z kierunkiem
if self.direction == 'E':
2024-04-12 22:11:58 +02:00
self.image= pygame.image.load('images/agent4.png')
elif self.direction == 'S':
2024-04-12 22:11:58 +02:00
self.image= pygame.image.load('images/agent1.png')
elif self.direction == 'W':
2024-04-12 22:11:58 +02:00
self.image= pygame.image.load('images/agent3.png')
else: # direction == 'N'
2024-04-12 22:11:58 +02:00
self.image= pygame.image.load('images/agent2.png')
self.image = pygame.transform.scale(self.image, (const.GRID_SIZE, const.GRID_SIZE))
const.screen.blit(self.image, (self.x * self.grid_size, self.y * self.grid_size))
if const.IS_NIGHT: draw_night(const)
2024-03-22 16:59:39 +01:00
2024-05-12 16:25:09 +02:00
def handle_event(self, event, max_x, max_y, animals, obstacles,const):
2024-03-22 16:59:39 +01:00
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
2024-05-12 16:25:09 +02:00
self.move('Go Forward', max_x, max_y, obstacles, animals,const)
elif event.key == pygame.K_LEFT:
2024-05-12 16:25:09 +02:00
self.move('Turn Left', max_x, max_y, obstacles, animals,const)
elif event.key == pygame.K_RIGHT:
2024-05-12 16:25:09 +02:00
self.move('Turn Right', max_x, max_y, obstacles, animals,const)
2024-03-22 16:59:39 +01:00
2024-05-12 16:25:09 +02:00
def move(self, action, max_x, max_y, obstacles, animals, goal,const):
if action == 'Go Forward':
new_x, new_y = self.x, self.y
if self.direction == 'N':
new_y -= 1
elif self.direction == 'E':
new_x += 1
elif self.direction == 'S':
new_y += 1
elif self.direction == 'W':
new_x -= 1
# Sprawdź, czy nowe położenie mieści się w granicach kraty i nie jest przeszkodą
if is_border(new_x, new_y, max_x, max_y) and not(is_obstacle(new_x, new_y, obstacles)):
self.x, self.y = new_x, new_y
elif action == 'Turn Left':
self.direction = {'N': 'W', 'W': 'S', 'S': 'E', 'E': 'N'}[self.direction]
elif action == 'Turn Right':
self.direction = {'N': 'E', 'E': 'S', 'S': 'W', 'W': 'N'}[self.direction]
self.istate = (self.x, self.y, self.direction)
2024-05-12 16:25:09 +02:00
feed_animal(self, animals, goal,const)
2024-05-08 21:18:55 +02:00
take_food(self)
2024-05-12 16:25:09 +02:00
def feed_animal(self, animals, goal,const):
2024-04-27 11:20:59 +02:00
goal_x, goal_y = goal
if self.x == goal_x and self.y == goal_y:
for animal in animals:
if animal.x == goal_x and animal.y == goal_y:
2024-05-12 16:25:09 +02:00
if (animal.activity == 'nocturnal' and const.IS_NIGHT) or (animal.activity == 'diurnal' and not(const.IS_NIGHT)):
activity_time = True
2024-05-08 21:18:55 +02:00
else:
2024-05-12 16:25:09 +02:00
activity_time = False
guests = random.randint(1, 15)
2024-05-12 21:07:31 +02:00
decision = feed_decision(animal.adult, activity_time, animal.ill, const.season, guests, animal._feed, self._dryfood, self._wetfood)
if decision != [1]:
if decision == [2]:
2024-05-12 16:25:09 +02:00
if animal.getting_hungry(const=Constants()) < self._wetfood :
self._wetfood -= animal._feed
animal._feed = 0
else:
animal._feed -= self._wetfood
2024-05-12 21:07:31 +02:00
self._wetfood = 0
2024-05-12 16:25:09 +02:00
print(animal.name, "fed with wet food")
else:
if animal.getting_hungry(const=Constants()) < self._dryfood :
self._dryfood -= animal._feed
animal._feed = 0
else:
animal._feed -= self._dryfood
self._dryfood = 0
print(animal.name, "fed with dry food")
print("Current wet food level: ", self._wetfood)
print("Current dry food level: ", self._dryfood)
else: print(animal.name, " not fed")
2024-05-08 21:18:55 +02:00
def take_food(self):
house_x = 3
house_y = 1
if self.x == house_x and self.y == house_y:
if self._dryfood < 1 or self._wetfood < 1:
2024-05-12 16:25:09 +02:00
self._dryfood = 50
self._wetfood = 50
print("Agent took food and current food level is", self._dryfood, self._wetfood)