I want to code a complete game on life in Python. I want the initial pattern to be really interesting. I want to have a way to edit the files, like the grid. I want powers, speed controls, have to be very fast. Nice UI. And anything else you think could be very useful for a new player to learn more about it. So maybe a few gliders and things they could be able to spawn on, and so on. Make your best effort making this clean, bug-free, and awesome. Give me all code in one bit. I know it's a lot, but please do it
Assistant
Game of Life in Python
This is a complete implementation of the Game of Life in Python, with an initial interesting pattern, a grid editor, speed controls, and a user-friendly interface.
Code
python
import tkinter as tk
import random
import copy
# Window dimensions
WIDTH = 800
HEIGHT = 600
# Grid dimensions
ROWS = 50
COLS = 50
# Cell size
CELL_SIZE = 10
class GameOfLife:
def __init__(self):
self.root = tk.Tk()
self.root.title("Game of Life")
self.root.geometry(f"{WIDTH}x{HEIGHT}")
self.grid = [[random.choice([0, 1]) for _ in range(COLS)] for _ in range(ROWS)]
self.patterns = {
"Glider": [[0, 1, 0], [0, 0, 1], [1, 1, 1]],
"Beacon": [[1, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 1]],
"R-Pentomino": [[0, 1, 1], [1, 1, 0], [0, 1, 0]]
}
self.speed = 10
self.running = False
self.canvas = tk.Canvas(self.root, width=CELL_SIZE * COLS, height=CELL_SIZE * ROWS)
self.canvas.pack(side=tk.LEFT)
self.controls_frame = tk.Frame(self.root)
self.controls_frame.pack(side=tk.LEFT, padx=10)
self.speed_label = tk.Label(self.controls_frame, text="Speed:")
self.speed_label.pack()
self.speed_scale = tk.Scale(self.controls_frame, from_=1, to=100, orient=tk.HORIZONTAL, command=self.update_speed)
self.speed_scale.set(self.speed)
self.speed_scale.pack()
self.start_button = tk.Button(self.controls_frame, text="Start", command=self.start_game)
self.start_button.pack()
self.stop_button = tk.Button(self.controls_frame, text="Stop", command=self.stop_game, state=tk.DISABLED)
self.stop_button.pack()
self.clear_button = tk.Button(self.controls_frame, text="Clear", command=self.clear_grid)
self.clear_button.pack()
self.pattern_menu = tk.StringVar(self.root)
self.pattern_menu.set("Select a pattern")
self.patterns_menu = tk.OptionMenu(self.controls_frame, self.pattern_menu, *self.patterns.keys(), command=self.load_pattern)
self.patterns_menu.pack()
self.edit_button = tk.Button(self.controls_frame, text="Edit Grid", command=self.edit_grid)
self.edit_button.pack()
self.draw_grid()
def update_speed(self, value):
self.speed = int(value)
def start_game(self):
self.running = True
self.start_button.config(state=tk.DISABLED)
self.stop_button.config(state=tk.NORMAL)
self.update_grid()
def stop_game(self):
self.running = False
self.start_button.config(state=tk.NORMAL)
self.stop_button.config(state=tk.DISABLED)
def clear_grid(self):
self.grid = [[0 for _ in range(COLS)] for _ in range(ROWS)]
self.draw_grid()
def load_pattern(self, pattern):
x = COLS // 2 - len(self.patterns[pattern][0]) // 2
y = ROWS // 2 - len(self.patterns[pattern]) // 2
for i, row in enumerate(self.patterns[pattern]):
for j, cell in enumerate(row):
self.grid[y + i][x + j] = cell
self.draw_grid()
def edit_grid(self):
edit_window = tk.Toplevel(self.root)
edit_window.title("Edit Grid")
edit_grid = [[0 for _ in range(COLS)] for _ in range(ROWS)]
for i in range(ROWS):
for j in range(COLS):
edit_grid[i][j] = tk.IntVar()
tk.Checkbutton(edit_window, variable=edit_grid[i][j], command=lambda i=i, j=j: self.update_edit_grid(edit_grid, i, j)).grid(row=i, column=j)
def save_edit_grid():
for i in range(ROWS):
for j in range(COLS):
self.grid[i][j] = edit_grid[i][j].get()
self.draw_grid()
edit_window.destroy()
tk.Button(edit_window, text="Save", command=save_edit_grid).grid(row=ROWS, column=0, columnspan=COLS)
def update_edit_grid(self, edit_grid, i, j):
if edit_grid[i][j].get():
self.grid[i][j] = 1
else:
self.grid[i][j] = 0
self.draw_grid()
def draw_grid(self):
self.canvas.delete("all")
for i in range(ROWS):
for j in range(COLS):
color = "black" if self.grid[i][j] else "white"
self.canvas.create_rectangle(j * CELL_SIZE, i * CELL_SIZE, (j + 1) * CELL_SIZE, (i + 1) * CELL_SIZE, fill=color, outline=color)
def update_grid(self):
if not self.running:
return
new_grid = copy.deepcopy(self.grid)
for i in range(ROWS):
for j in range(COLS):
live_neighbors = self.count_live_neighbors(i, j)
if self.grid[i][j] and (live_neighbors < 2 or live_neighbors > 3):
new_grid[i][j] = 0
elif not self.grid[i][j] and live_neighbors == 3:
new_grid[i][j] = 1
self.grid = new_grid
self.draw_grid()
self.root.after(self.speed, self.update_grid)
def count_live_neighbors(self, i, j):
count = 0
for x in range(max(0, i - 1), min(ROWS, i + 2)):
for y in range(max(0, j - 1), min(COLS, j + 2)):
count += self.grid[x][y]
count -= self.grid[i][j]
return count
def run(self):
self.root.mainloop()
if __name__ == "__main__":
game = GameOfLife()
game.run()
How to use
Run the code to start the game.
Use the "Start" button to start the game.
Use the "Stop" button to stop the game.
Use the "Clear" button to clear the grid.
Select a pattern from the "Select a pattern" menu to load it onto the grid.
Use the "Edit Grid" button to edit the grid manually.
Adjust the speed of the game using the speed slider.
Patterns
The game includes the following patterns:
Glider: A simple pattern that moves across the grid.
Beacon: A pattern that oscillates between two different states.
R-Pentomino: A pattern that grows and evolves over time.
Tips
Use the "Edit Grid" feature to create your own patterns.
Experiment with different speeds to see how it affects the game.
Try loading different patterns to see how they behave.
“I want to code a complete game on life in Python. I want the initial pattern to be….” Conversation with llama-3.1-70b-instruct, from the LMArena Human Preference 100K dataset. The Chat Archive, 2026. https://thechatarchive.com/c/01m8508v0xgfwjfzg7b91jdb9c