SZI-Smieciarka/game.py
2020-03-29 17:32:48 +02:00

130 lines
4.0 KiB
Python

import pygame
import modele
import numpy as np
# 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)
# wysokosc i szerokosc kazdej kratki
WIDTH = 60
HEIGHT = 60
# margines pomiedzy kratkami
MARGIN = 5
# rozmiar kratki
ILOSC_WIERSZY = 15
ILOSC_KOLUMN = 15
# rozmiar okna
WINDOW_SIZE = [980, 980]
# TWORZENIE OBIEKTOW
smieciarka = modele.Smieciarka(10, 10)
kontener_plastik = modele.Kontener(0, 0)
kontener_plastik.setImage(pygame.image.load("resources/pojemnik_plastik.png"))
kontener_metal = modele.Kontener(0, 4)
kontener_metal.setImage(pygame.image.load("resources/pojemnik_metal.png"))
kontener_organiczne = modele.Kontener(2, 2)
kontener_organiczne.setImage(pygame.image.load("resources/pojemnik_organiczne.png"))
kontener_papier = modele.Kontener(4, 0)
kontener_papier.setImage(pygame.image.load("resources/pojemnik_papier.png"))
kontener_szklo = modele.Kontener(4, 4)
kontener_szklo.setImage(pygame.image.load("resources/pojemnik_szklo.png"))
kratka_list = pygame.sprite.Group()
kontener_list = pygame.sprite.Group()
smieciarka_list = pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()
# kratka_list
kontener_list.add(kontener_plastik, kontener_metal, kontener_organiczne, kontener_papier, kontener_szklo)
smieciarka_list.add(smieciarka)
all_sprites_list.add(kontener_plastik, kontener_metal, kontener_organiczne, kontener_papier, kontener_szklo,
smieciarka)
def game():
pygame.init()
plansza = np.array([[0 for i in range(ILOSC_KOLUMN)] for j in range(ILOSC_WIERSZY)])
plansza[0][0] = 1
# Set the HEIGHT and WIDTH of the screen
obraz = pygame.display.set_mode(WINDOW_SIZE)
# 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:
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
pos = pygame.mouse.get_pos()
# Change the x/y screen coordinates to grid coordinates
column = pos[0] // (WIDTH + MARGIN)
row = pos[1] // (HEIGHT + MARGIN)
# Set that location to one
plansza[row][column] = 1
print("Click ", pos, "Grid coordinates: ", row, column)
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()
# Set the screen background
obraz.fill(BLACK)
# Draw the grid
for row in range(15):
for column in range(15):
color = GREY
# if grid[row][column] == 1:
# color = BLUE
pygame.draw.rect(obraz,
color,
[(MARGIN + WIDTH) * column + MARGIN,
(MARGIN + HEIGHT) * row + MARGIN,
WIDTH,
HEIGHT])
obraz.blit(pygame.image.load("resources/wysypisko.jpg"), (5, 5))
all_sprites_list.draw(obraz)
# Limit to 60 frames per second
clock.tick(60)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit()