roads and neighbours

This commit is contained in:
Neerka 2024-03-10 21:19:35 +01:00
parent 0b767bba75
commit c91d2fffe4
3 changed files with 132 additions and 19 deletions

9
QOLfunc.py Normal file
View File

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

75
classes.py Normal file
View File

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

67
main.py
View File

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