Skip to content

Instantly share code, notes, and snippets.

@expectocode
Last active April 13, 2017 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save expectocode/8f972e020b80fa055996da9a20b281dd to your computer and use it in GitHub Desktop.
Save expectocode/8f972e020b80fa055996da9a20b281dd to your computer and use it in GitHub Desktop.
Game of Life implementation to make an animated wallpaper
#!/usr/bin/env python3
#inspired by https://f.0x52.eu/blog/game_of_life
from collections import Counter
from PIL import Image,ImageDraw
from time import sleep
import subprocess as sub
from math import sin,cos,radians
width = round(1366/8)
height = round(768/8)
#if the y coord of a live cell is 0, 3 neighbours have y coord (height - 1)
#-1%50 = 49
def getneighbours(coords):
return [((coords[0])%width,(coords[1]+1)%height),
((coords[0])%width,(coords[1]-1)%height),
((coords[0]+1)%width,(coords[1]+1)%height),
((coords[0]+1)%width,(coords[1])%height),
((coords[0]+1)%width,(coords[1]-1)%height),
((coords[0]-1)%width,(coords[1]+1)%height),
((coords[0]-1)%width,(coords[1])%height),
((coords[0]-1)%width,(coords[1]-1)%height)]
#translate coords
def trans(coordlist,x,y):
return [((tup[0]+x)%width,(tup[1]+y)%height) for tup in coordlist]
def y_flip(coordlist):
return [(tup[0],tup[1]*-1) for tup in coordlist]
def x_flip(coordlist):
return [(tup[0]*-1,tup[1]) for tup in coordlist]
def rotate2d(degrees,point,origin):
"""
A rotation function that rotates a point around a point
to rotate around the origin use (0,0)
https://ubuntuforums.org/showthread.php?t=975315
"""
x = point[0] - origin[0]
yorz = point[1] - origin[1]
newx = (x*cos(radians(degrees))) - (yorz*sin(radians(degrees)))
newyorz = (x*sin(radians(degrees))) + (yorz*cos(radians(degrees)))
newx += origin[0]
newyorz += origin[1]
return (round(newx),round(newyorz))
def rotate(coordlist,degrees):
centercoord = (round(sum([tup[0] for tup in coordlist])/len(coordlist)),
round(sum([tup[1] for tup in coordlist])/len(coordlist)))
return [rotate2d(degrees,coord,centercoord) for coord in coordlist]
DEAD = (44, 62, 80) #RGB
ALIVE = (224, 224, 224) #RGB
glider = [(3,4),(4,5),(5,3),(5,4),(5,5)]
gospergun = [(24, 0), (22, 1), (24, 1), (12, 2), (13, 2), (20, 2), (21, 2), (34, 2), (35, 2), (11, 3), (15, 3), (20, 3), (21, 3), (34, 3), (35, 3), (0, 4), (1, 4), (10, 4), (16, 4), (20, 4), (21, 4), (0, 5), (1, 5), (10, 5), (14, 5), (16, 5), (17, 5), (22, 5), (24, 5), (10, 6), (16, 6), (24, 6), (11, 7), (15, 7), (12, 8), (13, 8)]
copperhead = [(0,3),(0,4),(1,3),(1,4),(4,1),(4,2),(4,3),(4,4),(4,5),(4,6),(5,0),(5,3),(5,4),(5,7),(6,0),(6,7),(8,0),(8,1),(8,2),(8,5),(8,6),(8,7),(9,0),(9,1),(9,2),(9,5),(9,6),(9,7),(10,1),(10,2),(10,5),(10,6),(11,1),(11,2),(11,5),(11,6)]
lwss = [(0,1),(0,2),(0,3),(1,0),(1,3),(2,3),(3,3),(4,0),(4,2)]
weekender = [(0, 2), (1, 0), (1, 1), (1, 3), (1, 4), (2, 2), (2, 5), (2, 7), (3, 7), (4, 7), (4, 9), (5, 7), (5, 10), (6, 5), (6, 6), (6, 10), (7, 5), (7, 6), (8, 5), (8, 6), (9, 5), (9, 6), (9, 10), (10, 7), (10, 10), (11, 7), (11, 9), (12, 7), (13, 2), (13, 5), (13, 7), (14, 0), (14, 1), (14, 3), (14, 4), (15, 2)]
shlick = [(1, 0), (4, 0), (0, 1), (0, 2), (4, 2), (0, 3), (1, 3), (2, 3), (3, 3), (13, 3), (14, 3), (6, 4), (7, 4), (8, 4), (14, 4), (15, 4), (6, 5), (7, 5), (9, 5), (10, 5), (17, 5), (18, 5), (19, 5), (6, 6), (7, 6), (8, 6), (14, 6), (15, 6), (0, 7), (1, 7), (2, 7), (3, 7), (13, 7), (14, 7), (0, 8), (4, 8), (0, 9), (1, 10), (4, 10)]
b52 = [(0, 4), (0, 5), (0, 8), (0, 14), (0, 15), (1, 0), (1, 1), (1, 4), (1, 5), (1, 9), (1, 13), (1, 16), (2, 0), (2, 1), (2, 9), (2, 13), (2, 15), (2, 18), (3, 5), (3, 6), (3, 7), (3, 8), (3, 14), (4, 15), (4, 16), (4, 18), (5, 17), (9, 4), (9, 5), (10, 4), (10, 5), (17, 18), (18, 17), (18, 19), (19, 2), (19, 18), (20, 1), (20, 3), (21, 2), (21, 8), (21, 9), (21, 10), (21, 15), (22, 8), (22, 10), (22, 16), (22, 17), (23, 10), (23, 15), (23, 16), (27, 6), (27, 7), (28, 7), (28, 8), (28, 15), (28, 16), (29, 15), (29, 16), (33, 3), (34, 2), (34, 4), (34, 5), (35, 6), (35, 15), (35, 16), (35, 19), (36, 2), (36, 5), (36, 7), (36, 11), (36, 12), (36, 15), (36, 16), (36, 20), (37, 4), (37, 7), (37, 11), (37, 12), (37, 20), (38, 5), (38, 6), (38, 16), (38, 17), (38, 18), (38, 19)]
def main():
#gen1_alivecells = trans(copperhead,85-6,48-4,)
gen1_alivecells = trans(gospergun,round(width/2)-17,round(height/2)-4)
#race
#gen1_alivecells = rotate(trans(weekender,round(width/2)-9,60),90) + trans(x_flip(lwss),round(width/2)-2,40) + trans(copperhead,round(width/2)-6,20)
#gen1_alivecells = trans(x_flip(lwss),round(width/2)-2,round(height/2)-2-20) + trans(copperhead,round(width/2)-6,round(width/2)-4-30)
#gen1_alivecells = trans(shlick,round(width/2)-10,round(height/2)-4)
#gen1_alivecells = rotate(trans(b52,round(width/2)-20,round(height/2)-10),90)
#glider formation
#gen1_alivecells = []
#for x in range(5):
# for y in range(5):
# gen1_alivecells.extend(trans(glider,60+10*x,30+6*y))
#main loop
while True:
im = Image.new('RGB', (width,height),color=DEAD)
gen1_neighbours_list = []
gen2_alivecells = []
for cell in gen1_alivecells:
gen1_neighbours_list.extend(getneighbours(cell))
gen1_neighbours_dict = Counter(gen1_neighbours_list)
for key,value in gen1_neighbours_dict.items():
if key in gen1_alivecells and (3 == value or 2 == value):
#print(key,value)
gen2_alivecells.append(key)
elif key not in gen1_alivecells and 3 == value:
#print(key,value)
gen2_alivecells.append(key)
gen1_alivecells = gen2_alivecells
draw = ImageDraw.Draw(im)
draw.point(gen1_alivecells,fill=ALIVE)
del draw
#print(gen1_alivecells)
scale_factor = 8
im2 = im.resize((im.width * scale_factor, im.height * scale_factor))
im2.save('out.png')
sleep(0.5)
sub.run(['feh', '--bg-fill', 'out.png'],stdout=sub.PIPE,stderr=sub.PIPE)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment