Compare commits

..

No commits in common. "bad659a7594422ac80bffe78ce1b68a3742df74e" and "bf1ac0349df965fb5b382d34e1284911cd0e9811" have entirely different histories.

6 changed files with 153 additions and 76 deletions

Binary file not shown.

Binary file not shown.

132
bfs.py
View File

@ -1,42 +1,83 @@
import pygame
from config import * from config import *
import queue
class Bfs(): class Bfs():
def __init__(self,game): def __init__(self,game):
self.game = game self.game = game
self.open_queue = []
self.close_queue = []
self.wall_cells = [] self.wall_cells = []
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,goal_cell): 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)
visited = set() cost_road=0
q = queue.Queue() self.open_queue.append(self.get_cell_number(self.game.agent.x,self.game.agent.y))
start_position = self.get_cell_number(self.game.agent.x,self.game.agent.y) # tutaj dodaje się cel agenta
q.put(start_position) #goal_cell = self.get_cell_number(self.game.flower.x,self.game.flower.y)
parent = {}
while not q.empty(): # główna pętla
current_pos = q.get()
visited.add(current_pos)
if current_pos == goal_cell:
print("Osiągnięto cel, konstruowanie drogi")
path = [] path = []
while current_pos != start_position: processing = True
path.append(current_pos) find_path = False
current_pos = parent[current_pos] while processing: # główna pętla
path.append(start_position) for event in pygame.event.get():
return path[::-1] if event.type == pygame.QUIT:
exit()
for successor in self.succesors(current_pos): if len(self.open_queue) > 0 :
if successor not in visited: current_node_cell = self.open_queue.pop(0)
q.put(successor)
parent[successor] = current_pos
print("Nieznaleziono drogi") if(current_node_cell in self.close_queue):
return None continue
print("Aktualna kratka: ", current_node_cell)
print("Cel znajduje sie na kratce: ", goal_cell)
if (current_node_cell == goal_cell):
self.close_queue.append(current_node_cell)
found_goal_cell = current_node_cell
print("Znaleziono cel, szukanie drogi z odwiedzonych wezlow, kolejka odwiedzonych:", self.close_queue)
processing = False
find_path = True
self.game.clock.tick(2)
else:
child_node_cells = self.get_child_nodes(current_node_cell)
self.close_queue.append(current_node_cell)
print("Sasiedzi: ", child_node_cells)
for child_node in child_node_cells:
if child_node not in self.open_queue and child_node not in self.close_queue:
self.open_queue.append(child_node)
print("Kolejka: ", self.open_queue, "\n")
else:
print("Brak nowych wezlow, kolejka: ",self. open_queue)
print("Odwiedzone : ", self.close_queue)
return self.close_queue
dead_end_nodes = []
while find_path:
path.append(self.close_queue[0])
for i in range(len(self.close_queue) -1):
from_cell = path[-1]
to_cell = self.close_queue[i+1]
if to_cell in dead_end_nodes:
continue
if self.verify_move(from_cell, to_cell):
path.append(to_cell)
if path[-1] == found_goal_cell:
find_path = False
else:
dead_end_nodes.append(path[-1])
path = []
print("Droga: ", path)
self.move_agent(path)
def get_cell_number(self,x, y): #zamienia koordynaty na numer kratki def get_cell_number(self,x, y): #zamienia koordynaty na numer kratki
@ -44,7 +85,7 @@ class Bfs():
cell_number =(x // TILE_SIZE) + (NUM_ROWS * (( y// TILE_SIZE))) cell_number =(x // TILE_SIZE) + (NUM_ROWS * (( y// TILE_SIZE)))
return cell_number return cell_number
def get_possible_moves(self,cell_number): def get_child_nodes(self,cell_number):
children = [] children = []
up = self.get_up_cell(cell_number) up = self.get_up_cell(cell_number)
if up is not None and up not in self.wall_cells and up not in self.enemy_cells: if up is not None and up not in self.wall_cells and up not in self.enemy_cells:
@ -111,6 +152,40 @@ class Bfs():
return False return False
def move_agent(self,path):
print("PATH:::::",path)
for cell_to_move in path:
x, y = self.get_coordinates(cell_to_move)
print("Ruch do kratki : ", cell_to_move, " z x: ", x, ", y: ", y, ", agent.x: ", self.game.agent.rect.x, ", agent.y: ", self.game.agent.rect.y)
if(self.get_cell_number(self.game.agent.x,self.game.agent.y)!=cell_to_move):
if x > self.game.agent.rect.x:
self.game.agent.direction = 0
elif y > self.game.agent.rect.y:
self.game.agent.direction = 1
elif x < self.game.agent.rect.x:
self.game.agent.direction = 2
elif y < self.game.agent.rect.y:
self.game.agent.direction = 3
if self.game.agent.direction==0:
print("DIRECTION: "+self.game.agent.AGENT_IMAGES[self.game.agent.direction])
self.game.agent.x_change += TILE_SIZE
elif self.game.agent.direction==1:
print("DIRECTION: "+self.game.agent.AGENT_IMAGES[self.game.agent.direction])
self.game.agent.y_change += TILE_SIZE
elif self.game.agent.direction==2:
print("DIRECTION: "+self.game.agent.AGENT_IMAGES[self.game.agent.direction])
self.game.agent.x_change -= TILE_SIZE
elif self.game.agent.direction==3:
print("DIRECTION: "+self.game.agent.AGENT_IMAGES[self.game.agent.direction])
self.game.agent.y_change -= TILE_SIZE
self.game.agent.rotate()
self.game.update()
self.game.map()
print("Polozenie agenta: agent.x: ", self.game.agent.rect.x, ", agent.y: ", self.game.agent.rect.y)
self.game.clock.tick(2)
def get_coordinates(self,cell_to_move): #zamienia numer kratki na koordynaty def get_coordinates(self,cell_to_move): #zamienia numer kratki na koordynaty
cell_row_number = cell_to_move // NUM_ROWS cell_row_number = cell_to_move // NUM_ROWS
cell_column_number = cell_to_move % NUM_ROWS cell_column_number = cell_to_move % NUM_ROWS
@ -118,12 +193,3 @@ class Bfs():
y = cell_row_number * TILE_SIZE y = cell_row_number * TILE_SIZE
x = cell_column_number * TILE_SIZE x = cell_column_number * TILE_SIZE
return x, y return x, y
def succesors(self,current_pos):
possible_moves = self.get_possible_moves(current_pos)
valid_moves = []
for move in possible_moves:
if self.verify_move(current_pos,move) == True:
valid_moves.append(move)
return valid_moves

64
main.py
View File

@ -3,6 +3,7 @@ from config import *
from agent import * from agent import *
from map_add_ons import * from map_add_ons import *
from mobs import * from mobs import *
#from unknown_mob import * #unknown mob
from bfs import * from bfs import *
from heapq import * from heapq import *
from nn import * from nn import *
@ -27,7 +28,6 @@ class Game:
self.bfs = Bfs(self) self.bfs = Bfs(self)
self.nn = NeuralN() 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()
@ -35,21 +35,23 @@ class Game:
self.grass_sprites = pygame.sprite.LayeredUpdates() self.grass_sprites = pygame.sprite.LayeredUpdates()
self.archer_orks = pygame.sprite.LayeredUpdates() self.archer_orks = pygame.sprite.LayeredUpdates()
self.infantry_orks = pygame.sprite.LayeredUpdates() self.infantry_orks = pygame.sprite.LayeredUpdates()
#self.infantry_orks2 = pygame.sprite.LayeredUpdates()
self.sauronL = pygame.sprite.LayeredUpdates() self.sauronL = pygame.sprite.LayeredUpdates()
self.flowers = pygame.sprite.LayeredUpdates() self.flowers = pygame.sprite.LayeredUpdates()
self.little_rock_sprites = pygame.sprite.LayeredUpdates() self.little_rock_sprites = pygame.sprite.LayeredUpdates()
#self.unknown_mobs = pygame.sprite.LayeredUpdates() #unknown mob
self.agent = Agent(self,1,1) self.agent = Agent(self,1,1)
self.archer_ork = Archer_ork(self,10,10) self.archer_ork = Archer_ork(self,10,10)
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.archer_ork.x,self.archer_ork.y)) self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.archer_ork.x,self.archer_ork.y))
self.infantry_ork = Infantry_ork(self,10,4) self.infantry_ork = Infantry_ork(self,10,4)
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.infantry_ork.x,self.infantry_ork.y)) self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.infantry_ork.x,self.infantry_ork.y))
#self.infantry_ork2 = Infantry_ork2(self,6,3)
#self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.infantry_ork2.x,self.infantry_ork2.y))
self.sauron = Sauron(self, 1, 10) self.sauron = Sauron(self, 1, 10)
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.sauron.x,self.sauron.y)) self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.sauron.x,self.sauron.y))
self.flower = Health_flower(self, 8,2) self.flower = Health_flower(self, 8,2)
#self.unknown_mob = Unknown_mob(self,8,8) #unknown mob
self.grass = Grass(self,0,2) self.grass = Grass(self,0,2)
self.grass = Grass(self,1,2) self.grass = Grass(self,1,2)
self.grass = Grass(self,0,3) self.grass = Grass(self,0,3)
@ -60,6 +62,18 @@ class Game:
for y in range(5): for y in range(5):
self.rock = Rocks(self,3,y) self.rock = Rocks(self,3,y)
self.bfs.wall_cells.append(self.bfs.get_cell_number(self.rock.x,self.rock.y)) self.bfs.wall_cells.append(self.bfs.get_cell_number(self.rock.x,self.rock.y))
'''
for i in range(10):
x = random.randint(0,12)
y = random.randint(0,11)
self.grass = Grass(self,x,y)
self.grass_cells.append(self.bfs.get_cell_number(self.grass.x,self.grass.y))
for y in range(5,8):
self.little_rocks = Little_Rocks(self,4,y)
'''
def update(self): def update(self):
@ -74,11 +88,11 @@ 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.get_cell_number(self.flower.x,self.flower.y))
x = self.sauron.x x = self.sauron.x
y = self.sauron.y y = self.sauron.y
mob_image = self.sauron.SAURON_IMG mob_image = self.sauron.SAURON_IMG
while True: #do poprawienia poprawne rozpoznawanie póki co nie będzie działać dobrze while True:
prediction = self.prediction_road(x,y,mob_image) prediction = self.prediction_road(x,y,mob_image)
if prediction == "SAURON": if prediction == "SAURON":
x = self.infantry_ork.x x = self.infantry_ork.x
@ -86,13 +100,13 @@ class Game:
mob_image = self.infantry_ork.INFANTRY_ORK_IMG mob_image = self.infantry_ork.INFANTRY_ORK_IMG
prediction = self.prediction_road(x,y,mob_image) prediction = self.prediction_road(x,y,mob_image)
if prediction == "ORK_INFANTRY": if prediction == "ORK_INFANTRY":
self.move_agent(self.bfs.bfs(self.bfs.get_cell_number(x,y))) 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 mob_image = self.archer_ork.ARCHER_ORK_IMG
prediction = self.prediction_road(x,y,mob_image) prediction = self.prediction_road(x,y,mob_image)
if prediction == "ORK_ARCHER": if prediction == "ORK_ARCHER":
self.move_agent(self.bfs.bfs(self.bfs.get_cell_number(x,y))) self.bfs.bfs(self.bfs.get_cell_number(x,y))
x = self.sauron.x x = self.sauron.x
y = self.sauron.y y = self.sauron.y
mob_image = self.sauron.SAURON_IMG mob_image = self.sauron.SAURON_IMG
@ -105,43 +119,11 @@ class Game:
goal = self.bfs.get_down_cell(mob_goal) goal = self.bfs.get_down_cell(mob_goal)
else: else:
goal = self.bfs.get_up_cell(mob_goal) goal = self.bfs.get_up_cell(mob_goal)
self.move_agent(self.bfs.bfs(goal)) self.bfs.bfs(goal)
prediction = self.nn.predict(mob_image) prediction = self.nn.predict(mob_image)
return prediction return prediction
def move_agent(self,path):
print("PATH:::::",path)
for cell_to_move in path:
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)
if(self.bfs.get_cell_number(self.agent.x,self.agent.y)!=cell_to_move):
if x > self.agent.rect.x:
self.agent.direction = 0
elif y > self.agent.rect.y:
self.agent.direction = 1
elif x < self.agent.rect.x:
self.agent.direction = 2
elif y < self.agent.rect.y:
self.agent.direction = 3
if self.agent.direction==0:
print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction])
self.agent.x_change += TILE_SIZE
elif self.agent.direction==1:
print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction])
self.agent.y_change += TILE_SIZE
elif self.agent.direction==2:
print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction])
self.agent.x_change -= TILE_SIZE
elif self.agent.direction==3:
print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction])
self.agent.y_change -= TILE_SIZE
self.agent.rotate()
self.update()
self.map()
print("Polozenie agenta: agent.x: ", self.agent.rect.x, ", agent.y: ", self.agent.rect.y)
self.clock.tick(2)
def map(self): # tworzenie mapy def map(self): # tworzenie mapy
self.clock.tick(FRAMERATE) self.clock.tick(FRAMERATE)

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

29
unknown_mob.py Normal file
View File

@ -0,0 +1,29 @@
import pygame
from config import *
class Unknown_mob(pygame.sprite.Sprite):
def __init__(self, game, x, y):
self.game = game
self.groups = self.game.all_sprites, self.game.unknown_mobs
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.UNKNOWN_MOB_IMG = pygame.image.load("./pozostale_zdjecia/dragon.jpg")
self.UKNOWN_MOB = pygame.transform.scale(self.UNKNOWN_MOB_IMG,(64,64))
self.image = pygame.Surface([self.width, self.height])
self.image.blit(self.UKNOWN_MOB, (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 = 1
self.damage = 50*self.level