Chess is one of the most popular indoor game that is played on a chessboard. The chessboard has a squared shape and consists of sixty-four subdivisions. Here, subdivisions are painted with alternate colors. Keeping this thing in mind, we will draw chessboard in turtle.

Several functions from the turtle library will be used. These are- `forward()`

, `right()`

, `penup()`

, `pendown()`

, `goto()`

, `fillcolor()`

, `begin_fill()`

and `end_fill()`

.

#Program to draw chessboard in Python Turtle import turtle chessboard = turtle.Turtle() chessboard.speed(8) #for speedily drawing chessboard for i in range(4): #for loop will run 4 times chessboard.forward(800) #forward turtle by 800 units chessboard.right(90) #turn turtle clockwise by 90 degree a = 0 #for controlling alternate colors in a row b = 0 #for controlling alternate colors in a column for i in range(8): #for loop will run 8 times as there are 8 rows in the chessboard if(b == 0): a=1 else: a=0 for j in range(8): #for loop will run 8 times as there are 8 columns in the chessboard chessboard.penup() chessboard.goto(j*100, i*100*(-1)) chessboard.pendown() if(a==0): chessboard.fillcolor('black') a=1 else: chessboard.fillcolor('white') a=0 chessboard.begin_fill() for k in range(4): chessboard.forward(100) chessboard.right(90) chessboard.end_fill() if(b==0): b=1 else: b=0

Output of the above program-

Explanation of the above code-

import turtle chessboard = turtle.Turtle()

To work with turtle module, you have to import it. After importing this module, you have to create a new drawing board and assign it to an object chessboard.

for i in range(4): chessboard.forward(800) chessboard.right(90)

We have moved the turtle in the forward direction by 800 units. Because the length of the outer boundary of the chessboard is 800 units. Then, we have turned the turtle by 90° as the angle between adjacent sides is 90°. This finishes one side of the chessboard. These same statements are repeated 3 times to obtain the remaining boundary of the chessboard.

for i in range(8): if(b == 0): a=1 else: a=0

b variable will be used to control the color of first square of each row.

for j in range(8): chessboard.penup() chessboard.goto(j*100, i*100*(-1)) chessboard.pendown() if(a==0): chessboard.fillcolor('black') a=1 else: chessboard.fillcolor('white') a=0 chessboard.begin_fill() for k in range(4): chessboard.forward(100) chessboard.right(90) chessboard.end_fill()

The above for loop will control the drawing of squares in a row. goto() will move the turtle to a specific left corner of a square. If the value of a is equal to 0 then we have set the fillcolor to black and otherwise we have set the fillcolor to white. After this, we have called begin_fill() to start filling the square with the selected fillcolor. Then, we have drawn the square of 100 units side.

- Python Turtle Programming Tutorial
- How to draw square and rectangle in Python Turtle?
- How to draw circle in Python Turtle?
- How to draw color filled shapes in Python Turtle?
- How to draw stars in Python Turtle?
- How to draw pentagon, hexagon and other polygons in Python Turtle?
- How to draw Olympics logo in Python Turtle?
- How to draw a spiral square and star in Python Turtle?
- How to draw spider web in Python Turtle?
- How to draw car in Python Turtle?
- How to draw snowman in Python Turtle?
- How to draw tally marks in Python Turtle?