import pygame import os class Button: def __init__(self, rect: pygame.Rect, texture): self.rect = rect self.texture = texture self.hovered = False self.pressed = False def handle_event(self, event): if event.type == pygame.MOUSEMOTION: self.hovered = self.rect.collidepoint(event.pos) elif event.type == pygame.MOUSEBUTTONDOWN: if self.hovered and event.button == 1: self.pressed = True elif event.type == pygame.MOUSEBUTTONUP: if self.pressed and self.hovered and event.button == 1: self.pressed = False return True # Button was clicked self.pressed = False return False def draw(self, screen): screen.blit(self.texture, self.rect) class Toast: def __init__(self, text, screen_width=None, screen_height=None, duration=2, font=None, color=(255, 255, 255), bg_color=(0, 0, 0), pos=None, font_size=28): self.text = text self.duration = duration # seconds self.start_time = None # Use a font that supports Chinese characters, or use the default system font if font is None: # Try to use a system font that supports UTF-8 try: self.font = pygame.font.SysFont("microsoftyahei,simsun,simhei", font_size) except: # Fall back to default font if specific fonts are not available self.font = pygame.font.Font(None, font_size) else: self.font = font self.color = color self.bg_color = bg_color self.pos = pos # (x, y) or None for center self.visible = False self.surface = None self.rect = None self.screen_width = screen_width self.screen_height = screen_height def show(self): self.start_time = pygame.time.get_ticks() self.visible = True txt_surface = self.font.render(self.text, True, self.color) padding = 16 self.surface = pygame.Surface((txt_surface.get_width() + padding, txt_surface.get_height() + padding), pygame.SRCALPHA) self.surface.fill(self.bg_color) self.surface.blit(txt_surface, (padding // 2, padding // 2)) self.rect = self.surface.get_rect() if self.pos: self.rect.topleft = self.pos elif self.screen_width and self.screen_height: # Center on screen using provided dimensions self.rect.center = (self.screen_width // 2, self.screen_height // 2) def draw(self, screen): if not self.visible: return elapsed = (pygame.time.get_ticks() - self.start_time) / 1000.0 if elapsed > self.duration: self.visible = False return if self.pos is None and (self.screen_width is None or self.screen_height is None): # Center on screen if dimensions weren't provided at initialization screen_rect = screen.get_rect() self.rect.center = screen_rect.center screen.blit(self.surface, self.rect) class InputBox: def __init__(self, rect: pygame.Rect = pygame.Rect(100, 100, 140, 32)) -> None: """ rect,传入矩形实体,传达输入框的位置和大小 """ self.boxBody: pygame.Rect = rect self.color_inactive = pygame.Color('lightskyblue3') # 未被选中的颜色 self.color_active = pygame.Color('dodgerblue2') # 被选中的颜色 self.color = self.color_inactive # 当前颜色,初始为未激活颜色 self.active = False self.text = '' # 输入的内容 self.done = False self.font = pygame.font.Font(None, 32) def dealEvent(self, event: pygame.event.Event): if(event.type == pygame.MOUSEBUTTONDOWN): if(self.boxBody.collidepoint(event.pos)): # 若按下鼠标且位置在文本框 self.active = not self.active else: self.active = False self.color = self.color_active if( self.active) else self.color_inactive if(event.type == pygame.KEYDOWN): # 键盘输入响应 if(self.active): if(event.key == pygame.K_RETURN): print(self.text) # self.text='' elif(event.key == pygame.K_BACKSPACE): self.text = self.text[:-1] else: self.text += event.unicode def draw(self, screen: pygame.surface.Surface): txtSurface = self.font.render( self.text, True, self.color) # 文字转换为图片 width = max(200, txtSurface.get_width()+10) # 当文字过长时,延长文本框 self.boxBody.w = width screen.blit(txtSurface, (self.boxBody.x+5, self.boxBody.y+5)) pygame.draw.rect(screen, self.color, self.boxBody, 2)