""" SafariChess_Classes.py Classes for button and keyboard events """ import pygame as pg class Button: def __init__(self, image, position, callback=None): self.image = image self.rect = image.get_rect(topleft=position) self.callback = callback def on_click(self, event): if event.button == 1: if self.rect.collidepoint(event.pos): self.callback(self) #下拉框 class DropDown(): def __init__(self, color_menu, color_option, x, y, w, h, font, main, options): self.color_menu = color_menu self.color_option = color_option self.rect = pg.Rect(x, y, w, h) self.font = font self.main = main self.options = options self.draw_menu = False self.menu_active = False self.active_option = -1 def draw(self, surf): pg.draw.rect(surf, self.color_menu[self.menu_active], self.rect, 0) msg = self.font.render(self.main, 1, (0, 0, 0)) surf.blit(msg, msg.get_rect(center = self.rect.center)) if self.draw_menu: for i, text in enumerate(self.options): rect = self.rect.copy() rect.y += (i+1) * self.rect.height pg.draw.rect(surf, self.color_option[1 if i == self.active_option else 0], rect, 0) msg = self.font.render(text, 1, (0, 0, 0)) surf.blit(msg, msg.get_rect(center = rect.center)) def update(self, event_list): mpos = pg.mouse.get_pos() self.menu_active = self.rect.collidepoint(mpos) self.active_option = -1 for i in range(len(self.options)): rect = self.rect.copy() rect.y += (i+1) * self.rect.height if rect.collidepoint(mpos): self.active_option = i break if not self.menu_active and self.active_option == -1: self.draw_menu = False for event in event_list: if event.type == pg.MOUSEBUTTONDOWN and event.button == 1: if self.menu_active: self.draw_menu = not self.draw_menu elif self.draw_menu and self.active_option >= 0: self.draw_menu = False return self.active_option return -1