Projekt_Si/app.py

119 lines
3.6 KiB
Python
Raw Normal View History

2024-03-13 23:25:49 +01:00
import pygame
import prefs
from classes.beerKeg import BeerKeg
from classes.coffeMachine import CoffeMachine
2024-04-15 10:43:01 +02:00
from classes.table import Table
2024-03-13 23:25:49 +01:00
from pygame.locals import K_w, K_s, K_a, K_d
from classes.cell import Cell
from classes.agent import Agent
2024-04-12 13:44:55 +02:00
from collections import deque
import threading
import time
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):
cell = Cell(i, j)
row.append(cell)
cells.append(row)
2024-03-19 11:20:31 +01:00
#test
2024-03-13 23:25:49 +01:00
# Na potrzeby prezentacji tworzę sobie prostokatne sciany na które nie da się wejść
x1 = 3
y1 = 6
2024-03-13 23:25:49 +01:00
for i in range(x1, x1+4):
for j in range(y1, y1+2):
cells[i][j].prepareTexture("sprites/wall.png")
2024-03-13 23:25:49 +01:00
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")
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
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):
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))
2024-03-13 23:25:49 +01:00
initBoard()
agent = Agent(prefs.SPAWN_POINT[0], prefs.SPAWN_POINT[1], cells)
2024-04-12 13:44:55 +02:00
#Wpisujemy miejsce w ktorym znajduje sie agent i miejsce docelowe do funkcji znajdujacej najkrotsza sciezke
2024-04-12 13:44:55 +02:00
start_cell = cells[5][5]
2024-04-15 22:39:31 +02:00
target_cell = cells[9][11]
shortest_path = agent.bfs(start_cell, target_cell, cells)
2024-04-12 13:44:55 +02:00
if shortest_path:
print("Sciezka: ", [(cell.X, cell.Y) for cell in shortest_path])
else:
print("Brak sciezki")
path = agent.convert_to_coordinates(shortest_path)
#osobny watek dla sciezki agenta zeby co *iles czasu* poruszal sie tam gdzie mowi path
def watekDlaSciezkiAgenta():
while True:
agent.sciezkaAgenta(agent,path)
time.sleep(1)
watek = threading.Thread(target=watekDlaSciezkiAgenta)
watek.daemon = True
watek.start()
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
# 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)
2024-03-13 23:25:49 +01:00
2024-03-13 23:25:49 +01:00
window.fill((255, 0, 0))
draw_grid(window, cells, agent)
agent.update(window)
2024-03-13 23:25:49 +01:00
pygame.display.update()
time.sleep(0.1)
2024-03-13 23:25:49 +01:00
pygame.quit()