Legend of the Gold Box... A game written for the LOWREZJAM 2018 game jam
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

148 行
4.0KB

  1. '''
  2. Filename display.py
  3. Author: Bryan "ObsidianBlk" Miller
  4. Date Created: 8/1/2018
  5. Python Version: 3.7
  6. '''
  7. import pygame
  8. import weakref
  9. from .events import Events
  10. class Flag:
  11. SWSURFACE = pygame.SWSURFACE
  12. HWSURFACE = pygame.HWSURFACE
  13. HWPALETTE = pygame.HWPALETTE
  14. DOUBLEBUF = pygame.DOUBLEBUF
  15. FULLSCREEN = pygame.FULLSCREEN
  16. OPENGL = pygame.OPENGL
  17. RESIZABLE = pygame.RESIZABLE
  18. NOFRAME = pygame.NOFRAME
  19. def isSet(flags, flag):
  20. return (flags & flag) == flag
  21. class _Display:
  22. def __init__(self, width=0, height=0):
  23. # NOTE: In the newest version of pygame, setting resolution to 0,0 should set the resolution to that of the screen... so we'll just mimic that.
  24. self._init = False
  25. self._resolution = (width, height)
  26. self._display_surface = None
  27. self._display_flags = Flag.HWSURFACE | Flag.DOUBLEBUF
  28. self._clear_color = pygame.Color(0,0,0)
  29. @property
  30. def init(self):
  31. return self._init
  32. @property
  33. def surface(self):
  34. return self._display_surface
  35. @property
  36. def width(self):
  37. if self._display_surface is None:
  38. return 0
  39. return self._resolution[0]
  40. @property
  41. def height(self):
  42. if self._display_surface is None:
  43. return 0
  44. return self._resolution[1]
  45. @property
  46. def resolution(self):
  47. if self._display_surface is None:
  48. return (0,0)
  49. return self._display_surface.get_size()
  50. @property
  51. def flags(self):
  52. return self._display_flags
  53. @property
  54. def fullscreen(self):
  55. return Flag.isSet(self._display_flags, Flag.FULLSCREEN)
  56. @property
  57. def double_buffered(self):
  58. return Flag.isSet(self._display_flags, Flag.DOUBLEBUF)
  59. @property
  60. def resizable(self):
  61. return Flag.isSet(self._display_flags, Flag.RESIZABLE)
  62. @property
  63. def no_frame(self):
  64. return Flag.isSet(self._display_flags, Flag.NOFRAME)
  65. @property
  66. def opengl(self):
  67. return Flag.isSet(self._display_flags, Flag.OPENGL)
  68. @property
  69. def caption(self):
  70. if pygame.display.get_init():
  71. return pygame.display.get_caption()
  72. @caption.setter
  73. def caption(self, caption):
  74. if pygame.display.get_init():
  75. pygame.display.set_caption(caption)
  76. def toggle_fullscreen(self):
  77. if self._isFlagSet(Flag.FULLSCREEN):
  78. self._display_flags ^= Flag.FULLSCREEN
  79. else:
  80. self._display_flags |= Flag.FULLSCREEN
  81. self.set_mode(self._resolution, flags)
  82. def watch_for_resize(self, enable):
  83. if enable == True:
  84. Events.listen("VIDEORESIZE", self._OnVideoResize)
  85. elif enable == False:
  86. Events.unlisten("VIDEORESIZE", self._OnVideoResize)
  87. def set_clear_color(self, r, g, b, a=255):
  88. self._clear_color = pygame.Color(r, g, b, a)
  89. def set_mode(self, resolution, flags):
  90. if (self._init == False):
  91. self._init = True
  92. pygame.init()
  93. self._display_surface = pygame.display.set_mode(resolution, flags)
  94. self._display_flags = self._display_surface.get_flags()
  95. self._resolution = self._display_surface.get_size()
  96. return self
  97. def clear(self):
  98. if self._display_surface is not None:
  99. self._display_surface.fill(self._clear_color)
  100. def flip(self):
  101. if self._init:
  102. pygame.display.flip()
  103. def init(self, width=0, height=0):
  104. if self._init == False:
  105. self._init = True
  106. pygame.display.init()
  107. pygame.font.init() # Because there's really no reason NOT to.
  108. self.set_mode((width, height), self._display_flags)
  109. return self
  110. def close(self):
  111. pygame.quit()
  112. def _OnVideoResize(self, event, data):
  113. self.set_mode(data["size"], self.flags)
  114. print("Resized to {}".format(self.resolution))
  115. # Creating an instance of the _Display class. Really, this game engine is only going to use ONE display.
  116. Display=_Display()