Projekt_Si/app.py

166 lines
4.5 KiB
Python

import pygame
import prefs
from classes.beerKeg import BeerKeg
from classes.coffeMachine import CoffeMachine
from pygame.locals import K_w, K_s, K_a, K_d
from classes.cell import Cell
from classes.agent import Agent
from collections import deque
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):
cell = Cell(i, j)
row.append(cell)
cells.append(row)
#test
# Na potrzeby prezentacji tworzę sobie prostokatne sciany 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
cells[6][4].interactableItem = BeerKeg(cells[6][4], "Beer Keg")
cells[4][10].interactableItem = CoffeMachine(cells[4][10], "Coffe Machine")
def draw_grid(window, cells, agent):
for i in range(prefs.GRID_SIZE):
for j in range(prefs.GRID_SIZE):
cells[i][j].update(window)
if(cells[i][j].interactableItem):
cells[i][j].interactableItem.update(window)
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))
initBoard()
def bfs(start, target, cells):
queue = deque([(start,[])])
visited = set()
while queue:
current, path = queue.popleft()
if current==target:
return path + [current]
if current in visited:
continue
visited.add(current)
for neighbor in get_neighbors(current, cells):
queue.append((neighbor, path + [current]))
return None
def get_neighbors(cell, cells):
neighbors = []
x, y = cell.X, cell.Y
if x > 0 and not cells[x-1][y].blocking_movement:
neighbors.append(cells[x-1][y])
if x < prefs.GRID_SIZE - 1 and not cells[x+1][y].blocking_movement:
neighbors.append(cells[x+1][y])
if y > 0 and not cells[x][y-1].blocking_movement:
neighbors.append(cells[x][y-1])
if y < prefs.GRID_SIZE - 1 and not cells[x][y+1].blocking_movement:
neighbors.append(cells[x][y+1])
return neighbors
#Wpisujemy miejsce w ktorym znajduje sie agent i miejsce docelowe
start_cell = cells[5][5]
target_cell = cells[10][10]
shortest_path = bfs(start_cell, target_cell, cells)
if shortest_path:
print("Sciezka: ", [(cell.X, cell.Y) for cell in shortest_path])
else:
print("Brak sciezki")
#oddaje tablice punktow jako sciezke agenta
def convert_to_coordinates(shortest_path):
coordinates = [(cell.X, cell.Y) for cell in shortest_path]
return coordinates
path = convert_to_coordinates(shortest_path)
#Wyjmuje pierwsze koordynaty do ruszenia agenta a potem usuwa je z listy
def pop_first_coordinates(coordinates):
if coordinates:
x, y = coordinates.pop(0)
return x, y
else:
print("Lista współrzędnych jest pusta.")
return None, None
agent = Agent(prefs.SPAWN_POINT[0], prefs.SPAWN_POINT[1], cells)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# takie głupie kontrolki do usunięcia potem, tylko do preznetacji
keys = pygame.key.get_pressed()
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
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)
if not agent.moved:
first_x, first_y = pop_first_coordinates(path)
if first_x is not None and first_y is not None:
agent.moveto(first_x,first_y)
window.fill((255, 0, 0))
draw_grid(window, cells, agent)
agent.update(window)
pygame.display.update()
pygame.quit()