dlg_pveFunc.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from game.ui.ui import UIInstance
  2. import pygame
  3. from game.file_loader.fileLoader import load_png
  4. import game.const.game as G
  5. class DlgPveFunc(UIInstance):
  6. def __init__(self):
  7. super().__init__("game", "dlg_pveFuncRouge")
  8. self.rect.x = 619
  9. self.rect.y = 537 + G.H_INCREMENT
  10. self.exitBtn = ExitBtn()
  11. W,H = self.rect.size
  12. # 创建一个W+200,H 的surface
  13. self.imageNew = pygame.Surface((W+G.W_INCREMENT,H), pygame.SRCALPHA, 32)
  14. from game.level.level import current_level
  15. self.imageNew.fill((20, 174, 227, 255), (0, 3, W+G.W_INCREMENT, H))
  16. self.imageNew.fill((106,40,126, 255), (0, 3, W+G.W_INCREMENT, 3+H))
  17. #self.imageNew.fill((122, 211, 245, 255), (5, 3, 190, H))
  18. self.imageNew.blit(self.image, (G.W_INCREMENT,0))
  19. self.image = self.imageNew
  20. def update(self):
  21. super().update()
  22. self.exitBtn.update()
  23. def draw(self, screen: pygame.Surface):
  24. super().draw(screen)
  25. self.exitBtn.draw(screen)
  26. class ExitBtn(pygame.sprite.Sprite):
  27. def __init__(self):
  28. super().__init__()
  29. self.image_on = load_png("res/img/ui/common/btn_leave_0_2.png")
  30. self.image_down = load_png("res/img/ui/common/btn_leave_0_3.png")
  31. # 默认图片为空
  32. self.defaultImage = pygame.Surface((self.image_on.get_width(), self.image_on.get_height()), pygame.SRCALPHA, 32)
  33. self.rect = self.image_on.get_rect()
  34. self.rect.x = 732 + G.W_INCREMENT
  35. self.rect.y = 545 + G.H_INCREMENT
  36. self.mouseOn = False # 鼠标是否在按钮上
  37. self.mouseDown = False # 鼠标是否按下
  38. self.mouseUp = False # 鼠标是否抬起
  39. def update(self):
  40. mousePos = pygame.mouse.get_pos()
  41. mouseState = pygame.mouse.get_pressed()
  42. if self.rect.collidepoint(mousePos):
  43. self.mouseOn = True
  44. if mouseState[0]:
  45. self.mouseDown = True
  46. else:
  47. if self.mouseDown:
  48. self.mouseUp = True
  49. self.mouseDown = False
  50. else:
  51. self.mouseOn = False
  52. self.mouseDown = False
  53. if self.mouseOn:
  54. if self.mouseDown:
  55. self.image = self.image_down
  56. else:
  57. self.image = self.image_on
  58. else:
  59. self.image = self.defaultImage
  60. if self.mouseUp:
  61. self.mouseUp = False # 退出游戏
  62. # app_task = tk.Tk()
  63. # roguetest.App(app_task)
  64. pygame.event.post(pygame.event.Event(pygame.QUIT))
  65. return True
  66. return False
  67. def draw(self, screen: pygame.Surface):
  68. screen.blit(self.image, self.rect)