Skip to content

Instantly share code, notes, and snippets.

@atarola
Last active August 30, 2020 01:30
Show Gist options
  • Save atarola/5859722 to your computer and use it in GitHub Desktop.
Save atarola/5859722 to your computer and use it in GitHub Desktop.
Pixel Spaceships!
#!/usr/bin/env python
import random
import pygame
from pygame.locals import *
#
# Pixel Spaceships!
# Original Idea: http://web.archive.org/web/20080228054410/http://www.davebollinger.com/works/pixelspaceships/
#
def pixel_spaceship(signature):
small = [[' ' for x in range(16)] for y in range(16)]
# always filled in pixels
filled_pixels = [
(6, 6), (6, 7), (7, 6), (7, 7), (8, 6),
(8, 7), (11, 6), (11, 7), (12, 6), (12, 7),
]
for pos in filled_pixels:
x, y = pos
small[x][y] = 3
# possible places to put a pixel
possible_pixels = [
(1, 6), (1, 7),
(2, 5), (2, 6), (2, 7),
(3, 5), (3, 6), (3, 7),
(4, 4), (4, 5), (4, 6), (4, 7),
(5, 4), (5, 5), (5, 6), (5, 7),
(6, 3), (6, 4), (6, 5),
(7, 3), (7, 4), (7, 5),
(8, 2), (8, 3), (8, 4), (8, 5),
(9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7),
(10, 1), (10, 2), (10, 3), (10, 4), (10, 5), (10, 6), (10, 7),
(11, 1), (11, 2), (11, 3), (11, 4), (11, 5),
(12, 1), (12, 2), (12, 3), (12, 4), (12, 5),
(13, 2), (13, 3), (13, 4), (13, 5), (13, 6), (13, 7),
(14, 3), (14, 4), (14, 5), (14, 6), (14, 7)
]
# use the signature to create the ship
for pos in possible_pixels:
# if the smallest bit in the signature is true
if (signature & 1 == 1):
x, y = pos
small[x][y] = 2
# shift the signature
signature >>= 1
# mirror the left side to the right
for row in small:
for i in range(8):
j = 15 - i
row[j] = row[i]
# expand the array to 32 x 32
big = [[' ' for x in range(32)] for y in range(32)]
ox = 0
oy = 0
for x in range(0, 32, 2):
for y in range(0, 32, 2):
value = small[ox][oy]
big[x][y] = value
big[x + 1][y] = value
big[x][y + 1] = value
big[x + 1][y + 1] = value
oy += 1
ox += 1
oy = 0
# wrap all the solid pixels twice
colors = [2, 3]
for x in range(32):
for y in range(32):
if big[x][y] in colors:
continue
if (x - 1 >= 0) and (big[x - 1][y] in colors):
big[x][y] = 1
continue
if (x + 1 < 32) and (big[x + 1][y] in colors):
big[x][y] = 1
continue
if (y - 1 >= 0) and (big[x][y - 1] in colors):
big[x][y] = 1
continue
if (y + 1 < 32) and (big[x][y + 1] in colors):
big[x][y] = 1
continue
colors = [1, 2, 3]
for x in range(32):
for y in range(32):
if big[x][y] in colors:
continue
if (x - 1 >= 0) and (big[x - 1][y] in colors):
big[x][y] = 0
continue
if (x + 1 < 32) and (big[x + 1][y] in colors):
big[x][y] = 0
continue
if (y - 1 >= 0) and (big[x][y - 1] in colors):
big[x][y] = 0
continue
if (y + 1 < 32) and (big[x][y + 1] in colors):
big[x][y] = 0
continue
return big
def draw_spaceship(surface, array):
surface.fill(pygame.Color('#999999FF'))
pixelarray = None
try:
pixelarray = pygame.PixelArray(surface)
color_map = {
0: '#00000088',
1: '#4E9A06FF',
2: '#73D216FF',
3: '#8AE234FF'
}
for x in range(32):
for y in range(32):
a = x + 48
b = y + 48
color = color_map.get(array[x][y], None)
if color is None:
continue
pixelarray[b][a] = surface.map_rgb(pygame.Color(color))
finally:
if pixelarray is not None:
pixelarray = None
stop = False
pygame.init()
clock = pygame.time.Clock()
pygame.display.set_caption('Pixel Spaceship!')
surface = pygame.display.set_mode((128, 128))
signature = random.getrandbits(58)
array = pixel_spaceship(signature)
draw_spaceship(surface, array)
while not stop:
events = pygame.event.get()
for event in events:
if event.type == QUIT:
stop = True
continue
if event.type == MOUSEBUTTONUP:
signature = random.getrandbits(58)
array = pixel_spaceship(signature)
draw_spaceship(surface, array)
pygame.display.flip()
clock.tick(30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment