SZI2019SmieciarzWmi/utils.py
Magdalena Wilczyńska a55be72342 Added statistics utils
2019-03-25 16:03:25 +01:00

123 lines
4.6 KiB
Python

import sys
import getopt
import random
from config import PLAY_WIDTH, PLAY_HEIGHT, home_amount
from sprites.cell import CELL_SIZE
from sprites.grass import Grass
from sprites.house import House
from sprites.landfill import Landfill
from sprites.garbage_collector import Garbage_collector
def generate_rand_coordinates(max_x, max_y):
return (random.randint(0, max_x), random.randint(0, (max_y)))
def add_frame_as_obstacles(obstacles_coords):
for x in range(0, PLAY_WIDTH//CELL_SIZE):
obstacles_coords.append((x,-1))
obstacles_coords.append((-1,x))
obstacles_coords.append((PLAY_WIDTH//CELL_SIZE,x))
obstacles_coords.append((x,PLAY_HEIGHT//CELL_SIZE))
##GENERATE GRASS##################################################################
def generate_grass(all_sprites):
grass = []
for k in range(0, (PLAY_WIDTH//CELL_SIZE)*(PLAY_HEIGHT//CELL_SIZE)):
x, y = (int(k % (PLAY_WIDTH//CELL_SIZE)),
int(k/(PLAY_WIDTH//CELL_SIZE)))
grass.append(Grass(x, y))
for item in grass:
all_sprites.add(item)
##################################################################################
##GENERATE HOUSES#################################################################
def generate_houses(all_sprites, obstacles_coords):
houses = []
home_counter = home_amount
while(home_counter != 0):
x, y = generate_rand_coordinates(
(PLAY_WIDTH//CELL_SIZE)-1, (PLAY_HEIGHT//CELL_SIZE)-1)
if(((x, y) or (x+1,y)) not in obstacles_coords):
houses.append(House(x, y, 10, 10, 10))
obstacles_coords.append((x, y))
home_counter = home_counter - 1
for item in houses:
all_sprites.add(item)
##################################################################################
##GENERATE LANDFILLS##############################################################
def generate_landfills(all_sprites, obstacles_coords):
landfills = []
landfill_counter = 3
# while(landfill_counter != 0):
# x, y = generate_rand_coordinates(
# (PLAY_WIDTH//CELL_SIZE)-1, (PLAY_HEIGHT//CELL_SIZE)-1)
# if((x, y) not in obstacles_coords):
# landfills.append(Landfill(x, y, landfill_counter-1))
# obstacles_coords.append((x, y))
# landfill_counter = landfill_counter - 1
y=0
for x in range(landfill_counter):
landfills.append(Landfill(x,y,x))
obstacles_coords.append((x,y))
for item in landfills:
all_sprites.add(item)
##################################################################################
##GENERATE GARBAGE COLLECTOR######################################################
def generate_garbage_collector(all_sprites, obstacles_coords):
while(True):
x, y = generate_rand_coordinates(
(PLAY_WIDTH//CELL_SIZE)-1, (PLAY_HEIGHT//CELL_SIZE)-1)
if((x, y) not in obstacles_coords):
gc = Garbage_collector(x, y)
break
all_sprites.add(gc)
return gc
##################################################################################
##GET STATISTICS##################################################################
def get_statistics(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)+" ###")
##################################################################################