diff --git a/Environment.py b/Environment.py index 7567b59..baa29a2 100644 --- a/Environment.py +++ b/Environment.py @@ -34,11 +34,11 @@ class Environment: for field in row: field.draw() self.grid.draw_grid() - self.astar.draw_path(self.window) + # self.astar.draw_path(self.window) pygame.display.flip() def update_all_elements(self,event): - self.use_astar() + # self.use_astar() self.update_truck(event) diff --git a/Moving_truck.py b/Moving_truck.py index 01022f4..ff5eb06 100644 --- a/Moving_truck.py +++ b/Moving_truck.py @@ -16,34 +16,29 @@ class Moving_truck: if isinstance(field_to_move_to, Empty): self.swap_fields(truck_x, truck_y, truck_x+x, truck_y+y) elif isinstance(field_to_move_to, Package) and not field_to_move_to.is_placed: - self.move_truck_with_package(x, y) + self.pick_up_package(x, y) + elif isinstance(field_to_move_to, Shelf) and self.truck.has_package and field_to_move_to.type == self.truck.package_type: + self.move_package_to_shelf(x, y) - def move_truck_with_package(self, x, y): + def pick_up_package(self, x, y): truck_x = self.truck.x truck_y = self.truck.y - field_to_move_to = self.enviroment_2d[truck_x+x][truck_y+y] - field_to_move_package_to = self.enviroment_2d[truck_x+( - x*2)][truck_y+(y*2)] - if isinstance(field_to_move_package_to, Shelf) and field_to_move_to.type != field_to_move_package_to.type: - return - if isinstance(field_to_move_package_to, Shelf): - self.move_package_to_shelf(x, y) - else: - self.swap_fields(truck_x+x, truck_y+y, - truck_x+(x*2), truck_y+(y*2)) - self.swap_fields(truck_x, truck_y, truck_x+x, truck_y+y) + package_x = truck_x+x + package_y = truck_y+y + package = self.enviroment_2d[package_x][package_y] + self.truck.has_package = True + self.truck.package_type = package.type + self.move_without_swapping(truck_x, truck_y, package_x, package_y) + def move_package_to_shelf(self, x, y): truck_x = self.truck.x truck_y = self.truck.y - package = self.enviroment_2d[truck_x+x][truck_y+y] - # self.enviroment_2d[truck_x+x][truck_y + - # y] = Placed_package(package) + self.enviroment_2d[truck_x+x][truck_y+y] = Package( + self.window, truck_x+x, truck_y+y, self.truck.package_type) self.enviroment_2d[truck_x+x][truck_y + y].is_placed = True - self.move_without_swapping( - truck_x+x, truck_y+y, truck_x+(x*2), truck_y+(y*2)) - self.move_without_swapping(truck_x, truck_y, truck_x+x, truck_y+y) + self.truck.has_package = False def swap_fields(self, x1, y1, x2, y2): self.enviroment_2d[x1][y1], self.enviroment_2d[x2][y2] = self.enviroment_2d[x2][y2], self.enviroment_2d[x1][y1] diff --git a/Package.py b/Package.py index 1c6b1b2..67a1476 100644 --- a/Package.py +++ b/Package.py @@ -3,15 +3,17 @@ import pygame import random from Field import Field from Global_variables import Global_variables as G_var +from Types_colors import Types_colors from Package_types import Package_types import math class Package(Field): - def __init__(self, window, x, y): + def __init__(self, window, x, y, type=random.choice(list(Package_types)) + ): Field.__init__(self, window, x, y) self.mark_image = self.get_marking_photo() - self.type = random.choice(list(Package_types)) + self.type = type self.is_placed = False def get_marking_photo(self): @@ -22,24 +24,17 @@ class Package(Field): return random_image def draw(self): - self.color = self.get_package_color(self.type) + self.color = Types_colors.get_package_color(self.type) block = pygame.Rect(self.x * G_var().RECT_SIZE, self.y * G_var().RECT_SIZE, G_var().RECT_SIZE, G_var().RECT_SIZE) pygame.draw.rect(self.window, self.color, block) - def get_package_color(self, package_type): - color = (100, 50, 20) - if package_type == Package_types.fragile: - color = (255, 57, 32) - elif package_type == Package_types.priority: - color = (10, 34, 255) - return color - def choose_the_closest_shelf(self, shelves): array = [] for shelf in shelves: if shelf.type == self.type: - segment = math.sqrt((shelf.x - self.x)**2 + (shelf.y - self.y)**2) + segment = math.sqrt((shelf.x - self.x) ** + 2 + (shelf.y - self.y)**2) array.append((segment, shelf)) array.sort(key=lambda y: y[0]) print(array) diff --git a/Shelf.py b/Shelf.py index da24d15..c29c97f 100644 --- a/Shelf.py +++ b/Shelf.py @@ -2,25 +2,18 @@ import pygame from Field import Field from Global_variables import Global_variables as G_var from Package_types import Package_types +from Types_colors import Types_colors class Shelf(Field): def __init__(self, window, x, y, type): Field.__init__(self, window, x, y) self.type = type - self.color = self.get_shelf_color(self.type) - self.block = pygame.Rect(self.x * G_var().RECT_SIZE, self.y * + self.color = Types_colors.get_shelf_color(type) + self.rect = pygame.Rect(self.x * G_var().RECT_SIZE, self.y * G_var().RECT_SIZE, G_var().RECT_SIZE, G_var().RECT_SIZE) def draw(self): - self.block = pygame.Rect(self.x * G_var().RECT_SIZE, self.y * + self.rect = pygame.Rect(self.x * G_var().RECT_SIZE, self.y * G_var().RECT_SIZE, G_var().RECT_SIZE, G_var().RECT_SIZE) - pygame.draw.rect(self.window, self.color, self.block) - - def get_shelf_color(self, shelf_type): - color = (143, 68, 33) - if shelf_type == Package_types.fragile: - color = (191, 35, 15) - elif shelf_type == Package_types.priority: - color = (33, 46, 140) - return color + pygame.draw.rect(self.window, self.color, self.rect) \ No newline at end of file diff --git a/Truck.py b/Truck.py index 29c1f9c..f845457 100644 --- a/Truck.py +++ b/Truck.py @@ -1,6 +1,8 @@ import pygame from Field import Field from Global_variables import Global_variables as G_var +from Types_colors import Types_colors +from Package_types import Package_types class Truck(Field): @@ -8,8 +10,15 @@ class Truck(Field): Field.__init__(self, window, x, y) self.image = pygame.image.load("resources/truck.jpeg").convert() self.has_package = False + self.package_type = Package_types.fragile # drawing the truck def draw(self): + block = (self.x * G_var().RECT_SIZE, self.y * G_var().RECT_SIZE) self.window.blit( - self.image, (self.x * G_var().RECT_SIZE, self.y * G_var().RECT_SIZE)) + self.image, block) + if self.has_package: + package_color = Types_colors.get_package_color(self.package_type) + rect = pygame.Rect(self.x * G_var().RECT_SIZE, self.y * + G_var().RECT_SIZE, G_var().RECT_SIZE/2, G_var().RECT_SIZE/2) + pygame.draw.rect(self.window, package_color, rect) diff --git a/Types_colors.py b/Types_colors.py new file mode 100644 index 0000000..d01063d --- /dev/null +++ b/Types_colors.py @@ -0,0 +1,21 @@ +from Package_types import Package_types + + +class Types_colors(object): + @staticmethod + def get_package_color(package_type): + color = (100, 50, 20) + if package_type == Package_types.fragile: + color = (255, 57, 32) + elif package_type == Package_types.priority: + color = (10, 34, 255) + return color + + @staticmethod + def get_shelf_color(shelf_type): + color = (143, 68, 33) + if shelf_type == Package_types.fragile: + color = (191, 35, 15) + elif shelf_type == Package_types.priority: + color = (33, 46, 140) + return color \ No newline at end of file