Dodanie pory dnia

Dodanie sowy i nietoperza
This commit is contained in:
s481832 2024-05-10 22:29:08 +02:00
parent 693a8617f7
commit 9b0ecde75e
18 changed files with 137 additions and 67 deletions

View File

@ -1,5 +1,4 @@
import random import random
import pygame import pygame
from abc import abstractmethod from abc import abstractmethod

View File

@ -3,6 +3,8 @@ from giraffe import Giraffe
from penguin import Penguin from penguin import Penguin
from parrot import Parrot from parrot import Parrot
from bear import Bear from bear import Bear
from owl import Owl
from bat import Bat
def create_animals(): def create_animals():
giraffe1 = Giraffe(0, 0, adult=True) giraffe1 = Giraffe(0, 0, adult=True)
@ -26,30 +28,37 @@ def create_animals():
elephant5 = Elephant(0, 0) elephant5 = Elephant(0, 0)
parrot1 = Parrot(0, 0) parrot1 = Parrot(0, 0)
parrot2 = Parrot(0, 0) parrot2 = Parrot(0, 0)
parrot3 = Parrot(0, 0) owl1 = Owl(0, 0)
parrot4 = Parrot(0, 0) owl2 = Owl(0, 0)
parrot5 = Parrot(0, 0) bat1 = Bat(0, 0)
bat2 = Bat(0, 0)
Animals = [giraffe1, giraffe2, giraffe3, giraffe4, giraffe5, Animals = [giraffe1, giraffe2, giraffe3, giraffe4, giraffe5,
bear1, bear2, bear3, bear4, bear5, bear1, bear2, bear3, bear4, bear5,
elephant1, elephant2, elephant3, elephant4, elephant5, elephant1, elephant2, elephant3, elephant4, elephant5,
penguin1, penguin2, penguin3, penguin4, penguin1, penguin2, penguin3, penguin4,
parrot1, parrot2, parrot3, parrot4, parrot5] parrot1, parrot2, owl1, owl2, bat1, bat2]
return Animals return Animals
def draw_Animals(Animals, const): def draw_Animals(Animals, const):
for Animal in Animals: for Animal in Animals:
Animal.draw(const.screen, const.GRID_SIZE) Animal.draw(const.screen, const.GRID_SIZE)
if Animal.getting_hungry() == 5:
if Animal.getting_hungry(const) == 5:
Animal.draw_exclamation(const.screen, const.GRID_SIZE, Animal.x, Animal.y) Animal.draw_exclamation(const.screen, const.GRID_SIZE, Animal.x, Animal.y)
Animal.draw_food(const.screen,const.GRID_SIZE,Animal.x,Animal.y,'images/empty_bowl.png') Animal.draw_food(const.screen,const.GRID_SIZE,Animal.x,Animal.y,'images/empty_bowl.png')
if Animal.getting_hungry() >= 4 and Animal.getting_hungry() < 5:
if Animal.getting_hungry(const) >= 4 and Animal.getting_hungry(const) < 5:
Animal.draw_exclamation(const.screen, const.GRID_SIZE, Animal.x, Animal.y) Animal.draw_exclamation(const.screen, const.GRID_SIZE, Animal.x, Animal.y)
Animal.draw_food(const.screen,const.GRID_SIZE,Animal.x,Animal.y,'images/almost_empty.png') Animal.draw_food(const.screen,const.GRID_SIZE,Animal.x,Animal.y,'images/almost_empty.png')
if Animal.getting_hungry() >= 2 and Animal.getting_hungry() < 4:
if Animal.getting_hungry(const) >= 2 and Animal.getting_hungry(const) < 4:
Animal.draw_food(const.screen,const.GRID_SIZE,Animal.x,Animal.y,'images/half_bowl.png') Animal.draw_food(const.screen,const.GRID_SIZE,Animal.x,Animal.y,'images/half_bowl.png')
if Animal.getting_hungry() < 2:
if Animal.getting_hungry(const) < 2:
Animal.draw_food(const.screen,const.GRID_SIZE,Animal.x,Animal.y,'images/full_bowl.png') Animal.draw_food(const.screen,const.GRID_SIZE,Animal.x,Animal.y,'images/full_bowl.png')
if Animal.ill: if Animal.ill:
Animal.draw_illness(const.screen, const.GRID_SIZE, Animal.x, Animal.y) Animal.draw_illness(const.screen, const.GRID_SIZE, Animal.x, Animal.y)

