Turtle is an inbuilt module in Python. It provides drawing using a screen (cardboard) and turtle (pen). To draw something on the screen, we need to move the turtle (pen). To move turtle, there are some functions i.e forward(), backward(), etc.
For drawing Chess Board following steps are used :
- Import turtle and making an object.
- Set screen size and turtle position.
- Define a method to draw a square
- Call the method 8 times under another loop for 8 times with alternative color.
- Hide the turtle object.
Below is the implementation :
python3
import turtle
sc = turtle.Screen()
pen = turtle.Turtle()
def draw():
for i in range ( 4 ):
pen.forward( 30 )
pen.left( 90 )
pen.forward( 30 )
if __name__ = = "__main__" :
sc.setup( 600 , 600 )
pen.speed( 100 )
for i in range ( 8 ):
pen.up()
pen.setpos( 0 , 30 * i)
pen.down()
for j in range ( 8 ):
if (i + j) % 2 = = 0 :
col = 'black'
else :
col = 'white'
pen.fillcolor(col)
pen.begin_fill()
draw()
pen.end_fill()
pen.hideturtle()
|
Output :
