• API
• FAQ
• Tools
• Archive
daily pastebin goal
2%
SHARE
TWEET

# Conway's Game of Life Python Numpy TKinter

a guest Jun 25th, 2011 487 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. """
2. lifecartoon.py, made with help from http://en.wikipedia.org/wiki/Conway's_Game_of_Life
3. """
4. from numpy import zeros
5. from Tkinter import *
6.
7. window = Tk()
8. canvas = Canvas(window, width = 800, height = 800)
9. canvas.pack()
10.
11. def aliveneighbourcounter(y,x):         # counts the number of alive neighbours (wraps around canvas edges)
12.         ancounter = 0
13.         if y==0: yminus1 = len(grid)-1
14.         else: yminus1 = y-1
15.         if y==len(grid)-1: yplus1 = 0
16.         else: yplus1 = y+1
17.         if x==0: xminus1 = len(grid)-1
18.         else: xminus1 = x-1
19.         if x==len(grid)-1: xplus1 = 0
20.         else: xplus1 = x+1
21.         if grid[yminus1][xminus1] == 1: ancounter += 1
22.         if grid[y][xminus1] == 1: ancounter += 1
23.         if grid[yminus1][x] == 1: ancounter += 1
24.         if grid[y][xplus1] == 1: ancounter += 1
25.         if grid[yplus1][x] == 1: ancounter += 1
26.         if grid[yplus1][xplus1] == 1: ancounter += 1
27.         if grid[yplus1][xminus1] == 1: ancounter += 1
28.         if grid[yminus1][xplus1] == 1: ancounter += 1
29.         return ancounter
30.
31. def kill(grid):                         # kills if the number of alive neighbours is other than 2 or 3
32.         gridkilled = zeros([40,40])
33.         for y in range(len(grid)):
34.                 for x in range(len(grid)):
35.                   gridkilled[y][x] = grid[y][x]
36.                   if grid[y][x] == 1:
37.                         ancounter = aliveneighbourcounter(y,x)
38.                         if ancounter<2 or ancounter>3:
39.                                 gridkilled[y][x] = 0
40.         return gridkilled
41.
42. def cometolife(grid):                   # brings to life if there are exactly 3 alive neighbours
43.         gridcametolife = zeros([40,40])
44.         for y in range(len(grid)):
45.                 for x in range(len(grid)):
46.                   if grid[y][x] == 0:
47.                         ancounter = aliveneighbourcounter(y,x)
48.                         if ancounter == 3:
49.                                 gridcametolife[y][x] = 1
50.         return gridcametolife
51.
52. def drawbox():                          # draws a blue box for 'alive', a green box for 'dead'
53.         for y in range(len(grid)):
54.                 for x in range(len(grid)):
55.                   if grid[y][x] == 1:
56.                         canvas.create_rectangle(20*x, 20*y, 20*x+20, 20*y+20, fill="#220C65", outline="#DFF2A6", width=1)
57.                   elif grid[y][x] == 0:
58.                         canvas.create_rectangle(20*x, 20*y, 20*x+20, 20*y+20, fill="#A4AD90", outline="#DFF2A6", width=1)
59.
60. # set up the initial state of the system (0 means 'dead', 1 means 'alive'):
61. grid = zeros([40,40])
62. grid[35][37] = grid[35][38] = grid[35][39] = 1                                          # blinker
63. grid[7][3] = grid[7][4] = grid[7][5] = grid[8][4] = grid[8][5] = grid[8][6] = 1         #toad
64. grid[20][20] = grid[21][20] = grid[22][20] = grid[22][21] = grid[21][22] = 1            # glider
65. grid[30][30] = grid[31][30] = grid[32][30] = grid[33][30] = grid[30][31] = grid[30][32] = grid[31][33] = grid[34][31] = grid[34][33] = 1 #lwss
66.
67. # animation loop:
68. while 1==1:
69.         gridkilled = kill(grid)
70.         gridcametolife = cometolife(grid)
71.         for y in range(len(grid)):
72.                 for x in range(len(grid)):
73.                         if gridkilled[y][x] == gridcametolife[y][x] == 1:
74.                                 grid[y][x] = 1
75.                         else:
76.                                 grid[y][x] = gridkilled[y][x] + gridcametolife[y][x]
77.         drawbox()
78.         canvas.after(30)
79.         canvas.update()
80.
81. window.mainloop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.