Skip to content

Instantly share code, notes, and snippets.

@diunko
Created March 24, 2017 10:16
Show Gist options
  • Save diunko/b95e376f1d0b87efdb2101b74f995c77 to your computer and use it in GitHub Desktop.
Save diunko/b95e376f1d0b87efdb2101b74f995c77 to your computer and use it in GitHub Desktop.
simple pygame-based visualisation of convay's life game
import logging
logging.basicConfig(level=logging.DEBUG)
from time import sleep
import pygame
import life_step
log = logging.getLogger("life")
X, Y = 10, 10
CELL_SIZE = 30
WIDTH, HEIGHT = CELL_SIZE*X, CELL_SIZE*Y
def make_field():
field = pygame.Surface((WIDTH, HEIGHT))
field.fill((255,255,150))
dx = 0
while dx <= WIDTH:
pygame.draw.line(field, (0,0,0), (dx, 0), (dx, HEIGHT))
dx += CELL_SIZE
dy = 0
while dy <= HEIGHT:
pygame.draw.line(field, (0,0,0), (0,dy), (WIDTH, dy))
dy += CELL_SIZE
return field
def pause(t, name="just pause"):
log.debug("sleeping %s", name)
sleep(t)
#log.debug("wake from %s", name)
def draw_map(field, Map):
y = 0
while y < Y:
x = 0
while x < X:
if Map[y][x] == 1:
pygame.draw.rect(field, (0,0,0),
[x*CELL_SIZE, y*CELL_SIZE,
CELL_SIZE, CELL_SIZE])
x = x + 1
y = y+1
def main():
Map = [
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,1,0,0,0,0,0,0,0,0],
[0,0,1,0,0,0,0,0,0,0],
[1,1,1,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0]
]
pygame.init()
screen = pygame.display.set_mode((WIDTH*2+5, HEIGHT*2+5))
while True:
field = make_field()
draw_map(field, Map)
screen.blit(field, (0,0))
screen.blit(field, (WIDTH+5,0))
screen.blit(field, (0, HEIGHT+5))
screen.blit(field, (WIDTH+5, HEIGHT+5))
pygame.display.update()
pause(1)
Map = life_step.step(Map)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment