SZI2019SmieciarzWmi/utils.py

101 lines
3.8 KiB
Python
Raw Normal View History

import sys
import getopt
import random
2019-03-27 10:20:22 +01:00
import csv
import datetime
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
2019-03-20 11:20:10 +01:00
def generate_rand_coordinates(max_x, max_y):
return (random.randint(0, max_x), random.randint(0, (max_y)))
##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)) not in obstacles_coords["homes"] and ((x, y)) not in obstacles_coords["landfills"] and ((x, y-1)) not in obstacles_coords["landfills"]):
houses.append(House(x, y, 10, 10, 10))
2019-03-25 16:00:01 +01:00
obstacles_coords["homes"].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
2019-03-25 13:44:16 +01:00
y=0
for x in range(landfill_counter):
landfills.append(Landfill(x,y,x))
2019-03-25 16:00:01 +01:00
obstacles_coords["landfills"].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)
2019-03-25 16:00:01 +01:00
if((x, y) not in obstacles_coords["landfills"] and (x, y) not in obstacles_coords["homes"]):
gc = Garbage_collector(x, y)
break
all_sprites.add(gc)
return gc
##################################################################################
2019-03-27 10:20:22 +01:00
##INIT CSV FILES#######################
def init_csv():
currentDT = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
stats_csv_name = "logs/stats_" + currentDT + ".csv"
with open(stats_csv_name, 'w', newline='') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
row = ["Plastic left", "Glass left", "Metal left", "GC plastic", "GC glass", "GC metal", "Total collected"]
filewriter.writerow(row)
csvfile.close()
houses_csv_name = "logs/houses_stats_" + currentDT + ".csv"
with open(houses_csv_name, 'w', newline='') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
row = []
for i in range(home_amount):
row.append("House "+str(i+1)+" plastic")
row.append("House "+str(i+1)+" glass")
row.append("House "+str(i+1)+" metal")
filewriter.writerow(row)
csvfile.close()
return stats_csv_name, houses_csv_name
#######################################