dodanie jedzenia

This commit is contained in:
Natalia Szymczak 2024-03-23 13:55:16 +01:00
parent 8508ac9b53
commit 2e4ba023d0
20 changed files with 60 additions and 30 deletions

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

@ -1,4 +1,5 @@
import pygame import pygame
class Agent: class Agent:
def __init__(self, x, y, image_path, grid_size): def __init__(self, x, y, image_path, grid_size):
self.x = x self.x = x
@ -29,5 +30,5 @@ class Agent:
for animal in animals: for animal in animals:
if self.x == animal.x and self.y == animal.y: if self.x == animal.x and self.y == animal.y:
if animal.feed()== 'True': if animal.feed()== 'True':
animal.feed_animal()
animal._feed = 0 animal._feed = 0
print(animal.name,"fed with",animal.food)

View File

@ -2,11 +2,14 @@ import pygame
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
class Animal: class Animal:
def __init__(self, x, y, image, adult=False): def __init__(self, x, y,name, image, food_image, food, adult=False):
self.x = x - 1 self.x = x - 1
self.y = y - 1 self.y = y - 1
self.name = name
self.image = image self.image = image
self.adult = adult self.adult = adult
self.food = food
self.food_image = food_image
self._feed = 0 #nowe zwierze jest głodne self._feed = 0 #nowe zwierze jest głodne
def draw(self, screen, grid_size): def draw(self, screen, grid_size):
@ -21,6 +24,18 @@ class Animal:
# If not adult, draw like normal Animal # If not adult, draw like normal Animal
screen.blit(self.image, (self.x * grid_size, self.y * grid_size)) screen.blit(self.image, (self.x * grid_size, self.y * grid_size))
def draw_exclamation(self, screen, grid_size, x, y):
exclamation_image = pygame.image.load('images/exclamation.png')
exclamation_image = pygame.transform.scale(exclamation_image, (grid_size,grid_size))
screen.blit(exclamation_image, (x*grid_size, y*grid_size - grid_size))
def draw_food(self, screen, grid_size, x, y):
food_image = pygame.image.load(self.food_image)
food_image = pygame.transform.scale(food_image, (grid_size,grid_size))
screen.blit(food_image, (x*grid_size, y*grid_size + grid_size))
@abstractmethod @abstractmethod
def feed(self): def feed(self):
@ -29,7 +44,3 @@ class Animal:
@abstractmethod @abstractmethod
def getting_hungry(self): def getting_hungry(self):
pass pass
def feed_animal(self):
if self.feed():
self._feed = 0

View File

@ -7,15 +7,17 @@ from datetime import datetime
class Bear(Animal): class Bear(Animal):
def __init__(self, x, y, adult=False): def __init__(self, x, y, adult=False):
Bear_image = pygame.image.load('images/bear.png') Bear_image = pygame.image.load('images/bear.png')
super().__init__(x, y, Bear_image, adult) name = 'bear'
self.food_type = "fish" bear_food = 'meat'
food_image = 'images/meat.png'
super().__init__(x, y,name, Bear_image, food_image,bear_food, adult)
self._starttime = datetime.now() self._starttime = datetime.now()
def feed(self): def feed(self):
self.getting_hungry() self.getting_hungry()
if self._feed < 5: if self._feed < 2:
return 'False' return 'False'
else: else:
return 'True' return 'True'

View File

@ -7,15 +7,22 @@ from datetime import datetime
class Elephant(Animal): class Elephant(Animal):
def __init__(self, x, y, adult=False): def __init__(self, x, y, adult=False):
Elephant_image = pygame.image.load('images/elephant.png') Elephant_image = pygame.image.load('images/elephant.png')
super().__init__(x, y, Elephant_image, adult) name = 'elphant'
self.food_type = "leaves" if adult:
elephant_food = 'leavs'
food_image = 'images/leaves.png'
else:
elephant_food = 'milk'
food_image = 'images/milk.png'
super().__init__(x, y,name, Elephant_image, food_image,elephant_food, adult)
self._starttime = datetime.now() self._starttime = datetime.now()
def feed(self): def feed(self):
self.getting_hungry() self.getting_hungry()
if self._feed < 1: if self._feed < 0.3:
return 'False' return 'False'
else: else:
return 'True' return 'True'

View File

