modyfikacja requirementsow i zmiana sciezek do zdjec

This commit is contained in:
Bowske 2020-03-29 23:37:25 +02:00
commit a7d4492559
5 changed files with 227 additions and 146 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ venv/
.vscode/ .vscode/
.ideagit .ideagit
.idea .idea
__pycache__/

213
game.py
View File

@ -1,57 +1,13 @@
# TODO: sledzenie pozycji smieciarki
# TODO: pole na ktorym wypisywane beda informacje
# TODO: obsluga kolizji
import pygame import pygame
import modele
import numpy as np
pygame.init() # kolory
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')
self.ruch = 0
pygame.sprite.Sprite.__init__(self)
self.rect = pygame.Rect(self.x * WIDTH + MARGIN * self.x + MARGIN, self.y * HEIGHT + MARGIN * self.y, WIDTH,
HEIGHT)
def w_lewo(self):
if self.x > 0:
self.x -= 1
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
def w_prawo(self):
if self.x < 14:
self.x += 1
self.rect.x = MARGIN + self.x * WIDTH + self.x * MARGIN
if self.ruch == 1:
self.image = pygame.transform.flip(
smieciarka.image, True, False)
self.ruch = 2
def w_gore(self):
if self.y > 0:
self.y -= 1
self.rect.y = self.y * HEIGHT + self.y * MARGIN
def w_dol(self):
if self.y < 14:
self.y += 1
self.rect.y = self.y * HEIGHT + self.y * MARGIN
class Kontener(pygame.sprite.Sprite):
def __init__(self, x, y):
self.x = x
self.y = y
pygame.sprite.Sprite.__init__(self)
self.rect = pygame.Rect(self.x * WIDTH + MARGIN * self.x + MARGIN, self.y * HEIGHT + MARGIN * self.y + MARGIN,
WIDTH, HEIGHT)
# Define some colors
BLACK = (0, 0, 0) BLACK = (0, 0, 0)
WHITE = (255, 255, 255) WHITE = (255, 255, 255)
GREEN = (0, 255, 0) GREEN = (0, 255, 0)
@ -59,30 +15,69 @@ RED = (255, 0, 0)
BLUE = (0, 0, 255) BLUE = (0, 0, 255)
GREY = (128, 128, 128) GREY = (128, 128, 128)
# This sets the WIDTH and HEIGHT of each grid location # wysokosc i szerokosc kazdej kratki
WIDTH = 60 WIDTH = 60
HEIGHT = 60 HEIGHT = 60
# This sets the margin between each cell # margines pomiedzy kratkami
MARGIN = 5 MARGIN = 5
# Create a 2 dimensional array. A two dimensional # rozmiar kratki
# array is simply a list of lists. ILOSC_WIERSZY = 15
grid = [] ILOSC_KOLUMN = 15
for row in range(100):
# Add an empty array that will hold each cell
# in this row
grid.append([])
for column in range(100):
grid[row].append(0) # Append a cell
# Set row 1, cell 5 to one. (Remember rows and # rozmiar okna
# column numbers start at zero.) WINDOW_SIZE = [980, 980]
grid[0][0] = 1
# Tworzenie planszy i kratek
plansza = np.array([[modele.Kratka(j, i) for i in range(ILOSC_KOLUMN)]
for j in range(ILOSC_WIERSZY)])
# smieciarka
smieciarka = modele.Smieciarka(10, 10)
plansza[10, 10].setJestSmieciarka(True)
# kontenery
kontener_plastik = modele.Kontener(0, 0)
kontener_plastik.setImage(pygame.image.load(
"resources/plansza/pojemnik_plastik.png"))
plansza[0, 0].setJestKontenerem(True)
kontener_metal = modele.Kontener(0, 4)
kontener_metal.setImage(pygame.image.load(
"resources/plansza/pojemnik_metal.png"))
plansza[0, 4].setJestKontenerem(True)
kontener_organiczne = modele.Kontener(2, 2)
kontener_organiczne.setImage(pygame.image.load(
"resources/plansza/pojemnik_organiczne.png"))
plansza[2, 2].setJestKontenerem(True)
kontener_papier = modele.Kontener(4, 0)
kontener_papier.setImage(pygame.image.load(
"resources/plansza/pojemnik_papier.png"))
plansza[4, 0].setJestKontenerem(True)
kontener_szklo = modele.Kontener(4, 4)
kontener_szklo.setImage(pygame.image.load(
"resources/plansza/pojemnik_szklo.png"))
plansza[4, 4].setJestKontenerem(True)
# ustawienie wysypiska, rozmiar wysypiska 5x5
for i in range(5):
for j in range(5):
plansza[i, j].setJestWysypiskiem(True)
all_sprites_list = pygame.sprite.Group()
all_sprites_list.add(kontener_plastik, kontener_metal, kontener_organiczne, kontener_papier, kontener_szklo,
smieciarka)
def game():
pygame.init()
# Set the HEIGHT and WIDTH of the screen # Set the HEIGHT and WIDTH of the screen
WINDOW_SIZE = [980, 980] obraz = pygame.display.set_mode(WINDOW_SIZE)
screen = pygame.display.set_mode(WINDOW_SIZE)
# Set title of screen # Set title of screen
pygame.display.set_caption("Inteligentna śmieciarka") pygame.display.set_caption("Inteligentna śmieciarka")
@ -93,47 +88,25 @@ done = False
# Used to manage how fast the screen updates # Used to manage how fast the screen updates
clock = pygame.time.Clock() clock = pygame.time.Clock()
smieciarka = Smieciarka(10, 10)
kontener_plastik = Kontener(0, 0)
kontener_plastik.image = pygame.image.load(
"resources/plansza/pojemnik_plastik.png")
kontener_metal = Kontener(0, 4)
kontener_metal.image = pygame.image.load(
"resources/plansza/pojemnik_metal.png")
kontener_organiczne = Kontener(2, 2)
kontener_organiczne.image = pygame.image.load(
"resources/plansza/pojemnik_organiczne.png")
kontener_papier = Kontener(4, 0)
kontener_papier.image = pygame.image.load(
"resources/plansza/pojemnik_papier.png")
kontener_szklo = Kontener(4, 4)
kontener_szklo.image = pygame.image.load(
"resources/plansza/pojemnik_szklo.png")
kontener_list = pygame.sprite.Group()
smieciarka_list = pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()
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)
# -------- Main Program Loop ----------- # -------- Main Program Loop -----------
while not done: while not done:
# obsluga zdarzen typu nacisniecie klawisza lub przycisku myszy
for event in pygame.event.get(): # User did something for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop done = True # Flag that we are done so we exit this loop
elif event.type == pygame.MOUSEBUTTONDOWN: elif event.type == pygame.MOUSEBUTTONDOWN:
# User clicks the mouse. Get the position # User clicks the mouse. Get the position
pos = pygame.mouse.get_pos() pozycja_myszki = pygame.mouse.get_pos()
# Change the x/y screen coordinates to grid coordinates # Change the x/y screen coordinates to grid coordinates
column = pos[0] // (WIDTH + MARGIN) kolumna = pozycja_myszki[0] // (WIDTH + MARGIN)
row = pos[1] // (HEIGHT + MARGIN) wiersz = pozycja_myszki[1] // (HEIGHT + MARGIN)
# Set that location to one
grid[row][column] = 1 print("Click ", pozycja_myszki,
print("Click ", pos, "Grid coordinates: ", row, column) "Grid coordinates: ", wiersz, kolumna)
plansza[wiersz, kolumna].setKolor(BLUE)
elif event.type == pygame.KEYDOWN: elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT: if event.key == pygame.K_LEFT:
smieciarka.w_lewo() smieciarka.w_lewo()
@ -144,30 +117,28 @@ while not done:
if event.key == pygame.K_DOWN: if event.key == pygame.K_DOWN:
smieciarka.w_dol() smieciarka.w_dol()
# Set the screen background
screen.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(screen,
color,
[(MARGIN + WIDTH) * column + MARGIN,
(MARGIN + HEIGHT) * row + MARGIN,
WIDTH,
HEIGHT])
screen.blit(pygame.image.load("resources/plansza/wysypisko.jpg"), (5, 5))
all_sprites_list.draw(screen)
# Limit to 60 frames per second # Limit to 60 frames per second
clock.tick(60) clock.tick(60)
# czarny kolor w tle
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))
all_sprites_list.draw(obraz)
# Go ahead and update the screen with what we've drawn. # Go ahead and update the screen with what we've drawn.
pygame.display.flip() pygame.display.flip()
# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit() pygame.quit()

