From adde0b22866414714489d6385d325b20d5cea3df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Wilczy=C5=84ska?= Date: Wed, 27 Mar 2019 08:37:51 +0100 Subject: [PATCH] Added importing stats to csv file --- game.py | 23 ++++++++++++++++++----- log.txt | Bin 0 -> 208 bytes logs/stats_20190327083321.csv | 18 ++++++++++++++++++ persons.csv | 0 sprites/hud.py | 9 ++++++++- 5 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 log.txt create mode 100644 logs/stats_20190327083321.csv create mode 100644 persons.csv diff --git a/game.py b/game.py index 01a22c5..67e47f3 100644 --- a/game.py +++ b/game.py @@ -6,6 +6,8 @@ from sprites.house import House from sprites.hud import Hud from pygame.locals import * import utils +import csv +import datetime ##INITIALIZE STATIC VARIABLES######### FPS = 20 @@ -39,9 +41,20 @@ utils.generate_houses(all_sprites, interactables) gc = utils.generate_garbage_collector(all_sprites, interactables) ## -##REMOVE THAT# +##COUNTER FOR HUB BRAKE IN CONSOLE##### hud_break = 0 -## +####################################### + +##INIT CSV FILE######################## +currentDT = datetime.datetime.now() +csv_name = "logs/stats_" + currentDT.strftime("%Y%m%d%H%M%S") + ".csv" + +with open(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() +####################################### ##GAME LOOP####################################################################### while(1): @@ -68,12 +81,12 @@ while(1): if(type(item) == House): item.generate_rubbish() - ##REMOVE THAT# + ##LIMIT LOGS TO 1 LOG EVERY 2s##### hud_break = (hud_break + 1) % (FPS*2) if(hud_break == 0): - ## - hud.get_statistics(all_sprites) + ################################### + hud.get_statistics(all_sprites, csv_name) display.flip() fps_clock.tick(FPS) ################################################################################## diff --git a/log.txt b/log.txt new file mode 100644 index 0000000000000000000000000000000000000000..c453a03e890be12e99b5728737d0c76a0b6e653b GIT binary patch literal 208 zcmY+8YYM_J5QM+qf_LZ%QlDr&f!9#+1*Vo5kzQW?nm-~#vcvA|?0nuGJ!f`Aj#Nwu zG`WR0@SrD*>*4`mv8(sEVP>r$!AqT|7aTD%H^o!`~qW->#~;L)m2 eI_!1I-U-Dcnqo*aV`9d}e=c8H*M%?T!~6^0iz9Xb literal 0 HcmV?d00001 diff --git a/logs/stats_20190327083321.csv b/logs/stats_20190327083321.csv new file mode 100644 index 0000000..dbcd3e9 --- /dev/null +++ b/logs/stats_20190327083321.csv @@ -0,0 +1,18 @@ +Plastic left,Glass left,Metal left,GC plastic,GC glass,GC metal,Total collected +27,21,18,0/10,0/10,0/10,0 +30,21,20,0/10,0/10,0/10,0 +31,23,21,0/10,0/10,0/10,0 +33,25,23,0/10,0/10,0/10,0 +34,26,24,0/10,0/10,0/10,0 +34,32,26,0/10,0/10,0/10,0 +34,32,26,0/10,0/10,0/10,0 +35,32,28,0/10,0/10,0/10,0 +38,36,33,0/10,0/10,0/10,0 +29,26,24,10/10,10/10,10/10,30 +30,26,25,0/10,10/10,10/10,30 +30,27,27,0/10,0/10,0/10,30 +23,18,18,10/10,10/10,10/10,60 +13,10,19,10/10,10/10,10/10,80 +13,11,22,1/10,1/10,10/10,82 +14,13,13,2/10,3/10,10/10,95 +14,14,15,0/10,0/10,0/10,95 diff --git a/persons.csv b/persons.csv new file mode 100644 index 0000000..e69de29 diff --git a/sprites/hud.py b/sprites/hud.py index ca2b94a..eb5a83b 100644 --- a/sprites/hud.py +++ b/sprites/hud.py @@ -2,6 +2,7 @@ import pygame from sprites.house import House from sprites.garbage_collector import Garbage_collector from config import HUD_HEIGHT +import csv HUD_COLOR = (51,21,4) WHITE = (255,255,255) class Hud(): @@ -25,7 +26,7 @@ class Hud(): overall_text = font.render("Garbage thrown away: 0",True,WHITE) GAMEWINDOW.blit(overall_text,(20, 20)) - def get_statistics(self, all_sprites): + def get_statistics(self, all_sprites, csv_name): ###Garbage collector stats### gc_taken_space_plastic = 0 gc_taken_space_metal = 0 @@ -54,3 +55,9 @@ class Hud(): 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)+" ###") + + with open(csv_name, 'a', newline='') as csvfile: + filewriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) + row = [str(plastic_left), str(glass_left), str(metal_left), str(gc_taken_space_plastic)+"/"+str(gc_trash_space), str(gc_taken_space_glass)+"/"+str(gc_trash_space), str(gc_taken_space_metal)+"/"+str(gc_trash_space), str(total_gathered)] + filewriter.writerow(row) + csvfile.close()