Simple Python Turtle Graphic and Code: Chess Board

Опубликовано: 22 Октябрь 2024
на канале: Basic Python Turtle Art
178
3

The playing board of Chess created by 4 sets of 16 squares and appropriate placement of the "fillcolor" command. Upon execution of "fillcolor", alternate squares in the two-column set of 16 are colored green because Python Turtle convention color-fills all shapes partly or fully enclosed by the line that connects the initial and final positions of the turtle.

Feel free to copy the basic Python Turtle code that is given below. Don't hesitate to ask questions about the code if you have any. Enjoy! Please comment, like, or subscribe :)

Incidentally, for more graphics and variations, please visit my author url at https://www.amazon.com/author/basicpy...


import turtle
t = turtle.Turtle() #Definitions and Initializations
screen = turtle.Screen()
t.pensize(4)
t.pencolor("green")
square_side_length = 60 #side length of 1 of the 64 squares; Changeable
x = -3 * square_side_length #x value of point where drawing starts
def squares(): #Procedure for drawing a group of 16 squares in 2 columns
t.fillcolor("green") #Color fill, by Python Turtle convention, applied to shapes enclosed by
t.begin_fill() #line connecting initial and final positions of moving turtle
for j in range(4): #For 4 groups of lines, each of which partially draws 2 rows of squares
t.forward(square_side_length) #Initial movement of turtle for a group of lines; initial position of
t.right(90) #turtle at x = -3 * square_side_length, not at -4 ..., to ensure that
t.forward(square_side_length) #the color-filled squares are alternate or do not share sides
t.right(90) #Turtle direction towards 2nd horizontal line
t.forward(2 * square_side_length) #Movement of turtle over horizontal line measuring 2 * square_side_length
t.left(90) #Turtle direction downward
t.forward(square_side_length) #Movement of turtle over vertical line measuring square_side_length
t.left(90) #Turtle direction towards 3rd horizontal line
t.forward(square_side_length) #Movement of turtle over horizontal line measuring square_side_length
t.end_fill() #Execution of green color-fill on 8 enclosed and alternating squares
def chess_board_outline(): #For outline of the whole chess board
for i in range(4):
t.forward(8 * square_side_length)
t.right(90)
for i in range(4): #Python_graphic start of drawing procedure; for 4 groups of 16 squares -- 8 on
t.penup() #first column and another 8 on the 2nd
t.goto(x, 4 * square_side_length) #Initial position of turtle to draw a group of 16 squares
t.pendown()
squares() #Function call to draw a group of 16 squares
x += 2 * square_side_length #x value change for the starting point of drawing the next group of
t.penup() #16 squares
t.goto(-4 * square_side_length, 4 * square_side_length) #Starting point to draw the outline of the whole chess board
t.pendown()
chess_board_outline()
t.hideturtle()
screen.exitonclick()