This commit is contained in:
Anna Nowak 2019-03-25 17:43:13 +01:00
commit eb4d87e6f3
9 changed files with 113 additions and 9 deletions

View File

@ -5,7 +5,6 @@
```yaml ```yaml
Modele: Modele:
Smieciarka: Smieciarka:
- Zbiera smieci
- posiada liste smieci (pojemnosc), - posiada liste smieci (pojemnosc),
- zbiera smieci, - zbiera smieci,
- segreguje smieci, - segreguje smieci,

View File

@ -24,3 +24,4 @@ home_amount = set_home_amount()
PLAY_WIDTH = (home_amount + 4)*CELL_SIZE PLAY_WIDTH = (home_amount + 4)*CELL_SIZE
PLAY_HEIGHT = PLAY_WIDTH PLAY_HEIGHT = PLAY_WIDTH
HUD_HEIGHT = int(home_amount*CELL_SIZE/4)

BIN
fonts/Bazgroly.ttf Normal file

Binary file not shown.

11
game.py
View File

@ -1,13 +1,14 @@
from pygame import * from pygame import *
import sys import sys
import random import random
from config import PLAY_WIDTH, PLAY_HEIGHT, home_amount from config import PLAY_WIDTH, PLAY_HEIGHT, HUD_HEIGHT, home_amount
from sprites.house import House from sprites.house import House
from sprites.hud import Hud
from pygame.locals import * from pygame.locals import *
import utils import utils
##INITIALIZE STATIC VARIABLES######### ##INITIALIZE STATIC VARIABLES#########
FPS = 60 FPS = 20
all_sprites = sprite.Group() all_sprites = sprite.Group()
fps_clock = time.Clock() fps_clock = time.Clock()
@ -22,8 +23,9 @@ interactables = {
##GAMEWINDOW########################## ##GAMEWINDOW##########################
WINDOW_WIDTH = PLAY_WIDTH WINDOW_WIDTH = PLAY_WIDTH
WINDOW_HEIGHT = PLAY_HEIGHT WINDOW_HEIGHT = PLAY_HEIGHT + HUD_HEIGHT
GAMEWINDOW = display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32) GAMEWINDOW = display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32)
hud = Hud(home_amount,WINDOW_WIDTH, WINDOW_HEIGHT,GAMEWINDOW)
display.set_caption('Smieciarz WMI') display.set_caption('Smieciarz WMI')
icon = image.load('images/icon.png') icon = image.load('images/icon.png')
display.set_icon(icon) display.set_icon(icon)
@ -60,6 +62,7 @@ while(1):
if(type(item) == House): if(type(item) == House):
item.generate_rubbish() item.generate_rubbish()
hud.get_statistics(all_sprites)
display.flip() display.flip()
fps_clock.tick(FPS) fps_clock.tick(FPS)
################################################################################## ##################################################################################

View File

@ -15,4 +15,4 @@ class Cell(pygame.sprite.Sprite):
def update(self): def update(self):
self.rect = pygame.Rect( self.rect = pygame.Rect(
self.x*CELL_SIZE, self.y*CELL_SIZE, CELL_SIZE, CELL_SIZE) self.x*CELL_SIZE, self.y*CELL_SIZE, CELL_SIZE, CELL_SIZE)

View File

@ -1,9 +1,12 @@
import pygame import pygame
from sprites.cell import Cell, CELL_SIZE from sprites.cell import Cell, CELL_SIZE
from
from config import PLAY_HEIGHT, PLAY_WIDTH from config import PLAY_HEIGHT, PLAY_WIDTH
class Garbage_collector(Cell): class Garbage_collector(Cell):
def __init__(self, x, y): def __init__(self, x, y):
GC_CAPACITY = 10
Cell.__init__(self, x, y) Cell.__init__(self, x, y)
self.image = pygame.image.load("images/garbage_collector.png") self.image = pygame.image.load("images/garbage_collector.png")
self.move_options = { self.move_options = {
@ -12,6 +15,12 @@ class Garbage_collector(Cell):
"left": lambda forbidden: ('x', self.x - 1) if (self.x - 1, self.y) not in forbidden and self.x - 1 >= 0 else ('x', self.x), "left": lambda forbidden: ('x', self.x - 1) if (self.x - 1, self.y) not in forbidden and self.x - 1 >= 0 else ('x', self.x),
"right": lambda forbidden: ('x', self.x + 1) if (self.x + 1, self.y) not in forbidden and self.x + 1 < PLAY_WIDTH // CELL_SIZE else ('x', self.x) "right": lambda forbidden: ('x', self.x + 1) if (self.x + 1, self.y) not in forbidden and self.x + 1 < PLAY_WIDTH // CELL_SIZE else ('x', self.x)
} }
self.trash_space_taken = {
"plastic": 0,
"glass": 0,
"metal": 0
}
self.trash_collected = 0
def move(self, direction, forbidden): def move(self, direction, forbidden):
(destination, value) = self.move_options[direction](forbidden) (destination, value) = self.move_options[direction](forbidden)
@ -20,4 +29,32 @@ class Garbage_collector(Cell):
elif(destination is 'y'): elif(destination is 'y'):
self.y = value self.y = value
self.update() self.update()
PLASTIC = 0 # blue
GLASS = 1 # green
METAL = 2 # yellow
def collect_trash(self, house):
rubbish = house.get_rubbish_data()
to_collect = rubbish
if(rubbish[0] > GC_CAPACITY - self.trash_space_taken.get("plastic")):
to_collect[0] = self.trash_space_taken.get("plastic")
self.trash_space_taken['plastic'] += to_collect[0]
self.trash_collected += to_collect[0]
if(rubbish[1] > GC_CAPACITY - self.trash_space_taken.get("glass")):
to_collect[1] = self.trash_space_taken.get("glass")
self.trash_space_taken['glass'] += to_collect[1]
self.trash_collected += to_collect[1]
if(rubbish[2] > GC_CAPACITY - self.trash_space_taken.get("metal")):
to_collect[2] = self.trash_space_taken.get("metal")
self.trash_space_taken['metal'] += to_collect[2]
self.trash_collected += to_collect[2]
house.give_away_rubbish(to_collect[0], to_collect[1], to_collect[2])
def get_collect_data(self):
return self.trash_collected
def get_space_data(self):
return self.trash_space_taken

View File

@ -22,7 +22,7 @@ class House(Cell):
self.max_metal = max_metal self.max_metal = max_metal
def generate_rubbish(self): def generate_rubbish(self):
if(random.randint(0, 50) == 1): # 1/5 szansa na wyrzucenie śmiecia w klatce if(random.randint(0, 25) == 1): # 1/25 szansa na wyrzucenie śmiecia w klatce
thrash_type = random.randint(0, 2) thrash_type = random.randint(0, 2)
self.rubbish[thrash_type] = self.rubbish[thrash_type] + 1 self.rubbish[thrash_type] = self.rubbish[thrash_type] + 1
@ -44,7 +44,15 @@ class House(Cell):
self.image = pygame.image.load(House_image.glass.value) #szklo self.image = pygame.image.load(House_image.glass.value) #szklo
elif(self.rubbish[METAL] > self.max_metal): elif(self.rubbish[METAL] > self.max_metal):
self.image = pygame.image.load(House_image.metal.value) #metal self.image = pygame.image.load(House_image.metal.value) #metal
def give_away_rubbish(self, plastic, glass, metal):
self.rubbish[PLASTIC] -= plastic
self.rubbish[GLASS] -= glass
self.rubbish[METAL] -= metal
def check_rubbish_status(self): def check_rubbish_status(self):
print("plastic: " + str(self.rubbish[PLASTIC]) + " glass: " + str( print("plastic: " + str(self.rubbish[PLASTIC]) + " glass: " + str(
self.rubbish[GLASS]) + " metal: " + str(self.rubbish[METAL])) self.rubbish[GLASS]) + " metal: " + str(self.rubbish[METAL]))
def get_rubbish_data(self):
return self.rubbish

56
sprites/hud.py Normal file
View File

@ -0,0 +1,56 @@
import pygame
from sprites.house import House
from sprites.garbage_collector import Garbage_collector
from config import HUD_HEIGHT
HUD_COLOR = (51,21,4)
WHITE = (255,255,255)
class Hud():
texts = []
def __init__(self,house_amount,WINDOW_WIDTH, WINDOW_HEIGHT,GAMEWINDOW):
pygame.init()
hud_upper = WINDOW_WIDTH - HUD_HEIGHT
hud_lower = WINDOW_WIDTH
height = hud_upper - hud_lower
font_type = 'fonts/Bazgroly.ttf'
font_size = house_amount * 2
GAMEWINDOW.fill(HUD_COLOR)
font = pygame.font.Font(font_type, font_size)
gc_plastic_text = font.render("Plastic: 0/0",True,WHITE)
gc_metal_text = font.render("Metal: 0/0",True,WHITE)
gc_glass_text = font.render("Glass: 0/0",True,WHITE)
map_plastic_text = font.render("Plastic: 0",True,WHITE)
map_metal_text = font.render("Metal: 0",True,WHITE)
map_glass_text = font.render("Glass: 0",True,WHITE)
overall_text = font.render("Garbage thrown away: 0",True,WHITE)
GAMEWINDOW.blit(overall_text,(20, 20))
def get_statistics(self, all_sprites):
###Garbage collector stats###
gc_taken_space_plastic = 0
gc_taken_space_metal = 0
gc_taken_space_glass = 0
gc_trash_space = 10
###Board stats###############
plastic_left = 0
metal_left = 0
glass_left = 0
total_gathered = 0
for item in all_sprites:
if(type(item) == House):
rubbish = item.get_rubbish_data()
plastic_left += rubbish[0]
glass_left += rubbish[1]
metal_left += rubbish[2]
if(type(item) == Garbage_collector):
space_taken = item.get_space_data()
gc_taken_space_plastic += space_taken.get("plastic")
gc_taken_space_glass += space_taken.get("glass")
gc_taken_space_metal += space_taken.get("metal")
total_gathered += item.get_collect_data()
print("plastic left: "+str(plastic_left)+" | glass left: "+str(glass_left)+" | metal left: "+str(metal_left))
print(" plastic: "+str(gc_taken_space_plastic)+"/"+str(gc_trash_space)+" | glass: "+str(gc_taken_space_glass)+"/"+str(gc_trash_space)+" | metal: "+str(gc_taken_space_metal)+"/"+str(gc_trash_space))
print("### TOTAL COLLECTED: "+str(total_gathered)+" ###")

View File

@ -8,4 +8,4 @@ class Landfill(Cell):
Cell.__init__(self, x, y) Cell.__init__(self, x, y)
types = ["plastic", "glass", "metal"] types = ["plastic", "glass", "metal"]
self.type = types[type] self.type = types[type]
self.image = pygame.image.load("images/landfill_%s.png" % (self.type)) self.image = pygame.image.load("images/landfill_%s.png" % (self.type))