losowe spawn zwierzat, sprawdzanie typu wybiegu, dodanie bramek

This commit is contained in:
LuminoX 2024-03-24 17:33:58 +01:00
parent 12b431e748
commit 654ab9f92f
10 changed files with 113 additions and 26 deletions

View File

@ -2,7 +2,7 @@ import pygame
from abc import ABC, abstractmethod
class Animal:
def __init__(self, x, y,name, image, food_image, food, adult=False):
def __init__(self, x, y,name, image, food_image, food, environment, adult=False,):
self.x = x - 1
self.y = y - 1
self.name = name
@ -10,7 +10,8 @@ class Animal:
self.adult = adult
self.food = food
self.food_image = food_image
self._feed = 0 #nowe zwierze jest głodne
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))

View File

@ -8,9 +8,10 @@ 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, adult)
super().__init__(x, y,name, Bear_image, food_image,bear_food,environment, adult)
self._starttime = datetime.now()

View File

@ -7,7 +7,8 @@ from datetime import datetime
class Elephant(Animal):
def __init__(self, x, y, adult=False):
Elephant_image = pygame.image.load('images/elephant.png')
name = 'elphant'
name = 'elephant'
environment = "hot"
if adult:
elephant_food = 'leavs'
food_image = 'images/leaves.png'
@ -15,7 +16,7 @@ class Elephant(Animal):
elephant_food = 'milk'
food_image = 'images/milk.png'
super().__init__(x, y,name, Elephant_image, food_image,elephant_food, adult)
super().__init__(x, y,name, Elephant_image, food_image,elephant_food, environment, adult)
self._starttime = datetime.now()

View File

@ -1,16 +1,36 @@
import pygame
class Enclosure:
def __init__(self, x1, y1, x2, y2, type, imageH, imageV):
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.type = type #hot/cold/medium
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))
@ -56,3 +76,4 @@ class Enclosure:

View File

@ -8,9 +8,10 @@ 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, adult)
super().__init__(x, y,name, Giraffe_image, food_image,giraffe_food, environment, adult)
self._starttime = datetime.now()

BIN
images/gate.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

47
main.py
View File

@ -7,6 +7,7 @@ from parrot import Parrot
from bear import Bear
from agent import Agent
from enclosure import Enclosure
from spawner import Spawner
BLACK = (0, 0, 0)
@ -26,9 +27,10 @@ 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')
blocked = set()
fences = set()
@ -41,11 +43,11 @@ an4 = Elephant(4,3)
Animals = [an1, an2, an3, an4, old_an1, old_an2]
en1 = Enclosure(1,5,9,11,"medium", fenceH, fenceV)
en2 = Enclosure(29,3, 13,1, 'medium', fenceH, fenceV)
en3 = Enclosure(11,5, 16,11, 'cold', fenceH, fenceV)
en4 = Enclosure(19,11, 30,5, 'hot', fenceH, fenceV)
en5 = Enclosure(4,13, 28,15, 'cold', fenceH, fenceV)
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]
@ -57,9 +59,17 @@ def draw_grid():
rect = pygame.Rect(x, y, GRID_SIZE, GRID_SIZE)
pygame.draw.rect(screen, BLACK, rect, 1)
def draw_enclosures(set):
def draw_enclosures():
for enclosure in Enclosures:
enclosure.draw(screen, GRID_SIZE, blocked)
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:
@ -69,28 +79,35 @@ def draw_Animals():
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)
def main():
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()
agent.handle_event(event,GRID_HEIGHT,GRID_WIDTH,Animals, blocked)
agent.handle_event(event, GRID_HEIGHT, GRID_WIDTH, Animals, fences)
screen.blit(background_image,(0,0))
draw_grid()
draw_enclosures(blocked)
#draw_Animals()
draw_enclosures()
draw_gates()
if not spawned:
spawn_all_animals()
spawned = True
draw_Animals()
opengates()
agent.draw(screen)
pygame.display.flip()
clock.tick(10)

View File

@ -8,9 +8,10 @@ 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, adult)
super().__init__(x, y,name, Parrot_image, food_image,parrot_food, environment, adult)
self._starttime = datetime.now()

View File

@ -8,9 +8,10 @@ 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, adult)
super().__init__(x, y,name, Penguin_image, food_image,penguin_food,environment, adult)
self._starttime = datetime.now()

43
spawner.py Normal file
View File

@ -0,0 +1,43 @@
import random
class Spawner:
def __init__(self, animal, enclosures):
self.animal = animal
self.enclosures = enclosures
def spawn_animal(self, blocked):
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):
break
def check(self, blocked):
x = self.animal.x
y = self.animal.y
if (x,y) in blocked:
return False
return True