Affichage des articles dont le libellé est Python Chess Board. Afficher tous les articles
Affichage des articles dont le libellé est Python Chess Board. Afficher tous les articles

Python Wooden Chess Board

How To Create a Wooden Chessboard in Python Using Tkinter

How To Create a Wooden Chessboard in Python Using Tkinter


In this Python Tutorial we will see How to Create a wooden chess board using Python's built-in Tkinter library.

What We Are Gonna Use In This Project:

- Python Programming Language.
- Tkinter (GUI).
- VS Editor.






Project Source Code:




import tkinter as tk
import random
import math

class WoodenChessboard:
def __init__(self, root):
"""
Initialize the chess board application
Parameters:
root: The main Tkinter window
"""
self.root = root
self.root.title("Modern Chess Board")
# Colors for the board
self.light_square_color = "#F0D9B5" # Light cream color
self.dark_square_color = "#B58863" # Medium brown
self.border_color = "#3E2723" # Dark chocolate brown
self.text_color = "#FFFFFF" # White text for better visibility
# Board dimensions
self.square_size = 70 # Size of each square in pixels
self.board_size = 8 * self.square_size # Total board size (8x8 squares)
self.padding = 30 # Border width around the board
# Set up the canvas with padding for borders
# Canvas is the drawing area where we'll create our chess board
self.canvas = tk.Canvas(
root,
width=self.board_size + 2 * self.padding,
height=self.board_size + 2 * self.padding,
bg=self.border_color
)
self.canvas.pack(padx=10, pady=10)
# Draw the border with wood texture
self.draw_wooden_border()
# Draw the chess board squares and coordinates
self.draw_board()
def draw_wooden_border(self):
"""
Draw a wooden border with grain texture around the chess board
"""
# Calculate total width and height including border
outer_width = self.board_size + 2 * self.padding
outer_height = self.board_size + 2 * self.padding
# Draw border background
self.canvas.create_rectangle(
0, 0, outer_width, outer_height,
fill=self.border_color, outline=""
)
# Create wood grain effect with different shades of brown
grain_colors = ["#3E2723", "#4E342E", "#5D4037"]
# Draw horizontal grain lines for top and bottom borders
for y in range(0, self.padding):
# Choose a random color variation for each line
color = random.choice(grain_colors)
# Add random waviness to make the grain look natural
waviness = random.randint(0, 3)
thickness = random.randint(1, 2)

# Top border grain
y_pos = y
pts = []
# Create wavy points for the line
for x in range(0, outer_width, 5):
wave = math.sin(x/20) * waviness
pts.extend([x, y_pos + wave])
if pts:
self.canvas.create_line(pts, fill=color, width=thickness, smooth=True)
# Bottom border grain (mirror of top)
y_pos = outer_height - y
pts = []
for x in range(0, outer_width, 5):
wave = math.sin(x/20) * waviness
pts.extend([x, y_pos + wave])
if pts:
self.canvas.create_line(pts, fill=color, width=thickness, smooth=True)
# Draw vertical grain lines for left and right borders
for x in range(0, self.padding):
color = random.choice(grain_colors)
waviness = random.randint(0, 3)
thickness = random.randint(1, 2)
# Left border grain
x_pos = x
pts = []
for y in range(0, outer_height, 5):
wave = math.sin(y/20) * waviness
pts.extend([x_pos + wave, y])
if pts:
self.canvas.create_line(pts, fill=color, width=thickness, smooth=True)
# Right border grain (mirror of left)
x_pos = outer_width - x
pts = []
for y in range(0, outer_height, 5):
wave = math.sin(y/20) * waviness
pts.extend([x_pos + wave, y])
if pts:
self.canvas.create_line(pts, fill=color, width=thickness, smooth=True)
def draw_board(self):
"""
Draw the chess board with squares and coordinate labels
"""
# Draw file labels (columns: a-h)
for i in range(8):
file_label = chr(97 + i) # ASCII: 'a' is 97, so we get 'a' through 'h'
# Bottom row labels
self.canvas.create_text(
self.padding + i * self.square_size + self.square_size // 2,
self.padding + self.board_size + 15,
text=file_label,
fill=self.text_color,
font=("Arial", 12, "bold")
)

# Top row labels
self.canvas.create_text(
self.padding + i * self.square_size + self.square_size // 2,
self.padding - 15,
text=file_label,
fill=self.text_color,
font=("Arial", 12, "bold")
)
# Draw rank labels (rows: 1-8)
for i in range(8):
rank_label = str(8 - i) # Count down from 8 to 1 (chess notation)
# Left side labels
self.canvas.create_text(
self.padding - 15,
self.padding + i * self.square_size + self.square_size // 2,
text=rank_label,
fill=self.text_color,
font=("Arial", 12, "bold")
)
# Right side labels
self.canvas.create_text(
self.padding + self.board_size + 15,
self.padding + i * self.square_size + self.square_size // 2,
text=rank_label,
fill=self.text_color,
font=("Arial", 12, "bold")
)
# Draw all 64 squares of the chess board
for row in range(8):
for col in range(8):
# Determine if this is a light or dark square
# If row+col is even, it's a light square; if odd, it's dark
is_light = (row + col) % 2 == 0
base_color = self.light_square_color if is_light else self.dark_square_color
# Calculate the position of the square on the canvas
x1 = self.padding + col * self.square_size
y1 = self.padding + row * self.square_size
x2 = x1 + self.square_size
y2 = y1 + self.square_size
# Create the square
square_id = self.canvas.create_rectangle(
x1, y1, x2, y2,
fill=base_color,
outline="", # No outline for a cleaner look
tags=f"square_{row}_{col}" # Tag to identify the square later if needed
)
# Add subtle wood grain texture to each square
self.add_wood_grain(x1, y1, x2, y2, is_light)
def add_wood_grain(self, x1, y1, x2, y2, is_light):
"""
Add subtle wood grain texture to a square
Parameters:
x1, y1: Top-left corner coordinates
x2, y2: Bottom-right corner coordinates
is_light: Boolean indicating if it's a light square
"""
if is_light:
# Light square grain (vertical lines with slight variation)
grain_color = "#E5C99E" # Slightly darker than the light square
grain_spacing = 4 # Space between grain lines

for i in range(int(x1) + 2, int(x2), grain_spacing):
# Add randomness to make grain look natural
waviness = random.randint(0, 2)
thickness = random.choice([1, 1, 2]) # Mostly thin lines

# Create a wavy line for the grain effect
points = []
for y in range(int(y1), int(y2), 5):
wave = math.sin(y/10) * waviness
points.extend([i + wave, y])

if points:
self.canvas.create_line(
points,
fill=grain_color,
width=thickness,
smooth=True
)
else:
# Dark square grain (more subtle)
grain_color = "#A07855" # Slightly lighter than the dark square
grain_spacing = 6

for i in range(int(x1) + 3, int(x2), grain_spacing):
waviness = random.randint(0, 1)
# Create subtle grain lines
points = []
for y in range(int(y1), int(y2), 10):
wave = math.sin(y/15) * waviness
points.extend([i + wave, y])

if points:
self.canvas.create_line(
points,
fill=grain_color,
width=1,
smooth=True
)



# Run the application
if __name__ == "__main__":
root = tk.Tk()
# Configure the window
root.configure(bg="#2C2C2C") # Dark background for modern look
# Create the chess board
app = WoodenChessboard(root)
# Start the application
root.mainloop()



The Final Result: