Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from tkinter import font
- import time
- import random
- class GameOfLife(Frame):
- def __init__(self, parent):
- Frame.__init__(self, parent)
- self.parent = parent
- self.grid(row = 0, column = 0)
- self.size_x = 50
- self.size_y = 20
- self.cell_buttons = []
- self.generate_next = True
- self.initialUI()
- def initialUI(self):
- self.parent.title("Game of Life")
- self.title_frame = Frame(self.parent)
- self.title_frame.grid(row = 0, column = 0, columnspan = 4)
- self.titleFont = font.Font(family="Helvetica", size=14)
- title = Label(self.title_frame, text = "Conway's Game of Life", font = self.titleFont)
- title.pack(side = TOP)
- prompt = Label(self.title_frame, text = "When the game is stopped you can click the cells, then press Start Game. \
- \nRules: Dead cells come alive when exactly 3 neighbor cells are alive. \
- \nLive cells stay alive when there are 2 or 3 live neighbor cells, else it dies")
- prompt.pack(side = BOTTOM)
- self.build_grid()
- self.start_button = Button(self.parent, text = "Start Game", command = self.simulate_game)
- self.start_button.grid(row = 1,column = 0)
- self.start_random_button = Button(self.parent, text = "Random Start", command = self.random_game)
- self.start_random_button.grid(row =1,column = 1)
- self.stop_button = Button(self.parent, text = "Stop Game", state = DISABLED, command = self.stop_game)
- self.stop_button.grid(row =1 , column = 2)
- def build_grid(self):
- self.game_frame = Frame(
- self.parent, width = self.size_x + 2, height = self.size_y + 2, borderwidth = 1, relief = SUNKEN)
- self.game_frame.grid(row = 2, column = 0, columnspan = 4)
- #instantiates buttons for choosing initial configuration
- 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)]
- # creates 2d array of buttons for grid
- for i in range(1, self.size_y + 1):
- for j in range(1, self.size_x + 1):
- self.cell_buttons[i][j].grid(row = i, column = j, sticky = W+E)
- self.cell_buttons[i][j]['command'] = lambda i=i, j=j:self.cell_toggle(self.cell_buttons[i][j])
- def simulate_game(self):
- self.disable_buttons()
- buttons_to_toggle = []
- for i in range(1, self.size_y + 1):
- for j in range(1, self.size_x + 1):
- coord = (i, j)
- # if cell dead and has 3 neighbors, add coordinate to list of coords to toggle
- if self.cell_buttons[i][j]['bg'] == "white" and self.neighbor_count(i, j) == 3:
- buttons_to_toggle.append(coord)
- # if cell alive and does not have 2 or 3 neighbors,, add coordinate to list of coords to toggle
- elif self.cell_buttons[i][j]['bg'] == "black" and self.neighbor_count(i, j) != 3 and self.neighbor_count(i, j) != 2:
- buttons_to_toggle.append(coord)
- for coord in buttons_to_toggle:
- self.cell_toggle(self.cell_buttons[coord[0]][coord[1]])
- if self.generate_next:
- self.after(100, self.simulate_game)
- else:
- self.enable_buttons()
- def random_game(self):
- for i in range(1, self.size_y + 1):
- for j in range(1, self.size_x + 1):
- self.cell_buttons[i][j]['bg'] = "white" if random.randint(0,1) else "black"
- self.generate_next = True
- self.simulate_game()
- def disable_buttons(self):
- if self.cell_buttons[1][1] != DISABLED:
- for i in range(0, self.size_y + 2):
- for j in range(0, self.size_x + 2):
- self.cell_buttons[i][j].configure(state = DISABLED)
- self.stop_button.configure(state = NORMAL)
- self.start_random_button.configure(state = DISABLED)
- self.start_button.configure(state = DISABLED)
- def enable_buttons(self):
- for i in range(0, self.size_y + 2):
- for j in range(0, self.size_x + 2):
- self.cell_buttons[i][j].configure(state = NORMAL)
- self.stop_button.configure(state = DISABLED)
- self.start_button.configure(state = NORMAL)
- self.start_random_button.configure(state = NORMAL)
- self.generate_next = True
- def neighbor_count(self, x_coord, y_coord):
- count = 0
- for i in range(x_coord - 1, x_coord + 2):
- for j in range(y_coord - 1, y_coord + 2):
- if (i != x_coord or j != y_coord) and self.cell_buttons[i][j]['bg'] == "black":
- count += 1
- return count
- def cell_toggle(self, cell):
- if cell['bg'] == "white":
- cell['bg'] = "black"
- else:
- cell['bg'] = "white"
- def stop_game(self):
- self.generate_next = False
- if __name__ == '__main__':
- root = Tk()
- game = GameOfLife(root)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment