Table of contents
1.
Introduction
2.
Steps to draw the chess board:
2.1.
Python implementation
2.1.1.
Output
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Chess Board using Turtle

Author Manan Singhal
0 upvote

Introduction

Turtle is a Python module that comes pre-installed. It allows you to draw using a screen (cardboard) and a turtle (pen). We must move the turtle to draw something on the screen (pen). Some functions, such as forward() and reverse(), can be used to move the turtle.

Steps to draw the chess board:

  • Import a turtle, and create an object from it.
  • Set the size of the screen and the turtle's position.
  • Define a square-drawing method.
  • Call the procedure eight times in a loop, each with a different color.
  • Remove the turtle object.

Python implementation

# importing turtle package to draw the chess board using turtle
import turtle 
  
# create screen object and turtle object
screen = turtle.Screen()
pen = turtle.Turtle()
  
# method to draw square
def draw():
  for i in range(4):
    pen.forward(30)
    pen.left(90)
  pen.forward(30)

# Main Code to draw the chess board using turtle
if __name__ == "__main__" :
      
    # set screen
    screen.setup(600, 600)
      
    # set turtle object speed
    pen.speed(1000)
      
    # loops for board
    for i in range(8):
      
      # not ready to draw
      pen.up()
      
      # set position for every row
      pen.setpos(0, 30 * i)
      
      # ready to draw
      pen.down()

      for j in range(8):
        # conditions for alternative color
        if (i + j)% 2 == 0:
          color ='black'
        else:
          color ='white'
        # fill with given color
        pen.fillcolor(color)
        # start filling with color
        pen.begin_fill()
        draw()
        pen.end_fill()
    # hide the turtle
    pen.hideturtle()

 
# Output: Draw the chess board using turtle
You can also try this code with Online Python Compiler
Run Code

Output

You can compile it with online python compiler.

You can also read about Palindrome Number in Python here.

FAQs

  1. Which command is used to reset the pen (turtle)?
    We may reset the pen using the command turtle.reset(). We get a blank page with an arrow on it after running this command.
     
  2. What is the use of the function turtle.shape()?
    This function is used to change the turtle shape to a named shape or, if no name is specified, to return the current shape's name.
     
  3. Explain the use of the function turtle.setpos()?
    The turtle is moved to an absolute position using this function. setpos, setposition, and goto are all aliases for this function.
     
  4. Explain the use of the function turtle.penup()?
    The turtle will be lifted off the "digital canvas" by .penup(), and it will not draw if it is moved while in this state.
     
  5. Explain the use of the function turtle.towards()?
    The angle between the line from turtle position to the point indicated by x, y, and the turtle's initial orientation is returned by this function.

Key Takeaways

In this article, we draw the chessboard using turtle in python. We hope that this blog is going to help you understand the concept of turtle library and one of the applications i.e. to draw the chessboard, and if you like to learn more about it, check out our other blogs on the turtle.

Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Happy Coding!

Live masterclass