Zmiania podnoszenia paczki i przenoszenia paczki na półkę

This commit is contained in:
tonywesoly 2022-05-08 20:57:20 +02:00
parent 85d981a177
commit fa0191d02c
6 changed files with 59 additions and 46 deletions

View File

@ -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)

View File

@ -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]

View File

@ -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)

View File

@ -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)

View File

@ -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)

21
Types_colors.py Normal file
View File

@ -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