39 lines
1.4 KiB
Python
39 lines
1.4 KiB
Python
import pygame
|
|
|
|
class Agent:
|
|
def __init__(self, x, y, image_path, grid_size):
|
|
self.x = x
|
|
self.y = y
|
|
self.grid_size = grid_size
|
|
self.image = pygame.image.load(image_path)
|
|
self.image = pygame.transform.scale(self.image, (grid_size, grid_size))
|
|
|
|
|
|
def draw(self, screen):
|
|
screen.blit(self.image, (self.x * self.grid_size, self.y * self.grid_size))
|
|
|
|
def move(self, dx, dy):
|
|
self.x += dx
|
|
self.y += dy
|
|
|
|
def handle_event(self, event, grid_height, grid_width, animals, dirty_cages):
|
|
if event.type == pygame.KEYDOWN:
|
|
if event.key == pygame.K_UP and self.y > 0:
|
|
self.move(0, -1)
|
|
elif event.key == pygame.K_DOWN and self.y < grid_height - 1:
|
|
self.move(0, 1)
|
|
elif event.key == pygame.K_LEFT and self.x > 0:
|
|
self.move(-1, 0)
|
|
elif event.key == pygame.K_RIGHT and self.x < grid_width - 1:
|
|
self.move(1, 0)
|
|
|
|
for animal in animals:
|
|
if self.x == animal.x and self.y == animal.y:
|
|
if animal.feed()== 'True':
|
|
animal._feed = 0
|
|
print(animal.name,"fed with",animal.food)
|
|
|
|
for cage in dirty_cages:
|
|
if (self.x, self.y) in cage.smell_icons:
|
|
cage.smell_icons.remove((self.x, self.y))
|
|
print("Smell icon removed at:", (self.x, self.y)) |