rozpoznany sauron = zmiana na inny cel

This commit is contained in:
Weranda 2023-06-01 17:23:53 +02:00
parent 7cb2903a1d
commit b01f3e48f9
8 changed files with 35 additions and 7 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

6
bfs.py
View File

@ -1,6 +1,6 @@
import pygame import pygame
from config import * from config import *
class Bfs: class Bfs():
def __init__(self,game): def __init__(self,game):
@ -12,12 +12,12 @@ class Bfs:
self.enemy_cells = [] self.enemy_cells = []
def heuristic(a,b): def heuristic(a,b):
return abs(a[0]-b[0])+abs(a[1]-b[1]) return abs(a[0]-b[0])+abs(a[1]-b[1])
def bfs(self): def bfs(self,goal_cell):
print("x: ", self.game.agent.x, "y: ", self.game.agent.y) print("x: ", self.game.agent.x, "y: ", self.game.agent.y)
cost_road=0 cost_road=0
self.open_queue.append(self.get_cell_number(self.game.agent.x,self.game.agent.y)) self.open_queue.append(self.get_cell_number(self.game.agent.x,self.game.agent.y))
# tutaj dodaje się cel agenta # tutaj dodaje się cel agenta
goal_cell = self.get_cell_number(self.game.flower.x,self.game.flower.y) #goal_cell = self.get_cell_number(self.game.flower.x,self.game.flower.y)
path = [] path = []
processing = True processing = True

30
main.py
View File

@ -26,6 +26,7 @@ class Game:
pygame.display.set_caption('Gra-SI') pygame.display.set_caption('Gra-SI')
self.bfs = Bfs(self) self.bfs = Bfs(self)
self.nn = NeuralN()
def new(self): # tworzy się nowa sesja grania def new(self): # tworzy się nowa sesja grania
self.all_sprites = pygame.sprite.LayeredUpdates() self.all_sprites = pygame.sprite.LayeredUpdates()
@ -87,7 +88,32 @@ class Game:
if event.type == pygame.MOUSEBUTTONDOWN: if event.type == pygame.MOUSEBUTTONDOWN:
mouse_presses = pygame.mouse.get_pressed() mouse_presses = pygame.mouse.get_pressed()
if mouse_presses[0]: if mouse_presses[0]:
self.bfs.bfs() #self.bfs.bfs(self.bfs.get_cell_number(self.flower.x,self.flower.y))
x = self.sauron.x
y = self.sauron.y
mob_image = self.sauron.SAURON_IMG
while True:
prediction = self.prediction_road(x,y,mob_image)
if prediction == "SAURON":
x = self.infantry_ork.x
y = self.infantry_ork.y
mob_image = self.infantry_ork.INFANTRY_ORK_IMG
prediction = self.prediction_road(x,y,mob_image)
else:
self.bfs.bfs(self.bfs.get_cell_number(x,y))
def prediction_road(self,x,y,mob_image):
mob_goal = (self.bfs.get_cell_number(x,y))
if self.bfs.get_up_cell(mob_goal) == None:
goal = self.bfs.get_down_cell(mob_goal)
else:
goal = self.bfs.get_up_cell(mob_goal)
self.bfs.bfs(goal)
prediction = self.nn.predict(mob_image)
return prediction
def map(self): # tworzenie mapy def map(self): # tworzenie mapy
self.clock.tick(FRAMERATE) self.clock.tick(FRAMERATE)
@ -213,6 +239,8 @@ print("path_true:",path_true)
g = Game() g = Game()
g.new() g.new()
while g.running: while g.running:

6
nn.py
View File

@ -12,7 +12,7 @@ import pathlib
class NeuralN: class NeuralN:
# @staticmethod # @staticmethod
# def predict(image): def predict(self,image):
data_dir = pathlib.Path('zdjecia') data_dir = pathlib.Path('zdjecia')
saved_model_path = pathlib.Path('trained_model.h5') saved_model_path = pathlib.Path('trained_model.h5')
class_names_path = pathlib.Path("class_names.pkl") class_names_path = pathlib.Path("class_names.pkl")
@ -93,7 +93,7 @@ class NeuralN:
tf.keras.layers.Softmax()]) tf.keras.layers.Softmax()])
#image_path = image #image_path = image
image_path = pathlib.Path('path_to_image') image_path = pathlib.Path('zdjecia\ORK_ARCHER\ork_archer (942).jpg')
image = Image.open(image_path) image = Image.open(image_path)
# Preprocess the image # Preprocess the image
@ -119,4 +119,4 @@ class NeuralN:
# Print the predicted label # Print the predicted label
print(predicted_label) print(predicted_label)
# return predicted_label, action return predicted_label#, action