Created
December 31, 2023 02:16
-
-
Save shauneccles/ec1fbfa7d6d6bc0d4e0e0dd0d5f1e954 to your computer and use it in GitHub Desktop.
game_of_life.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import timeit | |
import voluptuous as vol | |
from ledfx.effects.gradient import GradientEffect | |
from ledfx.effects.twod import Twod | |
import seagull as sg | |
import seagull.lifeforms as lf | |
import numpy as np | |
from PIL import Image | |
_LOGGER = logging.getLogger(__name__) | |
# Anywhere you see template, replace it with your own class reference / name | |
class GameOfLife(Twod, GradientEffect): | |
NAME = "Game of Life" | |
CATEGORY = "Matrix" | |
# add keys you want hidden or in advanced here | |
HIDDEN_KEYS = Twod.HIDDEN_KEYS + [] | |
ADVANCED_KEYS = Twod.ADVANCED_KEYS + [] | |
CONFIG_SCHEMA = vol.Schema( | |
{ | |
vol.Optional( | |
"A switch", | |
description="Does a boolean thing", | |
default=False, | |
): bool, | |
} | |
) | |
def __init__(self, ledfx, config): | |
super().__init__(ledfx, config) | |
self.bar = 0 | |
self.game_board = None | |
self.game_simulator = None | |
self.game_stats = None | |
def config_updated(self, config): | |
super().config_updated(config) | |
# copy over your configs here into variables | |
self.a_switch = self._config["A switch"] | |
def do_once(self): | |
super().do_once() | |
# defer things that can't be done when pixel_count is not known | |
# this is probably important for most 2d matrix where you want | |
# things to be initialized to the space | |
self.game_board = sg.Board(size=(self.r_height, self.r_width)) | |
self.game_simulator = sg.Simulator(self.game_board) | |
# drop a few gliders to test | |
self.game_board.add(lf.Glider(), loc=(4,4)) | |
self.game_board.add(lf.Glider(), loc=(10,4)) | |
def audio_data_updated(self, data): | |
# Grab your audio input here, such as bar oscillator | |
self.bar = data.bar_oscillator() | |
def draw(self): | |
# this is where you pixel mash, it will be a black image object each call | |
# a draw object is already attached | |
# self.matrix is the Image object | |
# self.m_draw is the attached draw object | |
# all rotation abstraction is done for you | |
# self.matrix.height | |
# self.matrix.width | |
# look in this function for basic lines etc, use pillow primitives | |
# for regular shapes | |
if self.test: | |
self.draw_test(self.m_draw) | |
self.game_stats = self.game_simulator.run(sg.rules.conway_classic, iters=1) | |
# Get the values of the last frame of history | |
self.last_game_frame = self.game_simulator.history[-1] | |
self.game_image_array = np.array(self.last_game_frame, dtype=np.uint8) * 255 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment