Dodanie stanu czystości klatek

This commit is contained in:
karolajoj 2024-03-24 17:10:52 +01:00
parent 2e4ba023d0
commit ea227dc384
14 changed files with 96 additions and 9 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
images/smell icon.psd

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -16,7 +16,7 @@ class Agent:
self.x += dx
self.y += dy
def handle_event(self, event, grid_height,grid_width, animals):
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)
@ -32,3 +32,8 @@ class Agent:
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))

34
cage.py Normal file
View File

@ -0,0 +1,34 @@
class Cage:
def __init__(self, top_left, bottom_right, name, temperature):
self.top_left = top_left
self.bottom_right = bottom_right
self.name = name
self.temperature = temperature
self.smell_icons = []
def get_area(self):
width = self.bottom_right[0] - self.top_left[0] + 1
height = self.bottom_right[1] - self.top_left[1] + 1
return width * height
def get_cleanliness_state(self):
area = self.get_area()
# print("Area:", area)
if area == 0:
return f"{self.name}: 0%"
cleanliness_state = len(self.smell_icons) / area * 100
return f"{self.name}: {cleanliness_state:.0f}%"
# Definiowanie klatek (top_left(x,y), bottom_right(x,y), nazwa, temperatura)
# 0------>x
# |
# |
# y
cage1 = Cage((8, 0), (18, 1), "Ptaszarnia", 24)
cage2 = Cage((0, 3), (5, 6), "Wybieg słoni", 24)
cage3 = Cage((12, 3), (19, 6), "Wybieg żyraf", 24)
cage4 = Cage((7, 3), (10, 6), "Akwarium pingwinów", -4)
cage5 = Cage((2, 8), (19, 9), "Klatka niedźwiedzi polarnych", -4)
Cages = [cage1, cage2, cage3, cage4, cage5]

BIN
images/smell_icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 894 KiB

BIN
images/smell_icon2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 MiB

61
main.py
View File

@ -1,15 +1,23 @@
import pygame
import sys
import random
import time
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 cage import Cages
# Stała bool na potrzeby debugowania
DEBUG_MODE = True
# Interwał w sekundach, po którym klatka staje się brudna
DIRTY_CAGE_INTERVAL = 1
BLACK = (0, 0, 0)
GRID_SIZE = 50
GRID_SIZE = 70
GRID_WIDTH = 20
GRID_HEIGHT = 10
@ -24,9 +32,9 @@ pygame.display.set_caption("Mini Zoo")
background_image = pygame.image.load('images/tło.jpg')
background_image = pygame.transform.scale(background_image, WINDOW_SIZE)
smell_icon = pygame.image.load('images/smell_icon2.png')
smell_icon = pygame.transform.scale(smell_icon, (GRID_SIZE, GRID_SIZE))
smell_icon.set_colorkey((213, 208, 126))
an1 = Parrot(16, 2)
an2 = Penguin(8, 6)
@ -43,6 +51,18 @@ def draw_grid():
rect = pygame.Rect(x, y, GRID_SIZE, GRID_SIZE)
pygame.draw.rect(screen, BLACK, rect, 1)
# Rysowanie obszarów klatek
if DEBUG_MODE:
for cage in Cages:
top_left = cage.top_left
bottom_right = cage.bottom_right
x1 = top_left[0] * GRID_SIZE
y1 = top_left[1] * GRID_SIZE
x2 = (bottom_right[0] + 1) * GRID_SIZE
y2 = (bottom_right[1] + 1) * GRID_SIZE
pygame.draw.rect(screen, (255, 0, 0), (x1, y1, x2 - x1, y2 - y1), 2)
def draw_Animals():
@ -53,25 +73,52 @@ def draw_Animals():
else:
Animal.draw_food(screen,GRID_SIZE,Animal.x,Animal.y)
def generate_dirty_cage():
cage_index = random.randint(0, len(Cages) - 1)
selected_cage = Cages[cage_index]
top_left = selected_cage.top_left
bottom_right = selected_cage.bottom_right
width = bottom_right[0] - top_left[0] + 1
height = bottom_right[1] - top_left[1] + 1
x = random.randint(top_left[0], bottom_right[0])
y = random.randint(top_left[1], bottom_right[1])
if (x, y) not in selected_cage.smell_icons:
selected_cage.smell_icons.append((x, y))
def draw_dirty_cages():
for cage in Cages:
for icon_pos in cage.smell_icons:
x, y = icon_pos
screen.blit(smell_icon, (x * GRID_SIZE, y * GRID_SIZE))
def main():
agent = Agent(0, 0, 'images/avatar.png', GRID_SIZE)
clock = pygame.time.Clock()
# Timer do śledzenia pojawienia się brudnej klatki
dirty_cage_timer = time.time()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
agent.handle_event(event,GRID_HEIGHT,GRID_WIDTH,Animals)
agent.handle_event(event,GRID_HEIGHT,GRID_WIDTH,Animals, Cages)
# Sprawdź, czy już czas na wygenerowanie brudnej klatki
if time.time() - dirty_cage_timer >= DIRTY_CAGE_INTERVAL:
generate_dirty_cage()
# Zresetuj timer
dirty_cage_timer = time.time()
for cage in Cages:
print(cage.get_cleanliness_state()) # Wyświetl stan czystości dla każdej klatki
screen.blit(background_image,(0,0))
draw_grid()
draw_Animals()
draw_dirty_cages()
agent.draw(screen)