This is a 2-Player game. Player 1 has to set a color pattern, it can be any color pattern desired, Make sure player 2 is watching player 1 make the pattern. After the pattern is set, player 1 clicks “Done”. Player 2 now tries to replicate the pattern but without the colors! If player two gets just one square wrong, player 1 wins. If player 2 gets all of them right, player 2 wins.

In order to use this code, copy the code below. After you copy the code, paste it into your python file. After it is pasted, run the code and it should bring you to the same screen as the video above!

Copy this code for FREE!:

import tkinter as tk

class ColorPatternGame:
def __init__(self):
self.colors = ["red", "orange", "yellow", "green", "blue", "purple", "pink", "brown", "gray"]
self.pattern = []
self.game_window = tk.Tk()
self.canvas = tk.Canvas(self.game_window, width=300, height=300)
self.canvas.pack()
self.canvas.bind("<Button-1>", self.on_color_click)
self.done_button = tk.Button(self.game_window, text="Done", command=self.start_game, state="disabled")
self.done_button.pack()
self.label = tk.Label(self.game_window, text="Player 1, set the color pattern.")
self.label.pack()
self.draw_color_boxes()
self.game_window.mainloop()

def draw_color_boxes(self):

box_width = 100
box_height = 100
for i, color in enumerate(self.colors):
row = i // 3
col = i % 3
x1 = col * box_width
y1 = row * box_height
x2 = x1 + box_width
y2 = y1 + box_height
self.canvas.create_rectangle(x1, y1, x2, y2, fill=color)

def on_color_click(self, event):

x = event.x
y = event.y
color = self.get_clicked_color(x, y)
if color:
self.pattern.append(color)
self.draw_selected_box(color)
if len(self.pattern) == 9:
self.done_button.config(state="normal")

def get_clicked_color(self, x, y):

box_width = 100
box_height = 100
col = x // box_width
row = y // box_height
index = row * 3 + col
if 0 <= index < len(self.colors):
return self.colors[index]
return None

def draw_selected_box(self, color):

box_width = 100
box_height = 100
index = self.colors.index(color)
row = index // 3
col = index % 3
x1 = col * box_width
y1 = row * box_height
x2 = x1 + box_width
y2 = y1 + box_height
self.canvas.create_rectangle(x1, y1, x2, y2, outline="white", width=2)

def start_game(self):
self.canvas.delete("all")
self.canvas.unbind("<Button-1>")
self.done_button.config(state="disabled")
self.label.config(text="Player 2, replicate the color pattern.")
self.canvas.bind("<Button-1>", self.on_replication_click)

def on_replication_click(self, event):

x = event.x
y = event.y
color = self.get_clicked_color(x, y)
if color:
self.draw_selected_box(color)
if len(self.pattern) > 0 and color != self.pattern[0]:
self.label.config(text="Wrong color! Player 1 wins.")
self.canvas.unbind("<Button-1>")
else:
self.pattern.pop(0)
if len(self.pattern) == 0:
self.label.config(text="Congratulations! Player 2 wins.")
self.canvas.unbind("<Button-1>")

game = ColorPatternGame()