Browse Source

game of life animation overhaul

master
informaniac 6 years ago
parent
commit
d51becbfc2
  1. 139
      generators/gameOfLife.py

139
generators/gameOfLife.py

@ -1,10 +1,9 @@ @@ -1,10 +1,9 @@
import sys
import ast
import time as t
import random as r
import blup.animation
import blup.frame
from blup import frame
from blup import animation
import blup.writebml
import argparse
r.seed()
@ -16,81 +15,89 @@ COLORS = 3 @@ -16,81 +15,89 @@ COLORS = 3
DELAY = 300
DEAD = 0
DEADCOLOR = (0,0,0)
DEAD_COLOR = (0, 0, 0)
ALIVE = 1
ALIVECOLOR = (0,0,255)
ALIVE_COLOR = (0, 0, 255)
dimension = blup.frame.FrameDimension(WIDTH, HEIGHT, COLOR_DEPTH, COLORS)
animation = blup.animation.Animation(dimension)
dimension = frame.FrameDimension(WIDTH, HEIGHT, COLOR_DEPTH, COLORS)
game_of_life_animation = animation.Animation(dimension)
def generate_rand_game_board():
board = []
for rows in range(HEIGHT):
x_coordinates = []
for cols in range(WIDTH):
x_coordinates.append(r.randint(0, 1))
board.append(x_coordinates)
return board
def generateRandomGameBoard():
gameBoard = []
for i in range(HEIGHT):
xCoordinates = []
for j in range(WIDTH):
xCoordinates.append(r.randint(0, 1))
gameBoard.append(xCoordinates)
return gameBoard
def get_neighbors(x, y):
for rowNumber in range(y-1, y+2):
if 0 <= rowNumber < HEIGHT:
for colNumber in range(x-1, x+2):
if 0 <= colNumber < WIDTH:
if not (y==rowNumber and x==colNumber):
yield gameBoard[rowNumber][colNumber]
if not (y == rowNumber and x == colNumber):
yield game_board[rowNumber][colNumber]
if len(sys.argv) == 1:
gameBoard = generateRandomGameBoard()
gameBoardIsSanitized = True
elif len(sys.argv) != 2:
print("check args. found "+str(len(sys.argv)))
exit()
if not gameBoardIsSanitized:
gameBoard = ast.literal_eval(sys.argv[1])
if __name__ == "__main__":
argument_parser = argparse.ArgumentParser(description="Game of Life bml generator")
argument_parser.add_argument('-w', dest="world", type=list, default=None, help="pre-constructed world (optional)")
argument_parser.add_argument('-o', dest="output_path", type=str, help="output file (mandatory)")
args = argument_parser.parse_args()
if len(gameBoard) != HEIGHT:
print("len(gameBoard) != "+str(HEIGHT)+". counted "+str(len(gameBoard)))
if args.output_path is None:
argument_parser.print_help()
exit()
for i in gameBoard:
if len(i) != WIDTH:
print("len(rows) != "+str(WIDTH)+". counted "+str(len(i)))
if args.world is None:
game_board = generate_rand_game_board()
else:
game_board = ast.literal_eval(args.world)
if len(game_board) != HEIGHT:
print("len(gameBoard) != " + str(HEIGHT) + ". counted " + str(len(game_board)))
exit()
for frame in range(500):
# construct frame
newframe = blup.animation.AnimationFrame(dimension, DELAY)
for rowIndex in range(HEIGHT):
for colIndex in range(WIDTH):
cellStatus = gameBoard[rowIndex][colIndex]
if cellStatus == ALIVE:
newframe.setPixel(colIndex, rowIndex, ALIVECOLOR)
else:
newframe.setPixel(colIndex, rowIndex, DEADCOLOR)
animation.addFrame(newframe)
# calculate next step
gameBoardNextStep = []
lastGameBoard = gameBoard
for rowIndex in range(HEIGHT):
newRow = []
for colIndex in range(WIDTH):
neighborList = list(get_neighbors(colIndex, rowIndex))
aliveNeighbors = neighborList.count(1)
if aliveNeighbors < 2:
newRow.append(DEAD)
elif aliveNeighbors == 2:
newRow.append(gameBoard[rowIndex][colIndex])
elif aliveNeighbors == 3:
newRow.append(ALIVE)
elif aliveNeighbors >= 4:
newRow.append(DEAD)
gameBoardNextStep.append(newRow)
gameBoard = gameBoardNextStep
if(gameBoard == lastGameBoard):
for i in range(20):
animation.addFrame(newframe)
break
blup.writebml.writeBml(animation, '/home/pi/animations/gameOfLife.bml')
for i in game_board:
if len(i) != WIDTH:
print("len(rows) != "+str(WIDTH)+". counted "+str(len(i)))
exit()
for frame in range(500):
# construct frame
new_frame = animation.AnimationFrame(dimension, DELAY)
for row_index in range(HEIGHT):
for col_index in range(WIDTH):
cell_status = game_board[row_index][col_index]
if cell_status == ALIVE:
new_frame.setPixel(col_index, row_index, ALIVE_COLOR)
else:
new_frame.setPixel(col_index, row_index, DEAD_COLOR)
game_of_life_animation.addFrame(new_frame)
# calculate next step
next_game_board_step = []
previous_game_board_step = game_board
for row_index in range(HEIGHT):
new_row = []
for col_index in range(WIDTH):
neighbor_list = list(get_neighbors(col_index, row_index))
alive_neighbors = neighbor_list.count(1)
if alive_neighbors < 2:
new_row.append(DEAD)
elif alive_neighbors == 2:
new_row.append(game_board[row_index][col_index])
elif alive_neighbors == 3:
new_row.append(ALIVE)
elif alive_neighbors >= 4:
new_row.append(DEAD)
next_game_board_step.append(new_row)
game_board = next_game_board_step
if game_board == previous_game_board_step:
for i in range(20):
game_of_life_animation.addFrame(new_frame)
break
blup.writebml.writeBml(game_of_life_animation, args.output_path)

Loading…
Cancel
Save