25
Animals/bat.py Normal file
View File

@ -0,0 +1,25 @@
from animal import Animal
import pygame
from datetime import datetime
class Bat(Animal):
def __init__(self, x, y, adult=False):
Bat_image = pygame.image.load('images/bat.png')
name = 'bat'
environment = "medium"
food_image = 'images/grains.png'
parrot_food = 'grains'
super().__init__(x, y,name, Bat_image, food_image,parrot_food, environment, adult)
self._starttime = datetime.now()
def getting_hungry(self, const):
checktime = datetime.now()
delta = checktime - self._starttime
minutes_passed = delta.total_seconds() / 30
self._starttime = checktime
if const.IS_NIGHT and self._feed < 5:
self._feed += minutes_passed
self._feed = min(self._feed, 5)
return self._feed

View File

@ -14,15 +14,14 @@ class Bear(Animal):
super().__init__(x, y,name, Bear_image, food_image,bear_food,environment, activity, ill, adult) super().__init__(x, y,name, Bear_image, food_image,bear_food,environment, activity, ill, adult)
self._starttime = datetime.now() self._starttime = datetime.now()
def getting_hungry(self, const):
def getting_hungry(self):
checktime = datetime.now() checktime = datetime.now()
delta = checktime - self._starttime delta = checktime - self._starttime
minutes_passed = delta.total_seconds() / 60 minutes_passed = delta.total_seconds() / 60
self._feed += minutes_passed
self._starttime = checktime self._starttime = checktime
if self._feed > 5:
self._feed = 5 if const.IS_NIGHT and self._feed < 5 and const.season != "winter":
self._feed += minutes_passed
self._feed = min(self._feed, 5)
return self._feed return self._feed

View File

@ -2,8 +2,6 @@ from animal import Animal
import pygame import pygame
from datetime import datetime 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')
@ -20,15 +18,14 @@ class Elephant(Animal):
super().__init__(x, y,name, Elephant_image, food_image,elephant_food, environment, activity, ill, adult) super().__init__(x, y,name, Elephant_image, food_image,elephant_food, environment, activity, ill, adult)
self._starttime = datetime.now() self._starttime = datetime.now()
def getting_hungry(self, const):
def getting_hungry(self):
checktime = datetime.now() checktime = datetime.now()
delta = checktime - self._starttime delta = checktime - self._starttime
minutes_passed = delta.total_seconds() / 50 minutes_passed = delta.total_seconds() / 50
self._feed += minutes_passed
self._starttime = checktime self._starttime = checktime
if self._feed > 5:
self._feed = 5 if not const.IS_NIGHT and self._feed < 5:
self._feed += minutes_passed
self._feed = min(self._feed, 5)
return self._feed return self._feed

View File

@ -2,8 +2,6 @@ from animal import Animal
import pygame import pygame
from datetime import datetime 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')
@ -16,14 +14,13 @@ class Giraffe(Animal):
super().__init__(x, y, name, Giraffe_image, food_image,giraffe_food, environment, activity, ill, adult) super().__init__(x, y, name, Giraffe_image, food_image,giraffe_food, environment, activity, ill, adult)
self._starttime = datetime.now() self._starttime = datetime.now()
def getting_hungry(self, const):
def getting_hungry(self):
checktime = datetime.now() checktime = datetime.now()
delta = checktime - self._starttime delta = checktime - self._starttime
minutes_passed = delta.total_seconds() / 30 minutes_passed = delta.total_seconds() / 30
self._feed += minutes_passed
self._starttime = checktime self._starttime = checktime
if self._feed > 5:
self._feed = 5 if not const.IS_NIGHT and self._feed < 5:
self._feed += minutes_passed
self._feed = min(self._feed, 5)
return self._feed return self._feed

25
Animals/owl.py Normal file
View File

@ -0,0 +1,25 @@
from animal import Animal
import pygame
from datetime import datetime
class Owl(Animal):
def __init__(self, x, y, adult=False):
Owl_image = pygame.image.load('images/owl.png')
name = 'owl'
environment = "medium"
food_image = 'images/grains.png'
parrot_food = 'grains'
super().__init__(x, y,name, Owl_image, food_image,parrot_food, environment, adult)
self._starttime = datetime.now()
def getting_hungry(self, const):
checktime = datetime.now()
delta = checktime - self._starttime
minutes_passed = delta.total_seconds() / 25
self._starttime = checktime
if const.IS_NIGHT and self._feed < 5:
self._feed += minutes_passed
self._feed = min(self._feed, 5)
return self._feed

View File

@ -2,8 +2,6 @@ from animal import Animal
import pygame import pygame
from datetime import datetime 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')
@ -16,14 +14,13 @@ class Parrot(Animal):
super().__init__(x, y, name, Parrot_image, food_image, parrot_food, environment, activity, ill, adult) super().__init__(x, y, name, Parrot_image, food_image, parrot_food, environment, activity, ill, adult)
self._starttime = datetime.now() self._starttime = datetime.now()
def getting_hungry(self, const):
def getting_hungry(self):
checktime = datetime.now() checktime = datetime.now()
delta = checktime - self._starttime delta = checktime - self._starttime
minutes_passed = delta.total_seconds() / 20 minutes_passed = delta.total_seconds() / 20
self._feed += minutes_passed
self._starttime = checktime self._starttime = checktime
if self._feed > 5:
self._feed = 5 if not const.IS_NIGHT and self._feed < 5:
self._feed += minutes_passed
self._feed = min(self._feed, 5)
return self._feed return self._feed

View File

@ -2,8 +2,6 @@ from animal import Animal
import pygame import pygame
from datetime import datetime 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')
@ -16,14 +14,13 @@ class Penguin(Animal):
super().__init__(x, y, name, Penguin_image, food_image, penguin_food, environment, activity, ill, adult) super().__init__(x, y, name, Penguin_image, food_image, penguin_food, environment, activity, ill, adult)
self._starttime = datetime.now() self._starttime = datetime.now()
def getting_hungry(self, const):
def getting_hungry(self):
checktime = datetime.now() checktime = datetime.now()
delta = checktime - self._starttime delta = checktime - self._starttime
minutes_passed = delta.total_seconds() / 15 minutes_passed = delta.total_seconds() / 15
self._feed += minutes_passed
self._starttime = checktime self._starttime = checktime
if self._feed > 5:
self._feed = 5 if not const.IS_NIGHT and self._feed < 5:
self._feed += minutes_passed
self._feed = min(self._feed, 5)
return self._feed return self._feed

View File

