snakeware

Python-based Linux distro for x86-64 and RPi
git clone git://squid-tech.com/snakeware.git
Log | Files | Refs | README

commit a163a0e0524225baba17378421e4e92b9182a8ba
parent 367bb5dff5b21ce0abea5420dec8e5c26d310cc4
Author: Josh Moore <jxm5210@g.rit.edu>
Date:   Tue, 16 Jun 2020 18:50:02 -0400

Merge pull request #126 from mdoege/cell

Add cell
Diffstat:
Asnakewm/apps/fun/cell/README.md | 17+++++++++++++++++
Asnakewm/apps/fun/cell/__init__.py | 14++++++++++++++
Asnakewm/apps/fun/cell/cell.py | 111+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 142 insertions(+), 0 deletions(-)

diff --git a/snakewm/apps/fun/cell/README.md b/snakewm/apps/fun/cell/README.md @@ -0,0 +1,17 @@ +# Cell + +Cell runs an [elementary cellular automaton](https://en.wikipedia.org/wiki/Elementary_cellular_automaton), selecting the rule, colors, and initial conditions randomly. + +The program uses the EGA/Sega Master System color palette (64 colors, 4 levels per channel). The window is resizable and will start a new automaton on each resize event. + +# Further reading + +* Wolfram, Stephen (2002): [A New Kind of Science](https://www.wolframscience.com/nks/) + +# Author + +Martin C. Doege + ++ github: https://github.com/mdoege + ++ date: 16 Jun 2020 diff --git a/snakewm/apps/fun/cell/__init__.py b/snakewm/apps/fun/cell/__init__.py @@ -0,0 +1,14 @@ +from .cell import Cell + + +def load(manager, params): + """ + Create and launch a new instance of Cell. + """ + # default position + pos = (100, 100) + + if params is not None and len(params) > 0: + pos = params[0] + + Cell(pos, manager) diff --git a/snakewm/apps/fun/cell/cell.py b/snakewm/apps/fun/cell/cell.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python + +# Cellular automaton + +import pygame, random +import pygame_gui +from pygame_gui.elements.ui_image import UIImage + +RULES = ( + 18, + 22, + 26, + 30, + 41, + 45, + 54, + 60, + 90, + 102, + 105, + 106, + 110, + 122, + 126, + 146, + 150, + 154, + 182, +) +RES = 300, 500 +NCELL = RES[0] + + +def getcol(): + return [85 * random.randint(0, 3) for q in (1, 2, 3)] + + +class Cell(pygame_gui.elements.UIWindow): + DIMS = RES + + def __init__(self, pos, manager): + super().__init__( + pygame.Rect(pos, (self.DIMS[0] + 32, self.DIMS[1] + 60)), + manager=manager, + window_display_title="cell", + object_id="#cell", + resizable=True, + ) + + self.dsurf = UIImage( + pygame.Rect((0, 0), self.DIMS), + pygame.Surface(self.DIMS).convert(), + manager=manager, + container=self, + parent_element=self, + ) + self.screen = pygame.Surface(self.DIMS) + self.manager = manager + self.cell = [random.randint(0, 1) for q in range(2 + NCELL)] + self.rule = random.choice(RULES) + self.gen = 1 + self.line = 0 + self.COL = getcol(), getcol() + + def process_event(self, event): + super().process_event(event) + r = super().get_abs_rect() + if event.type == pygame.MOUSEBUTTONUP and ( + r.w != self.DIMS[0] + 32 or r.h != self.DIMS[1] + 60 + ): + self.DIMS = r.w - 32, r.h - 60 + super().kill() + self.__init__((r.left, r.top), self.manager) + + def newgen(self): + self.gen += 1 + cell2 = [0 for q in range(2 + NCELL)] + for x in range(NCELL): + s = 2 ** (4 * self.cell[x] + 2 * self.cell[x + 1] + self.cell[x + 2]) + if s & self.rule: + cell2[x + 1] = 1 + # make cyclic + cell2[0] = cell2[-2] + cell2[-1] = cell2[1] + self.cell = cell2 + + def update(self, delta): + super().update(delta) + self.line += 1 + self.screen.scroll(dy=-1) + pygame.draw.line( + self.screen, + (0, 0, 0), + (0, self.DIMS[1] - 1), + (self.DIMS[0] - 1, self.DIMS[1] - 1), + 1, + ) + scrollfac = max(1, int(self.DIMS[0] / RES[0])) + if self.line % scrollfac == 0: + self.newgen() + for x in range(self.DIMS[0]): + xp = int(x / self.DIMS[0] * NCELL) + pygame.draw.line( + self.screen, + self.COL[self.cell[xp + 1]], + (x, self.DIMS[1] - 1), + (x, self.DIMS[1] - 1), + 1, + ) + super().set_display_title("cell (rule %u)" % self.rule) + self.dsurf.image.blit(self.screen, (0, 0))