Advertisement
Guest User

Frank Force's Tiny Sprite Generator in Python

a guest
Aug 31st, 2020
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.75 KB | None | 0 0
  1. # Tiny Sprite Sheet Generator by France Force, 2020
  2. #  Python version by Ben Ferguson - MIT License
  3.  
  4. # Requires Pillow for Python3
  5. from PIL import Image, ImageDraw
  6. import random
  7. import math
  8. from datetime import datetime
  9.  
  10. i = (16 * 32) - 1           # Sprite number
  11. x, j, pas, s, X, Y = 0, 0, 0, 0, 0, 0
  12.  
  13. px_b = (0, 0, 0)            # pixel colors - black
  14. px_c = (0, 0, 0)            # pen color
  15. img = Image.new('RGB', (512, 256), "white")
  16. pixels = img.load()         # loads in pixel data as assignable array
  17.  
  18. # Set seed to constant if you like!
  19. seed = datetime.now().timestamp()       # Sets random library seed to current system time
  20.  
  21. def R():                    # Define random function
  22.     global s
  23.     s = s + 1
  24.     r = int((math.sin(s + i*i) + 1)*1e9 % 256) | 0
  25.     return r
  26.  
  27. def PxRect(x, y, c):        # Draw a black border
  28.     pixels[x-1, y-1] = c
  29.     pixels[x, y-1] = c
  30.     pixels[x+1, y-1] = c
  31.     pixels[x-1, y] = c
  32.     pixels[x+1, y] = c
  33.     pixels[x-1, y+1] = c
  34.     pixels[x, y+1] = c
  35.     pixels[x+1, y+1] = c
  36.  
  37. i = (32 * 16)-1                 # 32 rows x 16 columns of sprites
  38. while i >= 0:              
  39.     pas = 4                     # 4 passes: outline left/right and fill left/right
  40.     while pas > 0:
  41.         s = seed
  42.         j = int(R()/5 + 50)|0       # sprite pixels between 50-101
  43.         while j > 0:
  44.             X = j & 7
  45.             Y = j >> 3              # X/Y pixel index of sprite
  46.             if (R()**2 / 2e3) <= (X*X + (Y-5)**2):
  47.                 j = j - 1           # Skip this iteration if distance is above rand
  48.                 continue    
  49.             if R() < 19:            # small chance of new random color
  50.                 px_c = (R(), R(), R())     
  51.             rx = 7 + i%32*16 - pas%2*2*X + X    # X pos flipped, if even
  52.             ry = 2 + (i>>5)*16 + Y              # y pos
  53.             if pas > 2:
  54.                 PxRect(rx, ry, px_b)            # first two passes draw outline rects
  55.             pixels[rx, ry] = px_c           # color pixel!
  56.             j = j - 1
  57.         pas = pas - 1
  58.     i = i - 1
  59. img.show()      # open created image
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement