agent sam ukańcza plansze (predictions do poprawy)

This commit is contained in:
Weranda 2023-06-13 15:50:03 +02:00
parent 062d2f00fe
commit 18d27c63c3
11 changed files with 81 additions and 104 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -9,7 +9,7 @@ class Agent(pygame.sprite.Sprite):
self.game = game self.game = game
self.groups = self.game.all_sprites self.groups = self.game.all_sprites
pygame.sprite.Sprite.__init__(self, self.groups) pygame.sprite.Sprite.__init__(self, self.groups)
# direction =['right','down','left','up'] 0 1 2 3 kierunek w ktory po kliknieciu do przodu pojdzie
self.AGENT_IMAGES =['gandalf-prawo','gandalf-dol','gandalf-lewo','gandalf-gora'] self.AGENT_IMAGES =['gandalf-prawo','gandalf-dol','gandalf-lewo','gandalf-gora']
self.x = x * TILE_SIZE self.x = x * TILE_SIZE
self.y = y * TILE_SIZE self.y = y * TILE_SIZE
@ -19,7 +19,6 @@ class Agent(pygame.sprite.Sprite):
self.x_change = 0 self.x_change = 0
self.y_change = 0 self.y_change = 0
#self.AGENT_IMG = pygame.image.load("./zdjecia/"+self.AGENT_IMAGES[self.direction]+".png")
self.AGENT_IMG_RIGHT = pygame.image.load("./pozostale_zdjecia/gandalf-prawo.png") self.AGENT_IMG_RIGHT = pygame.image.load("./pozostale_zdjecia/gandalf-prawo.png")
self.AGENT_RIGHT = pygame.transform.scale(self.AGENT_IMG_RIGHT,(64,64)) self.AGENT_RIGHT = pygame.transform.scale(self.AGENT_IMG_RIGHT,(64,64))
self.AGENT_IMG_DOWN = pygame.image.load("./pozostale_zdjecia/gandalf-dol.png") self.AGENT_IMG_DOWN = pygame.image.load("./pozostale_zdjecia/gandalf-dol.png")
@ -41,12 +40,14 @@ class Agent(pygame.sprite.Sprite):
self.level = 1 self.level = 1
self.current_health = 500 self.current_health = 200
self.max_health = 1000 self.max_health = 200
self.health_bar_length = 300 self.health_bar_length = 200
self.health_ratio = self.max_health/self.health_bar_length self.health_ratio = self.max_health/self.health_bar_length
self._layer = AGENT_LAYER self._layer = AGENT_LAYER
self.damage = 50*self.level
self.artifact = True
def update(self): def update(self):
@ -54,7 +55,6 @@ class Agent(pygame.sprite.Sprite):
self.movement() self.movement()
self.collide_mob() self.collide_mob()
self.collide_flower() self.collide_flower()
#self.end_game() gra sie konczy gdy wie gdzie sa wszyscy
self.disp_level() self.disp_level()
self.rect.x += self.x_change self.rect.x += self.x_change
@ -102,9 +102,6 @@ class Agent(pygame.sprite.Sprite):
if self.direction==3 and self.rect.y > 0: if self.direction==3 and self.rect.y > 0:
self.y_change -= TILE_SIZE self.y_change -= TILE_SIZE
def end_game(self):
if (-1 in self.game.state)==False:
pygame.quit()
def collide_flower(self): def collide_flower(self):
hits_flower = pygame.sprite.spritecollide(self, self.game.flowers, False) hits_flower = pygame.sprite.spritecollide(self, self.game.flowers, False)
@ -133,13 +130,7 @@ class Agent(pygame.sprite.Sprite):
hits_archer_ork = pygame.sprite.spritecollide(self, self.game.archer_orks, False) hits_archer_ork = pygame.sprite.spritecollide(self, self.game.archer_orks, False)
hits_infantry_ork = pygame.sprite.spritecollide(self, self.game.infantry_orks, False) hits_infantry_ork = pygame.sprite.spritecollide(self, self.game.infantry_orks, False)
#hits_infantry_ork2 = pygame.sprite.spritecollide(self, self.game.infantry_orks2, False)
hits_sauron = pygame.sprite.spritecollide(self, self.game.sauronL, False) hits_sauron = pygame.sprite.spritecollide(self, self.game.sauronL, False)
#hits_unknown_mob = pygame.sprite.spritecollide(self, self.game.unknown_mobs, False) #unknown mob
#if hits_unknown_mob:
# self.game.unknown_mob.kill()
# self.game.archer_orks = pygame.sprite.LayeredUpdates()
# self.game.archer_ork = Archer_ork(self,3,2)
@ -186,22 +177,6 @@ class Agent(pygame.sprite.Sprite):
self.level=self.level+1 self.level=self.level+1
pygame.quit() pygame.quit()
'''
if hits_infantry_ork2:
if self.game.infantry_ork2.level > self.level or self.game.infantry_ork2.damage > self.current_health:
self.game.state[4]=self.game.infantry_ork2.x
self.game.state[5]=self.game.infantry_ork2.y
print(self.game.state)
self.kill()
self.game.new()
else:
self.game.state[4]=self.game.infantry_ork2.x
self.game.state[5]=self.game.infantry_ork2.y
print(self.game.state)
self.game.infantry_ork2.kill()
self.get_damage(self.game.infantry_ork2.damage)
self.level=self.level+1
'''
@ -211,8 +186,6 @@ class Agent(pygame.sprite.Sprite):
if self.current_health <= 0: if self.current_health <= 0:
self.current_health = 0 self.current_health = 0
#zmienic potem na smierc oraz później trzeba będzie tutaj ująć wszystkie statystyki
#i ze statystyk obliczyć ile dmg dostanie agent
def get_health(self, amount): def get_health(self, amount):
if self.current_health < self.max_health: if self.current_health < self.max_health:
@ -228,7 +201,6 @@ class Agent(pygame.sprite.Sprite):
def disp_level(self): def disp_level(self):
font = pygame.font.SysFont(None, 40) font = pygame.font.SysFont(None, 40)
lvlDisplay = font.render(str(self.level)+" "+str(self.direction), 1, WHITE) lvlDisplay = font.render(str(self.level)+" "+str(self.direction), 1, WHITE)
#lvlDisplay = font.render(str(self.level), 1, WHITE)
pygame.draw.rect(self.game.SCREEN, BLACK, (370, 780, 40, 40)) pygame.draw.rect(self.game.SCREEN, BLACK, (370, 780, 40, 40))
self.game.SCREEN.blit(lvlDisplay, (370,780)) self.game.SCREEN.blit(lvlDisplay, (370,780))

