From 35636a256c4d69f2f311b094e03f1259483c98a5 Mon Sep 17 00:00:00 2001 From: Neerka Date: Mon, 25 Mar 2024 11:21:23 +0100 Subject: [PATCH] czyszczenie + nowe funkcje i klasy --- QOLfunc.py | 33 ++++++++++++++--------- classes/Garbage.py | 60 +++++++++++++++++++++++++++++++++++++++++ classes/Garbagetruck.py | 14 ++++++---- classes/Household.py | 17 +++--------- classes/Junkyard.py | 17 ------------ classes/Tilemap.py | 2 +- classes/Trash.py | 48 +++++++++++++++++++++++++++++++++ classes/Trashcan.py | 12 --------- generators.py | 37 +++++++++---------------- main.py | 44 +++++++++--------------------- 10 files changed, 166 insertions(+), 118 deletions(-) create mode 100644 classes/Garbage.py delete mode 100644 classes/Junkyard.py create mode 100644 classes/Trash.py diff --git a/QOLfunc.py b/QOLfunc.py index fee400e..2e741e9 100644 --- a/QOLfunc.py +++ b/QOLfunc.py @@ -1,17 +1,24 @@ # Tutaj wrzucam jakieś funkcje różne, co by nie zaśmiecać tym maina jak nie trzeba -import random +from classes.Household import * + +colors = { + "white": (255, 255, 255), + "black": (0, 0, 0), + "red": (255, 0, 0) +} -def chooseNeighbours(count: int, i: int) -> set: - """ - :param count: ile sąsiadów ma wybrać - :param i: id obiektu, żeby nie był sąsiadem sam ze sobą - :return: zbiór id wybranych losowo sąsiadów obiektu - """ - neigh = set() - while len(neigh) < count: - num = random.randint(0, 7) - if num != i: - neigh.add(num) - return neigh +def extractPredicts(houses: list) -> set: + preds = set() + for house in houses: + temp = [house, house.getGarbage().getPredict()] + preds.add(temp) + return preds + +def distance(object1, object2) -> int: + lok1 = object1.getPosition() + lok2 = object2.getPosition() + + dist = abs(lok1[0]-lok2[0]) + abs(lok1[1]-lok2[1]) + return dist diff --git a/classes/Garbage.py b/classes/Garbage.py new file mode 100644 index 0000000..969c8f1 --- /dev/null +++ b/classes/Garbage.py @@ -0,0 +1,60 @@ +import random +from classes.Trash import * + + +class Garbage: + def __init__(self): + self.content = [] + self.predict = [] + self.prob: float = 0.3 + + def getContent(self): + return self.content + + def setContent(self, content): + self.content = content + return self + + def addContent(self, item: Trash): + self.content.append(item) + return self + + def removeContent(self, item: int): + return self.content.pop(item) + + def getPredict(self): + return self.predict + + def setPredict(self, predict): + self.predict = predict + return self + + def addPredict(self, item): + self.predict.append(item) + return self + + def getProb(self): + return self.prob + + def setProb(self, prob): + self.prob = prob + return self + + def generatePredict(self, i=random.choice([2, 3])): + if i < 0: + self.predict.pop(i) + else: + for _ in range(i): + possible = [Papier(), MetalPlastik(), Szklo(), Mixed(), Bio()] + traf = random.choice(possible) + self.predict.append(traf) + for bruh in possible: + if bruh != traf: + del bruh + + + def generateContent(self): + mod = random.choice([1, -1]) + self.setContent(self.predict[:]) + if random.random() < self.prob: + self.generatePredict(mod) diff --git a/classes/Garbagetruck.py b/classes/Garbagetruck.py index dfac802..ca08325 100644 --- a/classes/Garbagetruck.py +++ b/classes/Garbagetruck.py @@ -2,11 +2,6 @@ import pygame class Garbagetruck: - image: object - position: object - trashweight: int - trash: list - capacity: int def __init__(self, file="sprites/garbage_truck.png"): self.capacity: int = 20 @@ -15,11 +10,20 @@ class Garbagetruck: self.image = pygame.image.load(file).convert_alpha() self.image = pygame.transform.scale(self.image, (32,32)) self.position = [800, 500] + self.predicts: set = set() + self.state = None self.route = None self.scanner = None self.planner = None self.driver = None + def setPredicts(self, predicts): + self.predicts = predicts + return self + + def getPredicts(self): + return self.predicts + def getCapacity(self) -> int: return self.capacity diff --git a/classes/Household.py b/classes/Household.py index da68e62..fce9024 100644 --- a/classes/Household.py +++ b/classes/Household.py @@ -1,21 +1,12 @@ +from classes.Garbage import * + + class Household: def __init__(self): self.id: int = 0 self.image: object = None self.position: tuple = None - self.neighbours: list = [] - self.garbage: object = None - - def setNeighbours(self, neighbours): - self.neighbours = neighbours - return self - - def addNeighbour(self, neighbour): - self.neighbours.append(neighbour) - return self - - def getNeighbours(self): - return self.neighbours + self.garbage: object = Garbage() def setPosition(self, position: tuple): self.position = position diff --git a/classes/Junkyard.py b/classes/Junkyard.py deleted file mode 100644 index c3445a0..0000000 --- a/classes/Junkyard.py +++ /dev/null @@ -1,17 +0,0 @@ -class Junkyard: - def __init__(self): - self.id = 0 - self.position = (1400, 500) - self.neighbours = [] - - def getPosition(self): - return self.position - - def addNeighbour(self, neighbour): - self.neighbours.append(neighbour) - - def getNeighbours(self): - return self.neighbours - - def getId(self): - return self.id diff --git a/classes/Tilemap.py b/classes/Tilemap.py index 7c917a9..8acc24c 100644 --- a/classes/Tilemap.py +++ b/classes/Tilemap.py @@ -8,7 +8,7 @@ class Tilemap: self.size = size self.tileset = tileset self.map = np.zeros(size, dtype=int) - h, w = self.size + w, h = self.size self.image = pygame.Surface((32*w, 32*h)) if rect: self.rect = pygame.Rect(rect) diff --git a/classes/Trash.py b/classes/Trash.py new file mode 100644 index 0000000..f883c19 --- /dev/null +++ b/classes/Trash.py @@ -0,0 +1,48 @@ +typelist = ["paper", "metals_and_plastics", "mixed", "bio_waste", "glass"] + + +class Trash: + + def getWaga(self): + return self.waga + + def setWaga(self, waga): + self.waga = waga + return self + + def getImage(self): + return self.image + + def setImage(self, image): + self.image = image + return self + + +class Papier(Trash): + def __init__(self): + self.waga = 2 + self.image = None + + +class MetalPlastik(Trash): + def __init__(self): + self.waga = 3 + self.image = None + + +class Mixed(Trash): + def __init__(self): + self.waga = 1 + self.image = None + + +class Bio(Trash): + def __init__(self): + self.waga = 2 + self.image = None + + +class Szklo(Trash): + def __init__(self): + self.waga = 5 + self.image = None diff --git a/classes/Trashcan.py b/classes/Trashcan.py index 1e40209..4e3c3e3 100644 --- a/classes/Trashcan.py +++ b/classes/Trashcan.py @@ -4,18 +4,6 @@ class Trashcan: self.image: object = None self.position: tuple = None self.trashtype: str = None - self.neighbours: list = [] - - def setNeighbours(self, neighbours: list): - self.neighbours = neighbours - return self - - def addNeighbour(self, neighbour: object): - self.neighbours.append(neighbour) - return self - - def getNeighbours(self): - return self.neighbours def setPosition(self, position: tuple): self.position = position diff --git a/generators.py b/generators.py index e171618..dd27f71 100644 --- a/generators.py +++ b/generators.py @@ -1,19 +1,20 @@ -from classes.Junkyard import * from classes.Trashcan import * from classes.Household import * from classes.Garbagetruck import * +from classes.Tilemap import * +from classes.Tileset import * from QOLfunc import * import pygame -def trashcanGenerator(junkyard: Junkyard) -> list: +def trashcanGenerator() -> list: typelist = ["paper", "metals_and_plastics", "mixed", "bio_waste", "glass"] trash_cans = [ - {'position': (1600, 300), 'type': 'paper'}, - {'position': (1600, 500), 'type': 'metals_and_plastics'}, - {'position': (1600, 700), 'type': 'mixed'}, - {'position': (1600, 900), 'type': 'bio_waste'}, - {'position': (1600, 100), 'type': 'glass'}, + {'position': (1, 0), 'type': 'paper'}, + {'position': (1, 1), 'type': 'metals_and_plastics'}, + {'position': (1, 2), 'type': 'mixed'}, + {'position': (1, 3), 'type': 'bio_waste'}, + {'position': (1, 4), 'type': 'glass'}, ] trash_can_images = { 'paper': pygame.image.load('sprites/trash_can_papier.jpg').convert_alpha(), @@ -24,19 +25,17 @@ def trashcanGenerator(junkyard: Junkyard) -> list: } trashcans = [] for key in trash_can_images: - trash_can_images[key] = pygame.transform.scale(trash_can_images[key], (90,90)) + trash_can_images[key] = pygame.transform.scale(trash_can_images[key], (32,32)) for i in range(5): trashcan = generateTrashcan(i, trash_can_images[typelist[i]], trash_cans[i]["position"], trash_cans[i]["type"]) - trashcan.addNeighbour(junkyard) - junkyard.addNeighbour(trashcan) trashcans.append(trashcan) return trashcans -def householdGenerator(junkyard): - new_house_size = (140, 120) - house_positions = [(450, 100), (250, 250), (200, 700), (850, 800), (1000, 200), (1050, 700), (800, 100), (400, 850)] +def householdGenerator(): + new_house_size = (32, 32) + house_positions = [(15, 5), (17, 5), (19, 5), (21, 5), (15, 8), (17, 8), (19, 8), (21, 8)] houses = [] for i in range(8): house_image = pygame.image.load(f'sprites/house_{i}.jpg') @@ -44,16 +43,4 @@ def householdGenerator(junkyard): house = generateHousehold(i, house_image, house_positions[i]) houses.append(house) - for i in range(len(houses)): - if i % 2 == 0: - neigh = chooseNeighbours(2, i) - else: - neigh = chooseNeighbours(3, i) - for num in neigh: - if len(houses[i].getNeighbours()) < 3 and len(houses[num].getNeighbours()) < 3: - houses[i].addNeighbour(houses[num]) - houses[num].addNeighbour(houses[i]) - if i == 7 or i == 1: - houses[i].addNeighbour(junkyard) - junkyard.addNeighbour(houses[i]) return houses diff --git a/main.py b/main.py index cfb3d84..63c4232 100644 --- a/main.py +++ b/main.py @@ -1,21 +1,15 @@ from generators import * +W = 960 +H = 640 +SIZE = (W, H) pygame.init() -screen = pygame.display.set_mode((1800, 1000)) - -colors = { - "white": (255, 255, 255), - "black": (0, 0, 0), - "red": (255, 0, 0) -} - -new_house_size = (140, 120) - -garbagetruck = Garbagetruck() -junkyard = Junkyard() -trashcans = trashcanGenerator(junkyard) -houses = householdGenerator(junkyard) +screen = pygame.display.set_mode(SIZE) +tilemap = Tilemap(Tileset("sprites/Tiles/1 Tiles/FieldsTile_38.png"), (30, 20)) +trashcans = trashcanGenerator() +houses = householdGenerator() +garbagetruck = Garbagetruck().setPredicts(extractPredicts(houses)) running = True while running: @@ -23,23 +17,9 @@ while running: if event.type == pygame.QUIT: running = False - # garbage_truck_position[0] += random.randint(-4, 4) - # garbage_truck_position[1] += random.randint(-4, 4) - - screen.fill(colors["white"]) - for trash_can in trashcans: - for neighbour in trash_can.getNeighbours(): - pygame.draw.line(screen, colors["black"], trash_can.getPosition(), neighbour.getPosition(), 3) - screen.blit(trash_can.getImage(), trash_can.getPosition()) - for house in houses: - for neighbour in house.getNeighbours(): - if neighbour.getId() < house.getId(): - pygame.draw.line(screen, colors["black"], house.getPosition(), neighbour.getPosition(), 3) - for house in houses: - screen.blit(house.getImage(), house.getPosition()) - pygame.draw.circle(screen, colors["red"], junkyard.getPosition(), 10) - - screen.blit(garbagetruck.getImage(), junkyard.getPosition()) - pygame.display.flip() + screen.fill((0, 0, 0)) + tilemap.render() + screen.blit(tilemap.image, tilemap.rect) + pygame.display.update() pygame.quit()