Projekt_Si/app.py

240 lines
8.2 KiB
Python
Raw Permalink Normal View History

2024-03-13 23:25:49 +01:00
import pygame
2024-05-08 19:19:52 +02:00
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
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
import threading
import time
2024-05-26 10:12:10 +02:00
import random
2024-05-26 11:50:42 +02:00
from classes.data.klient import Klient
2024-06-12 00:24:57 +02:00
from classes.data.klient import KlientCechy
2024-06-11 18:14:54 +02:00
import xml.etree.ElementTree as ET
from decisiontree import predict_client
2024-06-12 00:24:57 +02:00
from classes.Jimmy_Neuron.predict_image import predict_image
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")
2024-06-23 22:03:39 +02:00
table_coords = [(12, 2), (16, 11), (4, 4), (15, 4), (2, 9), (15, 15), (12, 12), (7, 8), (4, 15)]
table_ordered = [(4, 4), (12, 2), (15, 4), (16, 11), (15, 15), (12, 12), (7, 8), (4, 15), (2, 9)]
2024-06-11 18:14:54 +02:00
from classes.data.data_initializer import clients
2024-03-13 23:25:49 +01:00
2024-06-23 22:03:39 +02:00
orders = []
chosen_index = 0
chosen_coords = table_ordered[chosen_index]
2024-05-26 11:50:42 +02:00
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)
2024-03-13 23:25:49 +01:00
def initBoard():
2024-05-26 10:12:10 +02:00
wall_probability = 0.001
2024-03-13 23:25:49 +01:00
global cells
cells = []
for i in range(prefs.GRID_SIZE):
row = []
for j in range(prefs.GRID_SIZE):
2024-05-26 10:12:10 +02:00
waga = random.choices([1, 4, 5], weights=[0.7, 0.1, 0.1], k=1)[0]
cell = Cell(i, j, waga)
if (i, j) not in table_coords:
if waga == 5:
cell.prepareTexture("sprites/plama.png")
if waga == 4:
cell.prepareTexture("sprites/dywan.png")
if random.random() < wall_probability:
cell.prepareTexture("sprites/wall.png")
cell.blocking_movement = True
2024-03-13 23:25:49 +01:00
row.append(cell)
cells.append(row)
2024-05-26 10:12:10 +02:00
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):
cells[i][j].prepareTexture("sprites/wall.png")
cells[i][j].blocking_movement = True
cells[6][6].interactableItem = BeerKeg(cells[6][6], "Beer Keg")
cells[4][10].interactableItem = CoffeMachine(cells[4][10], "Coffe Machine")
2024-03-13 23:25:49 +01:00
2024-06-23 22:03:39 +02:00
for cell in table_ordered:
cells[cell[0]][cell[1]].interactableItem = Table(cells[cell[0]][cell[1]], "Table")
2024-05-09 14:51:58 +02:00
2024-05-26 10:12:10 +02:00
# cells[9][9].waga = 2
# cells[9][8].waga = 10
# cells[8][8].waga = 10
# 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-26 10:12:10 +02:00
# cells[9][7].waga = 2
# cells[10][6].waga = 2
# cells[7][7].waga = 2
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-26 10:12:10 +02:00
cells[i][j].update(window)
if(cells[i][j].interactableItem):
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)
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-06-23 22:03:39 +02:00
klients = [ Klient(table_ordered[i][0]+ 1, table_ordered[i][1]+ 1, cells) for i in range(len(table_ordered)) ]
2024-05-26 11:50:42 +02:00
target_x, target_y = klientx_target-1, klienty_target
def watekDlaSciezkiAgenta():
2024-06-12 00:24:57 +02:00
assigned = False
2024-06-23 22:03:39 +02:00
global chosen_index
time.sleep(1.5)
while True:
if len(path) > 0:
2024-06-23 22:03:39 +02:00
print(path)
element = path.pop(0)
if element == "left":
agent.rotate_left()
if element == "right":
2024-05-08 19:19:52 +02:00
agent.rotate_right()
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-06-12 00:24:57 +02:00
if not assigned:
neighbors = agent.get_neighbors(agent.current_cell, agent.cells)
for neighbor in neighbors:
2024-06-23 22:03:39 +02:00
if neighbor == klients[chosen_index].current_cell and len(path) == 0:
2024-06-12 00:24:57 +02:00
if not assigned:
random_client_data = random.choice(clients)
glasses = predict_image(random_client_data.zdjecie)
print(random_client_data.zdjecie)
2024-06-12 00:24:57 +02:00
prediction = predict_client(random_client_data, glasses)
print("\nClient data:")
print(random_client_data)
print("Prediction (Glasses):", glasses)
2024-06-12 00:24:57 +02:00
print("Prediction (Adult):", prediction)
2024-06-23 22:03:39 +02:00
print("Prediction: ", prediction)
orders.append(prediction)
2024-06-12 00:24:57 +02:00
assigned = True
2024-06-23 22:03:39 +02:00
if assigned:
2024-06-23 22:03:39 +02:00
if(chosen_index < len(klients) - 1):
chosen_index += 1
target_x, target_y = table_ordered[chosen_index][0], table_ordered[chosen_index][1]+1
move_back = agent.bfs2(target_x,target_y)
if move_back:
path.extend(move_back)
assigned = False
else:
print("Orders: ", orders)
return
time.sleep(0.3)
# def watekDlaSciezkiKlienta():
# time.sleep(1)
# 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(0.5)
2024-06-12 00:24:57 +02:00
2024-05-26 11:50:42 +02:00
2024-06-23 22:03:39 +02:00
# path2 = klient.bfs2(klientx_target, klienty_target)
# print("Najkrótsza ścieżka:", path2)
# watek = threading.Thread(target=watekDlaSciezkiKlienta)
# watek.daemon = True
# watek.start()
2024-05-26 11:50:42 +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()
2024-06-12 00:24:57 +02:00
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-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()
if keys[K_r]:
agent.move_direction()
2024-05-08 19:19:52 +02:00
if keys[K_b]:
path = agent.bfs2(target_x, target_y)
print("Najkrótsza ścieżka:", path)
watek = threading.Thread(target=watekDlaSciezkiAgenta)
watek.daemon = True
watek.start()
2024-05-09 10:08:41 +02:00
2024-05-08 19:19:52 +02: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))
draw_grid(window, cells, agent)
agent.update(window)
2024-06-23 22:03:39 +02:00
for klient in klients:
klient.update(window)
2024-03-13 23:25:49 +01:00
pygame.display.update()
time.sleep(0.05)
2024-03-13 23:25:49 +01:00
pygame.quit()