2022-06-04 20:03:32 +02:00
|
|
|
# from collections import deque
|
|
|
|
from queue import PriorityQueue
|
2022-06-03 02:50:23 +02:00
|
|
|
|
2022-06-04 20:03:32 +02:00
|
|
|
from path_algorithms.a_star import a_star
|
|
|
|
# from path_algorithms.bfs import bfs
|
2022-06-04 21:52:30 +02:00
|
|
|
from rubbish import *
|
2022-06-07 03:05:19 +02:00
|
|
|
from tree import evaluate_values, trash_selection
|
2022-06-03 02:50:23 +02:00
|
|
|
from truck import Truck
|
|
|
|
from surface import *
|
|
|
|
|
|
|
|
RESOLUTION = 900
|
|
|
|
SIZE = 60
|
|
|
|
|
|
|
|
# matrix for display
|
2022-06-04 21:52:30 +02:00
|
|
|
matrix = [[0, 1, 1, 2, 2, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 3, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 3, 0, 2, 1, 5, 3, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 5, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[3, 3, 3, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
2022-06-03 02:50:23 +02:00
|
|
|
]
|
|
|
|
pygame.init()
|
|
|
|
screen = pygame.display.set_mode([RESOLUTION, RESOLUTION])
|
2022-06-04 21:52:30 +02:00
|
|
|
|
2022-06-03 02:50:23 +02:00
|
|
|
truck = Truck(screen)
|
|
|
|
surface_list = []
|
2022-06-04 21:52:30 +02:00
|
|
|
rubbish_list = []
|
2022-06-03 02:50:23 +02:00
|
|
|
# x and y are swapped on display in pygame
|
|
|
|
for i in range(15):
|
|
|
|
for j in range(15):
|
2022-06-04 21:52:30 +02:00
|
|
|
if matrix[i][j] == 0:
|
2022-06-04 18:10:23 +02:00
|
|
|
surface_list.append(Grass(screen, j * 60, i * 60))
|
2022-06-04 21:52:30 +02:00
|
|
|
if matrix[i][j] == 1:
|
|
|
|
surface_list.append(Sand(screen, j * 60, i * 60))
|
2022-06-03 02:50:23 +02:00
|
|
|
if matrix[i][j] == 2:
|
2022-06-04 18:10:23 +02:00
|
|
|
surface_list.append(Rock(screen, j * 60, i * 60))
|
2022-06-03 02:50:23 +02:00
|
|
|
if matrix[i][j] == 3:
|
2022-06-04 18:10:23 +02:00
|
|
|
surface_list.append(Water(screen, j * 60, i * 60))
|
2022-06-04 21:52:30 +02:00
|
|
|
if matrix[i][j] == 5:
|
|
|
|
surface_list.append(Grass(screen, j * 60, i * 60))
|
|
|
|
rubbish_list.append(Rubbish(screen, j * 60, i * 60))
|
2022-06-03 02:50:23 +02:00
|
|
|
|
|
|
|
path = []
|
2022-06-07 03:05:19 +02:00
|
|
|
run = 1
|
2022-06-03 02:50:23 +02:00
|
|
|
while True:
|
|
|
|
pygame.time.delay(500)
|
|
|
|
|
|
|
|
for i in surface_list:
|
|
|
|
i.draw_surface()
|
2022-06-04 21:52:30 +02:00
|
|
|
for i in rubbish_list:
|
|
|
|
i.draw_rubbish()
|
2022-06-03 02:50:23 +02:00
|
|
|
truck.draw_truck()
|
|
|
|
|
2022-06-07 03:05:19 +02:00
|
|
|
if run == 1:
|
|
|
|
# func(rubbish_list[0])
|
|
|
|
data = rubbish_list[0].data_for_decision_tree()
|
|
|
|
test = trash_selection(evaluate_values(data))
|
|
|
|
print(test)
|
|
|
|
run = 0
|
|
|
|
|
2022-06-04 21:52:30 +02:00
|
|
|
if rubbish_list and not path:
|
|
|
|
start = (truck.y / 60, truck.x / 60)
|
2022-06-03 02:50:23 +02:00
|
|
|
direction = truck.direction
|
2022-06-04 21:52:30 +02:00
|
|
|
currentRubbish = rubbish_list[0]
|
|
|
|
endpoint = (currentRubbish.y / 60, currentRubbish.x / 60)
|
2022-06-04 20:03:32 +02:00
|
|
|
# path = bfs(surface_list, endpoint).tree_search(deque(), start, direction)
|
|
|
|
path = a_star(surface_list, endpoint).tree_search(PriorityQueue(), start, direction)
|
2022-06-03 02:50:23 +02:00
|
|
|
|
|
|
|
if path:
|
|
|
|
action = path.pop(0)
|
|
|
|
if action == 'M':
|
|
|
|
truck.move()
|
|
|
|
else:
|
|
|
|
truck.change_direction(action)
|
2022-06-04 21:52:30 +02:00
|
|
|
if not path:
|
|
|
|
if rubbish_list:
|
|
|
|
print(rubbish_list.pop(0).x)
|
2022-06-03 02:50:23 +02:00
|
|
|
|
|
|
|
pygame.display.flip()
|
|
|
|
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
exit()
|