movement basics

Signed-off-by: Neerka <kuba.markil0220@gmail.com>
This commit is contained in:
Neerka 2024-04-11 13:55:55 +02:00
parent 77a3a7b293
commit 2ce075c29f
8 changed files with 81 additions and 35 deletions

View File

@ -1,16 +1,16 @@
import pygame
from classes.Household import *
from classes.Trashcan import *
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,8 +24,16 @@ 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
def getOrientation(self):
return self.orientation
def setOrientation(self, orientation):
self.orientation = orientation % 4
return self
def getRunningtime(self):
return self.runningtime
@ -135,13 +143,39 @@ class Garbagetruck:
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 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

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,5 +1,6 @@
class Trashcan:
def __init__(self):
def __init__(self, mult):
self.mult = mult
self.id: int = 0
self.image: object = None
self.position: tuple = None
@ -35,10 +36,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 +47,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,23 @@ 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])
houses.append(house)
return houses

12
main.py
View File

@ -2,15 +2,15 @@ from generators import *
W = 30
H = 20
MULT = 32
MULT = 48
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))
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:
@ -19,7 +19,7 @@ while running:
running = False
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:

BIN
sprites/smieciara.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB