diff --git a/__pycache__/adult_animal.cpython-311.pyc b/__pycache__/adult_animal.cpython-311.pyc new file mode 100644 index 0000000..aa04f9e Binary files /dev/null and b/__pycache__/adult_animal.cpython-311.pyc differ diff --git a/__pycache__/agent.cpython-311.pyc b/__pycache__/agent.cpython-311.pyc new file mode 100644 index 0000000..4bfbab3 Binary files /dev/null and b/__pycache__/agent.cpython-311.pyc differ diff --git a/__pycache__/animal.cpython-311.pyc b/__pycache__/animal.cpython-311.pyc new file mode 100644 index 0000000..48fc46f Binary files /dev/null and b/__pycache__/animal.cpython-311.pyc differ diff --git a/__pycache__/bear.cpython-311.pyc b/__pycache__/bear.cpython-311.pyc new file mode 100644 index 0000000..7967955 Binary files /dev/null and b/__pycache__/bear.cpython-311.pyc differ diff --git a/__pycache__/combined_animal.cpython-311.pyc b/__pycache__/combined_animal.cpython-311.pyc new file mode 100644 index 0000000..fd6781a Binary files /dev/null and b/__pycache__/combined_animal.cpython-311.pyc differ diff --git a/__pycache__/elephant.cpython-311.pyc b/__pycache__/elephant.cpython-311.pyc new file mode 100644 index 0000000..92cd0a1 Binary files /dev/null and b/__pycache__/elephant.cpython-311.pyc differ diff --git a/__pycache__/giraffe.cpython-311.pyc b/__pycache__/giraffe.cpython-311.pyc new file mode 100644 index 0000000..4f975bc Binary files /dev/null and b/__pycache__/giraffe.cpython-311.pyc differ diff --git a/__pycache__/parrot.cpython-311.pyc b/__pycache__/parrot.cpython-311.pyc new file mode 100644 index 0000000..2142ed6 Binary files /dev/null and b/__pycache__/parrot.cpython-311.pyc differ diff --git a/__pycache__/penguin.cpython-311.pyc b/__pycache__/penguin.cpython-311.pyc new file mode 100644 index 0000000..4a2a7bb Binary files /dev/null and b/__pycache__/penguin.cpython-311.pyc differ diff --git a/adult_animal.py b/adult_animal.py deleted file mode 100644 index c0540ed..0000000 --- a/adult_animal.py +++ /dev/null @@ -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)) \ No newline at end of file diff --git a/agent.py b/agent.py new file mode 100644 index 0000000..bb463e2 --- /dev/null +++ b/agent.py @@ -0,0 +1,34 @@ +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, blocked_fields): + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_UP and self.y > 0 and (self.x, self.y-1) not in blocked_fields: + self.move(0, -1) + elif event.key == pygame.K_DOWN and self.y < grid_height - 1 and (self.x, self.y+1) not in blocked_fields: + self.move(0, 1) + elif event.key == pygame.K_LEFT and self.x > 0 and (self.x-1, self.y) not in blocked_fields: + self.move(-1, 0) + elif event.key == pygame.K_RIGHT and self.x < grid_width - 1 and (self.x+1, self.y) not in blocked_fields: + 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) diff --git a/animal.py b/animal.py index f19359d..2059ffe 100644 --- a/animal.py +++ b/animal.py @@ -1,8 +1,47 @@ -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)) \ No newline at end of file +class Animal: + def __init__(self, x, y,name, image, food_image, food, environment, adult=False,): + self.x = x - 1 + self.y = y - 1 + self.name = name + self.image = image + self.adult = adult + self.food = food + self.food_image = food_image + self._feed = 0 + self.environment = environment #hot/cold/medium + + 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)) + + 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 + def feed(self): + pass + + @abstractmethod + def getting_hungry(self): + pass diff --git a/bear.py b/bear.py new file mode 100644 index 0000000..e0aafe3 --- /dev/null +++ b/bear.py @@ -0,0 +1,32 @@ +from animal import Animal +import pygame +from datetime import datetime + + + +class Bear(Animal): + def __init__(self, x, y, adult=False): + Bear_image = pygame.image.load('images/bear.png') + name = 'bear' + environment = "cold" + bear_food = 'meat' + food_image = 'images/meat.png' + super().__init__(x, y,name, Bear_image, food_image,bear_food,environment, adult) + self._starttime = datetime.now() + + + + def feed(self): + self.getting_hungry() + if self._feed < 2: + 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 \ No newline at end of file diff --git a/elephant.py b/elephant.py new file mode 100644 index 0000000..4e63bfb --- /dev/null +++ b/elephant.py @@ -0,0 +1,37 @@ +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('images/elephant.png') + name = 'elephant' + environment = "hot" + 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, environment, adult) + self._starttime = datetime.now() + + + + def feed(self): + self.getting_hungry() + if self._feed < 0.3: + 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 \ No newline at end of file diff --git a/enclosure.py b/enclosure.py new file mode 100644 index 0000000..2a79c52 --- /dev/null +++ b/enclosure.py @@ -0,0 +1,79 @@ +import pygame + +class Enclosure: + def __init__(self, x1, y1, x2, y2, gate, type, imageH, imageV, imageGate): + self.x1 = x1 - 1 + self.y1 = y1 - 1 + #(x1,y1) - wierzchołek przekątnej + self.x2 = x2 - 1 + self.y2 = y2 - 1 + #(x2,y2) - 2 wierzchołek przekątnej + self.gate = gate + self.type = type + self.imageH = imageH + self.imageV = imageV + self.imageGate = imageGate + + def gatebuild(self, screen, grid_size): + self.imageGate = pygame.transform.scale(self.imageGate, (grid_size, grid_size)) + gate_x, gate_y = self.gate + gate_x-=1 + gate_y-=1 + rect = pygame.Rect(gate_x * grid_size, gate_y * grid_size, grid_size, grid_size) + pygame.draw.rect(screen, (0, 0, 0), rect) # Fill the area with + screen.blit(self.imageGate, (gate_x * grid_size, gate_y * grid_size)) + + def gateopen(self, blocked): + gate_x, gate_y = self.gate + gate_x -= 1 + gate_y -= 1 + if (gate_x, gate_y) in blocked: + blocked.remove((gate_x, gate_y)) + + + + def draw(self,screen, grid_size , blocked_fields): + self.imageH = pygame.transform.scale(self.imageH, (grid_size, grid_size)) + self.imageV = pygame.transform.scale(self.imageV, (grid_size, grid_size)) + if self.x1 < self.x2: + for i in range(self.x1, self.x2+1): + screen.blit(self.imageH, (i * grid_size, self.y1 * grid_size)) + blocked_fields.add((i, self.y1)) + screen.blit(self.imageH, (i * grid_size, self.y2 * grid_size)) + blocked_fields.add((i, self.y2)) + if self.y1 < self.y2: + for j in range(self.y1, self.y2+1): + screen.blit(self.imageH, (self.x1 * grid_size, j * grid_size)) + blocked_fields.add((self.x1, j)) + screen.blit(self.imageH, (self.x2 * grid_size, j * grid_size)) + blocked_fields.add((self.x2, j)) + if self.y1 > self.y2: + for j in range(self.y2, self.y1+1): + screen.blit(self.imageH, (self.x1 * grid_size, j * grid_size)) + blocked_fields.add((self.x1, j)) + screen.blit(self.imageH, (self.x2 * grid_size, j * grid_size)) + blocked_fields.add((self.x2, j)) + if self.x1 > self.x2: + for i in range(self.x2, self.x1+1): + screen.blit(self.imageH, (i * grid_size, self.y1 * grid_size)) + blocked_fields.add((i, self.y1)) + screen.blit(self.imageH, (i * grid_size, self.y2 * grid_size)) + blocked_fields.add((i, self.y2)) + if self.y1 < self.y2: + for j in range(self.y1, self.y2+1): + screen.blit(self.imageH, (self.x1 * grid_size, j * grid_size)) + blocked_fields.add((self.x1, j)) + screen.blit(self.imageH, (self.x2 * grid_size, j * grid_size)) + blocked_fields.add((self.x2, j)) + if self.y1 > self.y2: + for j in range(self.y2, self.y1+1): + screen.blit(self.imageH, (self.x1 * grid_size, j * grid_size)) + blocked_fields.add((self.x1, j)) + screen.blit(self.imageH, (self.x2 * grid_size, j * grid_size)) + blocked_fields.add((self.x2, j)) + + + + + + diff --git a/giraffe.py b/giraffe.py new file mode 100644 index 0000000..f5c191f --- /dev/null +++ b/giraffe.py @@ -0,0 +1,32 @@ +from animal import Animal +import pygame +from datetime import datetime + + + +class Giraffe(Animal): + def __init__(self, x, y, adult=False): + Giraffe_image = pygame.image.load('images/giraffe.png') + name = 'giraffe' + environment = "hot" + food_image = 'images/leaves.png' + giraffe_food = 'leaves' + super().__init__(x, y,name, Giraffe_image, food_image,giraffe_food, environment, adult) + self._starttime = datetime.now() + + + + def feed(self): + self.getting_hungry() + if self._feed < 0.8: + 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 \ No newline at end of file diff --git a/avatar.png b/images/avatar.png similarity index 100% rename from avatar.png rename to images/avatar.png diff --git a/images/bear.png b/images/bear.png new file mode 100644 index 0000000..e02a84f Binary files /dev/null and b/images/bear.png differ diff --git a/elephant.png b/images/elephant.png similarity index 100% rename from elephant.png rename to images/elephant.png diff --git a/images/exclamation.png b/images/exclamation.png new file mode 100644 index 0000000..4e7d6c7 Binary files /dev/null and b/images/exclamation.png differ diff --git a/images/fenceHor.png b/images/fenceHor.png new file mode 100644 index 0000000..8beb902 Binary files /dev/null and b/images/fenceHor.png differ diff --git a/images/fenceVer.png b/images/fenceVer.png new file mode 100644 index 0000000..2699618 Binary files /dev/null and b/images/fenceVer.png differ diff --git a/images/fish.png b/images/fish.png new file mode 100644 index 0000000..8a5f710 Binary files /dev/null and b/images/fish.png differ diff --git a/images/gate.png b/images/gate.png new file mode 100644 index 0000000..8f062fa Binary files /dev/null and b/images/gate.png differ diff --git a/images/giraffe.png b/images/giraffe.png new file mode 100644 index 0000000..8035ad6 Binary files /dev/null and b/images/giraffe.png differ diff --git a/images/grains.png b/images/grains.png new file mode 100644 index 0000000..e8fe6ce Binary files /dev/null and b/images/grains.png differ diff --git a/images/leaves.png b/images/leaves.png new file mode 100644 index 0000000..897288a Binary files /dev/null and b/images/leaves.png differ diff --git a/images/meat.png b/images/meat.png new file mode 100644 index 0000000..34a3d23 Binary files /dev/null and b/images/meat.png differ diff --git a/images/milk.png b/images/milk.png new file mode 100644 index 0000000..b51a627 Binary files /dev/null and b/images/milk.png differ diff --git a/images/parrot.png b/images/parrot.png new file mode 100644 index 0000000..b38bf98 Binary files /dev/null and b/images/parrot.png differ diff --git a/images/penguin.png b/images/penguin.png new file mode 100644 index 0000000..6e80909 Binary files /dev/null and b/images/penguin.png differ diff --git a/tło.jpg b/images/tło.jpg similarity index 100% rename from tło.jpg rename to images/tło.jpg diff --git a/main.py b/main.py index 1483ab7..18de593 100644 --- a/main.py +++ b/main.py @@ -1,13 +1,19 @@ import pygame import sys -from animal import Animal -from adult_animal import AdultAnimal +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 enclosure import Enclosure +from spawner import Spawner BLACK = (0, 0, 0) -GRID_SIZE = 100 -GRID_WIDTH = 20 -GRID_HEIGHT = 10 +GRID_SIZE = 50 +GRID_WIDTH = 30 +GRID_HEIGHT = 15 pygame.init() @@ -15,29 +21,38 @@ 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.image.load('images/tło.jpg') background_image = pygame.transform.scale(background_image, WINDOW_SIZE) +fenceH = pygame.image.load('images/fenceHor.png') +fenceV = pygame.image.load('images/fenceVer.png') +gate = pygame.image.load('images/gate.png') -animal_image = pygame.image.load('elephant.png') -animal_image = pygame.transform.scale(animal_image, (GRID_SIZE, GRID_SIZE)) -an1=Animal(10,1,animal_image) -an2=Animal(12,1,animal_image) -an3=Animal(14,7,animal_image) +fences = set() +animals_position = set() -old_an1=AdultAnimal(3,6, animal_image,width=2,height=2) -animals=[] -animals.append(an1) -animals.append(an2) -animals.append(an3) -old_animals=[] -old_animals.append(old_an1) +an1 = Parrot(16, 2) +an2 = Penguin(8, 6) +an3 = Bear(14, 9) +old_an2 = Giraffe(18,4, adult=True) +old_an1 = Elephant(4, 7, adult=True) +an4 = Elephant(4,3) + +Animals = [an1, an2, an3, an4, old_an1, old_an2] + +en1 = Enclosure(1,5,9,11,(9,6),"medium", fenceH, fenceV, gate) +en2 = Enclosure(29,3, 13,1,(16,3), 'medium', fenceH, fenceV, gate) +en3 = Enclosure(11,5, 16,11, (12,5),'cold', fenceH, fenceV, gate) +en4 = Enclosure(19,11, 30,5, (25,5),'hot', fenceH, fenceV, gate) +en5 = Enclosure(4,13, 28,15, (16,13),'cold', fenceH, fenceV, gate) + + +Enclosures = [en1, en2, en3, en4, en5] + def draw_grid(): for y in range(0, GRID_HEIGHT * GRID_SIZE, GRID_SIZE): @@ -45,43 +60,56 @@ 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_enclosures(): + for enclosure in Enclosures: + enclosure.draw(screen, GRID_SIZE, fences) + +def draw_gates(): + for enclosure in Enclosures: + enclosure.gatebuild(screen, GRID_SIZE) + +def opengates(): + for enclosure in Enclosures: + enclosure.gateopen(fences) + +def draw_Animals(): + for Animal in Animals: + Animal.draw(screen, GRID_SIZE) + if Animal.feed() == 'True': + Animal.draw_exclamation(screen, GRID_SIZE, Animal.x, Animal.y) + else: + Animal.draw_food(screen,GRID_SIZE,Animal.x,Animal.y) + +def spawn_all_animals(): + for Animal in Animals: + spawner1 = Spawner(Animal, Enclosures) + spawner1.spawn_animal(fences, animals_position) -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, 'images/avatar.png', GRID_SIZE) clock = pygame.time.Clock() - + spawned = False while True: for event in pygame.event.get(): 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, Animals, fences) + screen.blit(background_image,(0,0)) draw_grid() - draw_animals() - draw_old_animals() - draw_agent(agent_pos) + draw_enclosures() + draw_gates() + if not spawned: + spawn_all_animals() + spawned = True + draw_Animals() + opengates() + agent.draw(screen) pygame.display.flip() clock.tick(10) diff --git a/parrot.py b/parrot.py new file mode 100644 index 0000000..1873b53 --- /dev/null +++ b/parrot.py @@ -0,0 +1,32 @@ +from animal import Animal +import pygame +from datetime import datetime + + + +class Parrot(Animal): + def __init__(self, x, y, adult=False): + Parrot_image = pygame.image.load('images/parrot.png') + name = 'parrot' + environment = "medium" + food_image = 'images/grains.png' + parrot_food = 'grains' + super().__init__(x, y,name, Parrot_image, food_image,parrot_food, environment, adult) + self._starttime = datetime.now() + + + + def feed(self): + self.getting_hungry() + if self._feed < 1.5: + 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 \ No newline at end of file diff --git a/penguin.py b/penguin.py new file mode 100644 index 0000000..830f176 --- /dev/null +++ b/penguin.py @@ -0,0 +1,32 @@ +from animal import Animal +import pygame +from datetime import datetime + + + +class Penguin(Animal): + def __init__(self, x, y, adult=False): + Penguin_image = pygame.image.load('images/penguin.png') + name = 'penguin' + environment = "cold" + food_image = 'images/fish.png' + penguin_food = 'fish' + super().__init__(x, y,name, Penguin_image, food_image,penguin_food,environment, adult) + self._starttime = datetime.now() + + + + def feed(self): + self.getting_hungry() + if self._feed < 2: + 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 \ No newline at end of file diff --git a/spawner.py b/spawner.py new file mode 100644 index 0000000..181ea06 --- /dev/null +++ b/spawner.py @@ -0,0 +1,44 @@ +import random + + +class Spawner: + def __init__(self, animal, enclosures): + self.animal = animal + self.enclosures = enclosures + + def spawn_animal(self, blocked, taken): + possibilities = self.enclosures + fitting = [] + for option in possibilities: + if option.type == self.animal.environment: + fitting.append(option) + enclosure = random.choice(fitting) + while True: + if enclosure.x1 < enclosure.x2: + self.animal.x = random.randint(enclosure.x1, enclosure.x2) + if enclosure.y1 < enclosure.y2: + self.animal.y = random.randint(enclosure.y1, enclosure.y2) + if enclosure.y1 > enclosure.y2: + self.animal.y = random.randint(enclosure.y2, enclosure.y1) + if enclosure.x1 > enclosure.x2: + self.animal.x = random.randint(enclosure.x2, enclosure.x1) + if enclosure.y1 < enclosure.y2: + self.animal.y = random.randint(enclosure.y1, enclosure.y2) + if enclosure.y1 > enclosure.y2: + self.animal.y = random.randint(enclosure.y2, enclosure.y1) + if self.check(blocked, taken): + break + + def check(self, blocked, taken): + x = self.animal.x + y = self.animal.y + if (x,y) in blocked or (x,y) in taken: + return False + taken.add((x,y)) + return True + + + + + +