add house class
This commit is contained in:
parent
0cf34ed5b2
commit
21b874d089
@ -15,7 +15,7 @@ class AStarNode():
|
|||||||
return self.position == other.position
|
return self.position == other.position
|
||||||
|
|
||||||
|
|
||||||
def a_path(table, start, end):
|
def a_path(table, start, end, obstacles):
|
||||||
|
|
||||||
# Create start and end node
|
# Create start and end node
|
||||||
start_node = AStarNode(None, start)
|
start_node = AStarNode(None, start)
|
||||||
@ -67,7 +67,7 @@ def a_path(table, start, end):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
# If not obstacle
|
# If not obstacle
|
||||||
if table[node_position[0]][node_position[1]].field_type == 3:
|
if table[node_position[0]][node_position[1]].field_type in obstacles:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
||||||
|
108
UI/grid.py
108
UI/grid.py
@ -1,5 +1,10 @@
|
|||||||
import pygame as pg
|
import pygame as pg
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
import random as rd
|
||||||
|
from os import listdir
|
||||||
|
from os.path import isfile, join
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Grid:
|
class Grid:
|
||||||
@ -20,9 +25,84 @@ class Grid:
|
|||||||
|
|
||||||
def change_field(self, row: int, col: int, f_type: int):
|
def change_field(self, row: int, col: int, f_type: int):
|
||||||
self.table[row][col].change_field_type(f_type)
|
self.table[row][col].change_field_type(f_type)
|
||||||
|
|
||||||
|
def generate_trash(self,row: int, col: int):
|
||||||
|
self.table[row][col].generate_trash()
|
||||||
|
|
||||||
def draw_node(self, screen, row: int, col: int):
|
def draw_node(self, screen, row: int, col: int):
|
||||||
self.table[row][col].draw(screen)
|
self.table[row][col].draw(screen)
|
||||||
|
|
||||||
|
def get_trash_possition(self, trash: int):
|
||||||
|
trash_possition = []
|
||||||
|
for row in self.table:
|
||||||
|
for node in row:
|
||||||
|
if node.field_type == trash and node.house.empty == False:
|
||||||
|
trash_possition.append((node.row,node.col))
|
||||||
|
return trash_possition
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class House:
|
||||||
|
# define some basic colors
|
||||||
|
BLACK = (0, 0, 0)
|
||||||
|
WHITE = (255, 255, 255)
|
||||||
|
GREEN = (0, 255, 0)
|
||||||
|
SKYBLUE = (0,191,255)
|
||||||
|
RED = (255, 0, 0)
|
||||||
|
BLUE = (0, 0, 255)
|
||||||
|
ORANGE = (255, 165, 0)
|
||||||
|
PINK = (199,21,133)
|
||||||
|
GREY = (192,192,192)
|
||||||
|
|
||||||
|
#define trash
|
||||||
|
paper = (5,WHITE,"paper")
|
||||||
|
glass = (6,SKYBLUE,"glass")
|
||||||
|
metal = (7,GREY,"metal")
|
||||||
|
plastic = (8,ORANGE,"plastic")
|
||||||
|
|
||||||
|
#define days of the week
|
||||||
|
MONDAY = (paper)
|
||||||
|
TUESDAY = (glass)
|
||||||
|
WEDNESDAY=(plastic,metal)
|
||||||
|
THURSDAY = (glass)
|
||||||
|
FRIDAY = (paper,metal)
|
||||||
|
SATURDAY = (plastic)
|
||||||
|
SUNDAY = (metal)
|
||||||
|
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.empty = True
|
||||||
|
self.trash = None
|
||||||
|
self.trash_file = None
|
||||||
|
|
||||||
|
def find_trash_file(self, trash):
|
||||||
|
trash_files_list = []
|
||||||
|
|
||||||
|
file_names = [f for f in listdir("Images\\TestImages") if isfile(join("Images\\TestImages", f))]
|
||||||
|
#filter names
|
||||||
|
for f in file_names:
|
||||||
|
if trash[2] in f:
|
||||||
|
trash_files_list.append(f)
|
||||||
|
|
||||||
|
f = rd.randint(0,len(trash_files_list))
|
||||||
|
return trash_files_list[f-1]
|
||||||
|
|
||||||
|
def generate_trash(self):
|
||||||
|
self.empty = False
|
||||||
|
num = rd.randint(1, 4)
|
||||||
|
if num == 1:
|
||||||
|
self.trash = self.paper
|
||||||
|
self.trash_file = self.find_trash_file(self.trash)
|
||||||
|
elif num == 2:
|
||||||
|
self.trash = self.glass
|
||||||
|
self.trash_file = self.find_trash_file(self.trash)
|
||||||
|
elif num == 3:
|
||||||
|
self.trash = self.metal
|
||||||
|
self.trash_file = self.find_trash_file(self.trash)
|
||||||
|
elif num == 4:
|
||||||
|
self.trash = self.plastic
|
||||||
|
self.trash_file = self.find_trash_file(self.trash)
|
||||||
|
|
||||||
|
|
||||||
class Node:
|
class Node:
|
||||||
@ -36,16 +116,22 @@ class Node:
|
|||||||
BLACK = (0, 0, 0)
|
BLACK = (0, 0, 0)
|
||||||
WHITE = (255, 255, 255)
|
WHITE = (255, 255, 255)
|
||||||
GREEN = (0, 255, 0)
|
GREEN = (0, 255, 0)
|
||||||
|
SKYBLUE = (0,191,255)
|
||||||
RED = (255, 0, 0)
|
RED = (255, 0, 0)
|
||||||
BLUE = (0, 0, 255)
|
BLUE = (0, 0, 255)
|
||||||
ORANGE = (255, 165, 0)
|
ORANGE = (255, 165, 0)
|
||||||
PINK = (199,21,133)
|
PINK = (199,21,133)
|
||||||
|
GREY = (192,192,192)
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, row: int, col: int,
|
def __init__(self, row: int, col: int,
|
||||||
field_type: int = 0):
|
field_type: int = 0):
|
||||||
self.row = row
|
self.row = row
|
||||||
self.col = col
|
self.col = col
|
||||||
self.field_type = field_type
|
self.field_type = field_type
|
||||||
|
self.house = House()
|
||||||
|
self.house.generate_trash()
|
||||||
|
print(self.house.trash,self.house.trash_file)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -63,19 +149,41 @@ class Node:
|
|||||||
(row * (height + margin)) + margin,
|
(row * (height + margin)) + margin,
|
||||||
width, height))
|
width, height))
|
||||||
pg.display.flip() # refresh screen
|
pg.display.flip() # refresh screen
|
||||||
|
|
||||||
|
def generate_trash(self):
|
||||||
|
self.house.generate_trash()
|
||||||
|
self.field_type = self.house.trash[0]
|
||||||
|
|
||||||
|
|
||||||
def change_field_type(self, field_type: int):
|
def change_field_type(self, field_type: int):
|
||||||
self.field_type = field_type
|
self.field_type = field_type
|
||||||
|
|
||||||
def get_field_color(self) -> tuple:
|
def get_field_color(self) -> tuple:
|
||||||
"""Gets the color tuple of field"""
|
"""Gets the color tuple of field"""
|
||||||
|
#base color
|
||||||
if self.field_type == 0:
|
if self.field_type == 0:
|
||||||
return self.GREEN
|
return self.GREEN
|
||||||
|
#truck color
|
||||||
elif self.field_type == 1:
|
elif self.field_type == 1:
|
||||||
return self.RED
|
return self.RED
|
||||||
|
#end point
|
||||||
elif self.field_type == 2:
|
elif self.field_type == 2:
|
||||||
return self.BLUE
|
return self.BLUE
|
||||||
|
#obstacles color
|
||||||
elif self.field_type == 3:
|
elif self.field_type == 3:
|
||||||
return self.ORANGE
|
return self.ORANGE
|
||||||
|
#path color
|
||||||
elif self.field_type == 4:
|
elif self.field_type == 4:
|
||||||
return self.PINK
|
return self.PINK
|
||||||
|
#paper
|
||||||
|
elif self.field_type == 5:
|
||||||
|
return self.WHITE
|
||||||
|
#glass
|
||||||
|
elif self.field_type == 6:
|
||||||
|
return self.SKYBLUE
|
||||||
|
#metal
|
||||||
|
elif self.field_type == 7:
|
||||||
|
return self.GREY
|
||||||
|
#plastic
|
||||||
|
elif self.field_type == 8:
|
||||||
|
return self.ORANGE
|
||||||
|
42
UI/window.py
42
UI/window.py
@ -36,7 +36,7 @@ class Window():
|
|||||||
def obstacles(self, grid: Grid,option: int):
|
def obstacles(self, grid: Grid,option: int):
|
||||||
if option == 1:
|
if option == 1:
|
||||||
for x in range(len(grid.table)*8):
|
for x in range(len(grid.table)*8):
|
||||||
grid.change_field(random.randint(1,len(grid.table)-2),random.randint(1,len(grid.table)-2),3)
|
grid.generate_trash(random.randint(1,len(grid.table)-1),random.randint(1,len(grid.table)-1))
|
||||||
elif option == 2:
|
elif option == 2:
|
||||||
for x in range (13):
|
for x in range (13):
|
||||||
grid.change_field(x,14,3)
|
grid.change_field(x,14,3)
|
||||||
@ -52,18 +52,32 @@ class Window():
|
|||||||
#draw starting map
|
#draw starting map
|
||||||
self.grid.draw_map(self.screen)
|
self.grid.draw_map(self.screen)
|
||||||
|
|
||||||
#copy table
|
#list of trash to collect
|
||||||
array = [[self.grid.table[col][row] for row in range(cols)] for col in range(rows)]
|
to_collect = grid.get_trash_possition(5)
|
||||||
path = a_path(array,(start[0],start[1]),(end[0],end[1]))
|
sorted(to_collect)
|
||||||
print("Path:",path)
|
print(to_collect)
|
||||||
|
|
||||||
|
for ind, x in enumerate(to_collect):
|
||||||
|
#copy table
|
||||||
|
if ind == 0:
|
||||||
|
array = [[self.grid.table[col][row] for row in range(cols)] for col in range(rows)]
|
||||||
|
obs = [3,8,6,7]
|
||||||
|
path = a_path(array,(start[0],start[1]),(x[0],x[1]),obs)
|
||||||
|
print("Path:",path)
|
||||||
|
else:
|
||||||
|
array = [[self.grid.table[col][row] for row in range(cols)] for col in range(rows)]
|
||||||
|
obs = [3,6,7,8]
|
||||||
|
path = a_path(array,(to_collect[ind-1][0],to_collect[ind-1][1]),(x[0],x[1]),obs)
|
||||||
|
print("Path:",path)
|
||||||
|
|
||||||
|
#draw movement of garbage truck
|
||||||
|
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)
|
||||||
|
|
||||||
#draw movement of garbage truck
|
|
||||||
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)
|
|
||||||
pg.quit() # pylint: disable=no-member
|
pg.quit() # pylint: disable=no-member
|
||||||
|
6
main.py
6
main.py
@ -6,9 +6,9 @@ import PyQt5.QtWidgets as QtWidgets
|
|||||||
def main():
|
def main():
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
app = QtWidgets.QApplication(sys.argv)
|
#app = QtWidgets.QApplication(sys.argv)
|
||||||
Settings()
|
#Settings()
|
||||||
sys.exit(app.exec_())
|
#sys.exit(app.exec_())
|
||||||
# initialize grid
|
# initialize grid
|
||||||
grid = Grid(20, 20)
|
grid = Grid(20, 20)
|
||||||
# initialize window (grid, start,end,mode)
|
# initialize window (grid, start,end,mode)
|
||||||
|
Loading…
Reference in New Issue
Block a user