from pygame import * import sys import random from config import PLAY_WIDTH, PLAY_HEIGHT, HUD_HEIGHT, home_amount 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 all_sprites = sprite.Group() fps_clock = time.Clock() ###################################### interactables = { "homes": [], "landfills": [] } ##GAMEWINDOW########################## WINDOW_WIDTH = PLAY_WIDTH WINDOW_HEIGHT = PLAY_HEIGHT + HUD_HEIGHT GAMEWINDOW = display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32) hud = Hud(home_amount,WINDOW_WIDTH, WINDOW_HEIGHT,GAMEWINDOW) display.set_caption('Smieciarz WMI') icon = image.load('images/icon.png') display.set_icon(icon) ###################################### ## # Generate level utils.generate_grass(all_sprites) utils.generate_landfills(all_sprites, interactables) utils.generate_houses(all_sprites, interactables) gc = utils.generate_garbage_collector(all_sprites, interactables) ## ##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): for e in event.get(): if e.type == QUIT: quit() sys.exit() if e.type == KEYUP: if e.key == K_UP: gc.move('up', interactables["homes"] + interactables["landfills"]) if e.key == K_DOWN: gc.move('down', interactables["homes"] + interactables["landfills"]) if e.key == K_RIGHT: gc.move('right', interactables["homes"] + interactables["landfills"]) if e.key == K_LEFT: gc.move('left', interactables["homes"] + interactables["landfills"]) if e.key == K_SPACE: gc.select_object(all_sprites) all_sprites.update() all_sprites.draw(GAMEWINDOW) for item in all_sprites: if(type(item) == House): item.generate_rubbish() ##LIMIT LOGS TO 1 LOG EVERY 2s##### hud_break = (hud_break + 1) % (FPS*2) if(hud_break == 0): ################################### hud.get_statistics(all_sprites, csv_name) display.flip() fps_clock.tick(FPS) ##################################################################################