View File

@ -28,10 +28,13 @@ class Astar():
if current in came_from: if current in came_from:
path = self.print_path(came_from, came_from[current],path) path = self.print_path(came_from, came_from[current],path)
path.append(self.g.bfs.get_cell_number(current[0]*TILE_SIZE,current[1]*TILE_SIZE)) path.append(self.g.bfs.get_cell_number(current[0]*TILE_SIZE,current[1]*TILE_SIZE))
print("Budowanie ścieżki: ",path) #print("Budowanie ścieżki: ",path)
return path return path
def a_star(self,start, goal,path): def a_star(self, goal):
path = []
start = (self.g.agent.rect.x//TILE_SIZE, self.g.agent.rect.y//TILE_SIZE)
print(start,goal)
open_set = [] open_set = []
heapq.heappush(open_set, (0, start)) # Priority queue with the start position heapq.heappush(open_set, (0, start)) # Priority queue with the start position
came_from = {} came_from = {}

4
bfs.py
View File

@ -10,10 +10,10 @@ class Bfs():
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,goal_cell): def bfs(self,goal_cell):
print("x: ", self.game.agent.x, "y: ", self.game.agent.y) print("x: ", self.game.agent.rect.x, "y: ", self.game.agent.rect.y)
visited = set() visited = set()
q = queue.Queue() q = queue.Queue()
start_position = self.get_cell_number(self.game.agent.x,self.game.agent.y) start_position = self.get_cell_number(self.game.agent.rect.x,self.game.agent.rect.y)
q.put(start_position) q.put(start_position)
parent = {} parent = {}

78
main.py
View File

