SZI2019SmieciarzWmi/sprites/garbage_collector.py
Magdalena Wilczyńska 5c12f8acdc Logs, interaction and bug
Improved logs *stats are displayed every 2 seconds, *debug logs are visible; Added interaction between GC, house and landfill; Collecting trash bug;
2019-03-26 23:13:34 +01:00

79 lines
3.0 KiB
Python

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
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 = {
"up": lambda forbidden: ('y', self.y - 1) if (self.x, self.y - 1) not in forbidden and self.y - 1 >= 0 else ('y', self.y),
"down": lambda forbidden: ('y', self.y + 1) if (self.x, self.y + 1) not in forbidden and self.y + 1 < PLAY_HEIGHT // CELL_SIZE else ('y', self.y),
"left": lambda forbidden: ('x', self.x - 1) if (self.x - 1, self.y) not in forbidden and self.x - 1 >= 0 else ('x', self.x),
"right": lambda forbidden: ('x', self.x + 1) if (self.x + 1, self.y) not in forbidden and self.x + 1 < PLAY_WIDTH // CELL_SIZE else ('x', self.x)
}
self.trash_space_taken = {
"plastic": 0,
"glass": 0,
"metal": 0
}
self.trash_collected = 0
def move(self, direction, forbidden):
(destination, value) = self.move_options[direction](forbidden)
if(destination is 'x'):
self.x = value
elif(destination is 'y'):
self.y = value
self.update()
def collect_trash(self, house):
global GC_CAPACITY
rubbish = house.get_rubbish_data()
to_collect = [0,0,0]
to_collect = rubbish
dic = {
0: "plastic",
1: "glass",
2: "metal"
}
for i in range(0,3):
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
def get_space_data(self):
return self.trash_space_taken