Advertisement
cookertron

Sprite Assembler

May 9th, 2020
1,270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.89 KB | None | 0 0
  1. import lzma, base64
  2. from PIL import Image
  3.  
  4. def b(a):
  5.     return bytes([a])
  6.  
  7. def sl(string):
  8.     return bytes([len(string)]) + string
  9.  
  10. def w(a):
  11.     return int(a).to_bytes(2, "big")
  12.  
  13. def wl(a):
  14.     return int(len(a)).to_bytes(2, "big")
  15.  
  16. def d(a):
  17.     return int(a).to_bytes(4, "big")
  18.  
  19. def dl(a):
  20.     return int(len(a)).to_bytes(4, "big")
  21.  
  22. def t(i, a, r = None):
  23.     I = int(i).to_bytes(2, "big")
  24.     A = bytes([a])
  25.     if r:
  26.         R = int(r).to_bytes(2, "big")
  27.     else:
  28.         R = b''
  29.     return I + A + R
  30.  
  31. def wh(w, h):
  32.     return int(w).to_bytes(2, "big") + int(h).to_bytes(2, "big")
  33.  
  34. image = Image.open('Tiles9x13.png')
  35. spriteSheet = image.load()
  36.  
  37. COLUMNS = 9
  38. ROWS = 13
  39. TILE_WIDTH = int(image.size[0] / COLUMNS)
  40. TILE_HEIGHT = int(image.size[1] / ROWS)
  41. PALETTE = image.palette.tobytes()
  42. PALETTE_LENGTH = int(len(PALETTE) / 3)
  43. IMAGE = image.tobytes()
  44.  
  45.  
  46. #LZ_IMAGE = lzma.compress(image.tobytes())
  47. #LZB64_IMAGE = base64.b64encode(lzImg)
  48.  
  49. DATA = bytes([COLUMNS, ROWS, TILE_WIDTH, TILE_HEIGHT, PALETTE_LENGTH])
  50. DATA += PALETTE + IMAGE
  51.  
  52. """
  53. attributes:
  54. 0 = none
  55. 1 = flip horizontally
  56. 2 = flip vertically
  57. 3 = rotate 90
  58. 4 = rotate 180
  59. 5 = rotate 270
  60. 6 = none range
  61. 7 = fh range static ci
  62. 8 = fv range static ci
  63. 9 = r90 range static ci
  64. 10 = r180 range static ci
  65. 11 = r270 range static ci
  66. 12 = none range +ci
  67. 13 = fh range +ci
  68. 14 = fv range +ci
  69. 15 = r90 range +ci
  70. 16 = r180 range +ci
  71. 17 = r270 range +ci
  72. 18 = none range -ci
  73. 19 = fh range -ci
  74. 20 = fv range -ci
  75. 21 = r90 range -ci
  76. 22 = r180 range -ci
  77. 23 = r270 range -ci
  78. w = 0 copy name attribute (0 - 5)
  79. """
  80. BLANK = 114
  81.  
  82. W, H = TILE_WIDTH, TILE_HEIGHT
  83. SPRITES = \
  84. sl(b'pf') + wh(W, H) + t(0,0) + \
  85. sl(b'ls') + wh(W * 2, H) + t(3, 0) + t(3, 1) + \
  86. sl(b'eg') + wh(W, H) + t(4, 0) + \
  87. sl(b'hy') + wh(W, H) + t(5, 0) + \
  88. sl(b'lt') + wh(W * 2, H) + t(6, 6, 2) + \
  89. sl(b'hsr') + wh(12, H * 2) + t(7, 12, 4) + \
  90. sl(b'hwr') + wh(12, H * 2) + t(7, 12, 2) + t(11, 12, 2) + \
  91. sl(b'hsl') + w(0) + sl(b'hsr') + b(1) + \
  92. sl(b'hwl') + w(0) + sl(b'hwr') + b(1) + \
  93. sl(b'hc1') + wh(12, H * 2) + t(13, 12, 2) + t(17, 12, 2) + \
  94. sl(b'hc2') + wh(12, H * 2) + t(15, 12, 2) + t(19, 12, 2) + \
  95. sl(b'hc3') + wh(12, H * 2) + t(13, 12, 2) + t(21, 12, 2) + \
  96. sl(b'csr') + wh(W, H * 2) + t(23, 12, 2) + \
  97. sl(b'cwr') + wh(W, H * 2) + t(26, 12, 2) + \
  98. sl(b'csl') + w(0) + sl(b'csr') + b(1) + \
  99. sl(b'cwl') + w(0) + sl(b'cwr') + b(1) + \
  100. sl(b'cpr') + wh(W * 2, H * 2) + t(28, 12, 2) + t(24, 12, 2) + \
  101. sl(b'cpl') + w(0) + sl(b'cpr') + b(1) + \
  102. sl(b'cc1') + wh(W, H * 2) +  t(30, 12, 2) + \
  103. sl(b'cc2') + w(0) + sl(b'cc1') + b(1) + \
  104. sl(b'lv') + wh(W, H) + t(32, 0) + \
  105. sl(b'cg') + wh(4 * W, H * 5) + t(BLANK, 0) + t(33, 12, 2) + t(BLANK, 0) + t(35, 12, 2) + t(36, 19, 2) + t(37, 12, 2) + t(38, 19, 2) + \
  106.     t(37, 12, 2) + t(38, 19, 2) + t(39, 12, 2) + t(40, 19, 2) + \
  107. sl(b'bc1') + wh(2 * W, 2 * H) + t(BLANK, 0) + t(41, 12, 3) + \
  108. sl(b'bc2') + wh(2 * W, 2 * H) + t(BLANK, 0) + t(44, 12, 3) + \
  109. sl(b'be1') + wh(2 * W, 2 * H) + t(47, 12, 3) + t(49, 1) + \
  110. sl(b'be2') + w(0) + sl(b'be1') + b(3) + \
  111. sl(b'tle') + wh(W * 21, H * 2) + t(50, 12, 5) + t(54, 1) + t(50, 12, 2) + t(55, 12, 6) + t(BLANK, 0) + t(59, 12, 4) + t(61, 12, 2) +\
  112.     t(63, 12, 3) + t(65, 1) + t(66, 0) + t(66, 1) + t(63, 12, 2) + t(67, 12, 2) + t(57, 14, 2) + t(69, 0) + t(60, 2) + t(BLANK, 0) +\
  113.     t(69, 0) + t(60, 2) + t(70, 12, 2) + t(70, 12, 2) + \
  114. sl(b'fnt') + wh(W * 43, H) + t(72, 12, 43)
  115.  
  116.  
  117. DATA += SPRITES
  118.  
  119. LZ_DATA = lzma.compress(DATA)
  120. LZB64_DATA = base64.b64encode(LZ_DATA)
  121.  
  122. writableData = bytes("CHUCKIE_SPRITES = b'", "utf-8") + LZB64_DATA + b"'"
  123. writableDataLen = len(writableData)
  124. writableDataChunks = int(writableDataLen / 100)
  125.  
  126. #fn, ex = filename.split(".")
  127. f= open("sprites.py","wb")
  128. for index in range(writableDataChunks):
  129.     f.write(writableData[index * 100: index * 100 + 100] + b"\\\n")
  130. f.write(writableData[(index + 1) * 100:] + b"\n")
  131. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement