2020-03-28 12:02:27 +01:00
|
|
|
import pygame
|
2020-03-29 16:12:46 +02:00
|
|
|
import modele
|
2020-03-29 17:27:02 +02:00
|
|
|
import numpy as np
|
2020-04-03 17:39:57 +02:00
|
|
|
import random
|
2020-04-05 18:22:28 +02:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
2020-04-06 18:16:38 +02:00
|
|
|
pygame.init()
|
|
|
|
|
2020-03-29 16:12:46 +02:00
|
|
|
# kolory
|
2020-03-28 12:02:27 +01:00
|
|
|
BLACK = (0, 0, 0)
|
|
|
|
WHITE = (255, 255, 255)
|
|
|
|
GREEN = (0, 255, 0)
|
|
|
|
RED = (255, 0, 0)
|
|
|
|
BLUE = (0, 0, 255)
|
2020-03-29 05:01:12 +02:00
|
|
|
GREY = (128, 128, 128)
|
2020-03-28 12:02:27 +01:00
|
|
|
|
2020-03-29 16:12:46 +02:00
|
|
|
# wysokosc i szerokosc kazdej kratki
|
2020-03-28 12:02:27 +01:00
|
|
|
WIDTH = 60
|
|
|
|
HEIGHT = 60
|
|
|
|
|
2020-03-29 16:12:46 +02:00
|
|
|
# margines pomiedzy kratkami
|
2020-03-28 12:02:27 +01:00
|
|
|
MARGIN = 5
|
|
|
|
|
2020-03-29 17:27:02 +02:00
|
|
|
# rozmiar kratki
|
2020-03-29 17:32:48 +02:00
|
|
|
ILOSC_WIERSZY = 15
|
|
|
|
ILOSC_KOLUMN = 15
|
2020-03-29 17:27:02 +02:00
|
|
|
|
2020-03-29 16:12:46 +02:00
|
|
|
# rozmiar okna
|
2020-04-06 18:16:38 +02:00
|
|
|
WINDOW_SIZE = [1300, 980]
|
2020-03-28 15:34:30 +01:00
|
|
|
|
2020-03-29 19:59:24 +02:00
|
|
|
# Tworzenie planszy i kratek
|
2020-04-07 20:41:17 +02:00
|
|
|
plansza = np.array([[modele.Kratka(i, j) for i in range(ILOSC_KOLUMN)]
|
|
|
|
for j in range(ILOSC_WIERSZY)])
|
2020-04-03 17:39:57 +02:00
|
|
|
all_sprites_list = pygame.sprite.Group()
|
2020-03-29 16:12:46 +02:00
|
|
|
|
2020-03-29 19:59:24 +02:00
|
|
|
# smieciarka
|
2020-03-29 17:27:02 +02:00
|
|
|
smieciarka = modele.Smieciarka(10, 10)
|
2020-04-03 18:27:14 +02:00
|
|
|
plansza[10, 10].setKolor(BLUE)
|
2020-04-06 01:56:01 +02:00
|
|
|
plansza[10, 10].setObiekt(smieciarka)
|
2020-03-29 16:12:46 +02:00
|
|
|
|
2020-04-17 13:56:29 +02:00
|
|
|
# tworzenie wyswietlanego okna
|
|
|
|
obraz = pygame.display.set_mode(WINDOW_SIZE)
|
|
|
|
smieciarka.setObraz(obraz)
|
|
|
|
pygame.display.set_caption("Inteligentna śmieciarka")
|
|
|
|
|
2020-03-29 19:59:24 +02:00
|
|
|
# kontenery
|
2020-04-05 18:22:28 +02:00
|
|
|
if os.path.exists('resources/smieci w kontenerach'):
|
2020-04-06 01:36:54 +02:00
|
|
|
shutil.rmtree("resources/smieci w kontenerach")
|
2020-04-05 18:22:28 +02:00
|
|
|
os.makedirs('resources/smieci w kontenerach')
|
|
|
|
|
|
|
|
kontener_plastik = modele.Kontener(0, 0, "plastik")
|
2020-03-29 23:37:25 +02:00
|
|
|
kontener_plastik.setImage(pygame.image.load(
|
|
|
|
"resources/plansza/pojemnik_plastik.png"))
|
2020-03-29 21:02:00 +02:00
|
|
|
plansza[0, 0].setJestKontenerem(True)
|
2020-04-06 01:56:01 +02:00
|
|
|
plansza[0, 0].setObiekt(kontener_plastik)
|
2020-03-29 16:12:46 +02:00
|
|
|
|
2020-04-05 18:22:28 +02:00
|
|
|
kontener_metal = modele.Kontener(0, 4, "metal")
|
2020-03-29 23:37:25 +02:00
|
|
|
kontener_metal.setImage(pygame.image.load(
|
|
|
|
"resources/plansza/pojemnik_metal.png"))
|
2020-03-29 21:02:00 +02:00
|
|
|
plansza[0, 4].setJestKontenerem(True)
|
2020-04-06 01:56:01 +02:00
|
|
|
plansza[0, 4].setObiekt(kontener_metal)
|
2020-03-29 16:12:46 +02:00
|
|
|
|
2020-04-05 18:22:28 +02:00
|
|
|
kontener_organiczne = modele.Kontener(2, 2, "pozostale")
|
2020-03-29 23:37:25 +02:00
|
|
|
kontener_organiczne.setImage(pygame.image.load(
|
|
|
|
"resources/plansza/pojemnik_organiczne.png"))
|
2020-03-29 21:02:00 +02:00
|
|
|
plansza[2, 2].setJestKontenerem(True)
|
2020-04-06 01:56:01 +02:00
|
|
|
plansza[2, 2].setObiekt(kontener_organiczne)
|
2020-03-29 16:12:46 +02:00
|
|
|
|
2020-04-05 18:22:28 +02:00
|
|
|
kontener_papier = modele.Kontener(4, 0, "papier")
|
2020-03-29 23:37:25 +02:00
|
|
|
kontener_papier.setImage(pygame.image.load(
|
|
|
|
"resources/plansza/pojemnik_papier.png"))
|
2020-03-29 21:02:00 +02:00
|
|
|
plansza[4, 0].setJestKontenerem(True)
|
2020-04-06 01:56:01 +02:00
|
|
|
plansza[4, 0].setObiekt(kontener_papier)
|
2020-03-29 16:12:46 +02:00
|
|
|
|
2020-04-05 18:22:28 +02:00
|
|
|
kontener_szklo = modele.Kontener(4, 4, "szklo")
|
2020-03-29 23:37:25 +02:00
|
|
|
kontener_szklo.setImage(pygame.image.load(
|
|
|
|
"resources/plansza/pojemnik_szklo.png"))
|
2020-03-29 21:02:00 +02:00
|
|
|
plansza[4, 4].setJestKontenerem(True)
|
2020-04-06 01:56:01 +02:00
|
|
|
plansza[4, 4].setObiekt(kontener_szklo)
|
2020-03-29 21:02:00 +02:00
|
|
|
|
2020-04-03 17:39:57 +02:00
|
|
|
# domy
|
|
|
|
doms_array = ['resources/plansza/domy/dom1.png', 'resources/plansza/domy/dom2.png',
|
|
|
|
'resources/plansza/domy/dom3.png', 'resources/plansza/domy/dom4.png', 'resources/plansza/domy/dom5.png']
|
2020-04-03 18:17:25 +02:00
|
|
|
|
2020-04-05 18:22:28 +02:00
|
|
|
domy_lista = pygame.sprite.Group()
|
|
|
|
|
|
|
|
smieci_lista = [os.path.join(path, filename)
|
|
|
|
for path, dirs, files in os.walk("resources/smieci")
|
|
|
|
for filename in files]
|
2020-04-03 21:41:59 +02:00
|
|
|
|
2020-04-06 18:16:38 +02:00
|
|
|
# informacje o ilosci smieci w domach
|
2020-04-07 20:41:17 +02:00
|
|
|
font = pygame.font.SysFont("arial", 20, bold=True)
|
2020-04-06 18:16:38 +02:00
|
|
|
text_pozostalo = font.render("Pozostało śmieci w domach:", True, WHITE)
|
|
|
|
|
2020-04-07 20:41:17 +02:00
|
|
|
|
2020-04-06 18:16:38 +02:00
|
|
|
def liczSmieci(domy):
|
|
|
|
ile_metalu = 0
|
|
|
|
ile_szkla = 0
|
|
|
|
ile_papieru = 0
|
|
|
|
ile_plastiku = 0
|
|
|
|
ile_pozostalych = 0
|
|
|
|
for d in domy:
|
|
|
|
for s in d.smieci:
|
|
|
|
if "metal" in s:
|
|
|
|
ile_metalu += 1
|
|
|
|
elif "paper" in s:
|
|
|
|
ile_papieru += 1
|
|
|
|
elif "plastic" in s:
|
|
|
|
ile_plastiku += 1
|
|
|
|
elif "glass" in s:
|
|
|
|
ile_szkla += 1
|
|
|
|
elif "trash" in s:
|
|
|
|
ile_pozostalych += 1
|
|
|
|
|
|
|
|
text_metal = font.render("Metal: " + str(ile_metalu), True, WHITE)
|
|
|
|
text_papier = font.render("Papier: " + str(ile_papieru), True, WHITE)
|
|
|
|
text_plastik = font.render("Plastik: " + str(ile_plastiku), True, WHITE)
|
|
|
|
text_szklo = font.render("Szkło: " + str(ile_szkla), True, WHITE)
|
2020-04-07 20:41:17 +02:00
|
|
|
text_pozostale = font.render(
|
|
|
|
"Pozostałe: " + str(ile_pozostalych), True, WHITE)
|
|
|
|
text_odwiedzone_domy = font.render(
|
|
|
|
"Odwiedzone domy: " + str(smieciarka.getOdwiedzoneDomy()), True, WHITE)
|
|
|
|
|
|
|
|
return text_metal, text_papier, text_plastik, text_szklo, text_pozostale, text_odwiedzone_domy
|
2020-04-06 18:16:38 +02:00
|
|
|
|
|
|
|
|
2020-04-17 13:56:29 +02:00
|
|
|
wspolrzedne_domow = modele.generujWspolrzedneDomow(10)
|
2020-04-03 21:41:59 +02:00
|
|
|
for i in range(len(wspolrzedne_domow)):
|
2020-04-06 01:13:11 +02:00
|
|
|
dom = modele.Dom(wspolrzedne_domow[i][0], wspolrzedne_domow[i][1])
|
2020-04-03 17:39:57 +02:00
|
|
|
dom.setImage(pygame.image.load(random.choice(doms_array)))
|
2020-04-07 20:41:17 +02:00
|
|
|
plansza[wspolrzedne_domow[i][0],
|
|
|
|
wspolrzedne_domow[i][1]].setJestDomem(True)
|
2020-04-06 01:36:54 +02:00
|
|
|
plansza[wspolrzedne_domow[i][0], wspolrzedne_domow[i][1]].setObiekt(dom)
|
2020-04-05 18:22:28 +02:00
|
|
|
domy_lista.add(dom)
|
2020-04-03 17:39:57 +02:00
|
|
|
all_sprites_list.add(dom)
|
|
|
|
|
2020-04-06 18:16:38 +02:00
|
|
|
lista_domow = domy_lista.sprites()
|
|
|
|
for d in lista_domow:
|
2020-04-06 01:12:49 +02:00
|
|
|
for j in range(5):
|
2020-04-06 01:56:01 +02:00
|
|
|
smiec = random.choice(smieci_lista)
|
|
|
|
d.dodajSmiec(smiec)
|
|
|
|
smieci_lista.remove(smiec)
|
2020-04-06 01:12:49 +02:00
|
|
|
|
2020-04-07 20:41:17 +02:00
|
|
|
# TEST DODAWANIA SMIECI Z POJEDYNCZEGO DOMU DO FOLDERU KONTENERA ZE SZKLEM
|
2020-04-06 18:16:38 +02:00
|
|
|
# for d in lista_domow:
|
|
|
|
# for s in d.smieci:
|
|
|
|
# kontener_szklo.dodajSmiec(s)
|
2020-04-03 17:39:57 +02:00
|
|
|
|
2020-03-29 21:02:00 +02:00
|
|
|
# ustawienie wysypiska, rozmiar wysypiska 5x5
|
|
|
|
for i in range(5):
|
|
|
|
for j in range(5):
|
|
|
|
plansza[i, j].setJestWysypiskiem(True)
|
2020-03-29 16:12:46 +02:00
|
|
|
|
2020-03-29 17:27:02 +02:00
|
|
|
all_sprites_list.add(kontener_plastik, kontener_metal, kontener_organiczne, kontener_papier, kontener_szklo,
|
|
|
|
smieciarka)
|
|
|
|
|
|
|
|
|
|
|
|
def game():
|
2020-04-06 17:13:01 +02:00
|
|
|
# Petla az uzytkownik zamknie program
|
2020-03-29 16:12:46 +02:00
|
|
|
done = False
|
|
|
|
|
2020-04-06 17:13:01 +02:00
|
|
|
# -------- Glowna petla programu -----------
|
2020-03-29 16:12:46 +02:00
|
|
|
while not done:
|
2020-03-29 21:02:00 +02:00
|
|
|
|
2020-03-29 19:59:24 +02:00
|
|
|
# obsluga zdarzen typu nacisniecie klawisza lub przycisku myszy
|
2020-03-29 16:12:46 +02:00
|
|
|
for event in pygame.event.get(): # User did something
|
|
|
|
if event.type == pygame.QUIT: # If user clicked close
|
|
|
|
done = True # Flag that we are done so we exit this loop
|
|
|
|
elif event.type == pygame.MOUSEBUTTONDOWN:
|
2020-04-06 17:13:01 +02:00
|
|
|
# Zapisywanie pozycji myszki po kliknieciu
|
2020-03-29 19:59:24 +02:00
|
|
|
pozycja_myszki = pygame.mouse.get_pos()
|
|
|
|
|
2020-04-06 17:13:01 +02:00
|
|
|
# Zamiana pozycji na konkretne koordy
|
2020-03-29 19:59:24 +02:00
|
|
|
kolumna = pozycja_myszki[0] // (WIDTH + MARGIN)
|
|
|
|
wiersz = pozycja_myszki[1] // (HEIGHT + MARGIN)
|
2020-03-29 23:37:25 +02:00
|
|
|
print("Click ", pozycja_myszki,
|
|
|
|
"Grid coordinates: ", wiersz, kolumna)
|
2020-03-29 16:12:46 +02:00
|
|
|
elif event.type == pygame.KEYDOWN:
|
|
|
|
if event.key == pygame.K_LEFT:
|
|
|
|
smieciarka.w_lewo()
|
|
|
|
if event.key == pygame.K_RIGHT:
|
|
|
|
smieciarka.w_prawo()
|
|
|
|
if event.key == pygame.K_UP:
|
|
|
|
smieciarka.w_gore()
|
|
|
|
if event.key == pygame.K_DOWN:
|
|
|
|
smieciarka.w_dol()
|
|
|
|
|
2020-04-03 18:27:14 +02:00
|
|
|
smieciarka.rand_move()
|
2020-03-29 16:12:46 +02:00
|
|
|
pygame.quit()
|
2020-04-17 13:56:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
def rysowaniePlanszy():
|
|
|
|
obraz.fill(BLACK)
|
|
|
|
# rysowanie planszy
|
|
|
|
for i in range(ILOSC_WIERSZY):
|
|
|
|
for j in range(ILOSC_KOLUMN):
|
|
|
|
pygame.draw.rect(obraz,
|
|
|
|
plansza[i, j].kolor,
|
|
|
|
[(MARGIN + WIDTH) * plansza[i, j].pozY + MARGIN,
|
|
|
|
(MARGIN + HEIGHT) *
|
|
|
|
plansza[i, j].pozX + MARGIN,
|
|
|
|
WIDTH,
|
|
|
|
HEIGHT])
|
|
|
|
|
|
|
|
obraz.blit(pygame.image.load(
|
|
|
|
"resources/plansza/wysypisko.jpg"), (5, 5))
|
|
|
|
obraz.blit(pygame.image.load(
|
|
|
|
"resources/plansza/jezioro.png"), (395, 655))
|
|
|
|
plansza[6, 10].setJestPrzeszkoda(True)
|
|
|
|
plansza[6, 11].setJestPrzeszkoda(True)
|
|
|
|
plansza[7, 10].setJestPrzeszkoda(True)
|
|
|
|
plansza[7, 11].setJestPrzeszkoda(True)
|
|
|
|
all_sprites_list.draw(obraz)
|
|
|
|
text_metal, text_papier, text_plastik, text_szklo, text_pozostale, text_odwiedzone_domy = liczSmieci(
|
|
|
|
lista_domow)
|
|
|
|
obraz.blit(text_pozostalo,
|
|
|
|
(1020, 240))
|
|
|
|
obraz.blit(text_metal, (1020, 280))
|
|
|
|
obraz.blit(text_plastik, (1020, 320))
|
|
|
|
obraz.blit(text_szklo, (1020, 360))
|
|
|
|
obraz.blit(text_papier, (1020, 400))
|
|
|
|
obraz.blit(text_pozostale, (1020, 440))
|
|
|
|
obraz.blit(text_odwiedzone_domy, (1020, 480))
|
|
|
|
|
|
|
|
pygame.display.update()
|