-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChess-Board.py
More file actions
77 lines (69 loc) · 2.46 KB
/
Chess-Board.py
File metadata and controls
77 lines (69 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import turtle
# Chess Unicode pieces: White: K Q R B N P, Black: k q r b n p
pieces= [
["\u265C", "\u265E", "\u265D", "\u265B", "\u265A", "\u265D", "\u265E", "\u265C"], # Black back row
["\u265F"] * 8, # Black pawns
[""] * 8,
[""] * 8,
[""] * 8,
[""] * 8,
["\u2659"] * 8, # White Pawns
["\u2656", "\u2658", "\u2657", "\u2655", "\u2654", "\u2657", "\u2658", "\u2656"], # white back row
]
# Screen setup
screen=turtle.Screen()
screen.title("Chess Board with Numbering")
screen.bgcolor("white")
# Turtle for drawing
board=turtle.Turtle()
board.speed(0)
board.hideturtle()
board.penup()
# Board parameters
square_size=60
start_x= -240
start_y= 240
# Draw a square
def draw_square(x,y,color):
board.goto(x,y)
board.pendown()
board.fillcolor(color)
board.begin_fill()
for _ in range(4):
board.forward(square_size)
board.right(90)
board.end_fill()
board.penup()
# Draw the Chessboard
for row in range(8):
for col in range(8):
x= start_x + col * square_size
y= start_y - row * square_size
color = "white" if (row + col) % 2 == 0 else "green"
draw_square(x,y,color)
# Add rank number (1 to 8) on the left side
for row in range(8):
y = start_y - row * square_size - square_size / 2
board.goto(start_x - 25, y - 10)
board.color("black")
board.write(str(8 - row), align="center", font=("Arial", 14, "bold"))
# Add file letters (A to H) at the bottom
for col in range(8):
x = start_x + col * square_size + square_size / 2
board.goto(x, start_y - 8 * square_size - 25)
board.color("black")
board.write(chr(ord('A') + col), align="center", font=("Arial", 14, "bold"))
# Draw chess pieces
for row in range(8):
for col in range(8):
piece = pieces[row][col]
if piece != "":
x = start_x + col * square_size + square_size / 2
y = start_y - row * square_size - square_size * 0.75
board.goto(x, y)
if piece.isupper() or piece in ["\u2654", "\u2655", "\u2656", "\u2657", "\u2658", "\u2659"]: # This pieces are white pieces.
board.color("black")
else:
board.color("black")
board.write(piece, align="center", font=("Arial", 28, "bold"))
turtle.done()