@ -83,10 +83,8 @@ class Game:
pygame.quit() pygame.quit()
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE: if event.key == pygame.K_SPACE:
self.start_pos = (self.agent.x//TILE_SIZE, self.agent.y//TILE_SIZE)
self.goal_pos = (self.flower.x//TILE_SIZE, self.flower.y//TILE_SIZE) self.goal_pos = (self.flower.x//TILE_SIZE, self.flower.y//TILE_SIZE)
self.path = [] self.move_agent(self.astar.a_star(self.goal_pos))
self.move_agent(self.astar.a_star(self.start_pos, self.goal_pos,self.path))
@ -96,35 +94,63 @@ class Game:
x = self.sauron.x x = self.sauron.x
y = self.sauron.y y = self.sauron.y
mob_image = self.sauron.SAURON_IMG goal = x//TILE_SIZE,y//TILE_SIZE
mob_image = self.sauron.image_path
prediction = self.prediction_road(x,y,mob_image)
prediction = "SAURON"
while True: #do poprawienia poprawne rozpoznawanie póki co nie będzie działać dobrze, program się będzie zawieszać while True: #do poprawienia poprawne rozpoznawanie póki co nie będzie działać dobrze, program się będzie zawieszać
prediction = self.prediction_road(x,y,mob_image) if prediction == "SAURON" and self.agent.level < 3:
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)
if prediction == "ORK_INFANTRY":
self.move_agent(self.bfs.bfs(self.bfs.get_cell_number(x,y)))
x = self.archer_ork.x x = self.archer_ork.x
y = self.archer_ork.y y = self.archer_ork.y
mob_image = self.archer_ork.ARCHER_ORK_IMG goal = x//TILE_SIZE,y//TILE_SIZE
mob_image = self.archer_ork.image_path
prediction = self.prediction_road(x,y,mob_image) prediction = self.prediction_road(x,y,mob_image)
if prediction == "ORK_ARCHER": prediction = "ORK_ARCHER"
self.move_agent(self.bfs.bfs(self.bfs.get_cell_number(x,y))) elif prediction == "SAURON" and self.agent.level >= 3:
self.obstacles[1][10] = False
self.move_agent(self.astar.a_star(goal))
elif prediction == "ORK_INFANTRY":
self.obstacles[10][4] = False
self.move_agent(self.astar.a_star(goal))
if self.agent.current_health < self.agent.max_health:
goal = (self.flower.x//TILE_SIZE, self.flower.y//TILE_SIZE)
self.move_agent(self.astar.a_star(goal))
x = self.sauron.x x = self.sauron.x
y = self.sauron.y y = self.sauron.y
mob_image = self.sauron.SAURON_IMG goal = x//TILE_SIZE,y//TILE_SIZE
mob_image = self.sauron.image_path
prediction = self.prediction_road(x,y,mob_image)
prediction = "SAURON"
elif prediction == "ORK_ARCHER":
self.obstacles[10][10] = False
self.move_agent(self.astar.a_star(goal))
if self.agent.current_health < self.agent.max_health:
goal = (self.flower.x//TILE_SIZE, self.flower.y//TILE_SIZE)
self.move_agent(self.astar.a_star(goal))
x = self.infantry_ork.x
y = self.infantry_ork.y
goal = x//TILE_SIZE,y//TILE_SIZE
mob_image = self.infantry_ork.image_path
prediction = self.prediction_road(x,y,mob_image)
prediction = "ORK_INFANTRY"
def prediction_road(self,x,y,mob_image): def prediction_road(self,x,y,mob_image):
mob_goal = (self.bfs.get_cell_number(x,y)) mob_goal_cell = (self.bfs.get_cell_number(x,y))
if self.bfs.get_up_cell(mob_goal) == None: if self.bfs.get_up_cell(mob_goal_cell) == None:
goal = self.bfs.get_down_cell(mob_goal) goal_cell = self.bfs.get_down_cell(mob_goal_cell)
x,y = self.bfs.get_coordinates(goal_cell)
goal = x//TILE_SIZE,y//TILE_SIZE
self.move_agent(self.astar.a_star(goal))
prediction = self.nn.predict(mob_image)
else: else:
goal = self.bfs.get_up_cell(mob_goal) goal_cell = self.bfs.get_up_cell(mob_goal_cell)
self.move_agent(self.bfs.bfs(goal)) x,y = self.bfs.get_coordinates(goal_cell)
goal = x//TILE_SIZE,y//TILE_SIZE
self.move_agent(self.astar.a_star(goal))
prediction = self.nn.predict(mob_image) prediction = self.nn.predict(mob_image)
return prediction return prediction
@ -133,7 +159,7 @@ class Game:
for cell_to_move in path: for cell_to_move in path:
x, y = self.bfs.get_coordinates(cell_to_move) x, y = self.bfs.get_coordinates(cell_to_move)
print("Ruch do kratki : ", cell_to_move, " z x: ", x, ", y: ", y, ", agent.x: ", self.agent.rect.x, ", agent.y: ", self.agent.rect.y) print("Ruch do kratki : ", cell_to_move, " z x: ", x, ", y: ", y, ", agent.x: ", self.agent.rect.x, ", agent.y: ", self.agent.rect.y)
if(self.bfs.get_cell_number(self.agent.x,self.agent.y)!=cell_to_move): if(self.bfs.get_cell_number(self.agent.rect.x,self.agent.rect.y)!=cell_to_move):
if x > self.agent.rect.x: if x > self.agent.rect.x:
self.agent.direction = 0 self.agent.direction = 0
elif y > self.agent.rect.y: elif y > self.agent.rect.y:
@ -143,16 +169,16 @@ class Game:
elif y < self.agent.rect.y: elif y < self.agent.rect.y:
self.agent.direction = 3 self.agent.direction = 3
if self.agent.direction==0: if self.agent.direction==0:
print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction]) #print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction])
self.agent.x_change += TILE_SIZE self.agent.x_change += TILE_SIZE
elif self.agent.direction==1: elif self.agent.direction==1:
print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction]) #print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction])
self.agent.y_change += TILE_SIZE self.agent.y_change += TILE_SIZE
elif self.agent.direction==2: elif self.agent.direction==2:
print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction]) #print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction])
self.agent.x_change -= TILE_SIZE self.agent.x_change -= TILE_SIZE
elif self.agent.direction==3: elif self.agent.direction==3:
print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction]) #print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction])
self.agent.y_change -= TILE_SIZE self.agent.y_change -= TILE_SIZE
self.agent.rotate() self.agent.rotate()

