Projekt_Si/app.py

253 lines
8.8 KiB
Python

import pygame
import prefs
from classes.beerKeg import BeerKeg
from classes.coffeMachine import CoffeMachine
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
from classes.cell import Cell
from classes.agent import Agent
from collections import deque
import threading
import time
import random
from classes.data.klient import Klient
from classes.data.klient import KlientCechy
import xml.etree.ElementTree as ET
from decisiontree import predict_client
from classes.Jimmy_Neuron.predict_image import predict_image
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)]
from classes.data.data_initializer import clients
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
cells = []
for i in range(prefs.GRID_SIZE):
row = []
for j in range(prefs.GRID_SIZE):
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
row.append(cell)
cells.append(row)
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")
cells[4][4].interactableItem = Table(cells[4][4], "Table")
cells[4][prefs.GRID_SIZE-5].interactableItem = Table(cells[4][prefs.GRID_SIZE-5], "Table")
cells[prefs.GRID_SIZE-5][4].interactableItem = Table(cells[prefs.GRID_SIZE-5][4], "Table")
cells[prefs.GRID_SIZE-5][prefs.GRID_SIZE-5].interactableItem = Table(cells[prefs.GRID_SIZE-5][prefs.GRID_SIZE-5], "Table")
# 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
# cells[9][7].waga = 2
# cells[10][6].waga = 2
# cells[7][7].waga = 2
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)
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))
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
def watekDlaSciezkiAgenta():
assigned = False
has_visited_beer_keg = False
has_visited_coffee = False
adult = False
time.sleep(3)
while True:
if len(path) > 0:
element = path.pop(0)
print(element)
if element == "left":
agent.rotate_left()
if element == "right":
agent.rotate_right()
if element == "forward":
agent.move_direction()
elif isinstance(element, tuple): # Check if it's a tuple indicating movement coordinates
x, y = element
agent.moveto(x, y)
if not assigned:
neighbors = agent.get_neighbors(agent.current_cell, agent.cells)
for neighbor in neighbors:
if neighbor == klient.current_cell:
if not assigned:
random_client_data = random.choice(clients)
glasses = predict_image(random_client_data.zdjecie)
prediction = predict_client(random_client_data, glasses)
print("\nClient data:")
print(random_client_data)
print("Prediction (Adult):", prediction)
assigned = True
if prediction == "Yes":
path_beer = agent.bfs2(6,6)
if path_beer:
path.extend(path_beer)
elif prediction == "No":
path_coffee = agent.bfs2(4,10)
if path_coffee:
path.extend(path_coffee)
if assigned:
if prediction == "Yes":
if not has_visited_beer_keg:
if agent.current_cell == cells[6][6]:
has_visited_beer_keg = True
move_back = agent.bfs2(target_x,target_y)
if move_back:
path.extend(move_back)
elif prediction == "No":
if not has_visited_coffee:
if agent.current_cell == cells[4][10]:
has_visited_coffee = True
move_back = agent.bfs2(target_x,target_y)
if move_back:
path.extend(move_back)
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():
if event.type == pygame.QUIT:
running = False
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 keys[K_q]:
agent.rotate_left()
if keys[K_e]:
agent.rotate_right()
if keys[K_r]:
agent.move_direction()
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()
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)
window.fill((255, 0, 0))
draw_grid(window, cells, agent)
agent.update(window)
klient.update(window)
pygame.display.update()
time.sleep(0.1)
pygame.quit()