__init__.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. #!/usr/bin/env python
  2. #! coding: utf-8
  3. #! python3
  4. # Python ish game layer à la GM
  5. # Dirty as hay, if you learn python with this file, you will learn garbage.
  6. # 2022
  7. import random
  8. import pygame
  9. import pygame.freetype
  10. #===== Variable Functions =====
  11. #===== Asset Management =====
  12. #----- Assets And Tags -----
  13. #----- Animation Curves -----
  14. #----- Sprites -----
  15. #----- Tile Sets -----
  16. #----- Audio -----
  17. #----- Paths -----
  18. #----- Scripts -----
  19. #----- Shaders -----
  20. #----- Sequences -----
  21. #----- Fonts -----
  22. _internal_fonts = []
  23. def font_add( name=None, size=16, bold=False, italic=False, first=32, last=128 ) :
  24. global _internal_fonts
  25. if size < 1 : size = 1
  26. if name != None :
  27. font = pygame.freetype.Font( name, size )
  28. else :
  29. font = pygame.font.SysFont( None, size )
  30. _internal_fonts.append( font )
  31. return font
  32. #----- Timelines -----
  33. #----- Objects -----
  34. #----- Instances -----
  35. #----- Rooms -----
  36. #===== General Game Control =====
  37. game_id = 0
  38. game_save_id = game_id
  39. game_display_name = "Great Mishmash"
  40. game_project_name = "Undefined"
  41. #
  42. def game_end() :
  43. pygame.quit()
  44. #
  45. def game_restart( ) :
  46. pass
  47. #
  48. def game_load( ) :
  49. pass
  50. #
  51. def game_load_buffer( ) :
  52. pass
  53. #
  54. def game_save( ) :
  55. pass
  56. #
  57. def game_save_buffer( ) :
  58. pass
  59. #
  60. def game_get_speed( ) :
  61. pass
  62. #
  63. def game_set_speed( ) :
  64. pass
  65. #
  66. def highscore_add() :
  67. pass
  68. #
  69. def highscore_name() :
  70. pass
  71. #
  72. def highscore_value() :
  73. pass
  74. #
  75. def highscore_clear() :
  76. pass
  77. #
  78. def draw_highscore() :
  79. pass
  80. #
  81. def cursor_sprite() :
  82. pass
  83. #===== Movement And Collisions =====
  84. #===== Drawing =====
  85. _draw_surface_currentSurface = None
  86. #----- Colour And Alpha -----
  87. c_aqua = (0,255,255)
  88. c_black = (0,0,0)
  89. c_blue = (0,0,255)
  90. c_dkgray = (64,64,64)
  91. c_fuchsia = (255,0,255)
  92. c_gray = (128,128,128)
  93. c_green = (0,128,0)
  94. c_lime = (0,255,0)
  95. c_ltgray = (192,192,192)
  96. c_maroon = (128,0,0)
  97. c_navy = (0,0,128)
  98. c_olive = (128,128,0)
  99. c_orange = (64,160,0)
  100. c_purple = (128,0,128)
  101. c_red = (255,0,0)
  102. c_silver = (192,192,192)
  103. c_teal = (0,128,128)
  104. c_white = (255,255,255)
  105. c_yellow = (255,255,0)
  106. _draw_color_currentColor = c_black
  107. _draw_color_currentAlpha = 1
  108. #
  109. def colour_get_blue() :
  110. pass
  111. #
  112. def colour_get_green() :
  113. pass
  114. #
  115. def colour_get_red() :
  116. pass
  117. #
  118. def colour_get_hue() :
  119. pass
  120. #
  121. def colour_get_saturation() :
  122. pass
  123. #
  124. def colour_get_value() :
  125. pass
  126. #
  127. def draw_getpixel() :
  128. pass
  129. #
  130. def draw_getpixel_ext() :
  131. pass
  132. #
  133. def draw_get_colour() :
  134. pass
  135. #
  136. def draw_get_alpha() :
  137. pass
  138. #
  139. def make_colour_hsv() :
  140. pass
  141. #
  142. def make_colour_rgb( r=0, g=0, b=0) :
  143. return (r,g,b)
  144. #
  145. def merge_colour() :
  146. pass
  147. #
  148. def draw_clear( color=(0,0,0) ) :
  149. global _draw_surface_currentSurface
  150. _draw_surface_currentSurface.fill( color )
  151. #
  152. def draw_clear_alpha() :
  153. pass
  154. #
  155. def draw_set_alpha( alpha=0 ) :
  156. global _draw_color_currentAlpha
  157. _draw_color_currentAlpha = alpha
  158. #
  159. def draw_set_color( color=(0,0,0) ) :
  160. global _draw_color_currentColor
  161. _draw_color_currentColor = color
  162. #Non standard GM
  163. def draw_sanitize_color( color=0 ):
  164. # string families (Hex and HTML)
  165. if isinstance( color, str ) :
  166. #TODO: scan for $ or # for hex color, and maybe some html names
  167. color = (0,0,0)
  168. # RGB integer
  169. if isinstance( color, int ) :
  170. make_color_rgb( (color % 256), ((color/256)%256), ((color/65536)%256) )
  171. # multiple values
  172. if isinstance( color, tuple ) or isinstance( color, list ) :
  173. if len( color ) != 3 :
  174. color = (0,0,0)
  175. return color
  176. #----- GPU Control -----
  177. #----- Mipmapping -----
  178. #----- Basic Forms -----
  179. #----- Sprites And Tiles -----
  180. #----- Text -----
  181. fa_left = "left"
  182. fa_center = "center"
  183. fa_right = "right"
  184. fa_top = "top"
  185. fa_middle = "middle"
  186. fa_bottom = "bottom"
  187. _draw_text_currentFont = None
  188. _draw_text_hAlign = fa_left
  189. _draw_text_vAlign = fa_top
  190. #
  191. def draw_set_font( font=None ) :
  192. global _internal_fonts
  193. global _draw_text_currentFont
  194. if font not in _internal_fonts : font = _internal_fonts[0]
  195. _draw_text_currentFont = font
  196. #
  197. def draw_set_halign( halign=fa_left ) :
  198. global _draw_text_hAlign
  199. if halign not in [fa_left, fa_center, fa_right] :
  200. halign = fa_left
  201. _draw_text_hAlign = halign
  202. #
  203. def draw_set_valign( valign=fa_top ) :
  204. global _draw_text_vAlign
  205. if valign not in [fa_top, fa_middle, fa_bottom] :
  206. valign = fa_top
  207. _draw_text_vAlign = valign
  208. #
  209. def draw_get_font( ) :
  210. global _draw_text_currentFont
  211. return _draw_text_currentFont
  212. #
  213. def draw_get_halign( ) :
  214. global _draw_text_hAlign
  215. return _draw_text_hAlign
  216. #
  217. def draw_get_valign( ) :
  218. global _draw_text_vAlign
  219. return _draw_text_vAlign
  220. #
  221. def draw_text( coords=(0,0), string="Text" ) :
  222. draw_text_color( coords, string, _draw_color_currentColor, _draw_color_currentAlpha )
  223. #
  224. def draw_text_ext( ) :
  225. pass
  226. #
  227. def draw_text_color( coords=(0,0), string="Text", colors=( c_black, c_black, c_white, c_white), alpha=0.5 ) :
  228. global _draw_surface_currentSurface
  229. global _draw_text_currentFont
  230. global _draw_text_hAlign
  231. global _draw_text_vAlign
  232. error = None
  233. # Normalization and sanity checks
  234. if _draw_surface_currentSurface == None : error = "No surface"
  235. if _draw_text_currentFont == None : error = "No Font"
  236. if isinstance( coords, tuple ) or isinstance( coords, list ) :
  237. x,y = coords
  238. else :
  239. error = "Malformed coords"
  240. if not isinstance( string, str ) :
  241. string = str( string )
  242. if isinstance( colors, tuple ) or isinstance( colors, list ) :
  243. if len( colors ) == 3 :
  244. c = colors
  245. colors = (c,c,c,c)
  246. if error == None :
  247. textImage = _draw_text_currentFont.render( string, False, colors[0])
  248. # Compute coordinates
  249. if _draw_text_hAlign == fa_center :
  250. w,h = textImage.get_size()
  251. x -= w/2
  252. if _draw_text_hAlign == fa_right :
  253. w,h = textImage.get_size()
  254. x += w
  255. if _draw_text_vAlign == fa_middle :
  256. w,h = textImage.get_size()
  257. y -= h/2
  258. if _draw_text_vAlign == fa_bottom :
  259. w,h = textImage.get_size()
  260. y += h
  261. _draw_surface_currentSurface.blit( textImage, (x,y) )
  262. else :
  263. print( error )
  264. #
  265. def draw_text_transformed( ) :
  266. pass
  267. #
  268. def draw_text_ext_colour( ) :
  269. pass
  270. #
  271. def draw_text_ext_transformed( ) :
  272. pass
  273. #
  274. def draw_text_transformed_colour( ) :
  275. pass
  276. #
  277. def draw_text_ext_transformed_colour( ) :
  278. pass
  279. #----- Primitives And Vertex Formats -----
  280. #----- Surfaces -----
  281. _internal_surfaces = []
  282. #
  283. def surface_exists() :
  284. pass
  285. #
  286. def surface_create( size=(128,128) ) :
  287. global _internal_surfaces
  288. surface = pygame.Surface( size, pygame.SRCALPHA )
  289. _internal_surfaces.append( surface )
  290. return surface
  291. #
  292. def surface_create_ext() :
  293. pass
  294. #
  295. def surface_resize() :
  296. pass
  297. #
  298. def surface_set_target( surface=None ) :
  299. global _internal_surfaces
  300. global _draw_surface_currentSurface
  301. if surface not in _internal_surfaces : surface = _internal_surfaces[0]
  302. _draw_surface_currentSurface = surface
  303. #----- Lighting -----
  304. #----- Particles -----
  305. #----- Textures -----
  306. #----- Shaders -----
  307. #----- Video Playback -----
  308. #===== Cameras And Display =====
  309. #===== Game Input =====
  310. #===== Data Structures =====
  311. #===== Strings =====
  312. #===== Maths And Numbers =====
  313. #===== Physics =====
  314. #===== Asynchronous Functions =====
  315. #===== Networking =====
  316. #===== File Handling =====
  317. #===== Buffers =====
  318. #===== Debugging =====
  319. #===== Garbage Collection =====
  320. # Miscelaneous
  321. # Choose one item of a list at random
  322. def choose( l=None ) :
  323. r = None
  324. if l != None :
  325. r = l[ random.randrange( 0, len(l) ) ]
  326. return r
  327. # Initialisation
  328. if __name__!="__main__":
  329. WINDOW_SIZE = (1024,800)
  330. pygame.init()
  331. pygame.mixer.init()
  332. surface_create( WINDOW_SIZE )
  333. _internal_surfaces[0] = pygame.display.set_mode( WINDOW_SIZE )
  334. surface_set_target()
  335. _draw_text_currentFont = font_add()