Drawing a Chessboard using Python Turtle Module

Introduction

The Logo programming language is frequently linked to turtle graphics. In the late 1960s, Seymour Papert added turtle graphics support to Logo to support his version of the turtle robot, which is a simple robot controlled from the user’s workstation and designed to carry out the drawing functions assigned to it using a small retractable pen set into or attached to the robot’s body.

The standard library of the Python programming language now contains a Turtle graphics module. Turtle in Python, like its Logo ancestor, allows programmers to manipulate one or more turtles in a two-dimensional space.

Overview of the syntax

A location, an orientation (or direction), and a pen are the three qualities of the turtle. Color, width, and on/off state are all properties of the pen (also called down and up).

“Move ahead 10 spaces” and “turn left 90 degrees” are orders that the turtle responds to based on its current location. The turtle’s pen can also be managed by enabling it, changing its color, and adjusting its breadth. By visualizing what they would do if they were the turtle, a pupil may comprehend (and forecast and reason about) the turtle’s motion. This is referred to as “body syntonic” reasoning by Seymour Papert.

Basic syntax:

import turtle // start of the program
//body
//of the main
//code
turtle.done() //end of the program

Inorder to understand the codes to draw various shapes given below, Getting Started with Powerful yet Easy Python Graphics Module, Turtle.

Chess Game Board

The principles governs everything in every subject. They’re the basis on which all incredible exploits are built. To execute acts that appear magical to us simple humans, great scientists and artists equally rely on their most basic skills. Same is the case for python turtle module.

In order to make any drawings in python turtle, for this instance a chess game board, the fundamentals are mandatory. If you think about it a chess board is only a square board with 64 inner squares inside. All of the 64 squares begin sequentially white or black. Enough of explaining, let’s get coding!

The following is the code for drawing a chessboard using python turtle module:

import turtle

def draw_box(t,x,y,size,fill_color):
t.penup() # no drawing!
t.goto(x,y) # move the pen to a different position
t.pendown() # resume drawing

t.fillcolor(fill_color)
t.begin_fill()  # Shape drawn after this will be filled with this color!

for i in range(0,4):
board.forward(size) # move forward
board.right(90) # turn pen right 90 degrees

t.end_fill() # Go ahead and fill the rectangle!

def draw_chess_board():
square_color = "black" # first chess board square is black
start_x = 0 # starting x position of the chess board
start_y = 0 # starting y position of the chess board
box_size = 30 # pixel size of each square in the chess board
for i in range(0,8): # 8x8 chess board
for j in range(0,8):
draw_box(board,start_x+j*box_size,start_y+i*box_size,box_size,square_color)
square_color = 'black' if square_color == 'white' else 'white' # toggle after a column
square_color = 'black' if square_color == 'white' else 'white' # toggle after a row!

board = turtle.Turtle()
draw_chess_board()
turtle.done()

Output

The following is the output of drawing a chessboard using python turtle module:

SHARE Drawing a Chessboard using Python Turtle Module