diff --git a/game.py b/game.py index 4760b50..01a22c5 100644 --- a/game.py +++ b/game.py @@ -39,6 +39,10 @@ utils.generate_houses(all_sprites, interactables) gc = utils.generate_garbage_collector(all_sprites, interactables) ## +##REMOVE THAT# +hud_break = 0 +## + ##GAME LOOP####################################################################### while(1): for e in event.get(): @@ -54,6 +58,8 @@ while(1): 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) @@ -62,7 +68,12 @@ while(1): if(type(item) == House): item.generate_rubbish() - hud.get_statistics(all_sprites) + ##REMOVE THAT# + hud_break = (hud_break + 1) % (FPS*2) + + if(hud_break == 0): + ## + hud.get_statistics(all_sprites) display.flip() fps_clock.tick(FPS) ################################################################################## diff --git a/sprites/garbage_collector.py b/sprites/garbage_collector.py index ba06dc6..d691e14 100644 --- a/sprites/garbage_collector.py +++ b/sprites/garbage_collector.py @@ -1,12 +1,14 @@ import pygame from sprites.cell import Cell, CELL_SIZE from sprites.house import House +from sprites.landfill import Landfill from config import PLAY_HEIGHT, PLAY_WIDTH -class Garbage_collector(Cell): - def __init__(self, x, y): - GC_CAPACITY = 10 +GC_CAPACITY = 10 +class Garbage_collector(Cell): + + def __init__(self, x, y): Cell.__init__(self, x, y) self.image = pygame.image.load("images/garbage_collector.png") self.move_options = { @@ -31,7 +33,9 @@ class Garbage_collector(Cell): self.update() def collect_trash(self, house): + global GC_CAPACITY rubbish = house.get_rubbish_data() + to_collect = [0,0,0] to_collect = rubbish dic = { @@ -40,15 +44,33 @@ class Garbage_collector(Cell): 2: "metal" } - for i in range(0,2): + for i in range(0,3): - if(rubbish[0] > GC_CAPACITY - self.trash_space_taken.get(dic[i])): - to_collect[0] = self.trash_space_taken.get(dic[i]) + if(rubbish[i] > GC_CAPACITY - self.trash_space_taken.get(dic[i])): + to_collect[i] = GC_CAPACITY - self.trash_space_taken.get(dic[i]) self.trash_space_taken[dic[i]] += to_collect[i] self.trash_collected += to_collect[i] + print("GARBAGE COLLECTOR>> Took "+str(to_collect[i])+" "+dic[i]) house.give_away_rubbish(to_collect[0], to_collect[1], to_collect[2]) + def throw_trash(self, landfill): + landfill_type = landfill.get_type() + print("GARBAGE COLLECTOR>> REMOVED "+landfill_type) + self.trash_space_taken[landfill_type] = 0 + + def select_object(self, interactables): + print("### INTERACTION ###") + for item in interactables: + if(type(item)==House): + item_x, item_y = item.get_coordinates() + if((abs(self.x - item_x)==1 and abs(self.y - item_y)==0) or (abs(self.x - item_x)==0 and abs(self.y - item_y)==1)): + self.collect_trash(item) + elif(type(item)==Landfill): + item_x, item_y = item.get_coordinates() + if((abs(self.x - item_x)==1 and abs(self.y - item_y)==0) or (abs(self.x - item_x)==0 and abs(self.y - item_y)==1)): + self.throw_trash(item) + def get_collect_data(self): return self.trash_collected diff --git a/sprites/house.py b/sprites/house.py index 699a579..c66dbb9 100644 --- a/sprites/house.py +++ b/sprites/house.py @@ -24,31 +24,41 @@ class House(Cell): def generate_rubbish(self): if(random.randint(0, 25) == 1): # 1/25 szansa na wyrzucenie śmiecia w klatce thrash_type = random.randint(0, 2) - self.rubbish[thrash_type] = self.rubbish[thrash_type] + 1 + if(thrash_type == 0 and self.rubbish[thrash_type] < self.max_plastic): + self.rubbish[thrash_type] = self.rubbish[thrash_type] + 1 + if(thrash_type == 1 and self.rubbish[thrash_type] < self.max_glass): + self.rubbish[thrash_type] = self.rubbish[thrash_type] + 1 + if(thrash_type == 2 and self.rubbish[thrash_type] < self.max_metal): + self.rubbish[thrash_type] = self.rubbish[thrash_type] + 1 #mozna ladniej? - if(self.rubbish[PLASTIC] > self.max_plastic): - if(self.rubbish[GLASS] > self.max_glass): - if(self.rubbish[METAL] > self.max_metal): + if(self.rubbish[PLASTIC] == self.max_plastic): + if(self.rubbish[GLASS] == self.max_glass): + if(self.rubbish[METAL] == self.max_metal): self.image = pygame.image.load(House_image.full.value) #plastik, szklo, metal else: self.image = pygame.image.load(House_image.plastic_glass.value) #plastik, szklo - elif(self.rubbish[METAL] > self.max_metal): + elif(self.rubbish[METAL] == self.max_metal): self.image = pygame.image.load(House_image.plastic_metal.value) #plastik, metal else: self.image = pygame.image.load(House_image.plastic.value) #plastik - elif(self.rubbish[GLASS] > self.max_glass): - if(self.rubbish[METAL] > self.max_metal): + elif(self.rubbish[GLASS] == self.max_glass): + if(self.rubbish[METAL] == self.max_metal): self.image = pygame.image.load(House_image.glass_metal.value) #szklo, metal else: 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 + else: + self.image = pygame.image.load(House_image.house.value) #niezapelnione def give_away_rubbish(self, plastic, glass, metal): + print("HOUSE>> Before giving away "+str(self.rubbish[PLASTIC])+" plastic, "+str(self.rubbish[GLASS])+" glass, "+str(self.rubbish[METAL])+" metal, ") self.rubbish[PLASTIC] -= plastic self.rubbish[GLASS] -= glass self.rubbish[METAL] -= metal + print("HOUSE>> Gave away "+str(plastic)+" plastic, "+str(glass)+" glass, "+str(metal)+" metal, ") + print("HOUSE>> After giving away "+str(self.rubbish[PLASTIC])+" plastic, "+str(self.rubbish[GLASS])+" glass, "+str(self.rubbish[METAL])+" metal, ") def check_rubbish_status(self): print("plastic: " + str(self.rubbish[PLASTIC]) + " glass: " + str( @@ -56,3 +66,6 @@ class House(Cell): def get_rubbish_data(self): return self.rubbish + + def get_coordinates(self): + return self.x, self.y \ No newline at end of file diff --git a/sprites/landfill.py b/sprites/landfill.py index e4d2262..c702076 100644 --- a/sprites/landfill.py +++ b/sprites/landfill.py @@ -9,3 +9,9 @@ class Landfill(Cell): types = ["plastic", "glass", "metal"] self.type = types[type] self.image = pygame.image.load("images/landfill_%s.png" % (self.type)) + + def get_coordinates(self): + return self.x, self.y + + def get_type(self): + return self.type