diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..306f58e --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Current File", + "type": "python", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal", + "justMyCode": true + } + ] +} \ No newline at end of file diff --git a/Empty.py b/Empty.py new file mode 100644 index 0000000..8054b68 --- /dev/null +++ b/Empty.py @@ -0,0 +1,16 @@ +from Field import Field +from Global_variables import Global_variables as G_var +import pygame + + +class Empty(Field): + color = (188, 168, 139) + + def draw(self): + 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) diff --git a/Environment.py b/Environment.py index 11faee9..4d55c12 100644 --- a/Environment.py +++ b/Environment.py @@ -1,47 +1,72 @@ -import numpy as np +from Empty import Empty +from Moving_truck import Moving_truck +from Package import Package +from Package_types import Package_types +from Placed_package import Placed_package from Shelf import Shelf import pygame +import random from Grid import Grid -WINDOW_X = 1400 -WINDOW_Y = 750 -RECT_SIZE = 50 -RECT_COLOR = (70, 77, 87) +from Truck import Truck +from Global_variables import Global_variables as G_var +from pygame.constants import * class Environment: def __init__(self, window): self.window = window - self.grid = Grid(self.window) + self.grid = Grid(window) + self.initialize_eviroment_2d() + self.add_shelfs_to_enviroment_2d() + # TEST CREATE PACKAGE + new_package = Package(self.window, 26, 7) + self.enviroment_2d[26][7] = new_package + new_truck = Truck(window, 14, 7) + self.enviroment_2d[14][7] = new_truck + self.truck = new_truck + self.moving_truck = Moving_truck( + self.window, self.enviroment_2d, self.truck) - # draws grid&shelves - def draw_itself(self): - self.compute_coordinates_of_shelves() + def draw_all_elements(self): + for row in self.enviroment_2d: + for field in row: + field.draw() self.grid.draw_grid() + pygame.display.flip() - # computes shelves coordinates according to window size, might change later - def compute_coordinates_of_shelves(self): - matrix = self.create_data_world() - for idx, value in np.ndenumerate(matrix): - x = RECT_SIZE*idx[1] - y = RECT_SIZE*idx[0] - if value == 0: - pygame.draw.rect(self.window, RECT_COLOR, (x, y, RECT_SIZE, RECT_SIZE)) - for idx, value in np.ndenumerate(matrix): - x = RECT_SIZE*idx[1] - y = RECT_SIZE*idx[0] - if value == 1: - shelf = Shelf(self.window, x,y) - shelf.draw() + def update_truck(self, event): + if event.type == KEYDOWN: + if event.key == K_LEFT: + self.moving_truck.move(-1, 0) + if event.key == K_RIGHT: + self.moving_truck.move(1, 0) + if event.key == K_UP: + self.moving_truck.move(0, -1) + if event.key == K_DOWN: + self.moving_truck.move(0, 1) - def create_data_world(self): - matrix = np.zeros((16, 28)) - shelf_y = 0 - shelf_y1 = 9 + def gen_shelf_type(self): + shelve_types = list(Package_types) + while True: + yield random.choice(shelve_types) + def initialize_eviroment_2d(self): + self.enviroment_2d = [[ + Empty(self.window, j, i) + for i in range(G_var().DIMENSION_Y)] + for j in range(G_var().DIMENSION_X) + ] + + def add_shelfs_to_enviroment_2d(self): + shelf_2_offset = 9 + avaiable_types = self.gen_shelf_type() for x in range(2, 22, 3): - matrix[shelf_y][x] = 1 - matrix[shelf_y1][x] = 1 - print(matrix) - return matrix - - + type_of_new_shelf_1 = next(avaiable_types) + type_of_new_shelf_2 = next(avaiable_types) + for y in range(0, 6): + self.enviroment_2d[x][y] = Shelf( + self.window, x, y, type_of_new_shelf_1 + ) + self.enviroment_2d[x][y + shelf_2_offset] = Shelf( + self.window, x, (y + shelf_2_offset), type_of_new_shelf_2 + ) diff --git a/Field.py b/Field.py new file mode 100644 index 0000000..bf1ebef --- /dev/null +++ b/Field.py @@ -0,0 +1,5 @@ +class Field: + def __init__(self, window, x, y): + self.window = window + self.x = x + self.y = y diff --git a/Global_variables.py b/Global_variables.py new file mode 100644 index 0000000..9129f8e --- /dev/null +++ b/Global_variables.py @@ -0,0 +1,22 @@ +# Global Variables +class Global_variables(object): + _instance = None + + WINDOW_X = 1400 + WINDOW_Y = 750 + RECT_SIZE = 50 + DIMENSION_X = 28 + DIMENSION_Y = 15 + RECT_COLOR = (70, 77, 87) + SHELF_COLOR = (143, 68, 33) + + def __init__(self) -> None: + dim_x = 28 + dim_y = 15 + self.GRID = [["empty" for i in range(dim_x)] for j in range(dim_y)] + + def __new__(cls): + if cls._instance is None: + cls._instance = super(Global_variables, cls).__new__(cls) + + return cls._instance diff --git a/Grid.py b/Grid.py index d75a180..06e546d 100644 --- a/Grid.py +++ b/Grid.py @@ -1,8 +1,5 @@ import pygame -WINDOW_X = 1400 -WINDOW_Y = 750 -RECT_SIZE = 50 -RECT_COLOR = (70, 77, 87) +from Global_variables import Global_variables as G_var class Grid: @@ -12,8 +9,10 @@ class Grid: # function to draw a grid, it draws a line every 50px(RECT_SIZE) for both x and y axis def draw_grid(self): - for x in range(RECT_SIZE, WINDOW_X, RECT_SIZE): - pygame.draw.line(self.window, (255, 255, 255), (x, 0), (x, WINDOW_Y)) + for x in range(G_var().RECT_SIZE, G_var().WINDOW_X, G_var().RECT_SIZE): + pygame.draw.line(self.window, (255, 255, 255), + (x, 0), (x, G_var.WINDOW_Y)) - for y in range(RECT_SIZE, WINDOW_Y, RECT_SIZE): - pygame.draw.line(self.window, (255, 255, 255), (0, y), (WINDOW_X, y)) + for y in range(G_var().RECT_SIZE, G_var.WINDOW_Y, G_var().RECT_SIZE): + pygame.draw.line(self.window, (255, 255, 255), + (0, y), (G_var().WINDOW_X, y)) diff --git a/Moving_truck.py b/Moving_truck.py new file mode 100644 index 0000000..976efd4 --- /dev/null +++ b/Moving_truck.py @@ -0,0 +1,57 @@ +from Empty import Empty +from Package import Package +from Placed_package import Placed_package +from Shelf import Shelf + + +class Moving_truck: + def __init__(self, window, enviroment_2d, truck): + self.enviroment_2d = enviroment_2d + self.truck = truck + self.window = window + + def move(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] + 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 isinstance(field_to_move_to, Placed_package): + self.move_truck_with_package(x, y) + + def move_truck_with_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) + + 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.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) + + 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] + self.enviroment_2d[x1][y1].x, self.enviroment_2d[x2][y2].x = self.enviroment_2d[x2][y2].x, self.enviroment_2d[x1][y1].x + self.enviroment_2d[x1][y1].y, self.enviroment_2d[x2][y2].y = self.enviroment_2d[x2][y2].y, self.enviroment_2d[x1][y1].y + + def move_without_swapping(self, initial_x, initial_y, final_x, final_y): + self.enviroment_2d[final_x][final_y] = self.enviroment_2d[initial_x][initial_y] + self.enviroment_2d[final_x][final_y].x = final_x + self.enviroment_2d[final_x][final_y].y = final_y + self.enviroment_2d[initial_x][initial_y] = Empty( + self.window, initial_x, initial_y) diff --git a/Package.py b/Package.py index 5ddd535..e2b02b9 100644 --- a/Package.py +++ b/Package.py @@ -2,20 +2,16 @@ import numpy as np import glob2 import pygame import random -RECT_SIZE = 50 +from Field import Field +from Global_variables import Global_variables as G_var +from Package_types import Package_types -class Package: - def __init__(self, window): - self.window = window - self.length = np.random.randint(1, 3) * RECT_SIZE -1 - self.width = RECT_SIZE-1 - self.color = list(np.random.choice(range(256), size=3)) - self.x = 1251 - self.y = 351 - # self.mark = random.choice(['fragile', 'dont turn around', 'keep dry', 'glass']) +class Package(Field): + def __init__(self, window, x, y): + Field.__init__(self, window, x, y) self.mark_image = self.get_marking_photo() - self.block = pygame.Rect(self.x, self.y, self.width, self.length) + self.type = random.choice(list(Package_types)) def get_marking_photo(self): file_path_type = ["resources/package_markings/*.jpg"] @@ -25,6 +21,15 @@ class Package: return random_image def draw(self): - pygame.draw.rect(self.window, self.color, self.block) - pygame.display.flip() + self.color = self.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 diff --git a/Package_types.py b/Package_types.py new file mode 100644 index 0000000..5a3dcf3 --- /dev/null +++ b/Package_types.py @@ -0,0 +1,6 @@ +import enum + + +class Package_types(enum.Enum): + fragile = 1 + priority = 2 diff --git a/Placed_package.py b/Placed_package.py new file mode 100644 index 0000000..980d5c6 --- /dev/null +++ b/Placed_package.py @@ -0,0 +1,7 @@ +from Package import Package + + +class Placed_package(Package): + def __init__(self, package): + Package.__init__(self, package.window, package.x, package.y) + self.type = package.type diff --git a/Program.py b/Program.py index 4442039..03fc637 100644 --- a/Program.py +++ b/Program.py @@ -1,36 +1,21 @@ import pygame from pygame.locals import * -from Truck import Truck - - -WINDOW_X = 1400 -WINDOW_Y = 750 -RECT_SIZE = 50 +from Environment import Environment +from Global_variables import Global_variables as G_var class Program: def __init__(self): pygame.init() - self.window = pygame.display.set_mode((WINDOW_X, WINDOW_Y)) # decides window's size - self.track = Truck(self.window) - self.track.draw() + self.window = pygame.display.set_mode( + (G_var().WINDOW_X, G_var().WINDOW_Y)) # decides window's size + self.environment = Environment(self.window) def run(self): running = True while running: for event in pygame.event.get(): # integrating with keyboard - if event.type == KEYDOWN: - if event.key == K_LEFT: - self.track.move_left() - - if event.key == K_RIGHT: - self.track.move_right() - - if event.key == K_UP: - self.track.move_up() - - if event.key == K_DOWN: - self.track.move_down() - - elif event.type == QUIT: + if event.type == QUIT: running = False + self.environment.update_truck(event) + self.environment.draw_all_elements() diff --git a/Shelf.py b/Shelf.py index a6dfb5c..da24d15 100644 --- a/Shelf.py +++ b/Shelf.py @@ -1,15 +1,26 @@ import pygame +from Field import Field from Global_variables import Global_variables as G_var +from Package_types import Package_types -class Shelf: - def __init__(self, window, x, y): - self.window = window - self.width = G_var().RECT_SIZE - self.length = 6*G_var().RECT_SIZE - self.x = x - self.y = y - self.block = pygame.Rect(self.x, self.y, self.width, self.length) + +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 * + G_var().RECT_SIZE, G_var().RECT_SIZE, G_var().RECT_SIZE) def draw(self): - pygame.draw.rect(self.window, G_var().SHELF_COLOR, self.block) - # pygame.draw.line(self.window, (255, 255, 255), (self.x, self.y), (self.x, self.y + self.length)) + self.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, 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 diff --git a/Truck.py b/Truck.py index bd03a60..29c1f9c 100644 --- a/Truck.py +++ b/Truck.py @@ -1,51 +1,15 @@ import pygame -from Environment import Environment -from Package import Package +from Field import Field from Global_variables import Global_variables as G_var -class Truck: - def __init__(self, window, ): - self.window = window + +class Truck(Field): + def __init__(self, window, x, y): + Field.__init__(self, window, x, y) self.image = pygame.image.load("resources/truck.jpeg").convert() - self.x = 701 # (x,y) - position of the truck - self.y = 401 self.has_package = False - self.environment = Environment(window) - self.package = Package(self.window) - # self.speed # drawing the truck def draw(self): - self.environment.draw_itself() - self.package.draw() - self.window.blit(self.image, (self.x, self.y)) - pygame.display.flip() - - # moving the truck - def move_right(self): - self.x += G_var().RECT_SIZE - self.draw() - - def move_left(self): - self.x -= G_var().RECT_SIZE - self.draw() - - def move_up(self): - self.y -= G_var().RECT_SIZE - self.draw() - - def move_down(self): - self.y += G_var().RECT_SIZE - self.draw() - - # def collision_with_shelves(self,x,y): - # for row in self.environment.compute_coordinates_of_shelves(): - # if self.is_collision(x, y,row[0],row[1]): - # return True - # return False - # - # def is_collision(self, x1, y1, x2, y2): - # if x1 >= x2 and x1 <= x2 + RECT_SIZE: - # if y1 >= y2 and y1 <= y2 + 6*RECT_SIZE: - # return True - # + self.window.blit( + self.image, (self.x * G_var().RECT_SIZE, self.y * G_var().RECT_SIZE)) diff --git a/run.py b/run.py index ae0dc7d..5d3ca6f 100644 --- a/run.py +++ b/run.py @@ -1,4 +1,5 @@ from Program import Program +from Global_variables import Global_variables as G_var if __name__ == "__main__": program = Program()