Legend of the Gold Box... A game written for the LOWREZJAM 2018 game jam
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

141 lines
3.4KB

  1. '''
  2. Filename core.py
  3. Author: Bryan "ObsidianBlk" Miller
  4. Date Created: 8/1/2018
  5. Python Version: 3.7
  6. '''
  7. import time
  8. import pygame
  9. class Time:
  10. def __init__(self):
  11. self._dticks = 0
  12. self._ldelta = 0
  13. self._lastTick = 0
  14. @property
  15. def delta(self):
  16. tick = int(round(time.time() * 1000))
  17. dt = 0
  18. if self._lastTick > 0:
  19. dt = tick - self._lastTick
  20. self._lastTick = tick
  21. self._ldelta = dt
  22. self._dticks += dt
  23. return dt
  24. @property
  25. def last_delta(self):
  26. return self._ldelta
  27. @property
  28. def aliveTicks(self):
  29. tick = int(round(time.time() * 1000))
  30. dt = 0
  31. if self._lastTick > 0:
  32. dt = tick - self._lastTick
  33. return self._dticks + dt
  34. @property
  35. def aliveSeconds(self):
  36. return self.aliveTicks / 1000.0
  37. def reset(self):
  38. self.dticks = 0
  39. self._lastTick = int(round(time.time() * 1000))
  40. class Display:
  41. def __init__(self, width=640, height=480):
  42. self._init = False
  43. self._resolution = (width, height)
  44. self._display_surface = None
  45. self._display_flags = Display.FLAG_RESIZABLE | Display.FLAG_HWSURFACE | Display.FLAG_DOUBLEBUF
  46. def _isFlagSet(self, flag):
  47. return (self._display_flags & flag) == flag
  48. @property
  49. def surface(self):
  50. return self._display_surface
  51. @property
  52. def width(self):
  53. if self._display_surface is None:
  54. return 0
  55. return self._resolution[0]
  56. @property
  57. def height(self):
  58. if self._display_surface is None:
  59. return 0
  60. return self._resolution[1]
  61. @property
  62. def resolution(self):
  63. if self._display_surface is None:
  64. return (0,0)
  65. return self._display_surface.get_size()
  66. @property
  67. def fullscreen(self):
  68. return self._isFlagSet(Display.FLAG_FULLSCREEN)
  69. @property
  70. def double_buffered(self):
  71. return self._isFlagSet(Display.FLAG_DOUBLEBUF)
  72. @property
  73. def resizable(self):
  74. return self._isFlagSet(Display.FLAG_RESIZABLE)
  75. @property
  76. def no_frame(self):
  77. return self._isFlagSet(Display.FLAG_NOFRAME)
  78. @property
  79. def opengl(self):
  80. return self._isFlagSet(Display.FLAG_OPENGL)
  81. def toggle_fullscreen(self):
  82. if self._isFlagSet(Display.FLAG_FULLSCREEN):
  83. self._display_flags ^= Display.FLAG_FULLSCREEN
  84. else:
  85. self._display_flags |= Display.FLAG_FULLSCREEN
  86. self.set_mode(self._resolution, flags)
  87. def set_mode(self, resolution, flags):
  88. if (self._init == False):
  89. self._init = True
  90. pygame.init()
  91. self._display_surface = pygame.display.set_mode(resolution, flags)
  92. self._display_flags = self._display_surface.get_flags()
  93. self._resolution = self._display_surface.get_size()
  94. return self
  95. def init(self):
  96. if self._init == False:
  97. self._init = True
  98. pygame.init()
  99. self.set_mode(self._resolution, self._display_flags)
  100. return self
  101. def close(self):
  102. pygame.quit()
  103. FLAG_SWSURFACE = pygame.SWSURFACE
  104. FLAG_HWSURFACE = pygame.HWSURFACE
  105. FLAG_HWPALETTE = pygame.HWPALETTE
  106. FLAG_DOUBLEBUF = pygame.DOUBLEBUF
  107. FLAG_FULLSCREEN = pygame.FULLSCREEN
  108. FLAG_OPENGL = pygame.OPENGL
  109. FLAG_RESIZABLE = pygame.RESIZABLE
  110. FLAG_NOFRAME = pygame.NOFRAME