276 lines
10 KiB
Python
276 lines
10 KiB
Python
import pygame
|
|
from config import *
|
|
from agent import *
|
|
from map_add_ons import *
|
|
from mobs import *
|
|
from bfs import *
|
|
from heapq import *
|
|
from nn 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()
|
|
|
|
|
|
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()
|
|
|
|
self.agent = Agent(self,1,1)
|
|
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.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.sauron = Sauron(self, 1, 10)
|
|
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.sauron.x,self.sauron.y))
|
|
self.flower = Health_flower(self, 8,2)
|
|
|
|
self.grass = Grass(self,0,2)
|
|
self.grass = Grass(self,1,2)
|
|
self.grass = Grass(self,0,3)
|
|
self.grass = Grass(self,1,3)
|
|
self.grass = Grass(self,0,4)
|
|
self.grass = Grass(self,1,4)
|
|
cost_cell_1000=[13,26,27,40]
|
|
for y in range(5):
|
|
self.rock = Rocks(self,3,y)
|
|
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.MOUSEBUTTONDOWN:
|
|
mouse_presses = pygame.mouse.get_pressed()
|
|
if mouse_presses[0]:
|
|
|
|
x = self.sauron.x
|
|
y = self.sauron.y
|
|
mob_image = self.sauron.SAURON_IMG
|
|
while True: #do poprawienia poprawne rozpoznawanie póki co nie będzie działać dobrze
|
|
prediction = self.prediction_road(x,y,mob_image)
|
|
if prediction == "SAURON":
|
|
x = self.infantry_ork.x
|
|
y = self.infantry_ork.y
|
|
mob_image = self.infantry_ork.INFANTRY_ORK_IMG
|
|
prediction = self.prediction_road(x,y,mob_image)
|
|
if prediction == "ORK_INFANTRY":
|
|
self.move_agent(self.bfs.bfs(self.bfs.get_cell_number(x,y)))
|
|
x = self.archer_ork.x
|
|
y = self.archer_ork.y
|
|
mob_image = self.archer_ork.ARCHER_ORK_IMG
|
|
prediction = self.prediction_road(x,y,mob_image)
|
|
if prediction == "ORK_ARCHER":
|
|
self.move_agent(self.bfs.bfs(self.bfs.get_cell_number(x,y)))
|
|
x = self.sauron.x
|
|
y = self.sauron.y
|
|
mob_image = self.sauron.SAURON_IMG
|
|
|
|
|
|
|
|
def prediction_road(self,x,y,mob_image):
|
|
mob_goal = (self.bfs.get_cell_number(x,y))
|
|
if self.bfs.get_up_cell(mob_goal) == None:
|
|
goal = self.bfs.get_down_cell(mob_goal)
|
|
else:
|
|
goal = self.bfs.get_up_cell(mob_goal)
|
|
self.move_agent(self.bfs.bfs(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.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
|
|
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 = []
|
|
|
|
|
|
cols, rows = 13,12
|
|
def get_circle(x,y):
|
|
return (x * TILE_SIZE + TILE_SIZE//2, y* TILE_SIZE + TILE_SIZE//2), TILE_SIZE//4
|
|
def get_rect(x,y):
|
|
return x*TILE_SIZE +1, y* TILE_SIZE +1, TILE_SIZE -2, TILE_SIZE -2
|
|
'''
|
|
def get_next_nodes(x,y):
|
|
check_next_node = lambda x, y:True if 0<= x < cols and 0<=y < rows else False
|
|
ways =[-1,0],[0,-1],[1,0],[0,1]
|
|
return [(grid[y + dy][x + dx], (x + dx, y + dy)) for dx, dy in ways if check_next_node(x + dx, y + dy)]
|
|
'''
|
|
def get_neighbours(x, y):
|
|
check_neighbour = lambda x, y: True if 0 <= x < cols and 0 <= y < rows else False
|
|
ways = [-1, 0], [0, -1], [1, 0], [0, 1]
|
|
return [(grid[y + dy][x + dx], (x + dx, y + dy)) for dx, dy in ways if check_neighbour(x + dx, y + dy)]
|
|
def heuristic(a, b):
|
|
return abs(a[0] - b[0]) + abs(a[1] - b[1])
|
|
def dijkstra(start, goal, graph):
|
|
queue = []
|
|
heappush(queue, (0, start))
|
|
cost_visited = {start: 0}
|
|
visited = {start: None}
|
|
|
|
while queue:
|
|
cur_cost, cur_node = heappop(queue)
|
|
if cur_node == goal:
|
|
break
|
|
|
|
neighbours = graph[cur_node]
|
|
for neighbour in neighbours:
|
|
neigh_cost, neigh_node = neighbour
|
|
new_cost = cost_visited[cur_node] + neigh_cost
|
|
|
|
if neigh_node not in cost_visited or new_cost < cost_visited[neigh_node]:
|
|
priority = new_cost + heuristic(neigh_node, goal)
|
|
heappush(queue, (priority, neigh_node))
|
|
cost_visited[neigh_node] = new_cost
|
|
visited[neigh_node] = cur_node
|
|
return visited
|
|
grid =['2229222222222',
|
|
'2229222222222',
|
|
'9929222222222',
|
|
'9929222222222',
|
|
'9929222222222',
|
|
'2222222222222',
|
|
'2222222222222',
|
|
'2222222222222',
|
|
'2222222222222',
|
|
'2222222222222',
|
|
'2222222222222',
|
|
'2222222222222'
|
|
]
|
|
grid = [[int(char) for char in string ] for string in grid]
|
|
|
|
|
|
graph ={}
|
|
for y, row in enumerate(grid):
|
|
for x, col in enumerate(row):
|
|
graph[(x, y)] = graph.get((x, y), []) + get_neighbours(x, y)
|
|
#print("graph 2 0",graph[(2,0)])
|
|
|
|
start = (1,1)
|
|
goal =(0,5)
|
|
queue =[]
|
|
heappush(queue, (0,start))
|
|
cost_visited = {start:0}
|
|
visited = {start: None}
|
|
goall=1
|
|
while goall==1:
|
|
if queue:
|
|
visited=dijkstra(start,goal,graph)
|
|
goall=0
|
|
|
|
path=[]
|
|
path_head, path_segment = goal, goal
|
|
while path_segment:
|
|
# print("path_segment: ",path_segment)
|
|
|
|
path_segment =visited[path_segment]
|
|
path.append(path_segment)
|
|
#print("path_head",path_head)
|
|
path.pop(len(path)-1)
|
|
path.reverse()
|
|
path_true=[]
|
|
bfss =Bfs(Game)
|
|
|
|
|
|
for i in path:
|
|
z=str(i)
|
|
#print("Z:",z)
|
|
x=z[1]
|
|
y=z[4]
|
|
x=int(x)*64
|
|
y=int(y)*64
|
|
a=bfss.get_cell_number(x,y)
|
|
path_true.append(a)
|
|
#print("path:",path)
|
|
#print("path_true:",path_true)
|
|
#bfss.move_agent(path_true)
|
|
|
|
|
|
|
|
|
|
|
|
g = Game()
|
|
g.new()
|
|
while g.running:
|
|
g.main()
|