@ -1,5 +1,7 @@
import pygame import pygame
from constants import Constants
from state_space_search import is_border, is_obstacle from state_space_search import is_border, is_obstacle
from night import draw_night
class Agent: class Agent:
def __init__(self, istate, image_path, grid_size): def __init__(self, istate, image_path, grid_size):
@ -10,7 +12,7 @@ class Agent:
self.image = pygame.transform.scale(self.image, (grid_size, grid_size)) self.image = pygame.transform.scale(self.image, (grid_size, grid_size))
self._food = 0 self._food = 0
def draw(self, screen, grid_size): def draw(self, const):
# Obróć obrazek zgodnie z kierunkiem # Obróć obrazek zgodnie z kierunkiem
if self.direction == 'E': if self.direction == 'E':
self.image= pygame.image.load('images/agent4.png') self.image= pygame.image.load('images/agent4.png')
@ -20,8 +22,10 @@ class Agent:
self.image= pygame.image.load('images/agent3.png') self.image= pygame.image.load('images/agent3.png')
else: # direction == 'N' else: # direction == 'N'
self.image= pygame.image.load('images/agent2.png') self.image= pygame.image.load('images/agent2.png')
self.image = pygame.transform.scale(self.image, (grid_size, grid_size)) self.image = pygame.transform.scale(self.image, (const.GRID_SIZE, const.GRID_SIZE))
screen.blit(self.image, (self.x * self.grid_size, self.y * self.grid_size)) const.screen.blit(self.image, (self.x * self.grid_size, self.y * self.grid_size))
if const.IS_NIGHT: draw_night(const)
def handle_event(self, event, max_x, max_y, animals, obstacles): def handle_event(self, event, max_x, max_y, animals, obstacles):
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
@ -63,7 +67,7 @@ def feed_animal(self, animals, goal):
if self.x == goal_x and self.y == goal_y: if self.x == goal_x and self.y == goal_y:
for animal in animals: for animal in animals:
if animal.x == goal_x and animal.y == goal_y: if animal.x == goal_x and animal.y == goal_y:
if animal.getting_hungry() < self._food : if animal.getting_hungry(const=Constants()) < self._food :
self._food -= animal._feed self._food -= animal._feed
animal._feed = 0 animal._feed = 0
print(animal.name, "fed with", animal.food) print(animal.name, "fed with", animal.food)

View File

@ -1,4 +1,6 @@
import random
import pygame import pygame
import time
class Constants: class Constants:
def __init__(self): def __init__(self):
@ -10,6 +12,11 @@ class Constants:
self.WINDOW_SIZE = (self.GRID_WIDTH * self.GRID_SIZE, self.GRID_HEIGHT * self.GRID_SIZE) self.WINDOW_SIZE = (self.GRID_WIDTH * self.GRID_SIZE, self.GRID_HEIGHT * self.GRID_SIZE)
self.background_image = pygame.transform.scale(pygame.image.load('images/tło.jpg'), self.WINDOW_SIZE) self.background_image = pygame.transform.scale(pygame.image.load('images/tło.jpg'), self.WINDOW_SIZE)
self.IS_NIGHT = False
self.TIME_CHANGE = time.time() + 60
self.season = random.choice(["spring", "summer", "autumn", "winter"])
def init_pygame(const): def init_pygame(const):
pygame.init() pygame.init()
const.screen = pygame.display.set_mode(const.WINDOW_SIZE) const.screen = pygame.display.set_mode(const.WINDOW_SIZE)

View File

@ -19,4 +19,4 @@ def draw_house(const):
image_path = 'images/house.png' image_path = 'images/house.png'
image_surface = pygame.image.load(image_path) # Wczytanie obrazka do obiektu Surface image_surface = pygame.image.load(image_path) # Wczytanie obrazka do obiektu Surface
scaled_image = pygame.transform.scale(image_surface, (const.GRID_SIZE * 2, const.GRID_SIZE * 2)) scaled_image = pygame.transform.scale(image_surface, (const.GRID_SIZE * 2, const.GRID_SIZE * 2))
const.screen.blit(scaled_image, (X * const.GRID_SIZE, Y * const.GRID_SIZE)) const.screen.blit(scaled_image, (X * const.GRID_SIZE, Y * const.GRID_SIZE))

View File

@ -1,6 +1,5 @@
import pygame import pygame
class Enclosure: class Enclosure:
def __init__(self, x1, y1, x2, y2, gate1, gate2, type, imageH, imageV, imageGate): def __init__(self, x1, y1, x2, y2, gate1, gate2, type, imageH, imageV, imageGate):
self.x1 = x1 - 1 self.x1 = x1 - 1

BIN
images/bat.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 740 KiB

BIN
images/owl.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 286 KiB

13
main.py
View File

