2020-04-03 21:41:59 +02:00
|
|
|
# TODO: okienko z informacjami
|
2020-03-29 21:02:00 +02:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
# obsluga bledu odmowy dostepu do folderu
|
|
|
|
def onerror(func, path, exc_info):
|
|
|
|
import stat
|
|
|
|
if not os.access(path, os.W_OK):
|
|
|
|
os.chmod(path, stat.S_IWUSR)
|
|
|
|
func(path)
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2020-03-28 12:02:27 +01:00
|
|
|
|
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-03-28 12:02:27 +01:00
|
|
|
WINDOW_SIZE = [980, 980]
|
2020-03-28 15:34:30 +01:00
|
|
|
|
2020-03-29 19:59:24 +02:00
|
|
|
# Tworzenie planszy i kratek
|
2020-04-03 18:27:14 +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-03-29 16:12:46 +02:00
|
|
|
|
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'):
|
|
|
|
shutil.rmtree("resources/smieci w kontenerach", onerror=onerror)
|
|
|
|
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-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-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-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-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-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-06 01:13:11 +02:00
|
|
|
wspolrzedne_domow = modele.generujWspolrzedneDomow(10)
|
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
|
|
|
|
|
|
|
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-06 01:13:11 +02:00
|
|
|
plansza[wspolrzedne_domow[i][0], wspolrzedne_domow[i][1]].setJestDomem(True)
|
2020-04-05 18:22:28 +02:00
|
|
|
for j in range(5):
|
|
|
|
dom.dodajSmiec(random.choice(smieci_lista))
|
|
|
|
|
|
|
|
domy_lista.add(dom)
|
2020-04-03 17:39:57 +02:00
|
|
|
all_sprites_list.add(dom)
|
|
|
|
|
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():
|
|
|
|
pygame.init()
|
|
|
|
|
2020-03-29 16:12:46 +02:00
|
|
|
# Set the HEIGHT and WIDTH of the screen
|
2020-03-29 17:27:02 +02:00
|
|
|
obraz = pygame.display.set_mode(WINDOW_SIZE)
|
2020-03-29 16:12:46 +02:00
|
|
|
|
|
|
|
# Set title of screen
|
|
|
|
pygame.display.set_caption("Inteligentna śmieciarka")
|
|
|
|
|
|
|
|
# Loop until the user clicks the close button.
|
|
|
|
done = False
|
|
|
|
|
|
|
|
# Used to manage how fast the screen updates
|
|
|
|
clock = pygame.time.Clock()
|
|
|
|
|
|
|
|
# -------- Main Program Loop -----------
|
|
|
|
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:
|
|
|
|
# User clicks the mouse. Get the position
|
2020-03-29 19:59:24 +02:00
|
|
|
pozycja_myszki = pygame.mouse.get_pos()
|
|
|
|
|
2020-03-29 16:12:46 +02:00
|
|
|
# Change the x/y screen coordinates to grid coordinates
|
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-04-03 16:20:20 +02:00
|
|
|
# plansza[wiersz, kolumna].setKolor(BLUE)
|
2020-03-29 21:02:00 +02:00
|
|
|
|
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-03-29 21:02:00 +02:00
|
|
|
# Limit to 60 frames per second
|
2020-04-03 18:40:14 +02:00
|
|
|
clock.tick(5)
|
2020-03-29 21:02:00 +02:00
|
|
|
|
|
|
|
# czarny kolor w tle
|
2020-03-29 17:27:02 +02:00
|
|
|
obraz.fill(BLACK)
|
2020-03-29 16:12:46 +02:00
|
|
|
|
2020-03-29 21:02:00 +02:00
|
|
|
# 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,
|
2020-03-29 23:37:25 +02:00
|
|
|
(MARGIN + HEIGHT) *
|
|
|
|
plansza[i, j].pozX + MARGIN,
|
2020-03-29 21:02:00 +02:00
|
|
|
WIDTH,
|
|
|
|
HEIGHT])
|
|
|
|
|
2020-03-29 23:37:25 +02:00
|
|
|
obraz.blit(pygame.image.load(
|
|
|
|
"resources/plansza/wysypisko.jpg"), (5, 5))
|
2020-04-05 18:22:28 +02:00
|
|
|
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)
|
2020-03-29 17:27:02 +02:00
|
|
|
all_sprites_list.draw(obraz)
|
2020-04-03 18:27:14 +02:00
|
|
|
smieciarka.rand_move()
|
2020-03-29 16:12:46 +02:00
|
|
|
# Go ahead and update the screen with what we've drawn.
|
|
|
|
pygame.display.flip()
|
|
|
|
pygame.quit()
|