change enviroment, moving vehicle, package and shelf types etc.
This commit is contained in:
parent
592281ea2b
commit
bcc40c37e1
16
.vscode/launch.json
vendored
Normal file
16
.vscode/launch.json
vendored
Normal file
@ -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
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
16
Empty.py
Normal file
16
Empty.py
Normal file
@ -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)
|
@ -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
|
from Shelf import Shelf
|
||||||
import pygame
|
import pygame
|
||||||
|
import random
|
||||||
from Grid import Grid
|
from Grid import Grid
|
||||||
WINDOW_X = 1400
|
from Truck import Truck
|
||||||
WINDOW_Y = 750
|
from Global_variables import Global_variables as G_var
|
||||||
RECT_SIZE = 50
|
from pygame.constants import *
|
||||||
RECT_COLOR = (70, 77, 87)
|
|
||||||
|
|
||||||
|
|
||||||
class Environment:
|
class Environment:
|
||||||
def __init__(self, window):
|
def __init__(self, window):
|
||||||
self.window = 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_all_elements(self):
|
||||||
def draw_itself(self):
|
for row in self.enviroment_2d:
|
||||||
self.compute_coordinates_of_shelves()
|
for field in row:
|
||||||
|
field.draw()
|
||||||
self.grid.draw_grid()
|
self.grid.draw_grid()
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
# computes shelves coordinates according to window size, might change later
|
def update_truck(self, event):
|
||||||
def compute_coordinates_of_shelves(self):
|
if event.type == KEYDOWN:
|
||||||
matrix = self.create_data_world()
|
if event.key == K_LEFT:
|
||||||
for idx, value in np.ndenumerate(matrix):
|
self.moving_truck.move(-1, 0)
|
||||||
x = RECT_SIZE*idx[1]
|
if event.key == K_RIGHT:
|
||||||
y = RECT_SIZE*idx[0]
|
self.moving_truck.move(1, 0)
|
||||||
if value == 0:
|
if event.key == K_UP:
|
||||||
pygame.draw.rect(self.window, RECT_COLOR, (x, y, RECT_SIZE, RECT_SIZE))
|
self.moving_truck.move(0, -1)
|
||||||
for idx, value in np.ndenumerate(matrix):
|
if event.key == K_DOWN:
|
||||||
x = RECT_SIZE*idx[1]
|
self.moving_truck.move(0, 1)
|
||||||
y = RECT_SIZE*idx[0]
|
|
||||||
if value == 1:
|
|
||||||
shelf = Shelf(self.window, x,y)
|
|
||||||
shelf.draw()
|
|
||||||
|
|
||||||
def create_data_world(self):
|
def gen_shelf_type(self):
|
||||||
matrix = np.zeros((16, 28))
|
shelve_types = list(Package_types)
|
||||||
shelf_y = 0
|
while True:
|
||||||
shelf_y1 = 9
|
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):
|
for x in range(2, 22, 3):
|
||||||
matrix[shelf_y][x] = 1
|
type_of_new_shelf_1 = next(avaiable_types)
|
||||||
matrix[shelf_y1][x] = 1
|
type_of_new_shelf_2 = next(avaiable_types)
|
||||||
print(matrix)
|
for y in range(0, 6):
|
||||||
return matrix
|
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
|
||||||
|
)
|
||||||
|
5
Field.py
Normal file
5
Field.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
class Field:
|
||||||
|
def __init__(self, window, x, y):
|
||||||
|
self.window = window
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
22
Global_variables.py
Normal file
22
Global_variables.py
Normal file
@ -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
|
15
Grid.py
15
Grid.py
@ -1,8 +1,5 @@
|
|||||||
import pygame
|
import pygame
|
||||||
WINDOW_X = 1400
|
from Global_variables import Global_variables as G_var
|
||||||
WINDOW_Y = 750
|
|
||||||
RECT_SIZE = 50
|
|
||||||
RECT_COLOR = (70, 77, 87)
|
|
||||||
|
|
||||||
|
|
||||||
class Grid:
|
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
|
# function to draw a grid, it draws a line every 50px(RECT_SIZE) for both x and y axis
|
||||||
def draw_grid(self):
|
def draw_grid(self):
|
||||||
|
|
||||||
for x in range(RECT_SIZE, WINDOW_X, RECT_SIZE):
|
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, WINDOW_Y))
|
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):
|
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), (WINDOW_X, y))
|
pygame.draw.line(self.window, (255, 255, 255),
|
||||||
|
(0, y), (G_var().WINDOW_X, y))
|
||||||
|
57
Moving_truck.py
Normal file
57
Moving_truck.py
Normal file
@ -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)
|
31
Package.py
31
Package.py
@ -2,20 +2,16 @@ import numpy as np
|
|||||||
import glob2
|
import glob2
|
||||||
import pygame
|
import pygame
|
||||||
import random
|
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:
|
class Package(Field):
|
||||||
def __init__(self, window):
|
def __init__(self, window, x, y):
|
||||||
self.window = window
|
Field.__init__(self, window, x, y)
|
||||||
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'])
|
|
||||||
self.mark_image = self.get_marking_photo()
|
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):
|
def get_marking_photo(self):
|
||||||
file_path_type = ["resources/package_markings/*.jpg"]
|
file_path_type = ["resources/package_markings/*.jpg"]
|
||||||
@ -25,6 +21,15 @@ class Package:
|
|||||||
return random_image
|
return random_image
|
||||||
|
|
||||||
def draw(self):
|
def draw(self):
|
||||||
pygame.draw.rect(self.window, self.color, self.block)
|
self.color = self.get_package_color(self.type)
|
||||||
pygame.display.flip()
|
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
|
||||||
|
6
Package_types.py
Normal file
6
Package_types.py
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import enum
|
||||||
|
|
||||||
|
|
||||||
|
class Package_types(enum.Enum):
|
||||||
|
fragile = 1
|
||||||
|
priority = 2
|
7
Placed_package.py
Normal file
7
Placed_package.py
Normal file
@ -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
|
31
Program.py
31
Program.py
@ -1,36 +1,21 @@
|
|||||||
import pygame
|
import pygame
|
||||||
from pygame.locals import *
|
from pygame.locals import *
|
||||||
from Truck import Truck
|
from Environment import Environment
|
||||||
|
from Global_variables import Global_variables as G_var
|
||||||
|
|
||||||
WINDOW_X = 1400
|
|
||||||
WINDOW_Y = 750
|
|
||||||
RECT_SIZE = 50
|
|
||||||
|
|
||||||
|
|
||||||
class Program:
|
class Program:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pygame.init()
|
pygame.init()
|
||||||
self.window = pygame.display.set_mode((WINDOW_X, WINDOW_Y)) # decides window's size
|
self.window = pygame.display.set_mode(
|
||||||
self.track = Truck(self.window)
|
(G_var().WINDOW_X, G_var().WINDOW_Y)) # decides window's size
|
||||||
self.track.draw()
|
self.environment = Environment(self.window)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
running = True
|
running = True
|
||||||
while running:
|
while running:
|
||||||
for event in pygame.event.get(): # integrating with keyboard
|
for event in pygame.event.get(): # integrating with keyboard
|
||||||
if event.type == KEYDOWN:
|
if event.type == QUIT:
|
||||||
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:
|
|
||||||
running = False
|
running = False
|
||||||
|
self.environment.update_truck(event)
|
||||||
|
self.environment.draw_all_elements()
|
||||||
|
31
Shelf.py
31
Shelf.py
@ -1,15 +1,26 @@
|
|||||||
import pygame
|
import pygame
|
||||||
|
from Field import Field
|
||||||
from Global_variables import Global_variables as G_var
|
from Global_variables import Global_variables as G_var
|
||||||
|
from Package_types import Package_types
|
||||||
|
|
||||||
class Shelf:
|
|
||||||
def __init__(self, window, x, y):
|
class Shelf(Field):
|
||||||
self.window = window
|
def __init__(self, window, x, y, type):
|
||||||
self.width = G_var().RECT_SIZE
|
Field.__init__(self, window, x, y)
|
||||||
self.length = 6*G_var().RECT_SIZE
|
self.type = type
|
||||||
self.x = x
|
self.color = self.get_shelf_color(self.type)
|
||||||
self.y = y
|
self.block = pygame.Rect(self.x * G_var().RECT_SIZE, self.y *
|
||||||
self.block = pygame.Rect(self.x, self.y, self.width, self.length)
|
G_var().RECT_SIZE, G_var().RECT_SIZE, G_var().RECT_SIZE)
|
||||||
|
|
||||||
def draw(self):
|
def draw(self):
|
||||||
pygame.draw.rect(self.window, G_var().SHELF_COLOR, self.block)
|
self.block = pygame.Rect(self.x * G_var().RECT_SIZE, self.y *
|
||||||
# pygame.draw.line(self.window, (255, 255, 255), (self.x, self.y), (self.x, self.y + self.length))
|
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
|
||||||
|
50
Truck.py
50
Truck.py
@ -1,51 +1,15 @@
|
|||||||
import pygame
|
import pygame
|
||||||
from Environment import Environment
|
from Field import Field
|
||||||
from Package import Package
|
|
||||||
from Global_variables import Global_variables as G_var
|
from Global_variables import Global_variables as G_var
|
||||||
|
|
||||||
class Truck:
|
|
||||||
def __init__(self, window, ):
|
class Truck(Field):
|
||||||
self.window = window
|
def __init__(self, window, x, y):
|
||||||
|
Field.__init__(self, window, x, y)
|
||||||
self.image = pygame.image.load("resources/truck.jpeg").convert()
|
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.has_package = False
|
||||||
self.environment = Environment(window)
|
|
||||||
self.package = Package(self.window)
|
|
||||||
# self.speed
|
|
||||||
|
|
||||||
# drawing the truck
|
# drawing the truck
|
||||||
def draw(self):
|
def draw(self):
|
||||||
self.environment.draw_itself()
|
self.window.blit(
|
||||||
self.package.draw()
|
self.image, (self.x * G_var().RECT_SIZE, self.y * G_var().RECT_SIZE))
|
||||||
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
|
|
||||||
#
|
|
||||||
|
Loading…
Reference in New Issue
Block a user