11
main.py Normal file
View File

@ -0,0 +1,11 @@
import pygame
import game
def main():
print("poczatek programu")
game.game()
if __name__ == '__main__':
main()

97
modele.py Normal file
View File

@ -0,0 +1,97 @@
import pygame
# This sets the WIDTH and HEIGHT of each grid location
WIDTH = 60
HEIGHT = 60
# This sets the margin between each cell
MARGIN = 5
# 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)
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')
self.ruch = 0
pygame.sprite.Sprite.__init__(self)
self.rect = pygame.Rect(self.x * WIDTH + MARGIN * self.x + MARGIN, self.y * HEIGHT + MARGIN * self.y, WIDTH,
HEIGHT)
def w_lewo(self):
if self.x > 0:
self.x -= 1
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
def w_prawo(self):
if self.x < 14:
self.x += 1
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
def w_gore(self):
if self.y > 0:
self.y -= 1
self.rect.y = self.y * HEIGHT + self.y * MARGIN
def w_dol(self):
if self.y < 14:
self.y += 1
self.rect.y = self.y * HEIGHT + self.y * MARGIN
class Kontener(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)
def setImage(self, image):
self.image = image
class Kratka(pygame.sprite.Sprite):
def __init__(self, poz_x, poz_y):
self.pozX = poz_x
self.pozY = poz_y
self.jestSmieciarka = False
self.jestKontenerem = False
self.jestWysypiskiem = False
self.kolor = GREY
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.__class__
self.rect = pygame.Rect(self.pozX * WIDTH + MARGIN * self.pozX + MARGIN,
self.pozY * HEIGHT + MARGIN * self.pozY + MARGIN,
WIDTH, HEIGHT)
def setImage(self, image):
self.image = image
def setJestSmieciarka(self, bool):
self.jestSmieciarka = bool
def setJestKontenerem(self, bool):
self.jestKontenerem = bool
def setJestWysypiskiem(self, bool):
self.jestWysypiskiem = bool
def setKolor(self, kolor):
self.kolor = kolor

View File

@ -1 +1,2 @@
pygame==1.9.6 pygame==1.9.6
numpy==1.18.2