Created
November 6, 2019 19:59
-
-
Save jtpio/ac9fa41239fd0098ede03ec042aec574 to your computer and use it in GitHub Desktop.
ipycanvas - Game of Life
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
name: ipycanvas | |
channels: | |
- conda-forge | |
dependencies: | |
- branca | |
- ipycanvas | |
- voila |
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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# John Conway's Game Of Life: Threaded Edition" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Some of the following code is adapted from https://jakevdp.github.io/blog/2013/08/07/conways-game-of-life/" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from time import sleep\n", | |
"from threading import Thread\n", | |
"\n", | |
"import numpy as np\n", | |
"\n", | |
"from ipycanvas import MultiCanvas, hold_canvas" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def life_step(x):\n", | |
" \"\"\"Game of life step\"\"\"\n", | |
" nbrs_count = sum(np.roll(np.roll(x, i, 0), j, 1)\n", | |
" for i in (-1, 0, 1) for j in (-1, 0, 1)\n", | |
" if (i != 0 or j != 0))\n", | |
" return (nbrs_count == 3) | (x & (nbrs_count == 2))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def draw(x, canvas, color='black'):\n", | |
" with hold_canvas(canvas):\n", | |
" canvas.clear()\n", | |
" canvas.fill_style = color\n", | |
"\n", | |
" r = 0\n", | |
" for row in x:\n", | |
" c = 0\n", | |
" for value in row:\n", | |
" if value:\n", | |
" canvas.fill_rect(r * n_pixels, c * n_pixels, n_pixels, n_pixels)\n", | |
"\n", | |
" c += 1\n", | |
" r += 1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"glider_gun =\\\n", | |
"[[0,0,0,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,0],\n", | |
" [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0],\n", | |
" [0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1],\n", | |
" [0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1],\n", | |
" [1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],\n", | |
" [1,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0],\n", | |
" [0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],\n", | |
" [0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],\n", | |
" [0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]\n", | |
"\n", | |
"x = np.zeros((50, 70), dtype=bool)\n", | |
"x[1:10,1:37] = glider_gun" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"n_pixels = 10\n", | |
"\n", | |
"multi = MultiCanvas(2, size=(x.shape[1] * n_pixels, x.shape[0] * n_pixels))\n", | |
"multi[0].fill_style = '#FFF0C9'\n", | |
"multi[0].fill_rect(0, 0, multi.size[0], multi.size[1])\n", | |
"\n", | |
"multi" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"draw(x, multi[1], '#5770B3')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class GameOfLife(Thread):\n", | |
" def __init__(self, x, canvas):\n", | |
" self.x = x\n", | |
" self.canvas = canvas\n", | |
" super(GameOfLife, self).__init__()\n", | |
"\n", | |
" def run(self):\n", | |
" for _ in range(1_000):\n", | |
" self.x = life_step(self.x)\n", | |
" draw(self.x, self.canvas, '#5770B3')\n", | |
"\n", | |
" sleep(0.1)\n", | |
"\n", | |
"GameOfLife(x, multi[1]).start()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### The game is now running in a separate thread, nothing stops you from changing the background color:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"multi[0].fill_style = '#D0FFB3'\n", | |
"multi[0].fill_rect(0, 0, multi.size[0], multi.size[1])" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
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
{ | |
"VoilaConfiguration": { | |
"enable_nbextensions": true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment