def __init__(self, event_controller, waiter): # Constructor of base class: Observer Observer.__init__(self) # Register to receive mouse event: Mouse click and motion self.register(event_controller, 'mouse_down') self.register(event_controller, 'mouse_motion') # Constructor of base class: Observer Subject.__init__(self) player_motion_event = SubjectUnit() self.add_subject_unit(player_motion_event, 'player_motion') player_hammer_event = SubjectUnit() self.add_subject_unit(player_hammer_event, 'player_hammer') Customer.__init__(self, waiter) # Attributes # hammer self.hammer = Hammer(waiter, self) self.score = 0
class Player(Observer, Customer, Subject): def __init__(self, event_controller, waiter): # Constructor of base class: Observer Observer.__init__(self) # Register to receive mouse event: Mouse click and motion self.register(event_controller, 'mouse_down') self.register(event_controller, 'mouse_motion') # Constructor of base class: Observer Subject.__init__(self) player_motion_event = SubjectUnit() self.add_subject_unit(player_motion_event, 'player_motion') player_hammer_event = SubjectUnit() self.add_subject_unit(player_hammer_event, 'player_hammer') Customer.__init__(self, waiter) # Attributes # hammer self.hammer = Hammer(waiter, self) self.score = 0 def update(self, type_key, event): """ Handler mouse click :param event: Mouse click event and mouse motion :return: None """ if type_key == 'mouse_down': if event.type == pygame.MOUSEBUTTONDOWN: rect = self.hammer.hit() # blank = Factory.get_avatars('blank') # drawable_object1 = Drawable(blank[0], (rect[0], rect[1]), 2) # self.register_waiter('blank1', drawable_object1) # drawable_object2 = Drawable(blank[0], (rect[0] + rect[2],rect[1] + rect[3]), 2) # self.register_waiter('blank2', drawable_object2) self.set_change('player_hammer', rect) font = pygame.font.Font("resources/font.ttf", 40) drawable_object = Drawable(font.render(str(self.score), True, (255, 0, 0)), (150, 490), DRAWABLE_INDEX.TIMER_COUNTER) self.register_waiter('score', drawable_object) elif type_key == 'mouse_motion': if event.type == pygame.MOUSEMOTION: # Just draw the one avatar in list avatars of hammer pos = (event.pos[0] - self.hammer.distance[0], event.pos[1] - self.hammer.distance[1]) drawable_object = Drawable(self.hammer.get_avatar(), pos, DRAWABLE_INDEX.PLAYER) # Insert index as prefix keyword to sort key = str(drawable_object.index) + CUSTOMER_KEY.HAMMER self.register_waiter(key, drawable_object) # Update state for all observers want to know position of player self.set_change('player_motion', event.pos) def increase_score(self): self.score += 10 def decrease_score(self): return def close(self): self.hammer.close()