feat added moving client
This commit is contained in:
parent
997564080e
commit
7aeea71cfc
62
app.py
62
app.py
@ -10,12 +10,24 @@ from collections import deque
|
||||
import threading
|
||||
import time
|
||||
import random
|
||||
from classes.data.klient import Klient
|
||||
|
||||
pygame.init()
|
||||
window = pygame.display.set_mode((prefs.WIDTH, prefs.HEIGHT))
|
||||
pygame.display.set_caption("Game Window")
|
||||
table_coords = [(4, 4), (4, prefs.GRID_SIZE-5), (prefs.GRID_SIZE-5, 4), (prefs.GRID_SIZE-5, prefs.GRID_SIZE-5)]
|
||||
|
||||
chosen_coords = random.choice(table_coords)
|
||||
chosen_index = random.randint(0, len(table_coords)-1)
|
||||
chosen_coords = table_coords[chosen_index]
|
||||
klientx_target = chosen_coords[0] + 1
|
||||
klienty_target = chosen_coords[1] + 1
|
||||
# klientx_target = 16
|
||||
# klienty_target = 16
|
||||
|
||||
print("klientx_target:", klientx_target)
|
||||
print("klienty_target:", klienty_target)
|
||||
|
||||
def initBoard():
|
||||
wall_probability = 0.001
|
||||
global cells
|
||||
@ -36,19 +48,9 @@ def initBoard():
|
||||
if random.random() < wall_probability:
|
||||
cell.prepareTexture("sprites/wall.png")
|
||||
cell.blocking_movement = True
|
||||
|
||||
# Wybierz kolor dla płytki na podstawie jej położenia
|
||||
row.append(cell)
|
||||
cells.append(row)
|
||||
|
||||
# Test
|
||||
# Na potrzeby prezentacji tworzę sobie prostokątne ściany na które nie da się wejść
|
||||
# x1 = 3
|
||||
# y1 = 6
|
||||
# for i in range(x1, x1+4):
|
||||
# for j in range(y1, y1+2):
|
||||
# cells[i][j].prepareTexture("sprites/wall.png")
|
||||
# cells[i][j].blocking_movement = True
|
||||
for i in range(prefs.GRID_SIZE):
|
||||
for j in range(prefs.GRID_SIZE):
|
||||
if i == 0 or j==0 or j==prefs.GRID_SIZE-1 or (i == prefs.GRID_SIZE-1 and j != 17):
|
||||
@ -92,8 +94,10 @@ def draw_grid(window, cells, agent):
|
||||
|
||||
initBoard()
|
||||
agent = Agent(prefs.SPAWN_POINT[0], prefs.SPAWN_POINT[1], cells)
|
||||
klient = Klient(prefs.GRID_SIZE-1, 17,cells)
|
||||
target_x, target_y = klientx_target-1, klienty_target
|
||||
|
||||
|
||||
target_x, target_y = 18, 18
|
||||
|
||||
def watekDlaSciezkiAgenta():
|
||||
time.sleep(3)
|
||||
@ -112,6 +116,41 @@ def watekDlaSciezkiAgenta():
|
||||
agent.moveto(x, y)
|
||||
time.sleep(1)
|
||||
|
||||
def watekDlaSciezkiKlienta():
|
||||
time.sleep(3)
|
||||
while True:
|
||||
if len(path2) > 0:
|
||||
element2 = path2.pop(0)
|
||||
print(element2)
|
||||
if element2 == "left":
|
||||
klient.rotate_left()
|
||||
if element2 == "right":
|
||||
klient.rotate_right()
|
||||
if element2 == "forward":
|
||||
klient.move_direction()
|
||||
elif isinstance(element2, tuple): # Check if it's a tuple indicating movement coordinates
|
||||
x, y = element2
|
||||
klient.moveto(x, y)
|
||||
|
||||
if klient.current_cell == cells[klientx_target][klienty_target]:
|
||||
klient.przyStoliku = True
|
||||
klient.stolik = klient.current_cell
|
||||
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
path2 = klient.bfs2(klientx_target, klienty_target)
|
||||
print("Najkrótsza ścieżka:", path2)
|
||||
watek = threading.Thread(target=watekDlaSciezkiKlienta)
|
||||
watek.daemon = True
|
||||
watek.start()
|
||||
|
||||
path = agent.bfs2(target_x, target_y)
|
||||
print("Najkrótsza ścieżka:", path)
|
||||
watek = threading.Thread(target=watekDlaSciezkiAgenta)
|
||||
watek.daemon = True
|
||||
watek.start()
|
||||
|
||||
running = True
|
||||
while running:
|
||||
for event in pygame.event.get():
|
||||
@ -156,6 +195,7 @@ while running:
|
||||
window.fill((255, 0, 0))
|
||||
draw_grid(window, cells, agent)
|
||||
agent.update(window)
|
||||
klient.update(window)
|
||||
pygame.display.update()
|
||||
time.sleep(0.1)
|
||||
|
||||
|
@ -1,16 +1,103 @@
|
||||
import pygame
|
||||
from rachunek import Rachunek
|
||||
from classes.cell import Cell
|
||||
import prefs
|
||||
import random
|
||||
import heapq
|
||||
from collections import deque
|
||||
|
||||
class Klient:
|
||||
def __init__(self,imie,nazwisko,wiek,ulubiony_posilek=None, restrykcje_dietowe=None):
|
||||
self.imie = imie
|
||||
self.nazwisko = nazwisko
|
||||
self.wiek = wiek
|
||||
def __init__(self,x,y,cells):
|
||||
self.sprite = pygame.image.load("sprites/klient.png").convert_alpha()
|
||||
self.sprite = pygame.transform.scale(self.sprite, (prefs.CELL_SIZE, prefs.CELL_SIZE))
|
||||
self.current_cell = cells[x][y]
|
||||
self.current_x = x
|
||||
self.current_y = y
|
||||
# self.imie = imie
|
||||
# self.nazwisko = nazwisko
|
||||
# self.wiek = wiek
|
||||
przyStoliku = False
|
||||
self.stolik = None
|
||||
self.rachunek = Rachunek(random.randint(1,1000))
|
||||
self.ulubiony_posilek = ulubiony_posilek
|
||||
self.restrykcje_dietowe = restrykcje_dietowe
|
||||
# self.rachunek = Rachunek(random.randint(1,1000))
|
||||
# self.ulubiony_posilek = ulubiony_posilek
|
||||
# self.restrykcje_dietowe = restrykcje_dietowe
|
||||
self.cells = cells
|
||||
self.X = x
|
||||
self.Y = y
|
||||
self.last_move_time = pygame.time.get_ticks()
|
||||
self.last_interact_time = pygame.time.get_ticks()
|
||||
self.last_update_time = pygame.time.get_ticks()
|
||||
self.direction = 0
|
||||
self.directionPOM = 0
|
||||
self.xPOM = x
|
||||
self.yPOM = y
|
||||
self.g_scores = {}
|
||||
|
||||
|
||||
self.textures = [
|
||||
pygame.image.load("sprites/klient.png").convert_alpha(),
|
||||
pygame.image.load("sprites/klient.png").convert_alpha(),
|
||||
pygame.image.load("sprites/klient.png").convert_alpha(),
|
||||
pygame.image.load("sprites/klient.png").convert_alpha()
|
||||
]
|
||||
|
||||
def update(self, surface):
|
||||
surface.blit(self.sprite, (self.current_cell.X * prefs.CELL_SIZE,
|
||||
self.current_cell.Y * prefs.CELL_SIZE))
|
||||
|
||||
|
||||
def moveto(self,x,y):
|
||||
if not self.cells[x][y].blocking_movement:
|
||||
self.current_cell = self.cells[x][y]
|
||||
self.moved=True
|
||||
self.last_move_time=pygame.time.get_ticks()
|
||||
print("Agent moved to x,y: ",x,y)
|
||||
else:
|
||||
print("Agent cannot move to this direction")
|
||||
|
||||
|
||||
def move_direction(self):
|
||||
if self.direction == 0 and pygame.time.get_ticks()-self.last_move_time > 125 and self.current_cell.Y < prefs.GRID_SIZE-1 and not self.cells[self.current_cell.X][self.current_cell.Y+1].blocking_movement:
|
||||
self.current_cell = self.cells[self.current_cell.X][self.current_cell.Y+1]
|
||||
self.moved=True
|
||||
self.last_move_time=pygame.time.get_ticks()
|
||||
if self.direction == 1 and pygame.time.get_ticks()-self.last_move_time > 125 and self.current_cell.X > 0 and not self.cells[self.current_cell.X-1][self.current_cell.Y].blocking_movement:
|
||||
self.current_cell = self.cells[self.current_cell.X-1][self.current_cell.Y]
|
||||
self.moved=True
|
||||
self.last_move_time=pygame.time.get_ticks()
|
||||
|
||||
if self.direction == 2 and pygame.time.get_ticks()-self.last_move_time > 125 and self.current_cell.Y > 0 and not self.cells[self.current_cell.X][self.current_cell.Y-1].blocking_movement:
|
||||
self.current_cell = self.cells[self.current_cell.X][self.current_cell.Y-1]
|
||||
self.moved=True
|
||||
self.last_move_time=pygame.time.get_ticks()
|
||||
|
||||
if self.direction == 3 and pygame.time.get_ticks()-self.last_move_time > 125 and self.current_cell.X < prefs.GRID_SIZE-1 and not self.cells[self.current_cell.X+1][self.current_cell.Y].blocking_movement:
|
||||
self.current_cell = self.cells[self.current_cell.X+1][self.current_cell.Y]
|
||||
self.moved=True
|
||||
self.last_move_time=pygame.time.get_ticks()
|
||||
|
||||
|
||||
def rotate_left(self):
|
||||
if pygame.time.get_ticks()-self.last_move_time > 125:
|
||||
self.direction +=1
|
||||
if self.direction==4:
|
||||
self.direction=0
|
||||
self.sprite = self.textures[self.direction]
|
||||
self.sprite = pygame.transform.scale(self.sprite, (prefs.CELL_SIZE, prefs.CELL_SIZE))
|
||||
self.last_move_time=pygame.time.get_ticks()
|
||||
print(self.direction)
|
||||
|
||||
def rotate_right(self):
|
||||
if pygame.time.get_ticks()-self.last_move_time > 125:
|
||||
self.direction-=1
|
||||
if self.direction==-1:
|
||||
self.direction=3
|
||||
self.sprite = self.textures[self.direction]
|
||||
self.sprite = pygame.transform.scale(self.sprite, (prefs.CELL_SIZE, prefs.CELL_SIZE))
|
||||
self.last_move_time=pygame.time.get_ticks()
|
||||
print(self.direction)
|
||||
|
||||
|
||||
|
||||
def zloz_zamowienie(self,zamowienie,stolik):
|
||||
if self.stolik is None:
|
||||
self.stolik = stolik
|
||||
@ -22,4 +109,93 @@ class Klient:
|
||||
|
||||
def __str__(self):
|
||||
return f"Klient: {self.imie} {self.nazwisko} {self.wiek}, ulubione Danie: {self.ulubiony_posilek}, restrykcje diet: {self.restrykcje_dietowe}"
|
||||
|
||||
def get_possible_moves(self):
|
||||
possible_moves = []
|
||||
|
||||
if self.directionPOM == 0: # Patrzy w dół
|
||||
possible_moves.append((0, 'left'))
|
||||
possible_moves.append((0, 'right'))
|
||||
if self.yPOM < prefs.GRID_SIZE - 1 and not self.cells[self.xPOM][self.yPOM + 1].blocking_movement:
|
||||
possible_moves.append((self.directionPOM, 'forward'))
|
||||
elif self.directionPOM == 1: # Patrzy w lewo
|
||||
possible_moves.append((1, 'left'))
|
||||
possible_moves.append((1, 'right'))
|
||||
if self.xPOM > 0 and not self.cells[self.xPOM - 1][self.yPOM].blocking_movement:
|
||||
possible_moves.append((self.directionPOM, 'forward'))
|
||||
elif self.directionPOM == 2: # Patrzy w górę
|
||||
possible_moves.append((2, 'left'))
|
||||
possible_moves.append((2, 'right'))
|
||||
if self.yPOM > 0 and not self.cells[self.xPOM][self.yPOM - 1].blocking_movement:
|
||||
possible_moves.append((self.directionPOM, 'forward'))
|
||||
elif self.directionPOM == 3: # Patrzy w prawo
|
||||
possible_moves.append((3, 'left'))
|
||||
possible_moves.append((3, 'right'))
|
||||
if self.xPOM < prefs.GRID_SIZE - 1 and not self.cells[self.xPOM + 1][self.yPOM].blocking_movement:
|
||||
possible_moves.append((self.directionPOM, 'forward'))
|
||||
|
||||
return possible_moves
|
||||
|
||||
def calculate_priority(self, el):
|
||||
return el[0]
|
||||
|
||||
def bfs2(self, target_x, target_y):
|
||||
visited = set()
|
||||
self.directionPOM = self.direction
|
||||
start_state = (self.current_cell.X, self.current_cell.Y, self.directionPOM)
|
||||
#print(start_state)
|
||||
queue = []
|
||||
heapq.heappush(queue, (0,(start_state, [], 0)))
|
||||
while queue:
|
||||
_, que = heapq.heappop(queue)
|
||||
state, actions, gscore = que
|
||||
self.xPOM, self.yPOM, self.directionPOM = state
|
||||
if self.xPOM == target_x and self.yPOM == target_y:
|
||||
return actions
|
||||
|
||||
if (self.xPOM, self.yPOM, self.directionPOM) in visited:
|
||||
continue
|
||||
|
||||
visited.add((self.xPOM, self.yPOM, self.directionPOM))
|
||||
|
||||
possible_moves = self.get_possible_moves()
|
||||
for new_direction, action in possible_moves:
|
||||
new_x, new_y = self.xPOM, self.yPOM
|
||||
new_actions = actions + [action]
|
||||
|
||||
if action == 'left':
|
||||
new_direction = (self.directionPOM + 1) % 4
|
||||
elif action == 'right':
|
||||
new_direction = (self.directionPOM - 1) % 4
|
||||
else: # forward
|
||||
if self.directionPOM == 0:
|
||||
new_y += 1
|
||||
elif self.directionPOM == 1:
|
||||
new_x -= 1
|
||||
elif self.directionPOM == 2:
|
||||
new_y -= 1
|
||||
else: # direction == 3
|
||||
new_x += 1
|
||||
|
||||
if 0 <= new_x < prefs.GRID_SIZE and 0 <= new_y < prefs.GRID_SIZE \
|
||||
and not self.cells[new_x][new_y].blocking_movement:
|
||||
new_state = (new_x, new_y, new_direction)
|
||||
|
||||
if (action == 'left' or action == 'right') :
|
||||
gscore = gscore + 1
|
||||
else:
|
||||
gscore = gscore + self.cells[new_x][new_y].waga
|
||||
|
||||
f_score = gscore + self.heuristic((new_x,new_y), (target_x,target_y))
|
||||
|
||||
heapq.heappush(queue, (f_score, (new_state, new_actions, gscore)))
|
||||
|
||||
|
||||
return []
|
||||
|
||||
def heuristic(self, current, target):
|
||||
# Manhattan distance heuristic
|
||||
dx = abs(current[0] - target[0])
|
||||
dy = abs(current[1] - target[1])
|
||||
return dx + dy
|
||||
|
||||
|
BIN
sprites/klient.png
Normal file
BIN
sprites/klient.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 649 B |
Loading…
Reference in New Issue
Block a user