@ -1,6 +1,7 @@
import random import random
import pygame import pygame
import sys import sys
sys.path.append('./Animals') sys.path.append('./Animals')
from animals import create_animals, draw_Animals from animals import create_animals, draw_Animals
@ -11,7 +12,8 @@ from state_space_search import graphsearch, generate_cost_map
from terrain_obstacle import create_obstacles, draw_Terrain_Obstacles from terrain_obstacle import create_obstacles, draw_Terrain_Obstacles
from constants import Constants, init_pygame from constants import Constants, init_pygame
from draw import draw_goal, draw_grid, draw_house from draw import draw_goal, draw_grid, draw_house
from season import draw_background, draw_season from season import draw_background
from night import change_time
const = Constants() const = Constants()
init_pygame(const) init_pygame(const)
@ -76,7 +78,7 @@ def main():
actions = [] actions = []
clock = pygame.time.Clock() clock = pygame.time.Clock()
spawned = False spawned = False
season = draw_season()
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:
@ -84,7 +86,8 @@ def main():
sys.exit() sys.exit()
agent.handle_event(event, const.GRID_WIDTH, const.GRID_HEIGHT, Animals, obstacles) agent.handle_event(event, const.GRID_WIDTH, const.GRID_HEIGHT, Animals, obstacles)
draw_background(const, season) change_time(const)
draw_background(const)
draw_grid(const) draw_grid(const)
draw_enclosures(Enclosures, const) draw_enclosures(Enclosures, const)
draw_gates(Enclosures, const) draw_gates(Enclosures, const)
@ -96,12 +99,12 @@ def main():
spawn_obstacles() spawn_obstacles()
cost_map = generate_cost_map(Animals, Terrain_Obstacles) cost_map = generate_cost_map(Animals, Terrain_Obstacles)
for animal in Animals: for animal in Animals:
animal._feed = 3 animal._feed = 0
spawned = True spawned = True
draw_Animals(Animals, const) draw_Animals(Animals, const)
draw_Terrain_Obstacles(Terrain_Obstacles, const) draw_Terrain_Obstacles(Terrain_Obstacles, const)
agent.draw(const.screen, const.GRID_SIZE) agent.draw(const)
pygame.display.flip() pygame.display.flip()
clock.tick(10) clock.tick(10)

17
night.py Normal file
View File

@ -0,0 +1,17 @@
import time
import pygame
def draw_night(const):
overlay = pygame.Surface(const.WINDOW_SIZE)
overlay.fill((0, 0, 0))
overlay.set_alpha(128) # Ustawienie przezroczystości (0 - całkowicie przeźroczyste, 255 - nieprzeźroczyste)
const.screen.blit(overlay, (0, 0))
def change_time(const):
current_time = time.time()
# Sprawdzamy, czy nadszedł czas zmiany pory dnia
if current_time >= const.TIME_CHANGE:
# Zmieniamy porę dnia
const.IS_NIGHT = not const.IS_NIGHT # Jeśli było dzień, teraz będzie noc, i odwrotnie
const.TIME_CHANGE = current_time + 60 # Ustawiamy czas na kolejną zmianę za 1 minutę

View File

@ -1,16 +1,11 @@
import pygame import pygame
import random
def draw_season():
seasons = ["spring", "summer", "autumn", "winter"]
return random.choice(seasons)
def draw_background(const, season): def draw_background(const):
season_images = { season_images = {
"spring": "images/tłowiosna.jpg", "spring": "images/tłowiosna.jpg",
"summer": "images/tło.jpg", "summer": "images/tło.jpg",
"autumn": "images/tłojesień.jpg", "autumn": "images/tłojesień.jpg",
"winter": "images/tłozima.jpg" "winter": "images/tłozima.jpg"
} }
background_image = pygame.transform.scale(pygame.image.load(season_images[season]), const.WINDOW_SIZE) background_image = pygame.transform.scale(pygame.image.load(season_images[const.season]), const.WINDOW_SIZE)
const.screen.blit(background_image, (0, 0)) const.screen.blit(background_image, (0, 0))