Male_zoo_Projekt_SI/agent.py

35 lines
1.2 KiB
Python
Raw Normal View History

2024-03-22 16:59:39 +01:00
import pygame
2024-03-23 13:55:16 +01:00
2024-03-22 16:59:39 +01:00
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):
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
2024-03-23 13:55:16 +01:00
print(animal.name,"fed with",animal.food)