44
mobs.py
View File

@ -14,7 +14,8 @@ class Archer_ork(pygame.sprite.Sprite):
self.width = TILE_SIZE self.width = TILE_SIZE
self.height = TILE_SIZE self.height = TILE_SIZE
self.ARCHER_ORK_IMG = pygame.image.load("./pozostale_zdjecia/ork_lucznik.png") self.image_path = "./pozostale_zdjecia/ork_lucznik.png"
self.ARCHER_ORK_IMG = pygame.image.load(self.image_path)
self.ARCHER_ORK = pygame.transform.scale(self.ARCHER_ORK_IMG,(64,64)) self.ARCHER_ORK = pygame.transform.scale(self.ARCHER_ORK_IMG,(64,64))
self.image = pygame.Surface([self.width, self.height]) self.image = pygame.Surface([self.width, self.height])
@ -27,6 +28,7 @@ class Archer_ork(pygame.sprite.Sprite):
self.level = 1 self.level = 1
self.damage = 50*self.level self.damage = 50*self.level
self.health = 50
class Infantry_ork(pygame.sprite.Sprite): class Infantry_ork(pygame.sprite.Sprite):
@ -41,7 +43,8 @@ class Infantry_ork(pygame.sprite.Sprite):
self.width = TILE_SIZE self.width = TILE_SIZE
self.height = TILE_SIZE self.height = TILE_SIZE
self.INFANTRY_ORK_IMG = pygame.image.load("./pozostale_zdjecia/ork-piechota.png") self.image_path = "./pozostale_zdjecia/ork-piechota.png"
self.INFANTRY_ORK_IMG = pygame.image.load(self.image_path)
self.INFANTRY_ORK = pygame.transform.scale(self.INFANTRY_ORK_IMG,(64,64)) self.INFANTRY_ORK = pygame.transform.scale(self.INFANTRY_ORK_IMG,(64,64))
self.image = pygame.Surface([self.width, self.height]) self.image = pygame.Surface([self.width, self.height])
@ -54,37 +57,9 @@ class Infantry_ork(pygame.sprite.Sprite):
self.level = 2 self.level = 2
self.damage = 50*self.level self.damage = 50*self.level
self.health = 100
'''
class Infantry_ork2(pygame.sprite.Sprite):
def __init__(self, game, x, y):
self.game = game
self.groups = self.game.all_sprites, self.game.infantry_orks2
pygame.sprite.Sprite.__init__(self, self.groups)
self.x = x * TILE_SIZE
self.y = y * TILE_SIZE
self.width = TILE_SIZE
self.height = TILE_SIZE
self.INFANTRY_ORK2_IMG = pygame.image.load("./zdjecia/ork-piechota2.png")
self.INFANTRY_ORK2 = pygame.transform.scale(self.INFANTRY_ORK2_IMG,(64,64))
self.image = pygame.Surface([self.width, self.height])
self.image.blit(self.INFANTRY_ORK2, (0,0))
self.image.set_colorkey((0, 0, 0))
self.rect = self.image.get_rect()
self.rect.x = self.x
self.rect.y = self.y
self.level = 3
self.damage = 50*self.level
'''
class Sauron(pygame.sprite.Sprite): class Sauron(pygame.sprite.Sprite):
@ -98,7 +73,8 @@ class Sauron(pygame.sprite.Sprite):
self.width = TILE_SIZE self.width = TILE_SIZE
self.height = TILE_SIZE self.height = TILE_SIZE
self.SAURON_IMG = pygame.image.load("./pozostale_zdjecia/sauron.png") self.image_path = "./pozostale_zdjecia/sauron.png"
self.SAURON_IMG = pygame.image.load(self.image_path)
self.SAURON = pygame.transform.scale(self.SAURON_IMG,(64,64)) self.SAURON = pygame.transform.scale(self.SAURON_IMG,(64,64))
self.image = pygame.Surface([self.width, self.height]) self.image = pygame.Surface([self.width, self.height])
@ -109,6 +85,6 @@ class Sauron(pygame.sprite.Sprite):
self.rect.x = self.x self.rect.x = self.x
self.rect.y = self.y self.rect.y = self.y
self.level = 4 self.level = 3
self.damage = 50*self.level self.damage = 50*self.level
self.health = 150

2
nn.py
View File

@ -12,7 +12,7 @@ import pathlib
class NeuralN: class NeuralN:
# @staticmethod # @staticmethod
def predict(self,image): def predict(self,image_path):
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")