from client_local.ui.ui import UIInstance import pygame from client_local.file_loader.fileLoader import load_png import client_local.const.game as G class DlgPveFunc(UIInstance): def __init__(self): super().__init__("game", "dlg_pveFunc") self.rect.x = 619 self.rect.y = 537 + G.H_INCREMENT self.exitBtn = ExitBtn() W,H = self.rect.size # 创建一个W+200,H 的surface self.imageNew = pygame.Surface((W+G.W_INCREMENT,H), pygame.SRCALPHA, 32) from client_local.level.level import current_level self.imageNew.fill((20, 174, 227, 255), (0, 3, W+G.W_INCREMENT, H)) self.imageNew.fill((122, 211, 245, 255), (0, 3, W+G.W_INCREMENT, 3+H)) #self.imageNew.fill((122, 211, 245, 255), (5, 3, 190, H)) self.imageNew.blit(self.image, (G.W_INCREMENT,0)) self.image = self.imageNew def update(self): super().update() self.exitBtn.update() def draw(self, screen: pygame.Surface): super().draw(screen) self.exitBtn.draw(screen) class ExitBtn(pygame.sprite.Sprite): def __init__(self): super().__init__() self.image_on = load_png("res/img/ui/common/btn_leave_0_2.png") self.image_down = load_png("res/img/ui/common/btn_leave_0_3.png") # 默认图片为空 self.defaultImage = pygame.Surface((self.image_on.get_width(), self.image_on.get_height()), pygame.SRCALPHA, 32) self.rect = self.image_on.get_rect() self.rect.x = 732 + G.W_INCREMENT self.rect.y = 545 + G.H_INCREMENT self.mouseOn = False # 鼠标是否在按钮上 self.mouseDown = False # 鼠标是否按下 self.mouseUp = False # 鼠标是否抬起 def update(self): mousePos = pygame.mouse.get_pos() mouseState = pygame.mouse.get_pressed() if self.rect.collidepoint(mousePos): self.mouseOn = True if mouseState[0]: self.mouseDown = True else: if self.mouseDown: self.mouseUp = True self.mouseDown = False else: self.mouseOn = False self.mouseDown = False if self.mouseOn: if self.mouseDown: self.image = self.image_down else: self.image = self.image_on else: self.image = self.defaultImage if self.mouseUp: self.mouseUp = False # 退出游戏 pygame.event.post(pygame.event.Event(pygame.QUIT)) return True return False def draw(self, screen: pygame.Surface): screen.blit(self.image, self.rect)