reprezentacja_wiedzy2 #1

Merged
s481852 merged 7 commits from reprezentacja_wiedzy2 into master 2024-03-24 19:38:01 +01:00
36 changed files with 440 additions and 67 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.

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))

34
agent.py Normal file
View File

@ -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)

View File

@ -1,8 +1,47 @@
class Animal: import pygame
def __init__(self, x, y, image): from abc import ABC, abstractmethod
self.x = x-1
self.y = y-1
self.image = image
def draw(self, screen ,grid_size): class Animal:
screen.blit(self.image, (self.x *grid_size, self.y*grid_size)) 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

32
bear.py Normal file
View File

@ -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

37
elephant.py Normal file
View File

@ -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

79
enclosure.py Normal file
View File

@ -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))

32
giraffe.py Normal file
View File

@ -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

View File

Before

Width:  |  Height:  |  Size: 248 KiB

After

Width:  |  Height:  |  Size: 248 KiB

BIN
images/bear.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 MiB

View File

Before

Width:  |  Height:  |  Size: 642 KiB

After

Width:  |  Height:  |  Size: 642 KiB

BIN
images/exclamation.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

BIN
images/fenceHor.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

BIN
images/fenceVer.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

BIN
images/fish.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 934 KiB

BIN
images/gate.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

BIN
images/giraffe.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 256 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

BIN
images/parrot.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 MiB

BIN
images/penguin.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

View File

Before

Width:  |  Height:  |  Size: 209 KiB

After

Width:  |  Height:  |  Size: 209 KiB

116
main.py
View File

@ -1,13 +1,19 @@
import pygame import pygame
import sys import sys
from animal import Animal from elephant import Elephant
from adult_animal import AdultAnimal 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) BLACK = (0, 0, 0)
GRID_SIZE = 100 GRID_SIZE = 50
GRID_WIDTH = 20 GRID_WIDTH = 30
GRID_HEIGHT = 10 GRID_HEIGHT = 15
pygame.init() pygame.init()
@ -15,29 +21,38 @@ WINDOW_SIZE = (GRID_WIDTH * GRID_SIZE, GRID_HEIGHT * GRID_SIZE)
screen = pygame.display.set_mode(WINDOW_SIZE) screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption("Mini Zoo") 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) 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) fences = set()
an2=Animal(12,1,animal_image) animals_position = set()
an3=Animal(14,7,animal_image)
old_an1=AdultAnimal(3,6, animal_image,width=2,height=2)
animals=[]
animals.append(an1)
animals.append(an2)
animals.append(an3)
old_animals=[] an1 = Parrot(16, 2)
old_animals.append(old_an1) 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(): 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):
@ -45,43 +60,56 @@ 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_agent(agent_pos): def draw_enclosures():
x, y = agent_pos for enclosure in Enclosures:
screen.blit(agent_image, (x*GRID_SIZE,y*GRID_SIZE)) 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(): def main():
global agent_pos agent = Agent(0, 0, 'images/avatar.png', GRID_SIZE)
clock = pygame.time.Clock() clock = pygame.time.Clock()
spawned = False
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
pygame.quit() pygame.quit()
sys.exit() sys.exit()
elif event.type ==pygame.KEYDOWN: agent.handle_event(event, GRID_HEIGHT, GRID_WIDTH, Animals, fences)
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
screen.blit(background_image,(0,0)) screen.blit(background_image,(0,0))
draw_grid() draw_grid()
draw_animals() draw_enclosures()
draw_old_animals() draw_gates()
draw_agent(agent_pos) if not spawned:
spawn_all_animals()
spawned = True
draw_Animals()
opengates()
agent.draw(screen)
pygame.display.flip() pygame.display.flip()
clock.tick(10) clock.tick(10)

32
parrot.py Normal file
View File

@ -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

32
penguin.py Normal file
View File

@ -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

44
spawner.py Normal file
View File

@ -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