Compare commits
29 Commits
Test_branc
...
master
Author | SHA1 | Date | |
---|---|---|---|
ee864b16c7 | |||
![]() |
71b2ecc23f | ||
![]() |
936e0d7f4d | ||
749f87a428 | |||
74f58ec742 | |||
8a41a8e4f0 | |||
3dd6b8a9ee | |||
![]() |
e90f5b5d75 | ||
![]() |
18d27c63c3 | ||
![]() |
062d2f00fe | ||
![]() |
bad659a759 | ||
![]() |
5d1bc990ce | ||
![]() |
bf1ac0349d | ||
![]() |
de3ecc0926 | ||
![]() |
b01f3e48f9 | ||
7cb2903a1d | |||
![]() |
ca668b4b58 | ||
![]() |
c5ae2925d9 | ||
![]() |
e095eb596e | ||
![]() |
c7c363fd2d | ||
![]() |
2d25ad626b | ||
![]() |
23e6ca027c | ||
![]() |
bec4c4e275 | ||
a4ece14a16 | |||
8bcb2066e1 | |||
1c26165161 | |||
a1f1527319 | |||
6dd15eccf7 | |||
635d448799 |
3
.idea/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
8
.idea/Gra-SI_nn.iml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
6
.idea/inspectionProfiles/profiles_settings.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<settings>
|
||||
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||
<version value="1.0" />
|
||||
</settings>
|
||||
</component>
|
4
.idea/misc.xml
Normal file
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11" project-jdk-type="Python SDK" />
|
||||
</project>
|
8
.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/Gra-SI_nn.iml" filepath="$PROJECT_DIR$/.idea/Gra-SI_nn.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
6
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"python.analysis.extraPaths": [
|
||||
"./path_alghoritms",
|
||||
"./path_algorithms"
|
||||
]
|
||||
}
|
231
Astar.py
@ -1,231 +0,0 @@
|
||||
import pygame
|
||||
import heapq
|
||||
from config import *
|
||||
from queue import PriorityQueue
|
||||
|
||||
class Astar:
|
||||
|
||||
def __init__(self, game):
|
||||
self.game = game
|
||||
self.open_queue = []
|
||||
self.close_queue = []
|
||||
self.wall_cells = []
|
||||
self.enemy_cells = []
|
||||
self.little_rocks_cells = []
|
||||
self.grass_cells = []
|
||||
|
||||
def Astar(self):
|
||||
|
||||
start_cell = self.get_cell_number(self.game.agent.x,self.game.agent.y)
|
||||
goal_cell = self.get_cell_number(self.game.flower.x,self.game.flower.y)
|
||||
f_value = {start_cell: self.heuristic_value(self.game.agent.x, self.game.agent.y, self.game.flower)}
|
||||
g_values = {start_cell: 0}
|
||||
f2_value = {start_cell: 0}
|
||||
parents = {start_cell: None}
|
||||
new_child_node = {int, int}
|
||||
|
||||
priority_queue = []
|
||||
find_path = False
|
||||
open_queue = PriorityQueue()
|
||||
open_queue.put((0,start_cell))
|
||||
path = []
|
||||
|
||||
processing = True
|
||||
while processing:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
exit()
|
||||
if not open_queue.empty() :
|
||||
current_node_cell = open_queue.get()
|
||||
if(current_node_cell in self.close_queue):
|
||||
continue
|
||||
if (current_node_cell[1] == goal_cell):
|
||||
self.close_queue.append(current_node_cell[1])
|
||||
found_goal_cell = current_node_cell[1]
|
||||
print("Znaleziono cel, szukanie drogi z odwiedzonych węzłów, kolejka odwiedzonych:", self.close_queue)
|
||||
processing = False
|
||||
find_path = True
|
||||
self.game.clock.tick(2)
|
||||
else:
|
||||
self.close_queue.append(current_node_cell)
|
||||
child_node_cells = self.get_child_nodes(current_node_cell[1])
|
||||
for child_node in child_node_cells:
|
||||
if child_node not in [item[1] for item in open_queue.queue] and child_node not in [item[1] for item in self.close_queue]:
|
||||
x, y = self.get_coordinates(child_node)
|
||||
g_values[child_node] = g_values[current_node_cell[1]]
|
||||
f_value[child_node] = g_values[child_node] + self.heuristic_value(x,y, self.game.flower) + self.move_cost(current_node_cell[1], child_node)
|
||||
open_queue.put((f_value[child_node], child_node))
|
||||
parents[child_node] = current_node_cell[1]
|
||||
elif child_node in [item[1] for item in open_queue.queue]:
|
||||
f2_value[child_node] = g_values[child_node] + self.heuristic_value(x,y, self.game.flower) + self.move_cost(current_node_cell[1], child_node)
|
||||
print("CURRENT NODE:", current_node_cell)
|
||||
print("CURRENT CHILD:", child_node)
|
||||
print("F1:", f_value[child_node])
|
||||
print("F2:", f2_value[child_node])
|
||||
if f2_value[child_node] < f_value[child_node]:
|
||||
parents[child_node] = current_node_cell[1]
|
||||
open_queue.put((f2_value[child_node], child_node))
|
||||
else:
|
||||
print("Brak nowych węzłów, kolejka: ",self. open_queue)
|
||||
print("Odwiedzone : ", self.close_queue)
|
||||
return self.close_queue
|
||||
|
||||
|
||||
dead_end_nodes = []
|
||||
resultList = [t[1] for t in self.close_queue if isinstance(t, tuple)]
|
||||
resultList.append(goal_cell)
|
||||
print("F_VALUES:", f_value)
|
||||
print("RESULT LIST", resultList)
|
||||
print("PARENTS: ", parents)
|
||||
while find_path:
|
||||
path.append(resultList[0])
|
||||
|
||||
for i in range(len(resultList) -1):
|
||||
from_cell = path[-1]
|
||||
to_cell = resultList[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 heuristic_value(self, x,y, goal_cell):
|
||||
return abs(x - goal_cell.x) + abs(y - goal_cell.y)
|
||||
|
||||
def move_cost(self, node1, node2):
|
||||
if node2 in self.little_rocks_cells:
|
||||
return 50
|
||||
if node2 in self.grass_cells:
|
||||
return 5
|
||||
return 1
|
||||
|
||||
|
||||
|
||||
def get_cell_number(self,x, y): #zamienia koordynaty na numer kratki
|
||||
cell_number = None
|
||||
cell_number =(x // TILE_SIZE) + (NUM_ROWS * (( y// TILE_SIZE)))
|
||||
return cell_number
|
||||
|
||||
def get_coordinates(self,cell_to_move): #zamienia numer kratki na koordynaty
|
||||
cell_row_number = cell_to_move // NUM_ROWS
|
||||
cell_column_number = cell_to_move % NUM_ROWS
|
||||
|
||||
y = cell_row_number * TILE_SIZE
|
||||
x = cell_column_number * TILE_SIZE
|
||||
return x, y
|
||||
|
||||
def get_child_nodes(self,cell_number):
|
||||
children = []
|
||||
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:
|
||||
children.append(up)
|
||||
|
||||
right = self.get_right_cell(cell_number)
|
||||
if right is not None and right not in self.wall_cells and up not in self.enemy_cells:
|
||||
children.append(right)
|
||||
|
||||
down = self.get_down_cell(cell_number)
|
||||
if down is not None and down not in self.wall_cells and up not in self.enemy_cells:
|
||||
children.append(down)
|
||||
|
||||
left = self.get_left_cell(cell_number)
|
||||
if left is not None and left not in self.wall_cells and up not in self.enemy_cells:
|
||||
children.append(left)
|
||||
|
||||
return children
|
||||
|
||||
|
||||
def get_up_cell(self,cell_number):
|
||||
cell_row_number = cell_number // NUM_ROWS
|
||||
if (cell_row_number - 1 < 0):
|
||||
return None
|
||||
else:
|
||||
return (cell_number - NUM_ROWS)
|
||||
|
||||
def get_right_cell(self,cell_number):
|
||||
cell_column_number = cell_number % NUM_ROWS
|
||||
if (cell_column_number + 1 >= NUM_ROWS):
|
||||
return None
|
||||
else:
|
||||
return (cell_number + 1)
|
||||
|
||||
def get_down_cell(self,cell_number):
|
||||
cell_row_number = cell_number // NUM_ROWS
|
||||
if (cell_row_number + 1 >= NUM_ROWS):
|
||||
return None
|
||||
else:
|
||||
return (cell_number + NUM_ROWS)
|
||||
|
||||
def get_left_cell(self,cell_number):
|
||||
cell_column_number = cell_number % NUM_ROWS
|
||||
if (cell_column_number - 1 < 0):
|
||||
return None
|
||||
else:
|
||||
return (cell_number - 1)
|
||||
|
||||
def verify_move(self,from_cell, to_cell): #sprawdzenie czy ruch jest poprawny czyt. czy następna kratka to przeszkoda lub mob
|
||||
if (to_cell in self.wall_cells or to_cell in self.enemy_cells):
|
||||
return False
|
||||
|
||||
if(from_cell + 1 == to_cell):
|
||||
return True
|
||||
|
||||
if(from_cell - 1 == to_cell):
|
||||
return True
|
||||
|
||||
if(from_cell - NUM_ROWS == to_cell):
|
||||
return True
|
||||
|
||||
if(from_cell + NUM_ROWS == to_cell):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def move_agent(self,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("Położenie agenta: agent.x: ", self.game.agent.rect.x, ", agent.y: ", self.game.agent.rect.y)
|
||||
self.game.clock.tick(2)
|
||||
|
||||
|
BIN
__pycache__/astar.cpython-310.pyc
Normal file
BIN
__pycache__/drzewo_decyzyjne.cpython-310.pyc
Normal file
BIN
__pycache__/nn.cpython-310.pyc
Normal file
BIN
__pycache__/node.cpython-310.pyc
Normal file
58
agent.py
@ -9,7 +9,7 @@ class Agent(pygame.sprite.Sprite):
|
||||
self.game = game
|
||||
self.groups = self.game.all_sprites
|
||||
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.x = x * TILE_SIZE
|
||||
self.y = y * TILE_SIZE
|
||||
@ -19,14 +19,13 @@ class Agent(pygame.sprite.Sprite):
|
||||
self.x_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("./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_IMG_DOWN = pygame.image.load("./zdjecia/gandalf-dol.png")
|
||||
self.AGENT_IMG_DOWN = pygame.image.load("./pozostale_zdjecia/gandalf-dol.png")
|
||||
self.AGENT_DOWN = pygame.transform.scale(self.AGENT_IMG_DOWN,(64,64))
|
||||
self.AGENT_IMG_LEFT = pygame.image.load("./zdjecia/gandalf-lewo.png")
|
||||
self.AGENT_IMG_LEFT = pygame.image.load("./pozostale_zdjecia/gandalf-lewo.png")
|
||||
self.AGENT_LEFT = pygame.transform.scale(self.AGENT_IMG_LEFT,(64,64))
|
||||
self.AGENT_IMG_UP = pygame.image.load("./zdjecia/gandalf-gora.png")
|
||||
self.AGENT_IMG_UP = pygame.image.load("./pozostale_zdjecia/gandalf-gora.png")
|
||||
self.AGENT_UP = pygame.transform.scale(self.AGENT_IMG_UP,(64,64))
|
||||
|
||||
self.AGENT = self.AGENT_RIGHT
|
||||
@ -41,12 +40,14 @@ class Agent(pygame.sprite.Sprite):
|
||||
|
||||
self.level = 1
|
||||
|
||||
self.current_health = 500
|
||||
self.max_health = 1000
|
||||
self.health_bar_length = 300
|
||||
self.current_health = 200
|
||||
self.max_health = 200
|
||||
self.health_bar_length = 200
|
||||
self.health_ratio = self.max_health/self.health_bar_length
|
||||
self._layer = AGENT_LAYER
|
||||
|
||||
self.damage = 50*self.level
|
||||
|
||||
self.artifact = "tak"
|
||||
|
||||
def update(self):
|
||||
|
||||
@ -54,7 +55,6 @@ class Agent(pygame.sprite.Sprite):
|
||||
self.movement()
|
||||
self.collide_mob()
|
||||
self.collide_flower()
|
||||
#self.end_game() gra sie konczy gdy wie gdzie sa wszyscy
|
||||
self.disp_level()
|
||||
|
||||
self.rect.x += self.x_change
|
||||
@ -102,9 +102,6 @@ class Agent(pygame.sprite.Sprite):
|
||||
if self.direction==3 and self.rect.y > 0:
|
||||
self.y_change -= TILE_SIZE
|
||||
|
||||
def end_game(self):
|
||||
if (-1 in self.game.state)==False:
|
||||
pygame.quit()
|
||||
|
||||
def collide_flower(self):
|
||||
hits_flower = pygame.sprite.spritecollide(self, self.game.flowers, False)
|
||||
@ -133,14 +130,8 @@ class Agent(pygame.sprite.Sprite):
|
||||
|
||||
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_ork2 = pygame.sprite.spritecollide(self, self.game.infantry_orks2, 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)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -170,22 +161,7 @@ class Agent(pygame.sprite.Sprite):
|
||||
self.game.infantry_ork.kill()
|
||||
self.get_damage(self.game.infantry_ork.damage)
|
||||
self.level=self.level+1
|
||||
|
||||
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
|
||||
|
||||
|
||||
if hits_sauron:
|
||||
if self.game.sauron.level > self.level or self.game.sauron.damage > self.current_health:
|
||||
self.game.state[6]=self.game.sauron.x
|
||||
@ -200,7 +176,8 @@ class Agent(pygame.sprite.Sprite):
|
||||
self.game.sauron.kill()
|
||||
self.level=self.level+1
|
||||
pygame.quit()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def get_damage(self,amount):
|
||||
@ -208,9 +185,7 @@ class Agent(pygame.sprite.Sprite):
|
||||
self.current_health -= amount
|
||||
if 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):
|
||||
if self.current_health < self.max_health:
|
||||
@ -226,7 +201,6 @@ class Agent(pygame.sprite.Sprite):
|
||||
def disp_level(self):
|
||||
font = pygame.font.SysFont(None, 40)
|
||||
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))
|
||||
self.game.SCREEN.blit(lvlDisplay, (370,780))
|
||||
|
||||
|
67
astar.py
Normal file
@ -0,0 +1,67 @@
|
||||
from config import *
|
||||
import heapq
|
||||
|
||||
class Astar():
|
||||
|
||||
def __init__(self,game):
|
||||
|
||||
self.g = game
|
||||
|
||||
# Define the movement directions (up, down, left, right)
|
||||
self.directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
|
||||
|
||||
def heuristic(self,a, b):
|
||||
# Calculate the Manhattan distance between two points
|
||||
return abs(b[0] - a[0]) + abs(b[1] - a[1])
|
||||
|
||||
def get_successors(self,position):
|
||||
# Get the neighboring cells that can be traversed
|
||||
successors = []
|
||||
for direction in self.directions:
|
||||
neighbor = (position[0] + direction[0], position[1] + direction[1])
|
||||
if 0 <= neighbor[0] < TILE_SIZE and 0 <= neighbor[1] < TILE_SIZE and self.g.obstacles[neighbor[0]][neighbor[1]] == False:
|
||||
successors.append(neighbor)
|
||||
return successors
|
||||
|
||||
def print_path(self,came_from, current,path):
|
||||
# Recursively print the path from the start to the current position
|
||||
if current in came_from:
|
||||
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))
|
||||
#print("Budowanie ścieżki: ",path)
|
||||
return 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 = []
|
||||
heapq.heappush(open_set, (0, start)) # Priority queue with the start position
|
||||
came_from = {}
|
||||
g_scores = {start: 0} # Cost from start to each position
|
||||
f_scores = {start: self.heuristic(start, goal)} # Total estimated cost from start to goal through each position
|
||||
|
||||
while open_set:
|
||||
_, current = heapq.heappop(open_set)
|
||||
|
||||
if current == goal:
|
||||
# Goal reached, print the path
|
||||
path = self.print_path(came_from, goal,path)
|
||||
return path
|
||||
|
||||
for successor in self.get_successors(current):
|
||||
# Calculate the cost to move from the current position to the successor
|
||||
cost = self.g.cell_costs[successor[0]][successor[1]]
|
||||
tentative_g_score = g_scores[current] + cost
|
||||
|
||||
if successor not in g_scores or tentative_g_score < g_scores[successor]:
|
||||
# Update the cost and priority if it's a better path
|
||||
came_from[successor] = current
|
||||
g_scores[successor] = tentative_g_score
|
||||
f_scores[successor] = tentative_g_score + self.heuristic(successor, goal)
|
||||
heapq.heappush(open_set, (f_scores[successor], successor))
|
||||
# No path found
|
||||
print("No path found.")
|
||||
|
||||
|
||||
|
166
bfs.py
@ -1,90 +1,50 @@
|
||||
import pygame
|
||||
from config import *
|
||||
class Bfs:
|
||||
import queue
|
||||
class Bfs():
|
||||
|
||||
def __init__(self,game):
|
||||
|
||||
self.game = game
|
||||
|
||||
self.open_queue = []
|
||||
self.close_queue = []
|
||||
self.wall_cells = []
|
||||
self.enemy_cells = []
|
||||
def heuristic(a,b):
|
||||
return abs(a[0]-b[0])+abs(a[1]-b[1])
|
||||
def bfs(self,goal_cell):
|
||||
print("x: ", self.game.agent.rect.x, "y: ", self.game.agent.rect.y)
|
||||
visited = set()
|
||||
q = queue.Queue()
|
||||
start_position = self.get_cell_number(self.game.agent.rect.x,self.game.agent.rect.y)
|
||||
q.put(start_position)
|
||||
parent = {}
|
||||
|
||||
def bfs(self):
|
||||
print("x: ", self.game.agent.x, "y: ", 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
|
||||
goal_cell = self.get_cell_number(self.game.flower.x,self.game.flower.y)
|
||||
while not q.empty(): # główna pętla
|
||||
current_pos = q.get()
|
||||
visited.add(current_pos)
|
||||
|
||||
path = []
|
||||
processing = True
|
||||
find_path = False
|
||||
while processing: # główna pętla
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
exit()
|
||||
|
||||
if len(self.open_queue) > 0 :
|
||||
current_node_cell = self.open_queue.pop(0)
|
||||
if current_pos == goal_cell:
|
||||
print("Osiągnięto cel, konstruowanie drogi")
|
||||
path = []
|
||||
while current_pos != start_position:
|
||||
path.append(current_pos)
|
||||
current_pos = parent[current_pos]
|
||||
path.append(start_position)
|
||||
return path[::-1]
|
||||
|
||||
if(current_node_cell in self.close_queue):
|
||||
continue
|
||||
for successor in self.succesors(current_pos):
|
||||
if successor not in visited:
|
||||
q.put(successor)
|
||||
parent[successor] = current_pos
|
||||
|
||||
print("Aktualna kratka: ", current_node_cell)
|
||||
print("Cel znajduje się 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 węzłów, 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("Sąsiedzi: ", 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 węzłów, 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)
|
||||
print("Nieznaleziono drogi")
|
||||
return None
|
||||
|
||||
|
||||
def get_cell_number(self,x, y): #zamienia koordynaty na numer kratki
|
||||
cell_number = None
|
||||
cell_number =(x // TILE_SIZE) + (NUM_ROWS * (( y// TILE_SIZE)))
|
||||
cell_number =(x // TILE_SIZE) + (ROWS * (( y// TILE_SIZE)))
|
||||
return cell_number
|
||||
|
||||
def get_child_nodes(self,cell_number):
|
||||
def get_possible_moves(self,cell_number):
|
||||
children = []
|
||||
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:
|
||||
@ -106,28 +66,28 @@ class Bfs:
|
||||
|
||||
|
||||
def get_up_cell(self,cell_number):
|
||||
cell_row_number = cell_number // NUM_ROWS
|
||||
cell_row_number = cell_number // ROWS
|
||||
if (cell_row_number - 1 < 0):
|
||||
return None
|
||||
else:
|
||||
return (cell_number - NUM_ROWS)
|
||||
return (cell_number - ROWS)
|
||||
|
||||
def get_right_cell(self,cell_number):
|
||||
cell_column_number = cell_number % NUM_ROWS
|
||||
if (cell_column_number + 1 >= NUM_ROWS):
|
||||
cell_column_number = cell_number % ROWS
|
||||
if (cell_column_number + 1 >= ROWS):
|
||||
return None
|
||||
else:
|
||||
return (cell_number + 1)
|
||||
|
||||
def get_down_cell(self,cell_number):
|
||||
cell_row_number = cell_number // NUM_ROWS
|
||||
if (cell_row_number + 1 >= NUM_ROWS):
|
||||
cell_row_number = cell_number // ROWS
|
||||
if (cell_row_number + 1 >= ROWS):
|
||||
return None
|
||||
else:
|
||||
return (cell_number + NUM_ROWS)
|
||||
return (cell_number + ROWS)
|
||||
|
||||
def get_left_cell(self,cell_number):
|
||||
cell_column_number = cell_number % NUM_ROWS
|
||||
cell_column_number = cell_number % ROWS
|
||||
if (cell_column_number - 1 < 0):
|
||||
return None
|
||||
else:
|
||||
@ -143,51 +103,27 @@ class Bfs:
|
||||
if(from_cell - 1 == to_cell):
|
||||
return True
|
||||
|
||||
if(from_cell - NUM_ROWS == to_cell):
|
||||
if(from_cell - ROWS == to_cell):
|
||||
return True
|
||||
|
||||
if(from_cell + NUM_ROWS == to_cell):
|
||||
if(from_cell + ROWS == to_cell):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def move_agent(self,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("Położenie 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
|
||||
cell_row_number = cell_to_move // NUM_ROWS
|
||||
cell_column_number = cell_to_move % NUM_ROWS
|
||||
cell_row_number = cell_to_move // ROWS
|
||||
cell_column_number = cell_to_move % ROWS
|
||||
|
||||
y = cell_row_number * TILE_SIZE
|
||||
x = cell_column_number * TILE_SIZE
|
||||
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
|
||||
|
||||
|
BIN
class_names.pkl
Normal file
@ -3,7 +3,8 @@ WIDTH, HEIGHT = 832, 832
|
||||
TILE_SIZE = 64
|
||||
BLACK = ((0,0,0))
|
||||
WHITE = ((255,255,255))
|
||||
NUM_ROWS = WIDTH//TILE_SIZE
|
||||
AGENT_LAYER = 2
|
||||
FLOWER_LAYER = 1
|
||||
GRASS_LAYER = 3
|
||||
GRASS_LAYER = 3
|
||||
ROWS = HEIGHT // TILE_SIZE
|
||||
COLS = WIDTH // TILE_SIZE
|
289
data
Normal file
@ -0,0 +1,289 @@
|
||||
zdrowie_bohatera,moc_bohatera,moc_moba,lvl_wiekszy_bohater,mob_jest_strzelcem,zdrowie_moba,artefakt,akcja
|
||||
100,tak,tak,tak,tak,100,tak,walcz
|
||||
100,tak,tak,tak,tak,100,nie,zmien_kierunek
|
||||
100,tak,tak,tak,tak,50,tak,zmien_kierunek
|
||||
100,tak,tak,tak,tak,50,nie,zmien_kierunek
|
||||
100,tak,tak,tak,tak,1,tak,zmien_kierunek
|
||||
100,tak,tak,tak,tak,1,nie,zmien_kierunek
|
||||
100,tak,tak,tak,nie,100,tak,zmien_kierunek
|
||||
100,tak,tak,tak,nie,100,nie,zmien_kierunek
|
||||
100,tak,tak,tak,nie,50,tak,walcz
|
||||
100,tak,tak,tak,nie,50,nie,zmien_kierunek
|
||||
100,tak,tak,tak,nie,1,tak,walcz
|
||||
100,tak,tak,tak,nie,1,nie,zmien_kierunek
|
||||
100,tak,tak,nie,tak,100,tak,zmien_kierunek
|
||||
100,tak,tak,nie,tak,100,nie,zmien_kierunek
|
||||
100,tak,tak,nie,tak,50,tak,zmien_kierunek
|
||||
100,tak,tak,nie,tak,50,nie,zmien_kierunek
|
||||
100,tak,tak,nie,tak,1,tak,zmien_kierunek
|
||||
100,tak,tak,nie,tak,1,nie,zmien_kierunek
|
||||
100,tak,tak,nie,nie,100,tak,zmien_kierunek
|
||||
100,tak,tak,nie,nie,100,nie,zmien_kierunek
|
||||
100,tak,tak,nie,nie,50,tak,zmien_kierunek
|
||||
100,tak,tak,nie,nie,50,nie,zmien_kierunek
|
||||
100,tak,tak,nie,nie,1,tak,zmien_kierunek
|
||||
100,tak,tak,nie,nie,1,nie,zmien_kierunek
|
||||
100,tak,nie,tak,tak,100,tak,zmien_kierunek
|
||||
100,tak,nie,tak,tak,100,nie,zmien_kierunek
|
||||
100,tak,nie,tak,tak,50,tak,zmien_kierunek
|
||||
100,tak,nie,tak,tak,50,nie,zmien_kierunek
|
||||
100,tak,nie,tak,tak,1,tak,zmien_kierunek
|
||||
100,tak,nie,tak,tak,1,nie,zmien_kierunek
|
||||
100,tak,nie,tak,nie,100,tak,zmien_kierunek
|
||||
100,tak,nie,tak,nie,100,nie,zmien_kierunek
|
||||
100,tak,nie,tak,nie,50,tak,zmien_kierunek
|
||||
100,tak,nie,tak,nie,50,nie,zmien_kierunek
|
||||
100,tak,nie,tak,nie,1,tak,zmien_kierunek
|
||||
100,tak,nie,tak,nie,1,nie,zmien_kierunek
|
||||
100,tak,nie,nie,tak,100,tak,zmien_kierunek
|
||||
100,tak,nie,nie,tak,100,nie,zmien_kierunek
|
||||
100,tak,nie,nie,tak,50,tak,zmien_kierunek
|
||||
100,tak,nie,nie,tak,50,nie,zmien_kierunek
|
||||
100,tak,nie,nie,tak,1,tak,zmien_kierunek
|
||||
100,tak,nie,nie,tak,1,nie,zmien_kierunek
|
||||
100,tak,nie,nie,nie,100,tak,zmien_kierunek
|
||||
100,tak,nie,nie,nie,100,nie,zmien_kierunek
|
||||
100,tak,nie,nie,nie,50,tak,zmien_kierunek
|
||||
100,tak,nie,nie,nie,50,nie,zmien_kierunek
|
||||
100,tak,nie,nie,nie,1,tak,zmien_kierunek
|
||||
100,tak,nie,nie,nie,1,nie,zmien_kierunek
|
||||
100,nie,tak,tak,tak,100,tak,zmien_kierunek
|
||||
100,nie,tak,tak,tak,100,nie,zmien_kierunek
|
||||
100,nie,tak,tak,tak,50,tak,zmien_kierunek
|
||||
100,nie,tak,tak,tak,50,nie,zmien_kierunek
|
||||
100,nie,tak,tak,tak,1,tak,zmien_kierunek
|
||||
100,nie,tak,tak,tak,1,nie,zmien_kierunek
|
||||
100,nie,tak,tak,nie,100,tak,zmien_kierunek
|
||||
100,nie,tak,tak,nie,100,nie,zmien_kierunek
|
||||
100,nie,tak,tak,nie,50,tak,zmien_kierunek
|
||||
100,nie,tak,tak,nie,50,nie,zmien_kierunek
|
||||
100,nie,tak,tak,nie,1,tak,zmien_kierunek
|
||||
100,nie,tak,tak,nie,1,nie,zmien_kierunek
|
||||
100,nie,tak,nie,tak,100,tak,zmien_kierunek
|
||||
100,nie,tak,nie,tak,100,nie,zmien_kierunek
|
||||
100,nie,tak,nie,tak,50,tak,zmien_kierunek
|
||||
100,nie,tak,nie,tak,50,nie,zmien_kierunek
|
||||
100,nie,tak,nie,tak,1,tak,zmien_kierunek
|
||||
100,nie,tak,nie,tak,1,nie,zmien_kierunek
|
||||
100,nie,tak,nie,nie,100,tak,zmien_kierunek
|
||||
100,nie,tak,nie,nie,100,nie,zmien_kierunek
|
||||
100,nie,tak,nie,nie,50,tak,zmien_kierunek
|
||||
100,nie,tak,nie,nie,50,nie,zmien_kierunek
|
||||
100,nie,tak,nie,nie,1,tak,zmien_kierunek
|
||||
100,nie,tak,nie,nie,1,nie,zmien_kierunek
|
||||
100,nie,nie,tak,tak,100,tak,zmien_kierunek
|
||||
100,nie,nie,tak,tak,100,nie,zmien_kierunek
|
||||
100,nie,nie,tak,tak,50,tak,zmien_kierunek
|
||||
100,nie,nie,tak,tak,50,nie,zmien_kierunek
|
||||
100,nie,nie,tak,tak,1,tak,zmien_kierunek
|
||||
100,nie,nie,tak,tak,1,nie,zmien_kierunek
|
||||
100,nie,nie,tak,nie,100,tak,zmien_kierunek
|
||||
100,nie,nie,tak,nie,100,nie,zmien_kierunek
|
||||
100,nie,nie,tak,nie,50,tak,zmien_kierunek
|
||||
100,nie,nie,tak,nie,50,nie,zmien_kierunek
|
||||
100,nie,nie,tak,nie,1,tak,zmien_kierunek
|
||||
100,nie,nie,tak,nie,1,nie,zmien_kierunek
|
||||
100,nie,nie,nie,tak,100,tak,zmien_kierunek
|
||||
100,nie,nie,nie,tak,100,nie,zmien_kierunek
|
||||
100,nie,nie,nie,tak,50,tak,zmien_kierunek
|
||||
100,nie,nie,nie,tak,50,nie,zmien_kierunek
|
||||
100,nie,nie,nie,tak,1,tak,zmien_kierunek
|
||||
100,nie,nie,nie,tak,1,nie,zmien_kierunek
|
||||
100,nie,nie,nie,nie,100,tak,zmien_kierunek
|
||||
100,nie,nie,nie,nie,100,nie,zmien_kierunek
|
||||
100,nie,nie,nie,nie,50,tak,zmien_kierunek
|
||||
100,nie,nie,nie,nie,50,nie,zmien_kierunek
|
||||
100,nie,nie,nie,nie,1,tak,zmien_kierunek
|
||||
100,nie,nie,nie,nie,1,nie,zmien_kierunek
|
||||
50,tak,tak,tak,tak,100,tak,zmien_kierunek
|
||||
50,tak,tak,tak,tak,100,nie,zmien_kierunek
|
||||
50,tak,tak,tak,tak,50,tak,zmien_kierunek
|
||||
50,tak,tak,tak,tak,50,nie,zmien_kierunek
|
||||
50,tak,tak,tak,tak,1,tak,zmien_kierunek
|
||||
50,tak,tak,tak,tak,1,nie,zmien_kierunek
|
||||
50,tak,tak,tak,nie,100,tak,zmien_kierunek
|
||||
50,tak,tak,tak,nie,100,nie,zmien_kierunek
|
||||
50,tak,tak,tak,nie,50,tak,zmien_kierunek
|
||||
50,tak,tak,tak,nie,50,nie,zmien_kierunek
|
||||
50,tak,tak,tak,nie,1,tak,walcz
|
||||
50,tak,tak,tak,nie,1,nie,zmien_kierunek
|
||||
50,tak,tak,nie,tak,100,tak,zmien_kierunek
|
||||
50,tak,tak,nie,tak,100,nie,zmien_kierunek
|
||||
50,tak,tak,nie,tak,50,tak,zmien_kierunek
|
||||
50,tak,tak,nie,tak,50,nie,zmien_kierunek
|
||||
50,tak,tak,nie,tak,1,tak,zmien_kierunek
|
||||
50,tak,tak,nie,tak,1,nie,zmien_kierunek
|
||||
50,tak,tak,nie,nie,100,tak,zmien_kierunek
|
||||
50,tak,tak,nie,nie,100,nie,zmien_kierunek
|
||||
50,tak,tak,nie,nie,50,tak,zmien_kierunek
|
||||
50,tak,tak,nie,nie,50,nie,zmien_kierunek
|
||||
50,tak,tak,nie,nie,1,tak,zmien_kierunek
|
||||
50,tak,tak,nie,nie,1,nie,zmien_kierunek
|
||||
50,tak,nie,tak,tak,100,tak,zmien_kierunek
|
||||
50,tak,nie,tak,tak,100,nie,zmien_kierunek
|
||||
50,tak,nie,tak,tak,50,tak,zmien_kierunek
|
||||
50,tak,nie,tak,tak,50,nie,zmien_kierunek
|
||||
50,tak,nie,tak,tak,1,tak,zmien_kierunek
|
||||
50,tak,nie,tak,tak,1,nie,zmien_kierunek
|
||||
50,tak,nie,tak,nie,100,tak,zmien_kierunek
|
||||
50,tak,nie,tak,nie,100,nie,zmien_kierunek
|
||||
50,tak,nie,tak,nie,50,tak,zmien_kierunek
|
||||
50,tak,nie,tak,nie,50,nie,zmien_kierunek
|
||||
50,tak,nie,tak,nie,1,tak,zmien_kierunek
|
||||
50,tak,nie,tak,nie,1,nie,zmien_kierunek
|
||||
50,tak,nie,nie,tak,100,tak,zmien_kierunek
|
||||
50,tak,nie,nie,tak,100,nie,zmien_kierunek
|
||||
50,tak,nie,nie,tak,50,tak,zmien_kierunek
|
||||
50,tak,nie,nie,tak,50,nie,zmien_kierunek
|
||||
50,tak,nie,nie,tak,1,tak,zmien_kierunek
|
||||
50,tak,nie,nie,tak,1,nie,zmien_kierunek
|
||||
50,tak,nie,nie,nie,100,tak,zmien_kierunek
|
||||
50,tak,nie,nie,nie,100,nie,zmien_kierunek
|
||||
50,tak,nie,nie,nie,50,tak,zmien_kierunek
|
||||
50,tak,nie,nie,nie,50,nie,zmien_kierunek
|
||||
50,tak,nie,nie,nie,1,tak,zmien_kierunek
|
||||
50,tak,nie,nie,nie,1,nie,zmien_kierunek
|
||||
50,nie,tak,tak,tak,100,tak,zmien_kierunek
|
||||
50,nie,tak,tak,tak,100,nie,zmien_kierunek
|
||||
50,nie,tak,tak,tak,50,tak,zmien_kierunek
|
||||
50,nie,tak,tak,tak,50,nie,zmien_kierunek
|
||||
50,nie,tak,tak,tak,1,tak,zmien_kierunek
|
||||
50,nie,tak,tak,tak,1,nie,zmien_kierunek
|
||||
50,nie,tak,tak,nie,100,tak,zmien_kierunek
|
||||
50,nie,tak,tak,nie,100,nie,zmien_kierunek
|
||||
50,nie,tak,tak,nie,50,tak,zmien_kierunek
|
||||
50,nie,tak,tak,nie,50,nie,zmien_kierunek
|
||||
50,nie,tak,tak,nie,1,tak,zmien_kierunek
|
||||
50,nie,tak,tak,nie,1,nie,zmien_kierunek
|
||||
50,nie,tak,nie,tak,100,tak,zmien_kierunek
|
||||
50,nie,tak,nie,tak,100,nie,zmien_kierunek
|
||||
50,nie,tak,nie,tak,50,tak,zmien_kierunek
|
||||
50,nie,tak,nie,tak,50,nie,zmien_kierunek
|
||||
50,nie,tak,nie,tak,1,tak,zmien_kierunek
|
||||
50,nie,tak,nie,tak,1,nie,zmien_kierunek
|
||||
50,nie,tak,nie,nie,100,tak,zmien_kierunek
|
||||
50,nie,tak,nie,nie,100,nie,zmien_kierunek
|
||||
50,nie,tak,nie,nie,50,tak,zmien_kierunek
|
||||
50,nie,tak,nie,nie,50,nie,zmien_kierunek
|
||||
50,nie,tak,nie,nie,1,tak,zmien_kierunek
|
||||
50,nie,tak,nie,nie,1,nie,zmien_kierunek
|
||||
50,nie,nie,tak,tak,100,tak,zmien_kierunek
|
||||
50,nie,nie,tak,tak,100,nie,zmien_kierunek
|
||||
50,nie,nie,tak,tak,50,tak,zmien_kierunek
|
||||
50,nie,nie,tak,tak,50,nie,zmien_kierunek
|
||||
50,nie,nie,tak,tak,1,tak,zmien_kierunek
|
||||
50,nie,nie,tak,tak,1,nie,zmien_kierunek
|
||||
50,nie,nie,tak,nie,100,tak,zmien_kierunek
|
||||
50,nie,nie,tak,nie,100,nie,zmien_kierunek
|
||||
50,nie,nie,tak,nie,50,tak,zmien_kierunek
|
||||
50,nie,nie,tak,nie,50,nie,zmien_kierunek
|
||||
50,nie,nie,tak,nie,1,tak,zmien_kierunek
|
||||
50,nie,nie,tak,nie,1,nie,zmien_kierunek
|
||||
50,nie,nie,nie,tak,100,tak,zmien_kierunek
|
||||
50,nie,nie,nie,tak,100,nie,zmien_kierunek
|
||||
50,nie,nie,nie,tak,50,tak,zmien_kierunek
|
||||
50,nie,nie,nie,tak,50,nie,zmien_kierunek
|
||||
50,nie,nie,nie,tak,1,tak,zmien_kierunek
|
||||
50,nie,nie,nie,tak,1,nie,zmien_kierunek
|
||||
50,nie,nie,nie,nie,100,tak,zmien_kierunek
|
||||
50,nie,nie,nie,nie,100,nie,zmien_kierunek
|
||||
50,nie,nie,nie,nie,50,tak,zmien_kierunek
|
||||
50,nie,nie,nie,nie,50,nie,zmien_kierunek
|
||||
50,nie,nie,nie,nie,1,tak,zmien_kierunek
|
||||
50,nie,nie,nie,nie,1,nie,zmien_kierunek
|
||||
1,tak,tak,tak,tak,100,tak,zmien_kierunek
|
||||
1,tak,tak,tak,tak,100,nie,zmien_kierunek
|
||||
1,tak,tak,tak,tak,50,tak,zmien_kierunek
|
||||
1,tak,tak,tak,tak,50,nie,zmien_kierunek
|
||||
1,tak,tak,tak,tak,1,tak,zmien_kierunek
|
||||
1,tak,tak,tak,tak,1,nie,zmien_kierunek
|
||||
1,tak,tak,tak,nie,100,tak,zmien_kierunek
|
||||
1,tak,tak,tak,nie,100,nie,zmien_kierunek
|
||||
1,tak,tak,tak,nie,50,tak,zmien_kierunek
|
||||
1,tak,tak,tak,nie,50,nie,zmien_kierunek
|
||||
1,tak,tak,tak,nie,1,tak,zmien_kierunek
|
||||
1,tak,tak,tak,nie,1,nie,zmien_kierunek
|
||||
1,tak,tak,nie,tak,100,tak,zmien_kierunek
|
||||
1,tak,tak,nie,tak,100,nie,zmien_kierunek
|
||||
1,tak,tak,nie,tak,50,tak,zmien_kierunek
|
||||
1,tak,tak,nie,tak,50,nie,zmien_kierunek
|
||||
1,tak,tak,nie,tak,1,tak,zmien_kierunek
|
||||
1,tak,tak,nie,tak,1,nie,zmien_kierunek
|
||||
1,tak,tak,nie,nie,100,tak,zmien_kierunek
|
||||
1,tak,tak,nie,nie,100,nie,zmien_kierunek
|
||||
1,tak,tak,nie,nie,50,tak,zmien_kierunek
|
||||
1,tak,tak,nie,nie,50,nie,zmien_kierunek
|
||||
1,tak,tak,nie,nie,1,tak,zmien_kierunek
|
||||
1,tak,tak,nie,nie,1,nie,zmien_kierunek
|
||||
1,tak,nie,tak,tak,100,tak,zmien_kierunek
|
||||
1,tak,nie,tak,tak,100,nie,zmien_kierunek
|
||||
1,tak,nie,tak,tak,50,tak,zmien_kierunek
|
||||
1,tak,nie,tak,tak,50,nie,zmien_kierunek
|
||||
1,tak,nie,tak,tak,1,tak,zmien_kierunek
|
||||
1,tak,nie,tak,tak,1,nie,zmien_kierunek
|
||||
1,tak,nie,tak,nie,100,tak,zmien_kierunek
|
||||
1,tak,nie,tak,nie,100,nie,zmien_kierunek
|
||||
1,tak,nie,tak,nie,50,tak,zmien_kierunek
|
||||
1,tak,nie,tak,nie,50,nie,zmien_kierunek
|
||||
1,tak,nie,tak,nie,1,tak,zmien_kierunek
|
||||
1,tak,nie,tak,nie,1,nie,zmien_kierunek
|
||||
1,tak,nie,nie,tak,100,tak,zmien_kierunek
|
||||
1,tak,nie,nie,tak,100,nie,zmien_kierunek
|
||||
1,tak,nie,nie,tak,50,tak,zmien_kierunek
|
||||
1,tak,nie,nie,tak,50,nie,zmien_kierunek
|
||||
1,tak,nie,nie,tak,1,tak,zmien_kierunek
|
||||
1,tak,nie,nie,tak,1,nie,zmien_kierunek
|
||||
1,tak,nie,nie,nie,100,tak,zmien_kierunek
|
||||
1,tak,nie,nie,nie,100,nie,zmien_kierunek
|
||||
1,tak,nie,nie,nie,50,tak,zmien_kierunek
|
||||
1,tak,nie,nie,nie,50,nie,zmien_kierunek
|
||||
1,tak,nie,nie,nie,1,tak,zmien_kierunek
|
||||
1,tak,nie,nie,nie,1,nie,zmien_kierunek
|
||||
1,nie,tak,tak,tak,100,tak,zmien_kierunek
|
||||
1,nie,tak,tak,tak,100,nie,zmien_kierunek
|
||||
1,nie,tak,tak,tak,50,tak,zmien_kierunek
|
||||
1,nie,tak,tak,tak,50,nie,zmien_kierunek
|
||||
1,nie,tak,tak,tak,1,tak,zmien_kierunek
|
||||
1,nie,tak,tak,tak,1,nie,zmien_kierunek
|
||||
1,nie,tak,tak,nie,100,tak,zmien_kierunek
|
||||
1,nie,tak,tak,nie,100,nie,zmien_kierunek
|
||||
1,nie,tak,tak,nie,50,tak,zmien_kierunek
|
||||
1,nie,tak,tak,nie,50,nie,zmien_kierunek
|
||||
1,nie,tak,tak,nie,1,tak,zmien_kierunek
|
||||
1,nie,tak,tak,nie,1,nie,zmien_kierunek
|
||||
1,nie,tak,nie,tak,100,tak,zmien_kierunek
|
||||
1,nie,tak,nie,tak,100,nie,zmien_kierunek
|
||||
1,nie,tak,nie,tak,50,tak,zmien_kierunek
|
||||
1,nie,tak,nie,tak,50,nie,zmien_kierunek
|
||||
1,nie,tak,nie,tak,1,tak,zmien_kierunek
|
||||
1,nie,tak,nie,tak,1,nie,zmien_kierunek
|
||||
1,nie,tak,nie,nie,100,tak,zmien_kierunek
|
||||
1,nie,tak,nie,nie,100,nie,zmien_kierunek
|
||||
1,nie,tak,nie,nie,50,tak,zmien_kierunek
|
||||
1,nie,tak,nie,nie,50,nie,zmien_kierunek
|
||||
1,nie,tak,nie,nie,1,tak,zmien_kierunek
|
||||
1,nie,tak,nie,nie,1,nie,zmien_kierunek
|
||||
1,nie,nie,tak,tak,100,tak,zmien_kierunek
|
||||
1,nie,nie,tak,tak,100,nie,zmien_kierunek
|
||||
1,nie,nie,tak,tak,50,tak,zmien_kierunek
|
||||
1,nie,nie,tak,tak,50,nie,zmien_kierunek
|
||||
1,nie,nie,tak,tak,1,tak,zmien_kierunek
|
||||
1,nie,nie,tak,tak,1,nie,zmien_kierunek
|
||||
1,nie,nie,tak,nie,100,tak,zmien_kierunek
|
||||
1,nie,nie,tak,nie,100,nie,zmien_kierunek
|
||||
1,nie,nie,tak,nie,50,tak,zmien_kierunek
|
||||
1,nie,nie,tak,nie,50,nie,zmien_kierunek
|
||||
1,nie,nie,tak,nie,1,tak,zmien_kierunek
|
||||
1,nie,nie,tak,nie,1,nie,zmien_kierunek
|
||||
1,nie,nie,nie,tak,100,tak,zmien_kierunek
|
||||
1,nie,nie,nie,tak,100,nie,zmien_kierunek
|
||||
1,nie,nie,nie,tak,50,tak,zmien_kierunek
|
||||
1,nie,nie,nie,tak,50,nie,zmien_kierunek
|
||||
1,nie,nie,nie,tak,1,tak,zmien_kierunek
|
||||
1,nie,nie,nie,tak,1,nie,zmien_kierunek
|
||||
1,nie,nie,nie,nie,100,tak,zmien_kierunek
|
||||
1,nie,nie,nie,nie,100,nie,zmien_kierunek
|
||||
1,nie,nie,nie,nie,50,tak,zmien_kierunek
|
||||
1,nie,nie,nie,nie,50,nie,zmien_kierunek
|
||||
1,nie,nie,nie,nie,1,tak,zmien_kierunek
|
||||
1,nie,nie,nie,nie,1,nie,zmien_kierunek
|
150
drzewo_decyzyjne.py
Normal file
@ -0,0 +1,150 @@
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
|
||||
class Tree():
|
||||
|
||||
|
||||
# Obliczanie entropii dla całego zbioru danych
|
||||
def oblicz_calkowita_entropie(self,dane_treningowe, etykieta, lista_klas):
|
||||
liczba_wierszy = dane_treningowe.shape[0]
|
||||
calkowita_entropia = 0
|
||||
|
||||
for klasa in lista_klas:
|
||||
liczba_wystapien_klasy = dane_treningowe[dane_treningowe[etykieta] == klasa].shape[0]
|
||||
entropia_klasy = - (liczba_wystapien_klasy / liczba_wierszy) * np.log2(liczba_wystapien_klasy / liczba_wierszy)
|
||||
calkowita_entropia += entropia_klasy
|
||||
|
||||
return calkowita_entropia
|
||||
|
||||
|
||||
# Obliczanie entropii dla przefiltrowanego zbioru danych
|
||||
def oblicz_entropie(self,dane_wartosci_cechy, etykieta, lista_klas):
|
||||
liczba_wystapien_cechy = dane_wartosci_cechy.shape[0]
|
||||
entropia = 0
|
||||
|
||||
for klasa in lista_klas:
|
||||
liczba_wystapien_klasy = dane_wartosci_cechy[dane_wartosci_cechy[etykieta] == klasa].shape[0]
|
||||
entropia_klasy = 0
|
||||
|
||||
if liczba_wystapien_klasy != 0:
|
||||
prawdopodobienstwo_klasy = liczba_wystapien_klasy / liczba_wystapien_cechy
|
||||
entropia_klasy = - prawdopodobienstwo_klasy * np.log2(prawdopodobienstwo_klasy)
|
||||
|
||||
entropia += entropia_klasy
|
||||
|
||||
return entropia
|
||||
|
||||
|
||||
# Obliczanie przyrostu informacji dla danej cechy
|
||||
def oblicz_przyrost_informacji(self,nazwa_cechy, dane_treningowe, etykieta, lista_klas):
|
||||
unikalne_wartosci_cechy = dane_treningowe[nazwa_cechy].unique()
|
||||
liczba_wierszy = dane_treningowe.shape[0]
|
||||
informacja_cechy = 0.0
|
||||
|
||||
for wartosc_cechy in unikalne_wartosci_cechy:
|
||||
dane_wartosci_cechy = dane_treningowe[dane_treningowe[nazwa_cechy] == wartosc_cechy]
|
||||
liczba_wystapien_wartosci_cechy = dane_wartosci_cechy.shape[0]
|
||||
entropia_wartosci_cechy = self.oblicz_entropie(dane_wartosci_cechy, etykieta, lista_klas)
|
||||
prawdopodobienstwo_wartosci_cechy = liczba_wystapien_wartosci_cechy / liczba_wierszy
|
||||
informacja_cechy += prawdopodobienstwo_wartosci_cechy * entropia_wartosci_cechy
|
||||
|
||||
return self.oblicz_calkowita_entropie(dane_treningowe, etykieta, lista_klas) - informacja_cechy
|
||||
|
||||
|
||||
# Znajdowanie najbardziej informatywnej cechy (cechy o najwyższym przyroście informacji)
|
||||
def znajdz_najbardziej_informatywna_ceche(self,dane_treningowe, etykieta, lista_klas):
|
||||
lista_cech = dane_treningowe.columns.drop(etykieta)
|
||||
# Etykieta nie jest cechą, więc ją usuwamy
|
||||
max_przyrost_informacji = -1
|
||||
najbardziej_informatywna_cecha = None
|
||||
|
||||
for cecha in lista_cech:
|
||||
przyrost_informacji_cechy = self.oblicz_przyrost_informacji(cecha, dane_treningowe, etykieta, lista_klas)
|
||||
|
||||
if max_przyrost_informacji < przyrost_informacji_cechy:
|
||||
max_przyrost_informacji = przyrost_informacji_cechy
|
||||
najbardziej_informatywna_cecha = cecha
|
||||
|
||||
return najbardziej_informatywna_cecha
|
||||
|
||||
|
||||
# Dodawanie węzła do drzewa
|
||||
def generuj_poddrzewo(self,nazwa_cechy, dane_treningowe, etykieta, lista_klas):
|
||||
slownik_licznosci_wartosci_cechy = dane_treningowe[nazwa_cechy].value_counts(sort=False)
|
||||
drzewo = {}
|
||||
|
||||
for wartosc_cechy, liczba in slownik_licznosci_wartosci_cechy.items():
|
||||
dane_wartosci_cechy = dane_treningowe[dane_treningowe[nazwa_cechy] == wartosc_cechy]
|
||||
|
||||
przypisany_do_wezla = False
|
||||
for klasa in lista_klas:
|
||||
liczba_klasy = dane_wartosci_cechy[dane_wartosci_cechy[etykieta] == klasa].shape[0]
|
||||
|
||||
if liczba_klasy == liczba:
|
||||
drzewo[wartosc_cechy] = klasa
|
||||
dane_treningowe = dane_treningowe[dane_treningowe[nazwa_cechy] != wartosc_cechy]
|
||||
przypisany_do_wezla = True
|
||||
if not przypisany_do_wezla:
|
||||
drzewo[wartosc_cechy] = "?"
|
||||
|
||||
return drzewo, dane_treningowe
|
||||
|
||||
|
||||
# Wykonywanie algorytmu ID3 i generowanie drzewa
|
||||
def generuj_drzewo(self,korzen, poprzednia_wartosc_cechy, dane_treningowe, etykieta, lista_klas):
|
||||
if dane_treningowe.shape[0] != 0:
|
||||
najbardziej_informatywna_cecha = self.znajdz_najbardziej_informatywna_ceche(dane_treningowe, etykieta, lista_klas)
|
||||
drzewo, dane_treningowe = self.generuj_poddrzewo(najbardziej_informatywna_cecha, dane_treningowe, etykieta, lista_klas)
|
||||
nastepny_korzen = None
|
||||
|
||||
if poprzednia_wartosc_cechy is not None:
|
||||
korzen[poprzednia_wartosc_cechy] = dict()
|
||||
korzen[poprzednia_wartosc_cechy][najbardziej_informatywna_cecha] = drzewo
|
||||
nastepny_korzen = korzen[poprzednia_wartosc_cechy][najbardziej_informatywna_cecha]
|
||||
else:
|
||||
korzen[najbardziej_informatywna_cecha] = drzewo
|
||||
nastepny_korzen = korzen[najbardziej_informatywna_cecha]
|
||||
|
||||
for wezel, galezie in list(nastepny_korzen.items()):
|
||||
if galezie == "?":
|
||||
dane_wartosci_cechy = dane_treningowe[dane_treningowe[najbardziej_informatywna_cecha] == wezel]
|
||||
self.generuj_drzewo(nastepny_korzen, wezel, dane_wartosci_cechy, etykieta, lista_klas)
|
||||
|
||||
|
||||
# Znajdowanie unikalnych klas etykiety i rozpoczęcie algorytmu
|
||||
def id3(self,nasze_dane, etykieta):
|
||||
dane_treningowe = nasze_dane.copy()
|
||||
drzewo = {}
|
||||
lista_klas = dane_treningowe[etykieta].unique()
|
||||
self.generuj_drzewo(drzewo, None, dane_treningowe, etykieta, lista_klas)
|
||||
return drzewo
|
||||
|
||||
|
||||
# Przewidywanie na podstawie drzewa
|
||||
def przewiduj(self,drzewo, instancja):
|
||||
if not isinstance(drzewo, dict):
|
||||
return drzewo
|
||||
else:
|
||||
korzen = next(iter(drzewo))
|
||||
wartosc_cechy = instancja[korzen]
|
||||
if wartosc_cechy in drzewo[korzen]:
|
||||
return self.przewiduj(drzewo[korzen][wartosc_cechy], instancja)
|
||||
else:
|
||||
return 'walcz'
|
||||
|
||||
def tree(self,przyklad):
|
||||
# Wczytywanie danych
|
||||
nasze_dane = pd.read_csv("data")
|
||||
|
||||
drzewo = self.id3(nasze_dane, 'akcja')
|
||||
|
||||
return self.przewiduj(drzewo, przyklad)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#print(przewiduj(drzewo, przyklad))
|
||||
#print(drzewo)
|
590
main.py
@ -1,124 +1,466 @@
|
||||
import pygame
|
||||
from config import *
|
||||
from agent import *
|
||||
from map_add_ons import *
|
||||
from mobs import *
|
||||
#from unknown_mob import * #unknown mob
|
||||
import random
|
||||
from Astar import *
|
||||
from Astar import *
|
||||
|
||||
class Game:
|
||||
|
||||
def __init__(self):
|
||||
pygame.init()
|
||||
self.state =[-1,-1,-1,-1,-1,-1,-1,-1]
|
||||
self.SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
|
||||
self.running = True
|
||||
self.clock = pygame.time.Clock()
|
||||
|
||||
self.BACKGROUND_IMG= pygame.image.load("./zdjecia/podloze.jpg")
|
||||
self.BACKGROUND = pygame.transform.scale(self.BACKGROUND_IMG,(64,64))
|
||||
|
||||
self.LVL_ICON_PNG = pygame.image.load("./zdjecia/lvl_icon.png")
|
||||
self.LVL_ICON = pygame.transform.scale(self.LVL_ICON_PNG,(24,24))
|
||||
|
||||
pygame.display.set_caption('Gra-SI')
|
||||
|
||||
#self.Astar = Astar(self)
|
||||
self.Astar = Astar(self)
|
||||
|
||||
def new(self): # tworzy się nowa sesja grania
|
||||
self.all_sprites = pygame.sprite.LayeredUpdates()
|
||||
|
||||
self.rock_sprites = pygame.sprite.LayeredUpdates()
|
||||
self.grass_sprites = pygame.sprite.LayeredUpdates()
|
||||
self.archer_orks = pygame.sprite.LayeredUpdates()
|
||||
self.infantry_orks = pygame.sprite.LayeredUpdates()
|
||||
self.infantry_orks2 = pygame.sprite.LayeredUpdates()
|
||||
self.sauronL = pygame.sprite.LayeredUpdates()
|
||||
self.flowers = 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.archer_ork = Archer_ork(self,10,10)
|
||||
self.Astar.enemy_cells.append(self.Astar.get_cell_number(self.archer_ork.x,self.archer_ork.y))
|
||||
self.infantry_ork = Infantry_ork(self,10,4)
|
||||
self.Astar.enemy_cells.append(self.Astar.get_cell_number(self.infantry_ork.x,self.infantry_ork.y))
|
||||
self.infantry_ork2 = Infantry_ork2(self,6,3)
|
||||
self.Astar.enemy_cells.append(self.Astar.get_cell_number(self.infantry_ork2.x,self.infantry_ork2.y))
|
||||
self.sauron = Sauron(self, 1, 10)
|
||||
self.Astar.enemy_cells.append(self.Astar.get_cell_number(self.sauron.x,self.sauron.y))
|
||||
self.flower = Health_flower(self, 8,2)
|
||||
#self.unknown_mob = Unknown_mob(self,8,8) #unknown mob
|
||||
for y in range(5):
|
||||
self.rock = Rocks(self,3,y)
|
||||
self.Astar.wall_cells.append(self.Astar.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.Astar.get_cell_number(self.grass.x,self.grass.y))
|
||||
for y in range(5,8):
|
||||
self.little_rocks = Little_Rocks(self,4,y)
|
||||
self.Astar.little_rocks_cells.append(self.Astar.get_cell_number(self.little_rocks.x, self.little_rocks.y))
|
||||
|
||||
|
||||
|
||||
def update(self):
|
||||
self.all_sprites.update()
|
||||
|
||||
|
||||
def events(self):
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
self.running = False
|
||||
pygame.quit()
|
||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||
mouse_presses = pygame.mouse.get_pressed()
|
||||
if mouse_presses[0]:
|
||||
#self.bfs.bfs()
|
||||
self.Astar.Astar()
|
||||
|
||||
def map(self): # tworzenie mapy
|
||||
self.clock.tick(FRAMERATE)
|
||||
for x in range(0, WIDTH, TILE_SIZE):
|
||||
for y in range(0, 768, TILE_SIZE):
|
||||
self.SCREEN.blit(self.BACKGROUND,(x,y))
|
||||
self.rect = pygame.Rect(x, y, TILE_SIZE, TILE_SIZE)
|
||||
pygame.draw.rect(self.SCREEN, BLACK, self.rect, 1)
|
||||
self.flowers.draw(self.SCREEN)
|
||||
self.all_sprites.draw(self.SCREEN)
|
||||
self.rock_sprites.draw(self.SCREEN)
|
||||
self.grass_sprites.draw(self.SCREEN)
|
||||
self.SCREEN.blit(self.LVL_ICON, (340 ,780))
|
||||
pygame.display.update()
|
||||
|
||||
def main(self):
|
||||
self.events()
|
||||
self.update()
|
||||
self.map()
|
||||
|
||||
grass_cells = []
|
||||
|
||||
|
||||
'''def astar(self):
|
||||
|
||||
|
||||
def function(self):
|
||||
|
||||
|
||||
def cost(self):
|
||||
cost = 0
|
||||
while()
|
||||
'''
|
||||
|
||||
|
||||
|
||||
# Astar ALGORITHM
|
||||
|
||||
g = Game()
|
||||
g.new()
|
||||
while g.running:
|
||||
g.main()
|
||||
|
||||
import pygame
|
||||
from config import *
|
||||
from agent import *
|
||||
from map_add_ons import *
|
||||
from mobs import *
|
||||
from bfs import *
|
||||
from nn import *
|
||||
from astar import *
|
||||
import math
|
||||
import random
|
||||
from drzewo_decyzyjne import *
|
||||
|
||||
class Game:
|
||||
|
||||
def __init__(self):
|
||||
pygame.init()
|
||||
self.state =[-1,-1,-1,-1,-1,-1,-1,-1]
|
||||
self.SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
|
||||
self.running = True
|
||||
self.clock = pygame.time.Clock()
|
||||
|
||||
self.BACKGROUND_IMG= pygame.image.load("./pozostale_zdjecia/podloze.jpg")
|
||||
self.BACKGROUND = pygame.transform.scale(self.BACKGROUND_IMG,(64,64))
|
||||
|
||||
self.LVL_ICON_PNG = pygame.image.load("./pozostale_zdjecia/lvl_icon.png")
|
||||
self.LVL_ICON = pygame.transform.scale(self.LVL_ICON_PNG,(24,24))
|
||||
|
||||
pygame.display.set_caption('Gra-SI')
|
||||
|
||||
self.bfs = Bfs(self)
|
||||
self.nn = NeuralN()
|
||||
self.astar = Astar(self)
|
||||
self.tree = Tree()
|
||||
self.cell_costs = [[1 for _ in range(TILE_SIZE)] for _ in range(TILE_SIZE)]
|
||||
self.obstacles = [[False for _ in range(TILE_SIZE)] for _ in range(TILE_SIZE)]
|
||||
|
||||
|
||||
|
||||
|
||||
def new(self): # tworzy się nowa sesja grania
|
||||
self.all_sprites = pygame.sprite.LayeredUpdates()
|
||||
|
||||
self.rock_sprites = pygame.sprite.LayeredUpdates()
|
||||
self.grass_sprites = pygame.sprite.LayeredUpdates()
|
||||
self.archer_orks = pygame.sprite.LayeredUpdates()
|
||||
self.infantry_orks = pygame.sprite.LayeredUpdates()
|
||||
|
||||
self.sauronL = pygame.sprite.LayeredUpdates()
|
||||
self.flowers = pygame.sprite.LayeredUpdates()
|
||||
self.little_rock_sprites = pygame.sprite.LayeredUpdates()
|
||||
|
||||
#Agent,Archer_ork,infantry_ork,sauron,flower,grass x6, rocks x5 tablica 16 elementowa y=0-11 x=0-12 random.randint(x, y) = od x do y downolny int
|
||||
self.allpositions=[]
|
||||
self.allpositionsSet=set()
|
||||
while(len(self.allpositionsSet)<100):
|
||||
self.positions=[] #.append
|
||||
self.positionsSet=set() #.add
|
||||
for x in range(16):
|
||||
while len(self.positionsSet)<16:
|
||||
pos1=random.randint(0,12) #x
|
||||
pos2=random.randint(0,11) #y
|
||||
pom=(pos1,pos2)
|
||||
lenSetBefore=len(self.positionsSet)
|
||||
self.positionsSet.add(pom)
|
||||
lenSetAfter=len(self.positionsSet)
|
||||
if(lenSetAfter>lenSetBefore):
|
||||
self.positions.append(pom)
|
||||
AllPositionsSetB=len(self.allpositionsSet)
|
||||
self.allpositionsSet.add(tuple(self.positions))
|
||||
AllPositionsSetA=len(self.allpositionsSet)
|
||||
if(AllPositionsSetA>AllPositionsSetB):
|
||||
self.positions.append((1000))
|
||||
self.allpositions.append(self.positions)
|
||||
#print("TO SA KOLEJNE po randomowaniu")
|
||||
#print(self.allpositions)
|
||||
def sprawdz_powtorzenia(tablica):
|
||||
wystapienia = set()
|
||||
for element in tablica[:-1]:
|
||||
if tuple(element) in wystapienia:
|
||||
return True # Powtórzenie znalezione
|
||||
wystapienia.add(tuple(element))
|
||||
return False # Brak powtórzeń
|
||||
def ocena_tablicy(dane):
|
||||
for x in range(100):
|
||||
grade=0
|
||||
if(sprawdz_powtorzenia(dane[x])):
|
||||
dane[x][-1]=10000000
|
||||
else:
|
||||
x1,y1=dane[x][0]
|
||||
x2,y2=dane[x][1]
|
||||
x3,y3=dane[x][2]
|
||||
x4,y4=dane[x][3]
|
||||
x5,y5=dane[x][4]
|
||||
|
||||
|
||||
|
||||
r1=math.sqrt((x1 - x4)**2 + (y1 - y4)**2)
|
||||
r2=math.sqrt((x4 - x2)**2 + (y4 - y2)**2)
|
||||
r3=math.sqrt((x2 - x3)**2 + (y2 - y3)**2)
|
||||
r4=math.sqrt((x3 - x5)**2 + (y3 - y5)**2)
|
||||
r5=math.sqrt((x5 - x4)**2 + (y5 - y4)**2)
|
||||
r12=math.sqrt((x1 - x3)**2 + (y1 - y3)**2)
|
||||
r13=math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
|
||||
r14=math.sqrt((x1 - x5)**2 + (y1 - y5)**2)
|
||||
spr=math.sqrt((x2 - x3)**2 + (y2 - y3)**2)
|
||||
spr1=math.sqrt((x2 - x4)**2 + (y2 - y4)**2)
|
||||
spr2=math.sqrt((x2 - x5)**2 + (y2 - y5)**2)
|
||||
spr3=math.sqrt((x3 - x4)**2 + (y3 - y4)**2)
|
||||
spr4=math.sqrt((x3 - x5)**2 + (y3 - y5)**2)
|
||||
spr5=math.sqrt((x4 - x5)**2 + (y4 - y5)**2)
|
||||
avg=(r1+r2+r3+r4+r5)/5
|
||||
grade=abs(r1-avg)+abs(r2-avg)+abs(r3-avg)+abs(r4-avg)+abs(r5-avg)
|
||||
if(r1<5):
|
||||
grade=grade+2
|
||||
if(r12<5):
|
||||
grade=grade+2
|
||||
if(r13<5):
|
||||
grade=grade+2
|
||||
if(r14<5):
|
||||
grade=grade+2
|
||||
if(spr<5 or spr1<5 or spr2<5 or spr3<5 or spr4<5 or spr5<5):
|
||||
grade=grade+5
|
||||
|
||||
|
||||
x6,y6=dane[x][-2]
|
||||
x7,y7=dane[x][-3]
|
||||
x8,y8=dane[x][-4]
|
||||
x9,y9=dane[x][-5]
|
||||
x0,y0=dane[x][-6]
|
||||
rock1=math.sqrt((x6 - x7)**2 + (y6 - y7)**2)
|
||||
rock2=math.sqrt((x6 - x8)**2 + (y6 - y8)**2)
|
||||
rock3=math.sqrt((x6 - x9)**2 + (y6 - y9)**2)
|
||||
rock4=math.sqrt((x6 - x0)**2 + (y6 - y0)**2)
|
||||
|
||||
rock5=math.sqrt((x7 - x8)**2 + (y7 - y8)**2)
|
||||
rock6=math.sqrt((x7 - x9)**2 + (y7 - y9)**2)
|
||||
rock7=math.sqrt((x7 - x0)**2 + (y7 - y0)**2)
|
||||
|
||||
rock8=math.sqrt((x8 - x9)**2 + (y8 - y9)**2)
|
||||
rock9=math.sqrt((x8 - x0)**2 + (y8 - y0)**2)
|
||||
|
||||
rock0=math.sqrt((x9 - x0)**2 + (y9 - y0)**2)
|
||||
if(rock1<2 or rock2<2 or rock3<2 or rock4<2 or rock5<2 or rock6<2 or rock7<2 or rock8<2 or rock9<2 or rock0<2):
|
||||
grade=grade+3
|
||||
|
||||
|
||||
grade=round(grade,2)*100
|
||||
dane[x][-1]=grade
|
||||
return dane
|
||||
def sort_tablicy(dane):
|
||||
posortowana_tablica = sorted(dane, key=lambda x: x[-1])
|
||||
return posortowana_tablica
|
||||
def generuj_pary(n):
|
||||
pary = []
|
||||
for i in range(1, n+1):
|
||||
for j in range(i+1, n+1):
|
||||
pary.append([i, j])
|
||||
return pary
|
||||
self.WszystkiePary=generuj_pary(7)
|
||||
#print("")
|
||||
#print("")
|
||||
#print("WszystkiePary")
|
||||
#print(self.WszystkiePary)
|
||||
|
||||
def polacz_tablice(tablica1, tablica2,n):
|
||||
nowa_tablica = tablica1[:n] + tablica2[n:]
|
||||
return nowa_tablica
|
||||
self.positionsAfterGrade=ocena_tablicy(self.allpositions)
|
||||
#print("")
|
||||
#print("")
|
||||
#print("")
|
||||
#print("Po ocenie ")
|
||||
#print(self.positionsAfterGrade)
|
||||
self.sortedAfterGrade=sort_tablicy(self.positionsAfterGrade)
|
||||
#print("")
|
||||
#print("")
|
||||
#print("")
|
||||
#print("Po sortowaniu ")
|
||||
#print(self.sortedAfterGrade)
|
||||
n=100
|
||||
self.licznik=0
|
||||
|
||||
while(self.sortedAfterGrade[0][16]!=0 and self.licznik <n):
|
||||
#print("NUMER ITERACJI: "+str(self.licznik))
|
||||
#print("")
|
||||
#print("")
|
||||
|
||||
self.WynikKombinacji=[]
|
||||
pomWynikInt=0
|
||||
pomWszystkieParyInt=0
|
||||
|
||||
while(len(self.WynikKombinacji)<20 and pomWszystkieParyInt<21):
|
||||
|
||||
|
||||
gen1=self.sortedAfterGrade[self.WszystkiePary[pomWszystkieParyInt][0]-1]
|
||||
gen1[-1]=9966
|
||||
#print("gen1")
|
||||
#print(gen1)
|
||||
gen2=self.sortedAfterGrade[self.WszystkiePary[pomWszystkieParyInt][1]-1]
|
||||
gen2[-1]=9966
|
||||
#print("gen2")
|
||||
#print(gen2)
|
||||
rollKombinacja=random.randint(0,100)# chance 60%
|
||||
|
||||
#print("rollKombinacja:"+str(rollKombinacja))
|
||||
|
||||
if(rollKombinacja<61):
|
||||
KombInt=random.randint(1,4)
|
||||
#print("KombInt:"+str(KombInt))
|
||||
#print("Przed append")
|
||||
#print(self.WynikKombinacji)
|
||||
losujKtoGen1=random.randint(0,100)
|
||||
#print("")
|
||||
#print(losujKtoGen1)
|
||||
if(losujKtoGen1>50):
|
||||
self.WynikKombinacji.append(polacz_tablice(gen1,gen2,KombInt))
|
||||
else:
|
||||
self.WynikKombinacji.append(polacz_tablice(gen2,gen1,KombInt))
|
||||
#print("Po append")
|
||||
#print(self.WynikKombinacji)
|
||||
rollMutacja=random.randint(0,100)# chance 10%
|
||||
#print("rollMutacja:"+str(rollMutacja))
|
||||
if(rollMutacja<90):
|
||||
#print("rolowanie mutacji")
|
||||
MutacjaInt=random.randint(0,4)
|
||||
#print(MutacjaInt)
|
||||
xPoMutacji=random.randint(0,12) #x
|
||||
yPoMutacji=random.randint(0,11) #y
|
||||
#print("rolowanie x y")
|
||||
#print(xPoMutacji,yPoMutacji)
|
||||
self.WynikKombinacji[pomWynikInt][MutacjaInt]=[xPoMutacji,yPoMutacji]
|
||||
pomWynikInt=pomWynikInt+1
|
||||
pomWszystkieParyInt=pomWszystkieParyInt+1
|
||||
laczenieGeneracji=self.sortedAfterGrade[:(100-len(self.WynikKombinacji))]+self.WynikKombinacji
|
||||
#print("pewna czesc ")
|
||||
#print(self.sortedAfterGrade[:(100-len(self.WynikKombinacji))])
|
||||
#print("reszta ")
|
||||
#print("")
|
||||
#print(self.WynikKombinacji)
|
||||
|
||||
OcenaWszystkich=ocena_tablicy(laczenieGeneracji)
|
||||
#print("Przed DODANIEM GENERACJI")
|
||||
#print(self.sortedAfterGrade)
|
||||
#print("")
|
||||
#print("")
|
||||
self.sortedAfterGrade=sort_tablicy(OcenaWszystkich)
|
||||
#print("Po DODANIU GENERACJI")
|
||||
#print(self.sortedAfterGrade)
|
||||
self.licznik=self.licznik+1
|
||||
#self.sortedAfterGrade[0] najlepszy
|
||||
#print("")
|
||||
#print("")
|
||||
print(self.sortedAfterGrade)
|
||||
self.najlepszaGeneracja=self.sortedAfterGrade[0] #Agent,Archer_ork,infantry_ork,sauron,flower,grass x6, rocks x5 tablica 16 elementowa y=0-11 x=0-12 random.randint(x, y) = od x do y downolny int
|
||||
|
||||
|
||||
self.agent = Agent(self,self.najlepszaGeneracja[0][0],self.najlepszaGeneracja[0][1])
|
||||
self.archer_ork = Archer_ork(self,self.najlepszaGeneracja[1][0],self.najlepszaGeneracja[1][1])
|
||||
self.obstacles[self.najlepszaGeneracja[1][0]][self.najlepszaGeneracja[1][1]] = True
|
||||
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.archer_ork.x,self.archer_ork.y))
|
||||
self.infantry_ork = Infantry_ork(self,self.najlepszaGeneracja[2][0],self.najlepszaGeneracja[2][1])
|
||||
self.obstacles[self.najlepszaGeneracja[2][0]][self.najlepszaGeneracja[2][1]] = True
|
||||
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.infantry_ork.x,self.infantry_ork.y))
|
||||
|
||||
self.sauron = Sauron(self, self.najlepszaGeneracja[3][0], self.najlepszaGeneracja[3][1])
|
||||
self.obstacles[self.najlepszaGeneracja[3][0]][self.najlepszaGeneracja[3][1]] = True
|
||||
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.sauron.x,self.sauron.y))
|
||||
self.flower = Health_flower(self, self.najlepszaGeneracja[4][0],self.najlepszaGeneracja[4][1])
|
||||
|
||||
for y in range (6):
|
||||
self.grass = Grass(self,self.najlepszaGeneracja[y+5][0],self.najlepszaGeneracja[y+5][1])
|
||||
self.cell_costs[self.najlepszaGeneracja[y+5][0]][self.najlepszaGeneracja[y+5][1]] = 5
|
||||
|
||||
for y in range(5):
|
||||
self.rock = Rocks(self,self.najlepszaGeneracja[y+11][0],self.najlepszaGeneracja[y+11][1])
|
||||
self.obstacles[self.najlepszaGeneracja[y+11][0]][self.najlepszaGeneracja[y+11][1]] = True
|
||||
self.bfs.wall_cells.append(self.bfs.get_cell_number(self.rock.x,self.rock.y))
|
||||
|
||||
|
||||
def update(self):
|
||||
self.all_sprites.update()
|
||||
|
||||
|
||||
def events(self):
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
self.running = False
|
||||
pygame.quit()
|
||||
if event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_SPACE:
|
||||
self.goal_cell = self.bfs.get_cell_number(self.flower.x, self.flower.y)
|
||||
self.move_agent(self.bfs.bfs(self.goal_cell))
|
||||
|
||||
|
||||
|
||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||
mouse_presses = pygame.mouse.get_pressed()
|
||||
if mouse_presses[0]:
|
||||
|
||||
x = self.sauron.x
|
||||
y = self.sauron.y
|
||||
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
|
||||
print("goal: ",goal)
|
||||
if prediction == "SAURON":
|
||||
if self.agent.level < self.sauron.level:
|
||||
lvl = 'nie'
|
||||
else:
|
||||
lvl = 'tak'
|
||||
przyklad = {'zdrowie_bohatera': '100',
|
||||
'moc_bohatera': 'tak',
|
||||
'moc_moba': 'tak',
|
||||
'lvl_wiekszy_bohater': lvl,
|
||||
'mob_jest_strzelcem': self.sauron.archer,
|
||||
'zdrowie_moba': '50',
|
||||
'artefakt': self.agent.artifact}
|
||||
decision = self.tree.tree(przyklad)
|
||||
print(decision)
|
||||
if decision == "walcz":
|
||||
self.obstacles[self.najlepszaGeneracja[3][0]][self.najlepszaGeneracja[3][1]] = False
|
||||
self.move_agent(self.astar.a_star(goal))
|
||||
else:
|
||||
x = self.archer_ork.x
|
||||
y = self.archer_ork.y
|
||||
goal = x//TILE_SIZE,y//TILE_SIZE
|
||||
mob_image = self.archer_ork.image_path
|
||||
prediction = self.prediction_road(x,y,mob_image)
|
||||
prediction = "ORK_ARCHER"
|
||||
|
||||
elif prediction == "ORK_MELEE":
|
||||
if self.agent.level < self.infantry_ork.level:
|
||||
lvl = 'nie'
|
||||
else:
|
||||
lvl = 'tak'
|
||||
przyklad = {'zdrowie_bohatera': '100',
|
||||
'moc_bohatera': 'tak',
|
||||
'moc_moba': 'tak',
|
||||
'lvl_wiekszy_bohater': lvl,
|
||||
'mob_jest_strzelcem': self.infantry_ork.archer,
|
||||
'zdrowie_moba': '50',
|
||||
'artefakt': self.agent.artifact}
|
||||
decision = self.tree.tree(przyklad)
|
||||
print(decision)
|
||||
if decision == "walcz":
|
||||
self.obstacles[self.najlepszaGeneracja[2][0]][self.najlepszaGeneracja[2][1]] = 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
|
||||
y = self.sauron.y
|
||||
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":
|
||||
if self.agent.level < self.archer_ork.level:
|
||||
lvl = 'nie'
|
||||
else:
|
||||
lvl = 'tak'
|
||||
przyklad = {'zdrowie_bohatera': '100',
|
||||
'moc_bohatera': 'tak',
|
||||
'moc_moba': 'tak',
|
||||
'lvl_wiekszy_bohater': lvl,
|
||||
'mob_jest_strzelcem': self.archer_ork.archer,
|
||||
'zdrowie_moba': '50',
|
||||
'artefakt': self.agent.artifact}
|
||||
decision = self.tree.tree(przyklad)
|
||||
print(decision)
|
||||
if decision == "walcz":
|
||||
self.obstacles[self.najlepszaGeneracja[1][0]][self.najlepszaGeneracja[1][1]] = 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):
|
||||
mob_goal_cell = (self.bfs.get_cell_number(x,y))
|
||||
if self.bfs.get_up_cell(mob_goal_cell) == None:
|
||||
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:
|
||||
goal_cell = self.bfs.get_up_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)
|
||||
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.rect.x,self.agent.rect.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
|
||||
self.clock.tick(FRAMERATE)
|
||||
for x in range(0, WIDTH, TILE_SIZE):
|
||||
for y in range(0, 768, TILE_SIZE):
|
||||
self.SCREEN.blit(self.BACKGROUND,(x,y))
|
||||
self.rect = pygame.Rect(x, y, TILE_SIZE, TILE_SIZE)
|
||||
pygame.draw.rect(self.SCREEN, BLACK, self.rect, 1)
|
||||
self.flowers.draw(self.SCREEN)
|
||||
self.all_sprites.draw(self.SCREEN)
|
||||
self.rock_sprites.draw(self.SCREEN)
|
||||
self.grass_sprites.draw(self.SCREEN)
|
||||
self.SCREEN.blit(self.LVL_ICON, (340 ,780))
|
||||
pygame.display.update()
|
||||
|
||||
def main(self):
|
||||
self.events()
|
||||
self.update()
|
||||
self.map()
|
||||
|
||||
|
||||
g = Game()
|
||||
g.new()
|
||||
|
||||
while g.running:
|
||||
g.main()
|
||||
|
@ -14,7 +14,7 @@ class Rocks(pygame.sprite.Sprite):
|
||||
self.width = TILE_SIZE
|
||||
self.height = TILE_SIZE
|
||||
|
||||
self.ROCKS_PNG = pygame.image.load("./zdjecia/rock.png")
|
||||
self.ROCKS_PNG = pygame.image.load("./pozostale_zdjecia/rock.png")
|
||||
self.ROCKS = pygame.transform.scale(self.ROCKS_PNG,(64,64))
|
||||
|
||||
self.image = pygame.Surface([self.width, self.height])
|
||||
@ -39,7 +39,7 @@ class Grass(pygame.sprite.Sprite):
|
||||
self.width = TILE_SIZE
|
||||
self.height = TILE_SIZE
|
||||
|
||||
self.GRASS_PNG = pygame.image.load("./zdjecia/grass.png")
|
||||
self.GRASS_PNG = pygame.image.load("./pozostale_zdjecia/grass.png")
|
||||
self.GRASS = pygame.transform.scale(self.GRASS_PNG,(64,64))
|
||||
|
||||
self.image = pygame.Surface([self.width, self.height])
|
||||
@ -50,8 +50,6 @@ class Grass(pygame.sprite.Sprite):
|
||||
self.rect.x = self.x
|
||||
self.rect.y = self.y
|
||||
|
||||
self.cost = 10
|
||||
|
||||
self._layer = GRASS_LAYER
|
||||
|
||||
class Health_flower(pygame.sprite.Sprite):
|
||||
@ -67,7 +65,7 @@ class Health_flower(pygame.sprite.Sprite):
|
||||
self.width = TILE_SIZE
|
||||
self.height = TILE_SIZE
|
||||
|
||||
self.FLOWER_IMG = pygame.image.load("./zdjecia/flower.png")
|
||||
self.FLOWER_IMG = pygame.image.load("./pozostale_zdjecia/flower.png")
|
||||
self.FLOWER = pygame.transform.scale(self.FLOWER_IMG,(64,64))
|
||||
|
||||
self.image = pygame.Surface([self.width, self.height])
|
||||
@ -92,7 +90,7 @@ class Little_Rocks(pygame.sprite.Sprite):
|
||||
self.width = TILE_SIZE
|
||||
self.height = TILE_SIZE
|
||||
|
||||
self.LITTLE_ROCKS_PNG = pygame.image.load("./zdjecia/little_rocks.png")
|
||||
self.LITTLE_ROCKS_PNG = pygame.image.load("./pozostale_zdjecia/little_rocks.png")
|
||||
self.LITTLE_ROCKS = pygame.transform.scale(self.LITTLE_ROCKS_PNG,(64,64))
|
||||
|
||||
self.image = pygame.Surface([self.width, self.height])
|
||||
|
47
mobs.py
@ -14,7 +14,8 @@ class Archer_ork(pygame.sprite.Sprite):
|
||||
self.width = TILE_SIZE
|
||||
self.height = TILE_SIZE
|
||||
|
||||
self.ARCHER_ORK_IMG = pygame.image.load("./zdjecia/ork_lucznik.png")
|
||||
self.image_path = "./zdjecia/ORK_ARCHER/ork_archer (889).jpg"
|
||||
self.ARCHER_ORK_IMG = pygame.image.load(self.image_path)
|
||||
self.ARCHER_ORK = pygame.transform.scale(self.ARCHER_ORK_IMG,(64,64))
|
||||
|
||||
self.image = pygame.Surface([self.width, self.height])
|
||||
@ -27,6 +28,9 @@ class Archer_ork(pygame.sprite.Sprite):
|
||||
|
||||
self.level = 1
|
||||
self.damage = 50*self.level
|
||||
self.health = 50
|
||||
|
||||
self.archer = 'tak'
|
||||
|
||||
class Infantry_ork(pygame.sprite.Sprite):
|
||||
|
||||
@ -41,7 +45,8 @@ class Infantry_ork(pygame.sprite.Sprite):
|
||||
self.width = TILE_SIZE
|
||||
self.height = TILE_SIZE
|
||||
|
||||
self.INFANTRY_ORK_IMG = pygame.image.load("./zdjecia/ork-piechota.png")
|
||||
self.image_path = "C:\\mobs_photos\\ork_melee (11).jpg"#sciezka do zmiany
|
||||
self.INFANTRY_ORK_IMG = pygame.image.load(self.image_path)
|
||||
self.INFANTRY_ORK = pygame.transform.scale(self.INFANTRY_ORK_IMG,(64,64))
|
||||
|
||||
self.image = pygame.Surface([self.width, self.height])
|
||||
@ -54,36 +59,11 @@ class Infantry_ork(pygame.sprite.Sprite):
|
||||
|
||||
self.level = 2
|
||||
self.damage = 50*self.level
|
||||
|
||||
self.health = 100
|
||||
|
||||
class Infantry_ork2(pygame.sprite.Sprite):
|
||||
self.archer = 'nie'
|
||||
|
||||
|
||||
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):
|
||||
|
||||
|
||||
@ -97,7 +77,8 @@ class Sauron(pygame.sprite.Sprite):
|
||||
self.width = TILE_SIZE
|
||||
self.height = TILE_SIZE
|
||||
|
||||
self.SAURON_IMG = pygame.image.load("./zdjecia/sauron.png")
|
||||
self.image_path = "C:\\mobs_photos\\sauron (700).jpg"#sciezka do zmiany
|
||||
self.SAURON_IMG = pygame.image.load(self.image_path)
|
||||
self.SAURON = pygame.transform.scale(self.SAURON_IMG,(64,64))
|
||||
|
||||
self.image = pygame.Surface([self.width, self.height])
|
||||
@ -108,6 +89,8 @@ class Sauron(pygame.sprite.Sprite):
|
||||
self.rect.x = self.x
|
||||
self.rect.y = self.y
|
||||
|
||||
self.level = 4
|
||||
self.level = 3
|
||||
self.damage = 50*self.level
|
||||
|
||||
self.health = 150
|
||||
|
||||
self.archer = 'nie'
|
109
nn.py
Normal file
@ -0,0 +1,109 @@
|
||||
import os
|
||||
import glob
|
||||
import PIL
|
||||
from PIL import Image
|
||||
import tensorflow as tf
|
||||
import pickle
|
||||
from tensorflow import keras
|
||||
from keras import layers
|
||||
from keras.models import Sequential
|
||||
import pathlib
|
||||
|
||||
|
||||
class NeuralN:
|
||||
# @staticmethod
|
||||
def predict(self, image_path):
|
||||
data_dir = pathlib.Path('zdjecia')
|
||||
saved_model_path = pathlib.Path('trained_model.h5')
|
||||
class_names_path = pathlib.Path("class_names.pkl")
|
||||
image_count = sum(len(files) for _, _, files in os.walk(data_dir))
|
||||
|
||||
print(image_count)
|
||||
|
||||
# ORK_ARCHER = list(glob.glob('C:\\mobs_photos\\ORK_ARCHER'))
|
||||
# im = PIL.Image.open(ORK_ARCHER[0])
|
||||
# im.show()
|
||||
if os.path.exists(saved_model_path):
|
||||
model = tf.keras.models.load_model(saved_model_path)
|
||||
print("Saved model loaded")
|
||||
with open(class_names_path, 'rb') as f:
|
||||
class_names = pickle.load(f)
|
||||
print("Class names loaded.")
|
||||
|
||||
else:
|
||||
train_ds = tf.keras.utils.image_dataset_from_directory(
|
||||
data_dir,
|
||||
validation_split=0.2,
|
||||
subset="training",
|
||||
seed=123,
|
||||
image_size=(180, 180),
|
||||
batch_size=32)
|
||||
|
||||
val_ds = tf.keras.utils.image_dataset_from_directory(
|
||||
data_dir,
|
||||
validation_split=0.2,
|
||||
subset="validation",
|
||||
seed=123,
|
||||
image_size=(180, 180),
|
||||
batch_size=32)
|
||||
|
||||
class_names = train_ds.class_names
|
||||
print(class_names)
|
||||
|
||||
num_classes = len(class_names)
|
||||
|
||||
model = Sequential([
|
||||
layers.Rescaling(1. / 255, input_shape=(180, 180, 3)),
|
||||
layers.Conv2D(16, 3, padding='same', activation='relu'),
|
||||
layers.MaxPooling2D(),
|
||||
layers.Conv2D(32, 3, padding='same', activation='relu'),
|
||||
layers.MaxPooling2D(),
|
||||
layers.Conv2D(64, 3, padding='same', activation='relu'),
|
||||
layers.MaxPooling2D(),
|
||||
layers.Flatten(),
|
||||
layers.Dense(128, activation='relu'),
|
||||
layers.Dense(num_classes)
|
||||
])
|
||||
|
||||
model.compile(optimizer='adam',
|
||||
loss=tf.keras.losses.SparseCategoricalCrossentropy(
|
||||
from_logits=True),
|
||||
metrics=['accuracy'])
|
||||
model.summary()
|
||||
|
||||
epochs = 10
|
||||
history = model.fit(
|
||||
train_ds,
|
||||
validation_data=val_ds,
|
||||
epochs=epochs
|
||||
)
|
||||
model.save("trained_model.h5")
|
||||
print("Model trained and saved.")
|
||||
with open(class_names_path, 'wb') as f:
|
||||
pickle.dump(train_ds.class_names, f)
|
||||
print("Class names saved.")
|
||||
# loaded_model = tf.keras.models.load_model("trained_model.h5")
|
||||
probability_model = tf.keras.Sequential([model,
|
||||
tf.keras.layers.Softmax()])
|
||||
|
||||
# image_path = image
|
||||
#image_path = pathlib.Path('')
|
||||
image = Image.open(image_path)
|
||||
|
||||
# Preprocess the image
|
||||
|
||||
image = image.resize((180, 180)) # Resize to match the input size of the model
|
||||
image_array = tf.keras.preprocessing.image.img_to_array(image)
|
||||
image_array = image_array / 255.0 # Normalize pixel values
|
||||
|
||||
# Add an extra dimension to the image array
|
||||
image_array = tf.expand_dims(image, 0)
|
||||
# Make the prediction
|
||||
model = tf.keras.models.load_model("trained_model.h5")
|
||||
prediction = model.predict(image_array)
|
||||
# Convert the predictions to class labels
|
||||
predicted_label = class_names[prediction[0].argmax()]
|
||||
|
||||
# Print the predicted label
|
||||
print(predicted_label)
|
||||
return predicted_label
|
Before Width: | Height: | Size: 1.9 MiB After Width: | Height: | Size: 1.9 MiB |
Before Width: | Height: | Size: 170 KiB After Width: | Height: | Size: 170 KiB |
Before Width: | Height: | Size: 170 KiB After Width: | Height: | Size: 170 KiB |
Before Width: | Height: | Size: 165 KiB After Width: | Height: | Size: 165 KiB |
Before Width: | Height: | Size: 164 KiB After Width: | Height: | Size: 164 KiB |
Before Width: | Height: | Size: 29 KiB After Width: | Height: | Size: 29 KiB |
Before Width: | Height: | Size: 92 KiB After Width: | Height: | Size: 92 KiB |
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 16 KiB |
Before Width: | Height: | Size: 212 KiB After Width: | Height: | Size: 212 KiB |
Before Width: | Height: | Size: 227 KiB After Width: | Height: | Size: 227 KiB |
Before Width: | Height: | Size: 85 KiB After Width: | Height: | Size: 85 KiB |
Before Width: | Height: | Size: 49 KiB After Width: | Height: | Size: 49 KiB |
Before Width: | Height: | Size: 69 KiB After Width: | Height: | Size: 69 KiB |
Before Width: | Height: | Size: 135 KiB After Width: | Height: | Size: 135 KiB |
BIN
trained_model.h5
Normal file
@ -1,29 +0,0 @@
|
||||
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("./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
|
26
wyglad_drzewa
Normal file
@ -0,0 +1,26 @@
|
||||
moc_bohatera
|
||||
├── tak
|
||||
│ └── moc_moba
|
||||
│ ├── tak
|
||||
│ │ └── lvl_wiekszy_bohater
|
||||
│ │ ├── tak
|
||||
│ │ │ └── mob_jest_strelcem
|
||||
│ │ │ ├── tak: zmien_kierunek
|
||||
│ │ │ └── nie
|
||||
│ │ │ └── artefakt
|
||||
│ │ │ ├── tak
|
||||
│ │ │ │ └── zdrowie_bohatera
|
||||
│ │ │ │ ├── 100
|
||||
│ │ │ │ │ └── zdrowie_moba
|
||||
│ │ │ │ │ ├── 100: zmien_kierunek
|
||||
│ │ │ │ │ ├── 50: walcz
|
||||
│ │ │ │ │ └── 1: walcz
|
||||
│ │ │ │ ├── 50
|
||||
│ │ │ │ │ └── zdrowie_moba
|
||||
│ │ │ │ │ ├── 100: zmien_kierunek
|
||||
│ │ │ │ │ ├── 50: zmien_kierunek
|
||||
│ │ │ │ │ └── 1: walcz
|
||||
│ │ │ │ └── 1: zmien_kierunek
|
||||
│ │ │ └── nie: zmien_kierunek
|
||||
│ └── nie: zmien_kierunek
|
||||
└── nie: zmien_kierunek
|
BIN
zdjecia/ORK_ARCHER/ork_archer (1).jpg
Normal file
After Width: | Height: | Size: 94 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (1).png
Normal file
After Width: | Height: | Size: 406 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (10).jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (100).jpg
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (1000).jpg
Normal file
After Width: | Height: | Size: 6.6 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (1001).jpg
Normal file
After Width: | Height: | Size: 7.1 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (1002).jpg
Normal file
After Width: | Height: | Size: 5.4 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (1003).jpg
Normal file
After Width: | Height: | Size: 2.0 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (1004).jpg
Normal file
After Width: | Height: | Size: 5.7 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (1005).jpg
Normal file
After Width: | Height: | Size: 4.6 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (1006).jpg
Normal file
After Width: | Height: | Size: 5.4 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (1007).jpg
Normal file
After Width: | Height: | Size: 4.2 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (1008).jpg
Normal file
After Width: | Height: | Size: 5.1 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (1009).jpg
Normal file
After Width: | Height: | Size: 6.7 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (101).jpg
Normal file
After Width: | Height: | Size: 149 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (102).jpg
Normal file
After Width: | Height: | Size: 141 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (103).jpg
Normal file
After Width: | Height: | Size: 143 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (104).jpg
Normal file
After Width: | Height: | Size: 213 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (105).jpg
Normal file
After Width: | Height: | Size: 9.6 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (106).jpg
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (107).jpg
Normal file
After Width: | Height: | Size: 210 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (108).jpg
Normal file
After Width: | Height: | Size: 23 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (109).jpg
Normal file
After Width: | Height: | Size: 146 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (11).jpg
Normal file
After Width: | Height: | Size: 5.5 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (110).jpg
Normal file
After Width: | Height: | Size: 31 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (111).jpg
Normal file
After Width: | Height: | Size: 75 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (112).jpg
Normal file
After Width: | Height: | Size: 7.1 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (113).jpg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (114).jpg
Normal file
After Width: | Height: | Size: 21 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (115).jpg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (116).jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (117).jpg
Normal file
After Width: | Height: | Size: 5.2 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (118).jpg
Normal file
After Width: | Height: | Size: 89 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (119).jpg
Normal file
After Width: | Height: | Size: 482 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (12).jpg
Normal file
After Width: | Height: | Size: 7.6 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (120).jpg
Normal file
After Width: | Height: | Size: 7.5 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (121).jpg
Normal file
After Width: | Height: | Size: 5.7 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (122).jpg
Normal file
After Width: | Height: | Size: 7.1 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (123).jpg
Normal file
After Width: | Height: | Size: 4.7 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (124).jpg
Normal file
After Width: | Height: | Size: 8.4 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (125).jpg
Normal file
After Width: | Height: | Size: 5.7 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (126).jpg
Normal file
After Width: | Height: | Size: 6.6 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (127).jpg
Normal file
After Width: | Height: | Size: 7.5 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (128).jpg
Normal file
After Width: | Height: | Size: 5.3 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (129).jpg
Normal file
After Width: | Height: | Size: 7.3 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (13).jpg
Normal file
After Width: | Height: | Size: 23 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (130).jpg
Normal file
After Width: | Height: | Size: 6.6 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (131).jpg
Normal file
After Width: | Height: | Size: 6.8 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (132).jpg
Normal file
After Width: | Height: | Size: 6.7 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (133).jpg
Normal file
After Width: | Height: | Size: 7.4 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (134).jpg
Normal file
After Width: | Height: | Size: 4.0 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (135).jpg
Normal file
After Width: | Height: | Size: 369 KiB |
BIN
zdjecia/ORK_ARCHER/ork_archer (136).jpg
Normal file
After Width: | Height: | Size: 167 KiB |