SZI-Smieciarka/modele.py

231 lines
7.5 KiB
Python
Raw Normal View History

2020-03-29 16:12:46 +02:00
import pygame
2020-04-03 16:27:21 +02:00
import game
2020-04-03 16:20:20 +02:00
import random
2020-04-05 18:22:28 +02:00
import os
import shutil
2020-03-29 16:12:46 +02:00
2020-04-06 17:13:01 +02:00
# wysokosc i szerokosc kazdej kratki
2020-03-29 16:12:46 +02:00
WIDTH = 60
HEIGHT = 60
2020-04-06 17:13:01 +02:00
# margines pomiedzy kratkami
2020-03-29 16:12:46 +02:00
MARGIN = 5
2020-03-29 21:02:00 +02:00
# kolory
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
GREY = (128, 128, 128)
2020-03-29 16:12:46 +02:00
2020-03-29 16:12:46 +02:00
class Smieciarka(pygame.sprite.Sprite):
def __init__(self, x, y):
self.x = x
self.y = y
self.image = pygame.image.load('resources/plansza/smieciarka.png')
2020-03-29 16:12:46 +02:00
self.ruch = 0
2020-04-05 18:22:28 +02:00
self.plastik = []
self.szklo = []
self.papier = []
self.metal = []
self.pozostale = []
self.odwiedzone_domy = 0
self.wspolrzedne_odwiedzonych_domow = []
2020-03-29 16:12:46 +02:00
pygame.sprite.Sprite.__init__(self)
self.rect = pygame.Rect(self.x * WIDTH + MARGIN * self.x + MARGIN, self.y * HEIGHT + MARGIN * self.y, WIDTH,
HEIGHT)
2020-04-03 16:20:20 +02:00
def rand_move(self):
2020-04-03 18:09:41 +02:00
rand_int = random.randint(0, 3)
if rand_int == 0:
2020-04-03 16:20:20 +02:00
self.w_lewo()
2020-04-03 18:09:41 +02:00
elif rand_int == 1:
2020-04-03 16:20:20 +02:00
self.w_prawo()
2020-04-03 18:09:41 +02:00
elif rand_int == 2:
2020-04-03 16:20:20 +02:00
self.w_gore()
2020-04-03 18:09:41 +02:00
elif rand_int == 3:
2020-04-03 16:20:20 +02:00
self.w_dol()
2020-03-29 16:12:46 +02:00
def w_lewo(self):
2020-04-03 17:38:42 +02:00
if self.x > 0:
if game.plansza[self.x - 1, self.y].jestPrzeszkoda is not True:
if game.plansza[self.x - 1, self.y].jestDomem is True:
if [self.x - 1, self.y] not in self.wspolrzedne_odwiedzonych_domow:
self.wspolrzedne_odwiedzonych_domow.append([self.x - 1, self.y])
#print(self.wspolrzedne_odwiedzonych_domow)
self.zwiekszIloscOdwiedzonychDomow()
2020-04-03 17:38:42 +02:00
self.x -= 1
game.plansza[self.x, self.y].setKolor(BLUE)
2020-04-03 17:38:42 +02:00
self.rect.x = MARGIN + self.x * WIDTH + self.x * MARGIN
if self.ruch == 2:
self.image = pygame.image.load(
'resources/plansza/smieciarka.png')
self.ruch = 1
2020-03-29 16:12:46 +02:00
def w_prawo(self):
2020-04-03 17:38:42 +02:00
if self.x < 14:
if game.plansza[self.x + 1, self.y].jestPrzeszkoda is not True:
if game.plansza[self.x + 1, self.y].jestDomem is True:
if [self.x + 1, self.y] not in self.wspolrzedne_odwiedzonych_domow:
self.wspolrzedne_odwiedzonych_domow.append([self.x + 1, self.y])
#print(self.wspolrzedne_odwiedzonych_domow)
self.zwiekszIloscOdwiedzonychDomow()
2020-04-03 17:38:42 +02:00
self.x += 1
2020-04-03 18:27:14 +02:00
game.plansza[self.x, self.y].setKolor(BLUE)
2020-04-03 17:38:42 +02:00
self.rect.x = MARGIN + self.x * WIDTH + self.x * MARGIN
if self.ruch == 1:
self.image = pygame.transform.flip(self.image, True, False)
self.ruch = 2
2020-03-29 16:12:46 +02:00
def w_gore(self):
2020-04-03 17:38:42 +02:00
if self.y > 0:
if game.plansza[self.x, self.y - 1].jestPrzeszkoda is not True:
if game.plansza[self.x, self.y - 1].jestDomem is True:
if [self.x, self.y - 1] not in self.wspolrzedne_odwiedzonych_domow:
self.wspolrzedne_odwiedzonych_domow.append([self.x, self.y - 1])
#print(self.wspolrzedne_odwiedzonych_domow)
self.zwiekszIloscOdwiedzonychDomow()
2020-04-03 17:38:42 +02:00
self.y -= 1
2020-04-03 18:27:14 +02:00
game.plansza[self.x, self.y].setKolor(BLUE)
2020-04-03 17:38:42 +02:00
self.rect.y = self.y * HEIGHT + self.y * MARGIN
2020-03-29 16:12:46 +02:00
def w_dol(self):
2020-04-03 17:38:42 +02:00
if self.y < 14:
if game.plansza[self.x, self.y + 1].jestPrzeszkoda is not True:
if game.plansza[self.x, self.y + 1].jestDomem is True:
if [self.x, self.y + 1] not in self.wspolrzedne_odwiedzonych_domow:
self.wspolrzedne_odwiedzonych_domow.append([self.x, self.y + 1])
#print(self.wspolrzedne_odwiedzonych_domow)
self.zwiekszIloscOdwiedzonychDomow()
2020-04-03 17:38:42 +02:00
self.y += 1
2020-04-03 18:27:14 +02:00
game.plansza[self.x, self.y].setKolor(BLUE)
2020-04-03 17:38:42 +02:00
self.rect.y = self.y * HEIGHT + self.y * MARGIN
2020-03-29 16:12:46 +02:00
2020-04-05 18:22:28 +02:00
def dodajPlastik(self, smiec):
self.plastik.append(smiec)
def dodajSzklo(self, smiec):
self.szklo.append(smiec)
def dodajPapier(self, smiec):
self.papier.append(smiec)
def dodajMetal(self, smiec):
self.metal.append(smiec)
def zwiekszIloscOdwiedzonychDomow(self):
self.odwiedzone_domy += 1
def getOdwiedzoneDomy(self):
return self.odwiedzone_domy
2020-03-29 16:12:46 +02:00
2020-04-03 17:39:57 +02:00
class Dom(pygame.sprite.Sprite):
def __init__(self, x, y):
self.x = x
self.y = y
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.__class__
self.rect = pygame.Rect(self.x * WIDTH + MARGIN * self.x + MARGIN, self.y * HEIGHT + MARGIN * self.y + MARGIN,
WIDTH, HEIGHT)
2020-04-05 18:22:28 +02:00
self.smieci = []
2020-04-03 17:39:57 +02:00
def setImage(self, image):
self.image = image
2020-04-03 18:27:14 +02:00
2020-04-05 18:22:28 +02:00
def dodajSmiec(self, smiec):
self.smieci.append(smiec)
def usunSmiec(self, smiec):
self.smieci.remove(smiec)
2020-04-03 18:27:14 +02:00
2020-03-29 16:12:46 +02:00
class Kontener(pygame.sprite.Sprite):
2020-04-05 18:22:28 +02:00
def __init__(self, x, y, typ):
2020-03-29 16:12:46 +02:00
self.x = x
self.y = y
pygame.sprite.Sprite.__init__(self)
2020-03-29 17:27:02 +02:00
self.image = pygame.image.__class__
2020-03-29 16:12:46 +02:00
self.rect = pygame.Rect(self.x * WIDTH + MARGIN * self.x + MARGIN, self.y * HEIGHT + MARGIN * self.y + MARGIN,
WIDTH, HEIGHT)
2020-04-05 18:22:28 +02:00
self.smieci = []
self.typ = typ
os.makedirs("resources/smieci w kontenerach/" + self.typ)
def dodajSmiec(self, smiec):
self.smieci.append(smiec)
shutil.copy(smiec, "resources/smieci w kontenerach/" + self.typ)
2020-03-29 21:02:00 +02:00
def setImage(self, image):
2020-03-29 17:27:02 +02:00
self.image = image
2020-03-29 16:12:46 +02:00
class Kratka(pygame.sprite.Sprite):
2020-03-29 21:02:00 +02:00
def __init__(self, poz_x, poz_y):
2020-03-29 16:12:46 +02:00
self.pozX = poz_x
self.pozY = poz_y
2020-04-03 17:39:57 +02:00
self.jestDomem = False
2020-03-29 21:02:00 +02:00
self.jestKontenerem = False
self.jestWysypiskiem = False
2020-04-03 16:27:21 +02:00
self.jestPrzeszkoda = False
2020-03-29 21:02:00 +02:00
self.kolor = GREY
2020-04-06 01:36:54 +02:00
self.obiekt = None
2020-03-29 16:12:46 +02:00
pygame.sprite.Sprite.__init__(self)
2020-03-29 17:27:02 +02:00
self.image = pygame.image.__class__
2020-03-29 21:02:00 +02:00
self.rect = pygame.Rect(self.pozX * WIDTH + MARGIN * self.pozX + MARGIN,
self.pozY * HEIGHT + MARGIN * self.pozY + MARGIN,
2020-03-29 16:12:46 +02:00
WIDTH, HEIGHT)
def setImage(self, image):
self.image = image
2020-03-29 19:59:24 +02:00
def setObiekt(self, obiekt):
2020-04-06 01:36:54 +02:00
self.obiekt = obiekt
2020-04-03 17:39:57 +02:00
def setJestDomem(self, bool):
self.jestDomem = bool
2020-03-29 21:02:00 +02:00
def setJestSmieciarka(self, bool):
self.jestSmieciarka = bool
2020-04-03 16:27:21 +02:00
def setJestPrzeszkoda(self, bool):
self.jestPrzeszkoda = bool
2020-03-29 21:02:00 +02:00
def setJestKontenerem(self, bool):
self.jestKontenerem = bool
def setJestWysypiskiem(self, bool):
self.jestWysypiskiem = bool
def setKolor(self, kolor):
self.kolor = kolor
def generujWspolrzedneDomow(ilosc_domow):
wspolrzedne_domow = []
r = len(wspolrzedne_domow)
while r < ilosc_domow:
wspolrzedne_domu = []
x = random.randrange(15)
if x < 5:
y = random.randrange(5, 15)
else:
y = random.randrange(15)
wspolrzedne_domu.append(x)
wspolrzedne_domu.append(y)
2020-04-06 02:20:54 +02:00
if 6 <= wspolrzedne_domu[0] <= 7 and 10 <= wspolrzedne_domu[1] <= 11:
continue
if wspolrzedne_domu[0] == 10 and wspolrzedne_domu[1] == 10:
continue
if wspolrzedne_domu not in wspolrzedne_domow:
wspolrzedne_domow.append(wspolrzedne_domu)
2020-04-06 02:20:54 +02:00
r += 1
return wspolrzedne_domow