proffreda

Game of Life (tkinter version for Windows)

Apr 13th, 2016
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. from tkinter import *
  2. from tkinter import font
  3. import time
  4. import random
  5.  
  6. class GameOfLife(Frame):
  7.  
  8. def __init__(self, parent):
  9. Frame.__init__(self, parent)
  10. self.parent = parent
  11. self.grid(row = 0, column = 0)
  12. self.size_x = 50
  13. self.size_y = 20
  14. self.cell_buttons = []
  15. self.generate_next = True
  16. self.initialUI()
  17.  
  18. def initialUI(self):
  19. self.parent.title("Game of Life")
  20. self.title_frame = Frame(self.parent)
  21. self.title_frame.grid(row = 0, column = 0, columnspan = 4)
  22. self.titleFont = font.Font(family="Helvetica", size=14)
  23. title = Label(self.title_frame, text = "Conway's Game of Life", font = self.titleFont)
  24. title.pack(side = TOP)
  25.  
  26. prompt = Label(self.title_frame, text = "When the game is stopped you can click the cells, then press Start Game. \
  27. \nRules: Dead cells come alive when exactly 3 neighbor cells are alive. \
  28. \nLive cells stay alive when there are 2 or 3 live neighbor cells, else it dies")
  29. prompt.pack(side = BOTTOM)
  30. self.build_grid()
  31. self.start_button = Button(self.parent, text = "Start Game", command = self.simulate_game)
  32. self.start_button.grid(row = 1,column = 0)
  33. self.start_random_button = Button(self.parent, text = "Random Start", command = self.random_game)
  34. self.start_random_button.grid(row =1,column = 1)
  35. self.stop_button = Button(self.parent, text = "Stop Game", state = DISABLED, command = self.stop_game)
  36. self.stop_button.grid(row =1 , column = 2)
  37.  
  38. def build_grid(self):
  39. self.game_frame = Frame(
  40. self.parent, width = self.size_x + 2, height = self.size_y + 2, borderwidth = 1, relief = SUNKEN)
  41. self.game_frame.grid(row = 2, column = 0, columnspan = 4)
  42.  
  43. #instantiates buttons for choosing initial configuration
  44. self.cell_buttons = [[Button(self.game_frame, bg = "white", width = 2, height = 1) for i in range(self.size_x + 2)] for j in range(self.size_y + 2)]
  45. # creates 2d array of buttons for grid
  46. for i in range(1, self.size_y + 1):
  47. for j in range(1, self.size_x + 1):
  48. self.cell_buttons[i][j].grid(row = i, column = j, sticky = W+E)
  49. self.cell_buttons[i][j]['command'] = lambda i=i, j=j:self.cell_toggle(self.cell_buttons[i][j])
  50.  
  51. def simulate_game(self):
  52. self.disable_buttons()
  53. buttons_to_toggle = []
  54. for i in range(1, self.size_y + 1):
  55. for j in range(1, self.size_x + 1):
  56. coord = (i, j)
  57. # if cell dead and has 3 neighbors, add coordinate to list of coords to toggle
  58. if self.cell_buttons[i][j]['bg'] == "white" and self.neighbor_count(i, j) == 3:
  59. buttons_to_toggle.append(coord)
  60. # if cell alive and does not have 2 or 3 neighbors,, add coordinate to list of coords to toggle
  61. elif self.cell_buttons[i][j]['bg'] == "black" and self.neighbor_count(i, j) != 3 and self.neighbor_count(i, j) != 2:
  62. buttons_to_toggle.append(coord)
  63.  
  64. for coord in buttons_to_toggle:
  65. self.cell_toggle(self.cell_buttons[coord[0]][coord[1]])
  66. if self.generate_next:
  67. self.after(100, self.simulate_game)
  68. else:
  69. self.enable_buttons()
  70.  
  71. def random_game(self):
  72. for i in range(1, self.size_y + 1):
  73. for j in range(1, self.size_x + 1):
  74. self.cell_buttons[i][j]['bg'] = "white" if random.randint(0,1) else "black"
  75. self.generate_next = True
  76. self.simulate_game()
  77.  
  78. def disable_buttons(self):
  79. if self.cell_buttons[1][1] != DISABLED:
  80. for i in range(0, self.size_y + 2):
  81. for j in range(0, self.size_x + 2):
  82. self.cell_buttons[i][j].configure(state = DISABLED)
  83. self.stop_button.configure(state = NORMAL)
  84. self.start_random_button.configure(state = DISABLED)
  85. self.start_button.configure(state = DISABLED)
  86.  
  87. def enable_buttons(self):
  88. for i in range(0, self.size_y + 2):
  89. for j in range(0, self.size_x + 2):
  90. self.cell_buttons[i][j].configure(state = NORMAL)
  91.  
  92. self.stop_button.configure(state = DISABLED)
  93. self.start_button.configure(state = NORMAL)
  94. self.start_random_button.configure(state = NORMAL)
  95. self.generate_next = True
  96.  
  97. def neighbor_count(self, x_coord, y_coord):
  98. count = 0
  99. for i in range(x_coord - 1, x_coord + 2):
  100. for j in range(y_coord - 1, y_coord + 2):
  101. if (i != x_coord or j != y_coord) and self.cell_buttons[i][j]['bg'] == "black":
  102. count += 1
  103. return count
  104.  
  105. def cell_toggle(self, cell):
  106. if cell['bg'] == "white":
  107. cell['bg'] = "black"
  108. else:
  109. cell['bg'] = "white"
  110.  
  111. def stop_game(self):
  112. self.generate_next = False
  113.  
  114. if __name__ == '__main__':
  115. root = Tk()
  116. game = GameOfLife(root)
  117. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment