Python Painting Master-the stepping stone to the turtle library

[Agricultural University] Python Painting Master

    • 1. Let us analyze these functions used one by one.
    • 2. Let’s take a look at all the final code
    • 3. Finally, let’s take a look at the results of the operation.

In Python, we can use graphics libraries like turtle to draw chess. However, due to the complexity of chess, this may require a lot of code and patience. Here, I will provide a simple example to explain how to use turtle to draw a chessboard. Among them, the main functions used in this example are as follows:
draw_chessboard(x, y):
draw_camp():
marker(x, y):
draw_marker_gun():
draw_marker_soldier():
piece_cirle(radius):
piece_font(x, fontColor):
draw_boundary(x, y):
draw_piece_soldier(x, y):
draw_piece_gun(x, y):
draw_piece_other(x, y):

1. Let us analyze these functions used one by one

First, draw the squares of the chessboard based on the initial position coordinates x, y of the brush and the cell width.

def draw_chessboard(x, y):
   
    t.penup() # Lift the brush
    t.goto(x - width // 5, y - width // 5)
    t.pendown() # Drop the brush
    t.fillcolor('orange')
    t.begin_fill() # Start filling
    for i in range(2):
        t.forward(8 * width + 2 * width // 5)
        t.left(90)
        t.forward(9 * width + 2 * width // 5)
        t.left(90)
    t.end_fill()
    t.penup()
    for i in range(10):
        t.goto(x, y + i * width)
        t.pendown()
        t.forward(8 * width)
        t.penup()
    t.left(90)
    for i in range(9):
        t.goto(x + i * width, y)
        t.pendown()
        t.forward(4 * width)
        t.penup()
        t.forward(width)
        t.pendown()
        t.forward(4 * width)
        t.penup()

Then draw the diagonal line of the chessboard commander according to the coordinates x, y of the initial position of the brush and the width of the cell.

def draw_camp():
    
    t.penup()
    t.goto(-width, -4 * width)
    t.pendown()
    t.goto(width, -2 * width)
    t.goto(-width, -2 * width)
    t.goto(width, -4 * width)
    t.penup()
    t.goto(-width, 5 * width)
    t.pendown()
    t.goto(width, 3 * width)
    t.goto(-width, 3 * width)
    t.goto(width, 5 * width)
    t.penup()

Draw artillery marks

def marker(x, y):
    
    t.penup()
    t.goto(x - 3, y - 9)
    for i in range(4):
        t.pendown()
        t.forward(6)
        t.left(90)
        t.forward(6)
        t.right(90)
        t.penup()
        t.forward(6)
        t.pendown()
        t.right(90)
        t.penup()

Draw marks for both sides’ gun positions

def draw_marker_gun():
    
    maker_of_x = [pos_of_x + width, pos_of_x + 7 * width, pos_of_x + width, pos_of_x + 7 * width] # Cannon position x coordinate
    maker_of_y = [pos_of_y + 2 * width, pos_of_y + 2 * width, pos_of_y + 7 * width, pos_of_y + 7 * width]
    for i in range(4):
        marker(maker_of_x[i], maker_of_y[i])

Draw marks for the positions of soldiers on both sides

def draw_marker_soldier():
    
    for i in range(5):
        marker(pos_of_x + 2 * i * width, pos_of_y + 3 * width)
        marker(pos_of_x + 2 * i * width, pos_of_y + 6 * width)

Draw the circle of the chess pieces and fill it with white

def piece_cirle(radius):
    t.pensize(3)
    t.fillcolor('white') # Set the fill color to white
    t.begin_fill()
    t.circle(radius) # Draw a circle with radius radius
    t.end_fill()

Receive the text and color on the chess piece and write the text on the chess piece

def piece_font(x, fontColor):
    t.color(fontColor)
    t.write(x, font=('official script', width // 2, 'normal')) # Write on the chess pieces

Writing about Chu, He and Han realms

def draw_boundary(x, y):
    t.goto(x + 7 / 4 * width, y + 17 / 4 * width)
    t.pendown()
    t.color("green")
    t.write("Chu River Han Boundary", font=('official script', width // 2, 'normal'))
    t.penup()

Draw pawns and pawns for both black and red sides

def draw_piece_soldier(x, y):
    for j in range(2):
        for i in range(5):
            t.penup()
            t.goto(x + 2 * i * width + 1 / 3 * width, y + 3 * width + 3 * j * width)
            t.pendown()
            t.pencolor(font_color[j])
            piece_circle(1 / 3 * width)
            t.penup()
            t.goto(x + 2 * i * width - 1 / 3 * width, y + 3 * width - 1 / 4 * width + 3 * j * width)
            t.pendown()
            piece_font(chess_pieces[j][9], font_color[j])

Draw the chess pieces and cannons of both black and red sides

def draw_piece_gun(x, y):
        for j in range(2):
        for i in range(2):
            t.penup()
            t.goto(x + width + 6 * i * width + 1 / 3 * width, y + 2 * width + j * 5 * width)
            t.pendown()
            t.pencolor(font_color[j])
            piece_circle(1 / 3 * width)
            t.penup()
            t.goto(x + 2 / 3 * width + 6 * i * width, y + 2 * width - 1 / 4 * width + j * 5 * width)
            t.pendown()
            piece_font(chess_pieces[j][10], font_color[j])

Add text to other chess pieces

def draw_piece_other(x, y):
    
    for j in range(2):
        for i in range(9):
            t.penup()
            t.goto(x + i * width + 1 / 3 * width, y + 9 * j * width)
            t.pendown()
            t.pencolor(font_color[j])
            piece_circle(1 / 3 * width)
            t.penup()
            t.goto(x + i * width - 1 / 3 * width, y - 1 / 4 * width + 9 * j * width)
            t.pendown()
            piece_font(chess_pieces[j][i], font_color[j])

Finally, run the main program

if __name__ == '__main__':
    # t.tracer(10) # Uncommenting can speed up the refresh drawing speed
    t.speed(0) # Drawing speed
    t.pensize(2) # Brush thick line
    width = 60 # Set the width of each grid
    t.screensize(10 * width, 12 * width) # The canvas size is 12 times the cell width
    pos_of_x, pos_of_y = -4 * width, -4 * width # Initial coordinates of the brush
    chess_pieces = [['car', 'horse', 'phase', 'official', 'handsome', 'official', 'phase', 'horse\ ', 'car', 'soldier', 'cannon'],
                    ['Car', 'Horse', 'Xiang', 'Shi', 'Jiang', 'Shi', 'Xiang', 'Horse', \ 'Car', 'Pawn', 'Cannon']]
    font_color = ['red', 'green'] # Set the chess piece fill color
    draw_chessboard(pos_of_x, pos_of_y) # Draw an empty chessboard
    draw_camp() # Draw the general’s barracks
    draw_marker_gun() # Draw gun position markers
    draw_marker_soldier() # Draw soldier position markers
    draw_boundary(pos_of_x, pos_of_y) # Draw the Chu-Han boundary
    draw_piece_soldier(pos_of_x, pos_of_y) # Draw chess pieces and pawns
    draw_piece_gun(pos_of_x, pos_of_y) # Draw the chess piece gun
    draw_piece_other(pos_of_x, pos_of_y) # Draw other pieces
    t.hideturtle() # Hide brush
    t.update() # Update cache
    t.done() # End drawing

2. Let’s take a look at the final code

import turtle as t


def draw_chessboard(x, y):
    """According to the initial position coordinates x, y of the brush and the cell width, draw the squares of the chessboard"""
    t.penup() # Lift the brush
    t.goto(x - width // 5, y - width // 5)
    t.pendown() # Drop the brush
    t.fillcolor('orange')
    t.begin_fill() # Start filling
    for i in range(2):
        t.forward(8 * width + 2 * width // 5)
        t.left(90)
        t.forward(9 * width + 2 * width // 5)
        t.left(90)
    t.end_fill()
    t.penup()
    for i in range(10):
        t.goto(x, y + i * width)
        t.pendown()
        t.forward(8 * width)
        t.penup()
    t.left(90)
    for i in range(9):
        t.goto(x + i * width, y)
        t.pendown()
        t.forward(4 * width)
        t.penup()
        t.forward(width)
        t.pendown()
        t.forward(4 * width)
        t.penup()


def draw_camp():
    """According to the initial position coordinates x, y of the brush and the width of the cell, draw the diagonal line of the chessboard commander"""
    t.penup()
    t.goto(-width, -4 * width)
    t.pendown()
    t.goto(width, -2 * width)
    t.goto(-width, -2 * width)
    t.goto(width, -4 * width)
    t.penup()
    t.goto(-width, 5 * width)
    t.pendown()
    t.goto(width, 3 * width)
    t.goto(-width, 3 * width)
    t.goto(width, 5 * width)
    t.penup()


def marker(x, y):
    """Draw artillery marks"""
    t.penup()
    t.goto(x - 3, y - 9)
    for i in range(4):
        t.pendown()
        t.forward(6)
        t.left(90)
        t.forward(6)
        t.right(90)
        t.penup()
        t.forward(6)
        t.pendown()
        t.right(90)
        t.penup()


def draw_marker_gun():
    """Draw marks for both sides' gun positions"""
    maker_of_x = [pos_of_x + width, pos_of_x + 7 * width, pos_of_x + width, pos_of_x + 7 * width] # Cannon position x coordinate
    maker_of_y = [pos_of_y + 2 * width, pos_of_y + 2 * width, pos_of_y + 7 * width, pos_of_y + 7 * width]
    for i in range(4):
        marker(maker_of_x[i], maker_of_y[i])


def draw_marker_soldier():
    """Draw marks for the positions of soldiers on both sides"""
    for i in range(5):
        marker(pos_of_x + 2 * i * width, pos_of_y + 3 * width)
        marker(pos_of_x + 2 * i * width, pos_of_y + 6 * width)


def piece_cirle(radius):
    """Draw a circle of chess pieces and fill it with white"""
    t.pensize(3)
    t.fillcolor('white') # Set the fill color to white
    t.begin_fill()
    t.circle(radius) # Draw a circle with radius radius
    t.end_fill()


def piece_font(x, fontColor):
    """Receive the text and color on the chess piece, and write the text on the chess piece"""
    t.color(fontColor)
    t.write(x, font=('official script', width // 2, 'normal')) # Write on the chess pieces


def draw_boundary(x, y):
    """Written on the Chu, He and Han realms"""
    t.goto(x + 7 / 4 * width, y + 17 / 4 * width)
    t.pendown()
    t.color("green")
    t.write("Chu River Han Boundary", font=('official script', width // 2, 'normal'))
    t.penup()


def draw_piece_soldier(x, y):
    """Draw the pawns and pawns of the black and red sides"""
    for j in range(2):
        for i in range(5):
            t.penup()
            t.goto(x + 2 * i * width + 1 / 3 * width, y + 3 * width + 3 * j * width)
            t.pendown()
            t.pencolor(font_color[j])
            piece_circle(1 / 3 * width)
            t.penup()
            t.goto(x + 2 * i * width - 1 / 3 * width, y + 3 * width - 1 / 4 * width + 3 * j * width)
            t.pendown()
            piece_font(chess_pieces[j][9], font_color[j])


def draw_piece_gun(x, y):
    """Draw the chess pieces and cannons of the black and red sides"""
    for j in range(2):
        for i in range(2):
            t.penup()
            t.goto(x + width + 6 * i * width + 1 / 3 * width, y + 2 * width + j * 5 * width)
            t.pendown()
            t.pencolor(font_color[j])
            piece_circle(1 / 3 * width)
            t.penup()
            t.goto(x + 2 / 3 * width + 6 * i * width, y + 2 * width - 1 / 4 * width + j * 5 * width)
            t.pendown()
            piece_font(chess_pieces[j][10], font_color[j])


def draw_piece_other(x, y):
    """Add text for other chess pieces"""
    for j in range(2):
        for i in range(9):
            t.penup()
            t.goto(x + i * width + 1 / 3 * width, y + 9 * j * width)
            t.pendown()
            t.pencolor(font_color[j])
            piece_circle(1 / 3 * width)
            t.penup()
            t.goto(x + i * width - 1 / 3 * width, y - 1 / 4 * width + 9 * j * width)
            t.pendown()
            piece_font(chess_pieces[j][i], font_color[j])


if __name__ == '__main__':
    # t.tracer(10) # Uncommenting can speed up the refresh drawing speed
    t.speed(0) # Drawing speed
    t.pensize(2) # Brush thick line
    width = 60 # Set the width of each grid
    t.screensize(10 * width, 12 * width) # The canvas size is 12 times the cell width
    pos_of_x, pos_of_y = -4 * width, -4 * width # Initial coordinates of the brush
    chess_pieces = [['car', 'horse', 'phase', 'official', 'handsome', 'official', 'phase', 'horse\ ', 'car', 'soldier', 'cannon'],
                    ['Car', 'Horse', 'Xiang', 'Shi', 'Jiang', 'Shi', 'Xiang', 'Horse', \ 'Car', 'Pawn', 'Cannon']]
    font_color = ['red', 'green'] # Set the chess piece fill color
    draw_chessboard(pos_of_x, pos_of_y) # Draw an empty chessboard
    draw_camp() # Draw the general’s barracks
    draw_marker_gun() # Draw gun position markers
    draw_marker_soldier() # Draw soldier position markers
    draw_boundary(pos_of_x, pos_of_y) # Draw the Chu-Han boundary
    draw_piece_soldier(pos_of_x, pos_of_y) # Draw chess pieces and pawns
    draw_piece_gun(pos_of_x, pos_of_y) # Draw the chess piece gun
    draw_piece_other(pos_of_x, pos_of_y) # Draw other pieces
    t.hideturtle() # Hide brush
    t.update() # Update cache
    t.done() # End drawing

3. Finally, let’s take a look at the results