@ -7,15 +7,17 @@ from datetime import datetime
class Giraffe(Animal): class Giraffe(Animal):
def __init__(self, x, y, adult=False): def __init__(self, x, y, adult=False):
Giraffe_image = pygame.image.load('images/giraffe.png') Giraffe_image = pygame.image.load('images/giraffe.png')
super().__init__(x, y, Giraffe_image, adult) name = 'giraffe'
self.food_type = "leaves" food_image = 'images/leaves.png'
giraffe_food = 'leaves'
super().__init__(x, y,name, Giraffe_image, food_image,giraffe_food, adult)
self._starttime = datetime.now() self._starttime = datetime.now()
def feed(self): def feed(self):
self.getting_hungry() self.getting_hungry()
if self._feed < 3: if self._feed < 0.8:
return 'False' return 'False'
else: else:
return 'True' return 'True'

BIN
images/fish.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 934 KiB

BIN
images/grains.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 398 KiB

BIN
images/leaves.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 96 KiB

BIN
images/meat.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 186 KiB

BIN
images/milk.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 288 KiB

23
main.py
View File

@ -24,16 +24,18 @@ pygame.display.set_caption("Mini Zoo")
background_image = pygame.image.load('images/tło.jpg') background_image = pygame.image.load('images/tło.jpg')
background_image = pygame.transform.scale(background_image, WINDOW_SIZE) background_image = pygame.transform.scale(background_image, WINDOW_SIZE)
exclamation_image = pygame.image.load('images/exclamation.png')
exclamation_image = pygame.transform.scale(exclamation_image, (GRID_SIZE,GRID_SIZE))
an1 = Parrot(10, 2)
an2 = Penguin(12, 2)
an3 = Bear(14, 10)
an1 = Parrot(16, 2)
an2 = Penguin(8, 6)
an3 = Bear(14, 9)
old_an2 = Giraffe(18,4, adult=True) old_an2 = Giraffe(18,4, adult=True)
old_an1 = Elephant(3, 6, adult=True) old_an1 = Elephant(4, 7, adult=True)
an4 = Elephant(4,3)
Animals = [an1, an2, an3, old_an1, old_an2] Animals = [an1, an2, an3, an4, old_an1, old_an2]
def draw_grid(): def draw_grid():
for y in range(0, GRID_HEIGHT * GRID_SIZE, GRID_SIZE): for y in range(0, GRID_HEIGHT * GRID_SIZE, GRID_SIZE):
@ -41,14 +43,15 @@ def draw_grid():
rect = pygame.Rect(x, y, GRID_SIZE, GRID_SIZE) rect = pygame.Rect(x, y, GRID_SIZE, GRID_SIZE)
pygame.draw.rect(screen, BLACK, rect, 1) pygame.draw.rect(screen, BLACK, rect, 1)
def draw_exclamation(x, y):
screen.blit(exclamation_image, (x*GRID_SIZE, y*GRID_SIZE - GRID_SIZE))
def draw_Animals(): def draw_Animals():
for Animal in Animals: for Animal in Animals:
Animal.draw(screen, GRID_SIZE) Animal.draw(screen, GRID_SIZE)
if Animal.feed() == 'True': if Animal.feed() == 'True':
draw_exclamation(Animal.x, Animal.y) Animal.draw_exclamation(screen, GRID_SIZE, Animal.x, Animal.y)
else:
Animal.draw_food(screen,GRID_SIZE,Animal.x,Animal.y)

View File

@ -7,15 +7,17 @@ from datetime import datetime
class Parrot(Animal): class Parrot(Animal):
def __init__(self, x, y, adult=False): def __init__(self, x, y, adult=False):
Parrot_image = pygame.image.load('images/parrot.png') Parrot_image = pygame.image.load('images/parrot.png')
super().__init__(x, y, Parrot_image, adult) name = 'parrot'
self.food_type = "grain" food_image = 'images/grains.png'
parrot_food = 'grains'
super().__init__(x, y,name, Parrot_image, food_image,parrot_food, adult)
self._starttime = datetime.now() self._starttime = datetime.now()
def feed(self): def feed(self):
self.getting_hungry() self.getting_hungry()
if self._feed < 3: if self._feed < 1.5:
return 'False' return 'False'
else: else:
return 'True' return 'True'

View File

@ -7,8 +7,10 @@ from datetime import datetime
class Penguin(Animal): class Penguin(Animal):
def __init__(self, x, y, adult=False): def __init__(self, x, y, adult=False):
Penguin_image = pygame.image.load('images/penguin.png') Penguin_image = pygame.image.load('images/penguin.png')
super().__init__(x, y, Penguin_image, adult) name = 'penguin'
self.food_type = "fish" food_image = 'images/fish.png'
penguin_food = 'fish'
super().__init__(x, y,name, Penguin_image, food_image,penguin_food, adult)
self._starttime = datetime.now() self._starttime = datetime.now()