Merge pull request 'Movement gotowy' (#4) from premain into main

Reviewed-on: #4
This commit is contained in:
Jakub Markil 2024-04-13 14:11:08 +02:00
commit fe472d32c4
76 changed files with 272 additions and 38 deletions

View File

@ -1,16 +1,21 @@
import math
import random
import pygame
from classes.Household import *
from classes.Trashcan import *
from classes.Node import *
from classes.Trashcan import Trashcan
class Garbagetruck:
def __init__(self):
def __init__(self, mult):
self.mult = mult
self.capacity: int = 20
self.trash: list = []
self.trashweight: int = 0
self.image = pygame.image.load("sprites/garbage_truck.png").convert_alpha()
self.image = pygame.transform.scale(self.image, (32, 32))
self.image = pygame.image.load("sprites/smieciara.png").convert_alpha()
self.image = pygame.transform.scale(self.image, (mult, mult))
self.position = [3, 3]
self.houses: list = []
self.trashcans: list = []
@ -24,7 +29,34 @@ class Garbagetruck:
self.scanner = None
self.planner = None
self.driver = None
self.orientation = 3 # Niech numery będą tak: N - 0, W - 1, S - 2, E - 3 -- po prostu odwrotnie do zegara
self.runningtime = 0
self.movesequence = []
self.target = None
def getState(self):
return self.state
def setMovesequence(self, movesequence):
self.movesequence = movesequence
return self
def setTarget(self):
for place in self.houses:
if place.getFinal():
self.target = place
return
for place in self.trashcans:
if place.getFinal():
self.target = place
return
def getOrientation(self):
return self.orientation
def setOrientation(self, orientation):
self.orientation = orientation % 4
return self
def getRunningtime(self):
return self.runningtime
@ -91,7 +123,8 @@ class Garbagetruck:
def modPosiotion(self, modX, modY):
x = self.getPosition()[0] + modX
y = self.getPosition()[1] + modY
self.setPosition([x, y])
position = [x, y]
self.setPosition(position)
def getRoute(self):
return self.route
@ -124,24 +157,157 @@ class Garbagetruck:
dist = abs(lok1[0] - lok2[0]) + abs(lok1[1] - lok2[1])
return dist
def selfdistance(self, target):
if isinstance(target, (Trashcan, Household)):
lok = target.getPosition()
own = self.getPosition()
dist = abs(lok[0] - own[0]) + abs(lok[1] - own[1])
return dist
return math.inf
def scanTile(self):
self.state = None
temp = self.houses[:]
temp.append(self.trashcans[:])
temp.extend(self.trashcans[:])
for loc in temp:
if tuple(self.position) == loc.getPosition():
self.state = loc
return
else:
self.state = False
def printme(self):
x, y = self.getPosition()
return 32*x, 32*y
return self.mult*x, self.mult*y
def throwGarbage(self, trash):
if self.segregation[trash.getTtype()] == self.state.getTrashtype():
self.addTrashweight(trash.getWeight * (-1))
self.trash.remove(trash)
def rotateImage(self, arg):
self.image = pygame.transform.rotate(self.image, 90*arg)
def rotateLeft(self):
self.setOrientation(self.getOrientation()+1)
self.rotateImage(1)
def rotateRight(self):
self.setOrientation(self.getOrientation()-1)
self.rotateImage(-1)
def moveForward(self):
ort = self.orientation
x, y = self.getPosition()
stepX = 0
stepY = 0
if ort == 0 and y != 0:
stepY = -1
elif ort == 1 and x != 0:
stepX = -1
elif ort == 2 and y != 20:
stepY = 1
elif ort == 3 and x != 30:
stepX = 1
self.modPosiotion(stepX, stepY)
def graphsearch(self):
def succ(elem):
def virtRotateLeft(state):
ort = (state[-1] + 1) % 4
result = state[:]
result[-1] = ort
return result
def virtRotateRight(state):
ort = (state[-1] - 1) % 4
result = state[:]
result[-1] = ort
return result
def virtMoveForward(state):
ort = state[-1]
x, y = state[0], state[1]
stepX, stepY = 0, 0
if ort == 0 and y != 0:
stepY = -1
elif ort == 1 and x != 0:
stepX = -1
elif ort == 2 and y != 20:
stepY = 1
elif ort == 3 and x != 30:
stepX = 1
x += stepX
y += stepY
result = [x, y, ort]
return result
op = elem.getState()
forward = {"result": virtMoveForward(op), "action": "F"}
left = {"result": virtRotateLeft(op), "action": "L"}
right = {"result": virtRotateRight(op), "action": "R"}
# print("got children")
return [forward, left, right]
fringe = []
explored = []
target = self.target.getPosition()
temp = self.getPosition()[:]
temp.append(self.getOrientation())
initial = Node(temp)
fringe.append(initial)
while True:
fringPos = [item.getState() for item in fringe]
explPos = [item.getState() for item in explored]
if not fringe:
return False
elem = fringe.pop(0)
virtPos = elem.getState()[:-1]
dist = abs(virtPos[0]-target[0]) + abs(virtPos[1]-target[1])
if dist == 0:
def findWay(node):
temp = node
movelist = []
while temp:
movelist.append(temp.getAction())
temp = temp.getParent()
return movelist
lista = findWay(elem)
result = lista[::-1]
result.pop(0)
return result
explored.append(elem)
suc = succ(elem)
for wynik in suc:
if wynik['result'] not in fringPos and wynik['result'] not in explPos:
x = Node(wynik["result"])
x.setParent(elem)
x.setAction(wynik["action"])
fringe.append(x)
def executeMovement(self):
element = self.movesequence.pop(0) if self.movesequence else ""
if element == "L":
self.rotateLeft()
elif element == "R":
self.rotateRight()
elif element == "F":
self.moveForward()
def randomTarget(self):
wybor1 = random.choice([1,2])
if wybor1 == 1:
wybor2 = random.choice(self.houses)
else:
wybor2 = random.choice(self.trashcans)
wybor2.switchFinal()
# print(wybor2)
def classifyTrash(self):
pass
# Tutaj jest plan żeby dopiero napisać funkcję jak już będzie klasyfikator

View File

@ -2,11 +2,20 @@ from classes.Garbage import *
class Household:
def __init__(self):
def __init__(self, mult):
self.mult = mult
self.id: int = 0
self.image: object = None
self.position: tuple = None
self.garbage: object = Garbage()
self.final = False
def getFinal(self):
return self.final
def switchFinal(self):
self.final = False if self.final else True
return self
def setPosition(self, position: tuple):
self.position = position
@ -38,16 +47,16 @@ class Household:
def printme(self):
x, y = self.getPosition()
return 32*x, 32*y
return self.mult*x, self.mult*y
def generateHousehold(i, image, position):
def generateHousehold(mult, i, image, position):
"""
:param int i:
:param object image:
:param tuple position:
:return:
"""
house = Household()
house = Household(mult)
house.setId(i).setImage(image).setPosition(position)
return house

22
classes/Node.py Normal file
View File

@ -0,0 +1,22 @@
class Node:
def __init__(self, state):
self.state = state
self.action = ""
self.parent: Node = None
def getState(self):
return self.state
def getAction(self):
return self.action
def setAction(self, action):
self.action = action
return self
def getParent(self):
return self.parent if self.parent else False
def setParent(self, parent):
self.parent = parent
return self

View File

@ -4,32 +4,32 @@ import numpy as np
class Tilemap:
def __init__(self, tileset, size=(30, 20), rect=None):
def __init__(self, tileset, mult, size=(30, 20), rect=None):
self.size = size
self.tileset = tileset
self.map = np.zeros(size, dtype=int)
w, h = self.size
self.image = pygame.Surface((32*w, 32*h))
self.image = pygame.Surface((mult*w, mult*h))
if rect:
self.rect = pygame.Rect(rect)
else:
self.rect = self.image.get_rect()
def render(self):
def render(self, mult):
m, n = self.map.shape
for i in range(m):
for j in range(n):
tile = self.tileset.tiles[self.map[i, j]]
self.image.blit(tile, (i*32, j*32))
self.image.blit(tile, (i*mult, j*mult))
def set_zero(self):
def set_zero(self, mult):
self.map = np.zeros(self.size, dtype=int)
self.render()
self.render(mult)
def set_random(self):
def set_random(self, mult):
n = len(self.tileset.tiles)
self.map = np.random.randint(n, size=self.size)
self.render()
self.render(mult)
def __str__(self):
return f'{self.__class__.__name__} {self.size}'

View File

@ -3,12 +3,13 @@ import pygame
class Tileset:
def __init__(self, file, size=(32, 32), margin=1, spacing=1):
def __init__(self, file, mult, margin=1, spacing=1):
self.file = file
self.size = size
self.size = (mult, mult)
self.margin = margin
self.spacing = spacing
self.image = pygame.image.load(file)
self.image = pygame.transform.scale(self.image, self.size)
self.rect = self.image.get_rect()
self.tiles = []
self.load()

View File

@ -1,9 +1,18 @@
class Trashcan:
def __init__(self):
def __init__(self, mult):
self.mult = mult
self.id: int = 0
self.image: object = None
self.position: tuple = None
self.trashtype: str = None
self.final = False
def getFinal(self):
return self.final
def switchFinal(self):
self.final = False if self.final else True
return self
def setPosition(self, position: tuple):
self.position = position
@ -35,10 +44,10 @@ class Trashcan:
def printme(self):
x, y = self.getPosition()
return 32*x, 32*y
return self.mult*x, self.mult*y
def generateTrashcan(newId, image, position, trashtype):
def generateTrashcan(mult, newId, image, position, trashtype):
"""
:param int newId:
:param string image:
@ -46,6 +55,6 @@ def generateTrashcan(newId, image, position, trashtype):
:param string trashtype:
:return object Trashcan:
"""
trash = Trashcan()
trash = Trashcan(mult)
trash.setId(newId).setImage(image).setPosition(position).setTrashtype(trashtype)
return trash

View File

@ -7,7 +7,7 @@ from QOLfunc import *
import pygame
def trashcanGenerator() -> list:
def trashcanGenerator(mult) -> list:
typelist = ["paper", "metals_and_plastics", "mixed", "bio_waste", "glass"]
trash_cans = [
{'position': (1, 0), 'type': 'paper'},
@ -25,22 +25,25 @@ def trashcanGenerator() -> list:
}
trashcans = []
for key in trash_can_images:
trash_can_images[key] = pygame.transform.scale(trash_can_images[key], (32,32))
trash_can_images[key] = pygame.transform.scale(trash_can_images[key], (mult, mult))
for i in range(5):
trashcan = generateTrashcan(i, trash_can_images[typelist[i]], trash_cans[i]["position"], trash_cans[i]["type"])
trashcan = generateTrashcan(mult, i, trash_can_images[typelist[i]], trash_cans[i]["position"],
trash_cans[i]["type"])
trashcans.append(trashcan)
return trashcans
def householdGenerator():
new_house_size = (32, 32)
def householdGenerator(mult):
new_house_size = (mult, mult)
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/domek.png')
house_image = pygame.transform.scale(house_image, new_house_size)
house = generateHousehold(i, house_image, house_positions[i])
house = generateHousehold(mult, i, house_image, house_positions[i])
if i == 1:
house.switchFinal()
houses.append(house)
return houses

38
main.py
View File

@ -1,16 +1,21 @@
import pygame.transform
from generators import *
import time
W = 30
H = 20
MULT = 32
MULT = 50
SIZE = (MULT*W, MULT*H)
pygame.init()
screen = pygame.display.set_mode(SIZE)
tilemap = Tilemap(Tileset("sprites/Tiles/1 Tiles/FieldsTile_38.png"), (W, H))
tilemap = Tilemap(Tileset("sprites/Tiles/1 Tiles/FieldsTile_38.png", mult=MULT), mult=MULT, size=(W, H))
targimage = pygame.image.load("sprites/X.png").convert_alpha()
targimage = pygame.transform.scale(targimage, (MULT, MULT))
trashcans = trashcanGenerator()
houses = householdGenerator()
garbagetruck = Garbagetruck().setHouses(houses).setTrashcans(trashcans)
trashcans = trashcanGenerator(MULT)
houses = householdGenerator(MULT)
garbagetruck = Garbagetruck(MULT).setHouses(houses).setTrashcans(trashcans)
running = True
while running:
@ -18,14 +23,33 @@ while running:
if event.type == pygame.QUIT:
running = False
print(garbagetruck.movesequence)
garbagetruck.setTarget()
garbagetruck.executeMovement()
screen.fill((0, 0, 0))
tilemap.render()
tilemap.render(MULT)
screen.blit(tilemap.image, tilemap.rect)
screen.blit(garbagetruck.getImage(), garbagetruck.printme())
for i in trashcans:
screen.blit(i.getImage(), i.printme())
for h in houses:
screen.blit(h.getImage(), h.printme())
print(garbagetruck.getPosition())
bruh = garbagetruck.target.getPosition()
bruhlist = [i*MULT for i in bruh]
screen.blit(targimage, bruhlist)
screen.blit(garbagetruck.getImage(), garbagetruck.printme())
pygame.display.update()
garbagetruck.scanTile()
state = garbagetruck.getState()
if not garbagetruck.movesequence:
moves = garbagetruck.graphsearch()
garbagetruck.setMovesequence(moves)
if state:
if state.getFinal():
garbagetruck.getState().switchFinal()
elif not garbagetruck.movesequence:
garbagetruck.randomTarget()
time.sleep(0.5)
pygame.quit()

Binary file not shown.

After

Width:  |  Height:  |  Size: 462 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 515 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 522 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 530 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 484 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 560 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 521 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 591 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 544 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 541 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 454 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 571 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 532 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 540 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 589 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 512 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 529 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 456 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 544 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 464 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 521 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 580 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 504 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 613 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 546 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 559 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 443 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 592 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 554 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 584 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 551 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 571 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 487 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 519 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 538 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 531 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 505 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 139 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 584 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 588 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 602 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 599 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 643 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 597 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 530 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 535 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 590 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 626 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 602 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 593 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 577 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 580 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 549 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 529 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 561 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 555 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 555 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 554 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 571 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 551 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 548 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 537 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 557 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 545 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 594 B

BIN
sprites/X.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

BIN
sprites/smieciara.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB