2019SZI-Projekt/UI/window.py

135 lines
4.4 KiB
Python
Raw Normal View History

import pygame as pg
2019-04-25 20:56:02 +02:00
import numpy as np
2019-04-25 18:25:37 +02:00
import random
2019-06-09 18:15:22 +02:00
from math import hypot
2019-06-09 18:28:37 +02:00
import itertools as it
2019-04-29 13:49:54 +02:00
from UI.grid import Grid, Node
2019-05-01 02:21:02 +02:00
from Logic.Apath import a_path
2019-04-08 12:16:50 +02:00
class Window():
2019-06-09 15:32:10 +02:00
def __init__(self, grid: Grid,start: (int,int),end: (int,int)):
2019-06-09 13:25:00 +02:00
2019-04-28 16:09:06 +02:00
self.start = start
self.end = end
self.grid = grid
2019-04-28 16:09:06 +02:00
# assign to variables for brevity
2019-04-08 01:15:47 +02:00
cols = self.grid.cols
rows = self.grid.rows
width = Node.r_width
height = Node.r_height
margin = Node.r_margin
2019-04-28 16:09:06 +02:00
screen_width = cols * (width + margin) + 2 * margin
screen_height = rows * (height + margin) + 2 * margin
self.screen = pg.display.set_mode([screen_width, screen_height])
self.end = False
self.clock = pg.time.Clock()
2019-04-28 16:09:06 +02:00
2019-06-09 15:32:10 +02:00
def random_trash(grid: Grid):
for x in range(len(grid.table)*2):
grid.generate_trash(random.randint(1,len(grid.table)-1),random.randint(1,len(grid.table)-1))
2019-04-28 16:09:06 +02:00
2019-04-25 18:25:37 +02:00
2019-06-09 15:32:10 +02:00
#generate trash
random_trash(grid)
2019-04-28 16:09:06 +02:00
grid.change_field(start[0], start[1], 1)
grid.change_field(end[0], end[1], 2)
2019-06-09 18:15:22 +02:00
garbage = grid.garbage_to_collect()
2019-06-09 15:32:10 +02:00
#all obstacles, remove from list objects to collect
obs = [3,5,6,7,8]
for x in garbage:
obs.remove(x)
#list of garbage to collect
to_collect = []
for x in garbage:
to_collect.extend(grid.get_trash_possition(x))
2019-06-10 09:04:10 +02:00
print("\n",(len(to_collect)-1)," garbage to collect.\n")
2019-06-09 13:25:00 +02:00
#sort list of tuples to get minimum distance betwen all of them
2019-06-09 23:23:00 +02:00
#fajnie jakby sie udalo to zrobic wydajniej ale narazie niech bedzie tak
2019-06-10 02:02:38 +02:00
#change to diagonal distance
def dist(x,y):
return max(abs(x[0] - y[0]), abs(x[1] - y[1]))
2019-06-09 23:23:00 +02:00
2019-06-09 18:15:22 +02:00
to_collect_sorted = []
2019-06-10 02:02:38 +02:00
visited = []
for p in to_collect:
path_distances =[]
#poprzednik
if(len(to_collect_sorted) == 0):
s = (0,0)
else:
s = to_collect_sorted[len(to_collect_sorted)-1]
for upoint in to_collect:
if upoint in visited :
path_distances.append(100000)
2019-06-10 02:29:08 +02:00
elif dist(s,upoint) >= dist(end,upoint):
path_distances.append(50000)
2019-06-10 02:02:38 +02:00
else:
path_distances.append(dist(s,upoint))
2019-06-09 18:28:37 +02:00
min_index = np.argmin(path_distances)
2019-06-10 02:02:38 +02:00
visited.append(to_collect[min_index])
to_collect_sorted.append(to_collect[min_index])
2019-06-09 18:28:37 +02:00
2019-06-09 18:15:22 +02:00
2019-06-09 13:25:00 +02:00
2019-06-09 15:32:10 +02:00
#window init
2019-06-09 13:25:00 +02:00
pg.init() # pylint: disable=no-member
# setup window
pg.display.set_caption('Inteligentna śmieciarka')
#draw starting map
self.grid.draw_map(self.screen)
2019-06-09 12:13:31 +02:00
2019-06-09 13:37:29 +02:00
#move of the truck
def move_truck(path):
for index, t in enumerate(path):
x,y =t
if index != 0:
self.grid.change_field(path[index-1][0],path[index-1][1],4)
self.grid.change_field(x, y, 1)
self.grid.draw_node(self.screen, path[index-1][0],path[index-1][1])
self.grid.draw_node(self.screen, x, y)
pg.time.delay(500)
2019-06-10 00:52:47 +02:00
if(x != end[0] and y != end[1]):
print("collected:",grid.table[x][y].house.trash[2],"on possition: (",x,",",y,") ", grid.table[x][y].house.trash_file)
2019-06-09 13:37:29 +02:00
2019-06-09 15:32:10 +02:00
#visit all points from to_collect
2019-06-09 18:15:22 +02:00
for ind, x in enumerate(to_collect_sorted):
2019-06-09 15:32:10 +02:00
2019-06-09 12:13:31 +02:00
if ind == 0:
array = [[self.grid.table[col][row] for row in range(cols)] for col in range(rows)]
path = a_path(array,(start[0],start[1]),(x[0],x[1]),obs)
2019-06-09 13:47:19 +02:00
#print("Path:",path)
2019-06-09 12:13:31 +02:00
else:
array = [[self.grid.table[col][row] for row in range(cols)] for col in range(rows)]
2019-06-09 18:15:22 +02:00
path = a_path(array,(to_collect_sorted[ind-1][0],to_collect_sorted[ind-1][1]),(x[0],x[1]),obs)
2019-06-09 13:47:19 +02:00
#print("Path:",path)
2019-06-09 13:37:29 +02:00
2019-06-09 12:13:31 +02:00
#draw movement of garbage truck
2019-06-09 13:37:29 +02:00
move_truck(path)
#last move
array = [[self.grid.table[col][row] for row in range(cols)] for col in range(rows)]
2019-06-09 18:15:22 +02:00
path = a_path(array,(to_collect_sorted[len(to_collect)-1][0],to_collect_sorted[len(to_collect)-1][1]),(end[0],end[1]),obs)
2019-06-09 13:47:19 +02:00
#print("Path:",path)
2019-06-09 13:37:29 +02:00
move_truck(path)
2019-04-28 16:13:41 +02:00
2019-04-15 12:54:29 +02:00
pg.quit() # pylint: disable=no-member