dodanie głodu zwierząt i zmiana klas

This commit is contained in:
Natalia Szymczak 2024-03-22 16:59:39 +01:00
parent 82b2060409
commit df8dd868e7
11 changed files with 124 additions and 64 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,16 +0,0 @@
import pygame
from animal import Animal
class AdultAnimal(Animal):
def __init__(self,x,y,image, width=2,height=2):
super().__init__(x,y,image)
self.width=width
self.height=height
def draw(self, screen, grid_size):
new_width = grid_size * self.width
new_height = grid_size * self.height
scaled_image = pygame.transform.scale(self.image, (new_width, new_height))
screen.blit(scaled_image, (self.x * grid_size, self.y * grid_size))

33
agent.py Normal file
View File

@ -0,0 +1,33 @@
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):
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_animal()
animal._feed = 0

View File

@ -1,8 +1,35 @@
class Animal:
def __init__(self, x, y, image):
self.x = x-1
self.y = y-1
self.image = image
import pygame
from abc import ABC, abstractmethod
def draw(self, screen ,grid_size):
screen.blit(self.image, (self.x *grid_size, self.y*grid_size))
class Animal:
def __init__(self, x, y, image, adult=False):
self.x = x - 1
self.y = y - 1
self.image = image
self.adult = adult
self._feed = 0 #nowe zierze jest głodne
def draw(self, screen, grid_size):
self.image = pygame.transform.scale(self.image, (grid_size, grid_size))
if self.adult:
# If adult, draw like AdultAnimal
new_width = grid_size * 2
new_height = grid_size * 2
scaled_image = pygame.transform.scale(self.image, (new_width, new_height))
screen.blit(scaled_image, (self.x * grid_size, self.y * grid_size))
else:
# If not adult, draw like normal Animal
screen.blit(self.image, (self.x * grid_size, self.y * grid_size))
@abstractmethod
def feed(self):
pass
@abstractmethod
def getting_hungry(self):
pass
def feed_animal(self):
if self.feed():
self._feed = 0

29
elephant.py Normal file
View File

@ -0,0 +1,29 @@
from animal import Animal
import pygame
from datetime import datetime
class Elephant(Animal):
def __init__(self, x, y, adult=False):
Elephant_image = pygame.image.load('elephant.png')
super().__init__(x, y, Elephant_image, adult)
self.food_type = "leaves"
self._starttime = datetime.now()
def feed(self):
self.getting_hungry()
if self._feed < 1:
return 'False'
else:
return 'True'
def getting_hungry(self):
checktime = datetime.now()
delta = checktime - self._starttime
minutes_passed = delta.total_seconds() / 60
self._feed += minutes_passed
self._starttime = checktime

BIN
exclamation.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

69
main.py
View File

@ -1,11 +1,11 @@
import pygame
import sys
from animal import Animal
from adult_animal import AdultAnimal
from elephant import Elephant
from agent import Agent
BLACK = (0, 0, 0)
GRID_SIZE = 100
GRID_SIZE = 75
GRID_WIDTH = 20
GRID_HEIGHT = 10
@ -15,29 +15,21 @@ WINDOW_SIZE = (GRID_WIDTH * GRID_SIZE, GRID_HEIGHT * GRID_SIZE)
screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption("Mini Zoo")
agent_pos = [0,0]
agent_image = pygame.image.load('avatar.png')
agent_image = pygame.transform.scale(agent_image, (GRID_SIZE,GRID_SIZE))
background_image = pygame.image.load('tło.jpg')
background_image = pygame.transform.scale(background_image, WINDOW_SIZE)
animal_image = pygame.image.load('elephant.png')
animal_image = pygame.transform.scale(animal_image, (GRID_SIZE, GRID_SIZE))
exclamation_image = pygame.image.load('exclamation.png')
exclamation_image = pygame.transform.scale(exclamation_image, (GRID_SIZE,GRID_SIZE))
an1=Animal(10,1,animal_image)
an2=Animal(12,1,animal_image)
an3=Animal(14,7,animal_image)
an1 = Elephant(10, 2)
an2 = Elephant(12, 2)
an3 = Elephant(14, 7)
old_an1=AdultAnimal(3,6, animal_image,width=2,height=2)
old_an1 = Elephant(3, 6, adult=True)
animals=[]
animals.append(an1)
animals.append(an2)
animals.append(an3)
old_animals=[]
old_animals.append(old_an1)
Elephants = [an1, an2, an3,old_an1]
def draw_grid():
for y in range(0, GRID_HEIGHT * GRID_SIZE, GRID_SIZE):
@ -45,20 +37,19 @@ def draw_grid():
rect = pygame.Rect(x, y, GRID_SIZE, GRID_SIZE)
pygame.draw.rect(screen, BLACK, rect, 1)
def draw_agent(agent_pos):
x, y = agent_pos
screen.blit(agent_image, (x*GRID_SIZE,y*GRID_SIZE))
def draw_exclamation(x, y):
screen.blit(exclamation_image, (x*GRID_SIZE, y*GRID_SIZE - GRID_SIZE))
def draw_Elephants():
for Elephant in Elephants:
Elephant.draw(screen, GRID_SIZE)
if Elephant.feed() == 'True':
draw_exclamation(Elephant.x, Elephant.y)
def draw_animals():
for animal in animals:
animal.draw(screen,GRID_SIZE)
def draw_old_animals():
for animal in old_animals:
animal.draw(screen,GRID_SIZE)
def main():
global agent_pos
agent = Agent(0, 0, 'avatar.png', GRID_SIZE)
clock = pygame.time.Clock()
while True:
@ -66,22 +57,18 @@ def main():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type ==pygame.KEYDOWN:
if event.key == pygame.K_UP and agent_pos[1] > 0:
agent_pos[1] -= 1
elif event.key == pygame.K_DOWN and agent_pos[1] < GRID_HEIGHT - 1:
agent_pos[1] += 1
elif event.key == pygame.K_LEFT and agent_pos[0] > 0:
agent_pos[0] -= 1
elif event.key == pygame.K_RIGHT and agent_pos[0] < GRID_WIDTH - 1:
agent_pos[0] += 1
agent.handle_event(event,GRID_HEIGHT,GRID_WIDTH,Elephants)
for Elephant in Elephants:
Elephant.getting_hungry()
screen.blit(background_image,(0,0))
draw_grid()
draw_animals()
draw_old_animals()
draw_agent(agent_pos)
draw_Elephants()
agent.draw(screen)
pygame.display.flip()
clock.tick(10)