SZI-Smieciarka/game.py

164 lines
5.2 KiB
Python
Raw Normal View History

2020-03-28 12:02:27 +01:00
import pygame
2020-03-29 05:01:12 +02:00
pygame.init()
2020-03-28 12:21:26 +01:00
2020-03-28 15:34:30 +01:00
class Smieciarka(pygame.sprite.Sprite):
def __init__(self, x, y):
self.x = x
self.y = y
self.image = pygame.image.load('smieciarka.png')
2020-03-29 13:04:58 +02:00
self.ruch = 0
2020-03-28 15:34:30 +01:00
pygame.sprite.Sprite.__init__(self)
2020-03-29 05:01:12 +02:00
self.rect = pygame.Rect(self.x * WIDTH + MARGIN * self.x + MARGIN, self.y * HEIGHT + MARGIN * self.y, WIDTH,
HEIGHT)
2020-03-28 15:34:30 +01:00
def w_lewo(self):
if self.x > 0:
self.x -= 1
self.rect.x = MARGIN + self.x * WIDTH + self.x * MARGIN
2020-03-29 13:04:58 +02:00
if self.ruch == 2:
self.image = pygame.image.load('smieciarka.png')
self.ruch = 1
2020-03-28 15:34:30 +01:00
def w_prawo(self):
if self.x < 14:
self.x += 1
self.rect.x = MARGIN + self.x * WIDTH + self.x * MARGIN
2020-03-29 13:04:58 +02:00
if self.ruch == 1:
self.image = pygame.transform.flip(smieciarka.image, True, False)
self.ruch = 2
2020-03-28 15:34:30 +01:00
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
2020-03-29 05:01:12 +02:00
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)
2020-03-28 12:02:27 +01:00
# Define some colors
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
# This sets the WIDTH and HEIGHT of each grid location
WIDTH = 60
HEIGHT = 60
# This sets the margin between each cell
MARGIN = 5
# Create a 2 dimensional array. A two dimensional
# array is simply a list of lists.
grid = []
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
# column numbers start at zero.)
grid[0][0] = 1
# Set the HEIGHT and WIDTH of the screen
WINDOW_SIZE = [980, 980]
screen = 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()
2020-03-28 15:34:30 +01:00
smieciarka = Smieciarka(10, 10)
2020-03-29 05:01:12 +02:00
kontener_plastik = Kontener(0, 0)
kontener_plastik.image = pygame.image.load("pojemnik_plastik.png")
kontener_metal = Kontener(0, 4)
kontener_metal.image = pygame.image.load("pojemnik_metal.png")
kontener_organiczne = Kontener(2, 2)
kontener_organiczne.image = pygame.image.load("pojemnik_organiczne.png")
kontener_papier = Kontener(4, 0)
kontener_papier.image = pygame.image.load("pojemnik_papier.png")
kontener_szklo = Kontener(4, 4)
kontener_szklo.image = pygame.image.load("pojemnik_szklo.png")
kontener_list = pygame.sprite.Group()
2020-03-28 15:34:30 +01:00
smieciarka_list = pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()
2020-03-29 05:01:12 +02:00
kontener_list.add(kontener_plastik, kontener_metal, kontener_organiczne, kontener_papier, kontener_szklo)
2020-03-28 15:34:30 +01:00
smieciarka_list.add(smieciarka)
2020-03-29 05:01:12 +02:00
all_sprites_list.add(kontener_plastik, kontener_metal, kontener_organiczne, kontener_papier, kontener_szklo, smieciarka)
2020-03-28 15:34:30 +01:00
2020-03-28 12:02:27 +01:00
# -------- 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
grid[row][column] = 1
print("Click ", pos, "Grid coordinates: ", row, column)
2020-03-28 15:34:30 +01: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-28 12:02:27 +01:00
# Set the screen background
screen.fill(BLACK)
# Draw the grid
for row in range(15):
for column in range(15):
2020-03-29 05:01:12 +02:00
color = GREY
# if grid[row][column] == 1:
# color = BLUE
2020-03-28 12:02:27 +01:00
pygame.draw.rect(screen,
color,
[(MARGIN + WIDTH) * column + MARGIN,
(MARGIN + HEIGHT) * row + MARGIN,
WIDTH,
HEIGHT])
2020-03-29 05:01:12 +02:00
screen.blit(pygame.image.load("wysypisko.jpg"), (5, 5))
all_sprites_list.draw(screen)
2020-03-28 12:02:27 +01:00
# 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()