2024-03-13 23:25:49 +01:00
|
|
|
import pygame
|
2024-05-08 19:19:52 +02:00
|
|
|
import prefs
|
2024-03-14 13:53:26 +01:00
|
|
|
from classes.beerKeg import BeerKeg
|
2024-03-14 14:22:16 +01:00
|
|
|
from classes.coffeMachine import CoffeMachine
|
2024-04-15 10:43:01 +02:00
|
|
|
from classes.table import Table
|
2024-05-08 19:50:30 +02:00
|
|
|
from pygame.locals import K_w, K_s, K_a, K_d, K_q, K_e, K_r, K_b,K_g
|
2024-03-13 23:25:49 +01:00
|
|
|
from classes.cell import Cell
|
|
|
|
from classes.agent import Agent
|
2024-04-12 13:44:55 +02:00
|
|
|
from collections import deque
|
2024-04-13 11:53:13 +02:00
|
|
|
import threading
|
|
|
|
import time
|
2024-05-08 19:50:30 +02:00
|
|
|
|
2024-03-13 23:25:49 +01:00
|
|
|
pygame.init()
|
|
|
|
window = pygame.display.set_mode((prefs.WIDTH, prefs.HEIGHT))
|
|
|
|
pygame.display.set_caption("Game Window")
|
|
|
|
|
|
|
|
def initBoard():
|
|
|
|
global cells
|
|
|
|
cells = []
|
|
|
|
for i in range(prefs.GRID_SIZE):
|
|
|
|
row = []
|
|
|
|
for j in range(prefs.GRID_SIZE):
|
2024-05-05 12:39:12 +02:00
|
|
|
cell = Cell(i, j, 1)
|
2024-05-09 10:08:41 +02:00
|
|
|
# Wybierz kolor dla płytki na podstawie jej położenia
|
|
|
|
if i == 0 or i == prefs.GRID_SIZE - 1 or j == 0 or j == prefs.GRID_SIZE - 1:
|
|
|
|
color = (100, 20, 20)
|
|
|
|
elif i == 1 or i == prefs.GRID_SIZE - 2 or j == 1 or j == prefs.GRID_SIZE - 2:
|
|
|
|
color = (20, 100, 20)
|
|
|
|
elif i == 2 or i == prefs.GRID_SIZE - 3 or j == 2 or j == prefs.GRID_SIZE - 3:
|
|
|
|
color = (20, 20, 100)
|
|
|
|
else:
|
|
|
|
color = (150, 200, 200)
|
|
|
|
cell.color = color
|
2024-03-13 23:25:49 +01:00
|
|
|
row.append(cell)
|
|
|
|
cells.append(row)
|
2024-03-14 13:53:26 +01:00
|
|
|
|
2024-05-08 19:50:30 +02:00
|
|
|
# Test
|
|
|
|
# Na potrzeby prezentacji tworzę sobie prostokątne ściany na które nie da się wejść
|
2024-03-13 23:25:49 +01:00
|
|
|
x1 = 3
|
2024-03-14 14:33:51 +01:00
|
|
|
y1 = 6
|
2024-03-13 23:25:49 +01:00
|
|
|
for i in range(x1, x1+4):
|
|
|
|
for j in range(y1, y1+2):
|
2024-05-08 19:19:52 +02:00
|
|
|
cells[i][j].prepareTexture("sprites/wall.png")
|
2024-03-13 23:25:49 +01:00
|
|
|
cells[i][j].blocking_movement = True
|
|
|
|
|
2024-03-14 14:33:51 +01:00
|
|
|
cells[6][4].interactableItem = BeerKeg(cells[6][4], "Beer Keg")
|
|
|
|
cells[4][10].interactableItem = CoffeMachine(cells[4][10], "Coffe Machine")
|
2024-04-15 10:43:01 +02:00
|
|
|
cells[9][10].interactableItem = Table(cells[9][10], "Table")
|
|
|
|
cells[8][2].interactableItem = Table(cells[8][2], "Table")
|
|
|
|
cells[6][2].interactableItem = Table(cells[6][2], "Table")
|
|
|
|
cells[4][2].interactableItem = Table(cells[4][2], "Table")
|
2024-03-13 23:25:49 +01:00
|
|
|
|
2024-05-09 14:51:58 +02:00
|
|
|
for cell in cells:
|
|
|
|
for cel in cell:
|
|
|
|
cel.waga = Agent.get_cost((cel.X, cel.Y), cells)
|
|
|
|
|
2024-05-05 12:39:12 +02:00
|
|
|
cells[9][9].waga = 2
|
2024-05-09 14:04:02 +02:00
|
|
|
cells[9][8].waga = 10
|
|
|
|
cells[8][8].waga = 10
|
2024-05-09 14:45:32 +02:00
|
|
|
cells[prefs.SPAWN_POINT[0]+1][prefs.SPAWN_POINT[1]].waga = 100
|
|
|
|
cells[prefs.SPAWN_POINT[0]][prefs.SPAWN_POINT[1]-1].waga = 100
|
2024-05-09 14:04:02 +02:00
|
|
|
|
2024-05-05 12:39:12 +02:00
|
|
|
cells[9][7].waga = 2
|
|
|
|
cells[10][6].waga = 2
|
|
|
|
cells[7][7].waga = 2
|
|
|
|
|
2024-03-14 14:50:46 +01:00
|
|
|
def draw_grid(window, cells, agent):
|
2024-03-13 23:25:49 +01:00
|
|
|
for i in range(prefs.GRID_SIZE):
|
|
|
|
for j in range(prefs.GRID_SIZE):
|
2024-05-09 10:08:41 +02:00
|
|
|
cell = cells[i][j]
|
|
|
|
color = cell.color
|
|
|
|
pygame.draw.rect(window, cell.color, (i*prefs.CELL_SIZE, j*prefs.CELL_SIZE, prefs.CELL_SIZE, prefs.CELL_SIZE))
|
2024-03-14 13:53:26 +01:00
|
|
|
if(cells[i][j].interactableItem):
|
2024-03-14 14:33:51 +01:00
|
|
|
cells[i][j].interactableItem.update(window)
|
2024-05-09 14:45:32 +02:00
|
|
|
if(not cells[i][j].blocking_movement):
|
|
|
|
cells[i][j].blit_text(cells[i][j].waga, i*50+6, j*52+6, 12,window)
|
2024-03-14 14:50:46 +01:00
|
|
|
font = pygame.font.SysFont('Comic Sans MS', 30)
|
|
|
|
scoreText = font.render("Score: {}".format(str(round(agent.score,2))), 1, (0, 0, 0))
|
|
|
|
multiplierText = font.render("Multiplier: {}".format(str(round(agent.multiplier,2))), 1, (0, 0, 0))
|
|
|
|
|
|
|
|
window.blit(scoreText, (0, 0))
|
|
|
|
window.blit(multiplierText, (0, 50))
|
2024-03-13 23:25:49 +01:00
|
|
|
|
|
|
|
initBoard()
|
2024-04-15 22:49:39 +02:00
|
|
|
agent = Agent(prefs.SPAWN_POINT[0], prefs.SPAWN_POINT[1], cells)
|
2024-04-12 13:44:55 +02:00
|
|
|
|
2024-05-08 19:50:30 +02:00
|
|
|
target_x, target_y = 9, 11
|
|
|
|
|
2024-05-04 13:30:12 +02:00
|
|
|
def watekDlaSciezkiAgenta():
|
|
|
|
time.sleep(3)
|
|
|
|
while True:
|
|
|
|
if len(path) > 0:
|
|
|
|
element = path.pop(0)
|
|
|
|
print(element)
|
2024-05-08 19:50:30 +02:00
|
|
|
if element == "left":
|
2024-05-04 13:30:12 +02:00
|
|
|
agent.rotate_left()
|
2024-05-08 19:50:30 +02:00
|
|
|
if element == "right":
|
2024-05-08 19:19:52 +02:00
|
|
|
agent.rotate_right()
|
2024-05-08 19:50:30 +02:00
|
|
|
if element == "forward":
|
2024-05-08 19:19:52 +02:00
|
|
|
agent.move_direction()
|
2024-05-09 11:50:45 +02:00
|
|
|
elif isinstance(element, tuple): # Check if it's a tuple indicating movement coordinates
|
|
|
|
x, y = element
|
|
|
|
agent.moveto(x, y)
|
2024-05-04 13:30:12 +02:00
|
|
|
time.sleep(1)
|
|
|
|
|
2024-03-13 23:25:49 +01:00
|
|
|
running = True
|
|
|
|
while running:
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
running = False
|
2024-05-08 19:19:52 +02:00
|
|
|
|
2024-03-13 23:25:49 +01:00
|
|
|
keys = pygame.key.get_pressed()
|
2024-05-08 19:50:30 +02:00
|
|
|
|
2024-03-13 23:25:49 +01:00
|
|
|
if keys[K_w] and not agent.moved:
|
|
|
|
agent.move_up()
|
|
|
|
if keys[K_s] and not agent.moved:
|
|
|
|
agent.move_down()
|
|
|
|
if keys[K_a] and not agent.moved:
|
|
|
|
agent.move_left()
|
|
|
|
if keys[K_d] and not agent.moved:
|
|
|
|
agent.move_right()
|
|
|
|
if not any([keys[K_w], keys[K_s], keys[K_a], keys[K_d]]):
|
|
|
|
agent.moved = False
|
2024-05-08 19:19:52 +02:00
|
|
|
|
2024-04-20 11:05:31 +02:00
|
|
|
if keys[K_q]:
|
|
|
|
agent.rotate_left()
|
|
|
|
if keys[K_e]:
|
|
|
|
agent.rotate_right()
|
2024-04-20 13:38:47 +02:00
|
|
|
|
|
|
|
if keys[K_r]:
|
|
|
|
agent.move_direction()
|
2024-05-08 19:19:52 +02:00
|
|
|
|
|
|
|
if keys[K_b]:
|
2024-05-08 19:50:30 +02:00
|
|
|
path = agent.bfs2(target_x, target_y)
|
|
|
|
print("Najkrótsza ścieżka:", path)
|
|
|
|
watek = threading.Thread(target=watekDlaSciezkiAgenta)
|
|
|
|
watek.daemon = True
|
|
|
|
watek.start()
|
|
|
|
|
|
|
|
if keys[K_g]:
|
2024-05-09 10:08:41 +02:00
|
|
|
path, cost = agent.astar((target_x, target_y), start_cost=0)
|
2024-05-08 19:50:30 +02:00
|
|
|
print("Shortest path:", path)
|
2024-05-09 10:08:41 +02:00
|
|
|
print("Total cost:", cost)
|
|
|
|
watek = threading.Thread(target=watekDlaSciezkiAgenta)
|
|
|
|
watek.daemon = True
|
|
|
|
watek.start()
|
|
|
|
|
2024-05-08 19:19:52 +02:00
|
|
|
|
2024-03-14 13:53:26 +01:00
|
|
|
if pygame.key.get_pressed()[pygame.K_e]:
|
|
|
|
if agent.current_cell.interactableItem and pygame.time.get_ticks() - agent.last_interact_time > 500:
|
|
|
|
agent.last_interact_time = pygame.time.get_ticks()
|
|
|
|
agent.current_cell.interactableItem.interact(agent)
|
2024-03-13 23:25:49 +01:00
|
|
|
|
|
|
|
window.fill((255, 0, 0))
|
2024-03-14 14:50:46 +01:00
|
|
|
draw_grid(window, cells, agent)
|
2024-03-14 14:33:51 +01:00
|
|
|
agent.update(window)
|
2024-03-13 23:25:49 +01:00
|
|
|
pygame.display.update()
|
2024-04-13 11:53:13 +02:00
|
|
|
time.sleep(0.1)
|
2024-03-13 23:25:49 +01:00
|
|
|
|
|
|
|
pygame.quit()
|