Ponowne naprawienie spawnowania zwierząt na płocie

This commit is contained in:
karolajoj 2024-04-12 21:50:11 +02:00
parent 75e597b3c6
commit abb75def38
15 changed files with 30 additions and 19 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.pyc

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

@ -27,19 +27,15 @@ class Agent:
def handle_event(self, event, max_x, max_y, animals, obstacles):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
self.move('Go Forward', max_x, max_y, obstacles)
self.move('Go Forward', max_x, max_y, obstacles, animals)
elif event.key == pygame.K_LEFT:
self.move('Turn Left', max_x, max_y, obstacles)
self.move('Turn Left', max_x, max_y, obstacles, animals)
elif event.key == pygame.K_RIGHT:
self.move('Turn Right', max_x, max_y, obstacles)
self.move('Turn Right', max_x, max_y, obstacles, animals)
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)
feed_animal(self, animals)
def move(self, action, max_x, max_y, obstacles):
def move(self, action, max_x, max_y, obstacles, animals):
if action == 'Go Forward':
new_x, new_y = self.x, self.y
if self.direction == 'N':
@ -59,4 +55,13 @@ class Agent:
self.direction = {'N': 'W', 'W': 'S', 'S': 'E', 'E': 'N'}[self.direction]
elif action == 'Turn Right':
self.direction = {'N': 'E', 'E': 'S', 'S': 'W', 'W': 'N'}[self.direction]
self.direction = {'N': 'E', 'E': 'S', 'S': 'W', 'W': 'N'}[self.direction]
feed_animal(self, animals)
def feed_animal(self, animals):
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)

23
main.py
View File

@ -1,3 +1,4 @@
import random
import pygame
import sys
from elephant import Elephant
@ -8,7 +9,6 @@ from bear import Bear
from agent import Agent
from enclosure import Enclosure
from spawner import Spawner
from state_space_search import succ
from state_space_search import graphsearch
BLACK = (0, 0, 0)
@ -159,12 +159,9 @@ def main():
obstacles = generate_obstacles()
actions = []
spawn_all_animals()
actions = graphsearch(agent.istate, (an1.x, an1.y), GRID_WIDTH, GRID_HEIGHT, obstacles)
# actions = graphsearch(agent.istate, (25,1), GRID_WIDTH, GRID_HEIGHT, obstacles)
clock = pygame.time.Clock()
spawned = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
@ -172,12 +169,20 @@ def main():
sys.exit()
agent.handle_event(event, GRID_WIDTH, GRID_HEIGHT, Animals, obstacles)
screen.blit(background_image,(0,0))
draw_grid()
draw_enclosures()
draw_gates()
if not spawned:
spawn_all_animals()
# region Test szukania ścieżki
animal = random.choice(Animals)
actions = graphsearch(agent.istate, (animal.x, animal.y), GRID_WIDTH, GRID_HEIGHT, obstacles)
animal._feed = 2 # Ustawienie aby zwierzę było głodne
# endregion
spawned = True
draw_Animals()
opengates()
agent.draw(screen)
@ -186,7 +191,7 @@ def main():
if actions:
action = actions.pop(0)
agent.move(action, GRID_WIDTH, GRID_HEIGHT, obstacles)
agent.move(action, GRID_WIDTH, GRID_HEIGHT, obstacles, Animals)
pygame.time.wait(200)
if __name__ == "__main__":