2023-03-29 16:09:57 +02:00
|
|
|
import pygame
|
2023-03-29 16:58:24 +02:00
|
|
|
from config import *
|
2023-03-29 18:11:34 +02:00
|
|
|
from agent import *
|
2023-03-30 15:51:20 +02:00
|
|
|
from map_add_ons import *
|
2023-03-30 19:54:48 +02:00
|
|
|
from archer_ork import *
|
|
|
|
from infantry_ork import *
|
|
|
|
from infantry_ork2 import *
|
|
|
|
from sauron import *
|
2023-04-29 12:00:36 +02:00
|
|
|
from health_flower import *
|
2023-03-31 15:26:54 +02:00
|
|
|
#from unknown_mob import * #unknown mob
|
2023-03-29 16:09:57 +02:00
|
|
|
|
2023-03-29 18:11:34 +02:00
|
|
|
class Game:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
pygame.init()
|
2023-04-21 13:11:47 +02:00
|
|
|
self.state =[-1,-1,-1,-1,-1,-1,-1,-1]
|
2023-03-29 18:11:34 +02:00
|
|
|
self.SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
|
|
|
|
self.running = True
|
|
|
|
self.clock = pygame.time.Clock()
|
2023-03-30 19:54:48 +02:00
|
|
|
|
2023-03-30 15:51:20 +02:00
|
|
|
self.BACKGROUND_IMG= pygame.image.load("./zdjecia/podloze.jpg")
|
|
|
|
self.BACKGROUND = pygame.transform.scale(self.BACKGROUND_IMG,(64,64))
|
2023-03-29 18:11:34 +02:00
|
|
|
|
2023-04-18 22:15:23 +02:00
|
|
|
self.LVL_ICON_PNG = pygame.image.load("./zdjecia/lvl_icon.png")
|
|
|
|
self.LVL_ICON = pygame.transform.scale(self.LVL_ICON_PNG,(24,24))
|
|
|
|
|
2023-03-31 15:10:37 +02:00
|
|
|
pygame.display.set_caption('Gra-SI')
|
2023-03-29 18:11:34 +02:00
|
|
|
|
|
|
|
def new(self): # tworzy się nowa sesja grania
|
2023-03-30 15:51:20 +02:00
|
|
|
self.all_sprites = pygame.sprite.LayeredUpdates()
|
2023-03-30 20:10:29 +02:00
|
|
|
self.rock_sprites = pygame.sprite.LayeredUpdates()
|
2023-03-30 22:09:13 +02:00
|
|
|
self.archer_orks = pygame.sprite.LayeredUpdates()
|
|
|
|
self.infantry_orks = pygame.sprite.LayeredUpdates()
|
|
|
|
self.infantry_orks2 = pygame.sprite.LayeredUpdates()
|
|
|
|
self.sauronL = pygame.sprite.LayeredUpdates()
|
2023-04-29 12:00:36 +02:00
|
|
|
self.flowers = pygame.sprite.LayeredUpdates()
|
2023-03-31 15:26:54 +02:00
|
|
|
#self.unknown_mobs = pygame.sprite.LayeredUpdates() #unknown mob
|
2023-03-29 18:11:34 +02:00
|
|
|
self.agent = Agent(self,1,1)
|
2023-03-30 19:54:48 +02:00
|
|
|
self.archer_ork = Archer_ork(self,10,10)
|
2023-05-02 20:36:45 +02:00
|
|
|
self.enemy_cells.append(self.get_cell_number(self.archer_ork.x,self.archer_ork.y))
|
2023-03-31 12:01:43 +02:00
|
|
|
self.infantry_ork = Infantry_ork(self,10,4)
|
2023-05-02 20:36:45 +02:00
|
|
|
self.enemy_cells.append(self.get_cell_number(self.infantry_ork.x,self.infantry_ork.y))
|
2023-03-30 20:27:31 +02:00
|
|
|
self.infantry_ork2 = Infantry_ork2(self,6,3)
|
2023-05-02 20:36:45 +02:00
|
|
|
self.enemy_cells.append(self.get_cell_number(self.infantry_ork2.x,self.infantry_ork2.y))
|
2023-03-30 19:54:48 +02:00
|
|
|
self.sauron = Sauron(self, 1, 10)
|
2023-05-02 20:36:45 +02:00
|
|
|
self.enemy_cells.append(self.get_cell_number(self.sauron.x,self.sauron.y))
|
2023-04-29 12:00:36 +02:00
|
|
|
self.flower = Health_flower(self, 8,2)
|
2023-03-31 15:26:54 +02:00
|
|
|
#self.unknown_mob = Unknown_mob(self,8,8) #unknown mob
|
2023-03-30 15:51:20 +02:00
|
|
|
for y in range(5):
|
|
|
|
self.rock = Rocks(self,3,y)
|
2023-05-02 20:36:45 +02:00
|
|
|
self.wall_cells.append(self.get_cell_number(self.rock.x,self.rock.y))
|
2023-03-29 18:11:34 +02:00
|
|
|
|
2023-03-31 15:26:54 +02:00
|
|
|
|
2023-03-31 11:03:11 +02:00
|
|
|
def update(self):
|
2023-03-29 18:11:34 +02:00
|
|
|
self.all_sprites.update()
|
2023-03-30 20:10:29 +02:00
|
|
|
|
2023-03-29 18:11:34 +02:00
|
|
|
|
|
|
|
def events(self):
|
2023-03-29 16:09:57 +02:00
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
2023-03-29 18:11:34 +02:00
|
|
|
self.running = False
|
2023-03-29 16:09:57 +02:00
|
|
|
pygame.quit()
|
2023-05-02 20:36:45 +02:00
|
|
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
|
|
|
mouse_presses = pygame.mouse.get_pressed()
|
|
|
|
if mouse_presses[0]:
|
2023-05-03 13:29:17 +02:00
|
|
|
self.bfs()
|
2023-03-29 16:09:57 +02:00
|
|
|
|
2023-03-29 18:11:34 +02:00
|
|
|
def map(self): # tworzenie mapy
|
|
|
|
self.clock.tick(FRAMERATE)
|
|
|
|
for x in range(0, WIDTH, TILE_SIZE):
|
2023-04-18 21:38:22 +02:00
|
|
|
for y in range(0, 768, TILE_SIZE):
|
2023-03-30 15:51:20 +02:00
|
|
|
self.SCREEN.blit(self.BACKGROUND,(x,y))
|
2023-03-29 18:11:34 +02:00
|
|
|
self.rect = pygame.Rect(x, y, TILE_SIZE, TILE_SIZE)
|
2023-03-30 15:51:20 +02:00
|
|
|
pygame.draw.rect(self.SCREEN, BLACK, self.rect, 1)
|
|
|
|
self.all_sprites.draw(self.SCREEN)
|
|
|
|
self.rock_sprites.draw(self.SCREEN)
|
2023-04-18 22:15:23 +02:00
|
|
|
self.SCREEN.blit(self.LVL_ICON, (340 ,780))
|
2023-03-29 18:11:34 +02:00
|
|
|
pygame.display.update()
|
|
|
|
|
|
|
|
def main(self):
|
|
|
|
self.events()
|
|
|
|
self.update()
|
|
|
|
self.map()
|
2023-05-02 20:36:45 +02:00
|
|
|
|
2023-05-03 13:29:17 +02:00
|
|
|
# BFS ALGORITHM
|
|
|
|
|
2023-05-02 20:36:45 +02:00
|
|
|
open_queue = []
|
|
|
|
close_queue = []
|
|
|
|
wall_cells = []
|
|
|
|
enemy_cells = []
|
|
|
|
|
2023-05-03 13:29:17 +02:00
|
|
|
def bfs(self):
|
|
|
|
print("x: ", self.agent.x, "y: ", self.agent.y)
|
2023-05-02 20:36:45 +02:00
|
|
|
|
|
|
|
self.open_queue.append(self.get_cell_number(self.agent.x,self.agent.y))
|
2023-05-03 13:29:17 +02:00
|
|
|
# tutaj dodaje się cel agenta
|
|
|
|
goal_cell = self.get_cell_number(448,128)
|
2023-05-02 20:36:45 +02:00
|
|
|
|
|
|
|
path = []
|
|
|
|
processing = True
|
|
|
|
find_path = False
|
2023-05-03 13:29:17 +02:00
|
|
|
while processing: # główna pętla
|
2023-05-02 20:36:45 +02:00
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
exit()
|
|
|
|
|
|
|
|
if len(self.open_queue) > 0 :
|
2023-05-03 13:29:17 +02:00
|
|
|
current_node_cell = self.open_queue.pop(0)
|
2023-05-02 20:36:45 +02:00
|
|
|
|
|
|
|
if(current_node_cell in self.close_queue):
|
|
|
|
continue
|
|
|
|
|
2023-05-03 13:29:17 +02:00
|
|
|
print("Aktualna kratka: ", current_node_cell)
|
|
|
|
print("Cel znajduje się na kratce: ", goal_cell)
|
2023-05-02 20:36:45 +02:00
|
|
|
|
|
|
|
if (current_node_cell == goal_cell):
|
|
|
|
self.close_queue.append(current_node_cell)
|
|
|
|
found_goal_cell = current_node_cell
|
2023-05-03 13:29:17 +02:00
|
|
|
print("Znaleziono cel, szukanie drogi z odwiedzonych węzłów, kolejka odwiedzonych:", self.close_queue)
|
2023-05-02 20:36:45 +02:00
|
|
|
processing = False
|
|
|
|
find_path = True
|
2023-05-03 13:29:17 +02:00
|
|
|
self.clock.tick(2)
|
2023-05-02 20:36:45 +02:00
|
|
|
else:
|
2023-05-03 13:29:17 +02:00
|
|
|
child_node_cells = self.get_child_nodes(current_node_cell)
|
|
|
|
self.close_queue.append(current_node_cell)
|
|
|
|
print("Sąsiedzi: ", child_node_cells)
|
2023-05-02 20:36:45 +02:00
|
|
|
for child_node in child_node_cells:
|
|
|
|
if child_node not in self.open_queue and child_node not in self.close_queue:
|
2023-05-03 13:29:17 +02:00
|
|
|
self.open_queue.append(child_node)
|
|
|
|
print("Kolejka: ", self.open_queue, "\n")
|
2023-05-02 20:36:45 +02:00
|
|
|
else:
|
2023-05-03 13:29:17 +02:00
|
|
|
print("Brak nowych węzłów, kolejka: ",self. open_queue)
|
|
|
|
print("Odwiedzone : ", self.close_queue)
|
2023-05-02 20:36:45 +02:00
|
|
|
return self.close_queue
|
|
|
|
|
|
|
|
dead_end_nodes = []
|
|
|
|
while find_path:
|
2023-05-03 13:29:17 +02:00
|
|
|
path.append(self.close_queue[0])
|
2023-05-02 20:36:45 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-05-03 13:29:17 +02:00
|
|
|
if self.verify_move(from_cell, to_cell):
|
2023-05-02 20:36:45 +02:00
|
|
|
path.append(to_cell)
|
|
|
|
|
|
|
|
if path[-1] == found_goal_cell:
|
|
|
|
find_path = False
|
|
|
|
else:
|
|
|
|
dead_end_nodes.append(path[-1])
|
2023-05-03 13:29:17 +02:00
|
|
|
path = []
|
2023-05-02 20:36:45 +02:00
|
|
|
|
2023-05-03 13:29:17 +02:00
|
|
|
print("Droga: ", path)
|
2023-05-02 20:36:45 +02:00
|
|
|
self.move_agent(path)
|
|
|
|
|
|
|
|
|
2023-05-03 13:29:17 +02:00
|
|
|
def get_cell_number(self,x, y): #zamienia koordynaty na numer kratki
|
2023-05-02 20:36:45 +02:00
|
|
|
cell_number = None
|
|
|
|
cell_number =(x // TILE_SIZE) + (NUM_ROWS * (( y// TILE_SIZE)))
|
|
|
|
return cell_number
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-05-03 13:29:17 +02:00
|
|
|
|
2023-05-02 20:36:45 +02:00
|
|
|
def get_up_cell(self,cell_number):
|
2023-05-03 13:29:17 +02:00
|
|
|
cell_row_number = cell_number // NUM_ROWS
|
|
|
|
if (cell_row_number - 1 < 0):
|
2023-05-02 20:36:45 +02:00
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return (cell_number - NUM_ROWS)
|
|
|
|
|
|
|
|
def get_right_cell(self,cell_number):
|
2023-05-03 13:29:17 +02:00
|
|
|
cell_column_number = cell_number % NUM_ROWS
|
2023-05-02 20:36:45 +02:00
|
|
|
if (cell_column_number + 1 >= NUM_ROWS):
|
|
|
|
return None
|
|
|
|
else:
|
2023-05-03 13:29:17 +02:00
|
|
|
return (cell_number + 1)
|
2023-05-02 20:36:45 +02:00
|
|
|
|
|
|
|
def get_down_cell(self,cell_number):
|
2023-05-03 13:29:17 +02:00
|
|
|
cell_row_number = cell_number // NUM_ROWS
|
|
|
|
if (cell_row_number + 1 >= NUM_ROWS):
|
2023-05-02 20:36:45 +02:00
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return (cell_number + NUM_ROWS)
|
|
|
|
|
|
|
|
def get_left_cell(self,cell_number):
|
2023-05-03 13:29:17 +02:00
|
|
|
cell_column_number = cell_number % NUM_ROWS
|
2023-05-02 20:36:45 +02:00
|
|
|
if (cell_column_number - 1 < 0):
|
|
|
|
return None
|
|
|
|
else:
|
2023-05-03 13:29:17 +02:00
|
|
|
return (cell_number - 1)
|
2023-05-02 20:36:45 +02:00
|
|
|
|
2023-05-03 13:29:17 +02:00
|
|
|
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):
|
2023-05-02 20:36:45 +02:00
|
|
|
return False
|
|
|
|
|
2023-05-03 13:29:17 +02:00
|
|
|
if(from_cell + 1 == to_cell):
|
2023-05-02 20:36:45 +02:00
|
|
|
return True
|
|
|
|
|
2023-05-03 13:29:17 +02:00
|
|
|
if(from_cell - 1 == to_cell):
|
2023-05-02 20:36:45 +02:00
|
|
|
return True
|
|
|
|
|
2023-05-03 13:29:17 +02:00
|
|
|
if(from_cell - NUM_ROWS == to_cell):
|
2023-05-02 20:36:45 +02:00
|
|
|
return True
|
|
|
|
|
2023-05-03 13:29:17 +02:00
|
|
|
if(from_cell + NUM_ROWS == to_cell):
|
2023-05-02 20:36:45 +02:00
|
|
|
return True
|
|
|
|
|
2023-05-03 13:29:17 +02:00
|
|
|
return False
|
|
|
|
|
2023-05-02 20:36:45 +02:00
|
|
|
def move_agent(self,path):
|
|
|
|
for cell_to_move in path:
|
2023-05-03 13:29:17 +02:00
|
|
|
x, y = self.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.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.update()
|
|
|
|
self.map()
|
|
|
|
|
|
|
|
print("Położenie agenta: agent.x: ", self.agent.rect.x, ", agent.y: ", self.agent.rect.y)
|
2023-05-02 20:36:45 +02:00
|
|
|
self.clock.tick(2)
|
|
|
|
|
2023-05-03 13:29:17 +02:00
|
|
|
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
|
2023-05-02 20:36:45 +02:00
|
|
|
|
|
|
|
y = cell_row_number * TILE_SIZE
|
|
|
|
x = cell_column_number * TILE_SIZE
|
|
|
|
return x, y
|
|
|
|
|
2023-03-30 15:51:20 +02:00
|
|
|
|
2023-03-29 18:11:34 +02:00
|
|
|
g = Game()
|
|
|
|
g.new()
|
|
|
|
while g.running:
|
2023-03-31 11:03:11 +02:00
|
|
|
g.main()
|