def option_boxes_up(self,window): begin = end = window.get_time() speed = 0.03 if len(self.option_box): #trigger the animation of the option boxes while self.option_box[0].y > window.height - self.option_box[0].height: end = window.get_time() if end > begin + speed: for i in self.option_box: if i.y > window.height-i.height: i.y -= 80 begin += speed window.render(menu=self,optionboxes=self.option_box) #change the icon of the option bow where mouse is on. mouse_x, mouse_y = window.get_mouse_pos() for item in self.option_box: if item.x < mouse_x < item.x + item.width\ and item.y < mouse_y < item.y + item.height: item.onclick_listener(True) else: item.onclick_listener(False) window.render(menu=self,optionboxes=self.option_box)
def menu_show(self,window): ''' show the process of the menu. ID of the option box at the leftmost is 0, and IDs of option boxes increases toward right. :param window: instance of class Window. :return: return the ID of the option box that player chooses. ''' choose = -1 animation_height = window.height-self.option_box[0].height while True: #check whether player triggers exit mechanism #if so, exit the program if(window.monitor_event()): exit() #control the animation of option boxes mouse_x, mouse_y = window.get_mouse_pos() if animation_height < mouse_y < window.height: self.option_boxes_up(window) else: self.option_boxes_down(window) #check whether player chooses one of the option box. #if so, return the ID of that option box choose = self.check_mouse_click(window) if choose != -1: break window.render(menu=self,optionboxes=self.option_box) return choose
def check_mouse_click(self,window): left_key,middle_key,right_key = window.get_mouse_key() mouse_x, mouse_y = window.get_mouse_pos() if left_key: for item in self.option_box: if item.x < mouse_x < item.x + item.width\ and item.y < mouse_y < item.y + item.height: return self.option_box.index(item) return -1