Inteligentny_Wozek/wozek.py

139 lines
5.0 KiB
Python
Raw Normal View History

2023-05-06 13:53:01 +02:00
import ekran
2023-05-06 14:54:04 +02:00
import pygame,math
2023-05-29 00:13:02 +02:00
# from packageList import listOfPackages
from ekran import lista_paczek
2023-05-29 00:13:02 +02:00
listOfPackages = lista_paczek
class Wozek(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
2023-05-06 13:53:01 +02:00
self.obecnyStan = None
2023-05-06 14:54:04 +02:00
# self.x = 55
# self.y = 55
self.x_change = 0
self.y_change = 0
self.height = 64
self.width = 64
self.image = pygame.image.load("images/pusty_wozek.png")
self.rect = self.image.get_rect()
# Credit: Forklift icons created by Smashicons - Flaticon
# https://www.flaticon.com/free-icons/forklift
2023-04-20 20:54:58 +02:00
self.__zainicjuj_stan_poczatkowy()
def draw(self):
2023-05-06 13:53:01 +02:00
ekran.screen.blit(self.image, (self.obecnyStan.x, self.obecnyStan.y))
# self.update_position(self.obecnyStan.x, self.obecnyStan.y)
2023-05-06 14:54:04 +02:00
#storage = ["none"] * 10
storage = []
max_size = 10
2023-05-06 14:54:04 +02:00
ln = len(storage)
2023-05-06 14:54:04 +02:00
def add_element(self, element):
if self.ln < self.max_size:
self.storage.append(element)
self.ln=self.ln+1
else:
print("I'm full!")
2023-05-06 14:54:04 +02:00
def remove_element(self):
if self.ln > 0:
element = self.storage.pop()
self.ln=self.ln-1
return element
else:
print("I'm empty!")
def update_position(self, x, y):
self.obecnyStan.x = x
self.obecnyStan.y = y
self.rect.topleft = (x, y)
2023-05-06 14:54:04 +02:00
def dynamic_wozek_picture(self):
if self.ln == 0:
self.image = pygame.image.load("images/pusty_wozek.png")
elif ((self.ln > 0) and (self.ln < 4)):
self.image = pygame.image.load("images/pelny_wozek_1_crate.png")
elif ((self.ln > 3) and (self.ln < 10)):
self.image = pygame.image.load("images/pelny_wozek_2_crates.png")
elif (self.ln == 10):
self.image = pygame.image.load("images/pelny_wozek_full_3_crates.png")
self.rect = self.image.get_rect()
2023-05-06 14:54:04 +02:00
def load_package(self):
2023-05-29 11:00:58 +02:00
#for package in listOfPackages.list:
# for package in listOfPackages:
# dist = math.sqrt((self.obecnyStan.x - package.x)**2 + (self.obecnyStan.y - package.y)**2)
# if dist <= 50:
# self.add_element(package)
# # listOfPackages.list.pop()
# listOfPackages.pop()
package = listOfPackages.pop()
dist = math.sqrt((self.obecnyStan.x - package.x)**2 + (self.obecnyStan.y - package.y)**2)
if dist <= 50:
self.add_element(package)
# listOfPackages.list.pop()
def load_packages_collision(self):
pass
# packages_collision = pygame.sprite.spritecollide(self, listOfPackages.package_group, False)
# if packages_collision:
# for package in packages_collision:
# self.add_element(package)
# listOfPackages.list.remove(package) # collision doesn't work, collision always happens
2023-05-06 14:54:04 +02:00
def drop_package(self):
package = self.remove_element()
if package is not None:
package.x = self.obecnyStan.x
2023-05-06 14:54:04 +02:00
package.y = self.obecnyStan.y
2023-05-29 11:00:58 +02:00
# listOfPackages.list.append(package)
listOfPackages.append(package)
2023-04-20 20:54:58 +02:00
def __zainicjuj_stan_poczatkowy(self):
from wyszukiwanie import Stan
2023-05-30 21:00:08 +02:00
self.obecnyStan = Stan(0, 0, 1)
2023-04-20 20:54:58 +02:00
2023-05-06 13:53:01 +02:00
def przemiesc_wozek_po_sciezce(self, sciezka):
kierunek_poprzedni = self.obecnyStan.kierunek
for wezel in sciezka:
self.obecnyStan = wezel.stan
kierunek_obecny = self.obecnyStan.kierunek
self.ustaw_wozek_w_kierunku(kierunek_obecny, kierunek_poprzedni)
kierunek_poprzedni = kierunek_obecny
ekran.odswiez_ekran(self)
pygame.time.wait(500)
def ustaw_wozek_w_kierunku(self, kierunek_obecny, kierunek_poprzedni):
2023-05-06 14:54:04 +02:00
if kierunek_poprzedni < kierunek_obecny:
# obrot w lewo
if kierunek_poprzedni == 0 and kierunek_obecny == 3:
self.image = pygame.transform.rotate(self.image, 90)
# obrot w prawo
else:
self.image = pygame.transform.rotate(self.image, -90)
elif kierunek_poprzedni > kierunek_obecny:
# obrot w prawo
if kierunek_poprzedni == 3 and kierunek_obecny == 0:
self.image = pygame.transform.rotate(self.image, -90)
# obrot w lewo
else:
self.image = pygame.transform.rotate(self.image, 90)
def picks_up_package(self):
paczka_pop = ekran.lista_paczek.pop()
ekran.lista_paczek_na_regalach.append(paczka_pop)
paczka_pop.is_in_move = True
self.storage.append(paczka_pop)
self.ln = self.ln + 1
def drops_package(self, paczka, reg, predictions):
paczka.update_position(reg.numerWiersza*70, reg.numerKolumny*70)
paczka.is_in_move = False
self.storage.pop()
self.ln = self.ln - 1
reg.put_package_on_the_regal(paczka, predictions)
2023-04-20 20:54:58 +02:00