From c91d2fffe4ed32e6f108d8c14d3bb85399cc696e Mon Sep 17 00:00:00 2001 From: Neerka Date: Sun, 10 Mar 2024 21:19:35 +0100 Subject: [PATCH] roads and neighbours --- QOLfunc.py | 9 +++++++ classes.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 67 ++++++++++++++++++++++++++++++++++-------------- 3 files changed, 132 insertions(+), 19 deletions(-) create mode 100644 QOLfunc.py create mode 100644 classes.py diff --git a/QOLfunc.py b/QOLfunc.py new file mode 100644 index 0000000..3dd0053 --- /dev/null +++ b/QOLfunc.py @@ -0,0 +1,9 @@ +import random + +def chooseNeighbours(count: int, i: int) -> set: + neigh = set() + while len(neigh) < count: + num = random.randint(0,7) + if num != i: + neigh.add(num) + return neigh \ No newline at end of file diff --git a/classes.py b/classes.py new file mode 100644 index 0000000..6d0a4d5 --- /dev/null +++ b/classes.py @@ -0,0 +1,75 @@ +class Trashcan: + def __init__(self, id, image, position, trashtype): + self.id = id + self.image = image + assert isinstance(position, tuple) + self.position = position + self.type = trashtype + self.neighbours = [] + + def setNeighbours(self, neighbours): + self.neighbours = neighbours + + def addNeighbour(self, neighbour): + self.neighbours.append(neighbour) + + def getNeighbours(self): + return self.neighbours + + def getPosition(self): + return self.position + + def getImage(self): + return self.image + + def getType(self): + return self.type + + def getId(self): + return self.id + + +class Household: + def __init__(self, id, image, position): + self.id = id + self.image = image + assert isinstance(position, tuple) + self.position = position + self.neighbours = [] + + def setNeighbours(self, neighbours): + self.neighbours = neighbours + + def addNeighbour(self, neighbour): + self.neighbours.append(neighbour) + + def getNeighbours(self): + return self.neighbours + + def getPosition(self): + return self.position + + def getImage(self): + return self.image + + def getId(self): + return self.id + + +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/main.py b/main.py index ae84efe..510d1d8 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,17 @@ import pygame import random +from classes import * +from QOLfunc import * pygame.init() screen = pygame.display.set_mode((1800, 1000)) +colors = { + "white": (255, 255, 255), + "black": (0, 0, 0), + "red": (255, 0, 0) +} + garbage_truck_image = pygame.image.load('garbage_truck.png') trash_can_images = { @@ -27,43 +35,64 @@ for key in trash_can_images: garbage_truck_position = [800, 500] 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': (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'}, ] +junkyard = Junkyard() +trashcans = [] +typelist = ["paper", "metals_and_plastics", "mixed", "bio_waste", "glass"] +for i in range(5): + trashcan = Trashcan(i, trash_can_images[typelist[i]], trash_cans[i]["position"], trash_cans[i]["type"]) + trashcan.addNeighbour(junkyard) + junkyard.addNeighbour(trashcan) + trashcans.append(trashcan) houses = [] -house_positions = [[100, 200], [400, 400], [150, 600], [800, 800], [1000, 200], [1200, 700], [700, 100], [500, 800]] +house_positions = [(100, 200), (400, 400), (150, 600), (800, 800), (1000, 200), (1200, 700), (700, 100), (500, 800)] for i in range(8): house_image = pygame.image.load(f'house_{i}.jpg') house_image = pygame.transform.scale(house_image, new_house_size) - house = { - 'position': house_positions[i], - 'image': house_image, - 'trash_bin': [] - } + house = Household(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: + houses[i].addNeighbour(houses[num]) + houses[num].addNeighbour(houses[i]) + if i == 7 or i == 1: + houses[i].addNeighbour(junkyard) + junkyard.addNeighbour(houses[i]) + running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False - garbage_truck_position[0] += random.randint(-4, 4) - garbage_truck_position[1] += random.randint(-4, 4) + # garbage_truck_position[0] += random.randint(-4, 4) + # garbage_truck_position[1] += random.randint(-4, 4) - screen.fill((255, 255, 255)) - screen.blit(garbage_truck_image, garbage_truck_position) - for trash_can in trash_cans: - screen.blit(trash_can_images[trash_can['type']], trash_can['position']) + 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: - screen.blit(house['image'], house['position']) + for neighbour in house.getNeighbours(): + if neighbour.getId() < house.getId(): + pygame.draw.line(screen, colors["black"], house.getPosition(), neighbour.getPosition(), 3) + screen.blit(house.getImage(), house.getPosition()) + pygame.draw.circle(screen, colors["red"], junkyard.getPosition(), 10) + screen.blit(garbage_truck_image, junkyard.getPosition()) pygame.display.flip() pygame.quit() -