用python中的海龟绘图绘出孙延两字并给出代码

用python中的海龟绘图绘出孙延两字并给出代码需附图,并给出两个字的代码

这个chatgpt是写不出来的,我先写了一个字,你看看。

img

import turtle
def draw_sun():
    turtle.penup()
    turtle.goto(-150, 50)
    turtle.pendown()
    turtle.right(0)
    turtle.forward(15)
    turtle.right(130)
    turtle.forward(13)
    turtle.left(40)
    turtle.forward(26)
    turtle.right(130)
    turtle.forward(10)
    turtle.penup()
    turtle.goto(-152, 26)
    turtle.pendown()
    turtle.right(98)
    turtle.forward(23)
    turtle.penup()
    turtle.goto(-125, 50)
    turtle.pendown()
    turtle.right(132)
    turtle.forward(35)
    turtle.right(130)
    turtle.forward(10)
    turtle.penup()
    turtle.goto(-130, 35)
    turtle.pendown()
    turtle.right(252)
    turtle.forward(10)
    turtle.penup()
    turtle.goto(-120, 35)
    turtle.pendown()
    turtle.left(52)
    turtle.forward(10)


turtle.speed(0) # 设置画笔速度为最快
draw_sun()

turtle.done() # 绘制完成后不退出窗口

可通过爬虫获取获取汉字的笔画坐标信息,之后根据汉字的笔画坐标绘制汉字即可。代码如下:

import turtle
import requests
from urllib.parse import quote
import re


# 这个函数是爬虫获取汉字的笔画坐标信息
def obtain_coordinate(target_word):  # 获取汉字的坐标
    """
    获取汉字的坐标
    :param target_word:
    :return:
    """
    url = "https://bihua.bmcx.com/web_system/bmcx_com_www/system/file/bihua/get_0/"

    params = {
        'font': quote(target_word).replace("%", "").lower(),
        'shi_fou_zi_dong': '1',
        'cache_sjs1': '20031914',
    }
    response = requests.get(url, params=params)
    content = response.text
    content = content.replace('hzbh.main(', '').split(');document.getElementById')[0]
    content = content.split('{')[-1].split("}")[0]
    pattern = re.compile(r'\w:\[(.+?)\]')
    result = re.split(pattern, content)
    order_xy_routine = []
    words_cnt = 0
    for r in result:
        sec = re.findall(r'\'.+?\'', r)
        if len(sec):
            orders = sec[1].split('#')
            for order in orders:
                order_str = re.findall(r'\(\d+,\d+\)', order)
                order_xy = [eval(xy) for xy in order_str]
                order_xy_routine.append(order_xy)
            words_cnt += 1
    print(order_xy_routine)
    return order_xy_routine


# 这个函数是根据汉字的笔画坐标信息,打印汉字 有笔画的轨迹
def draw_words(target_words, startx, starty, lineNum=1):  # 画汉字
    """
    画汉字
    :param target_words:
    :param startx:
    :param starty:
    :param lineNum:
    :return:
    """
    turtle.color("black", "black")  # 设置画笔颜色
    turtle.pu()  # 抬起画笔
    coordinates = obtain_coordinate(target_words)
    for index, coordinate in enumerate(coordinates):
        turtle.goto((startx + coordinate[0][0]) / 2, -(starty + coordinate[0][1]) / 2)
        turtle.pd()
        for xy in coordinate:
            x, y = xy
            turtle.goto((startx + x) / 2, -(starty + y) / 2)
        turtle.pu()


if __name__ == '__main__':
    # 画汉字
    draw_words("孙", -900, -300)
    draw_words("延", 0, -300)

    turtle.done()



运行结果:

img


import turtle

# 设置画笔初始位置和方向
turtle.penup()
turtle.goto(-200, 0)
turtle.pendown()

# 绘制孙
turtle.left(90)
turtle.forward(200)
turtle.right(90)
turtle.circle(-100, 180)
turtle.left(180)
turtle.circle(-100, 180)

# 调整画笔位置和方向
turtle.penup()
turtle.goto(-100, 0)
turtle.pendown()

# 绘制延
turtle.left(90)
turtle.forward(200)
turtle.right(90)
turtle.circle(100, 180)
turtle.left(180)
turtle.circle(100, 180)

# 隐藏画笔
turtle.hideturtle()

# 关闭绘图窗口
turtle.done()

可以借鉴下

1.
import turtle

# 绘制“孙”字
turtle.penup()
turtle.goto(-100, 0)
turtle.pendown()
turtle.setheading(90)
turtle.forward(100)
turtle.right(90)
turtle.circle(-50,180)
turtle.left(180)
turtle.circle(-50,180)
turtle.right(90)
turtle.forward(100)

# 绘制“延”字
turtle.penup()
turtle.goto(50, 0)
turtle.pendown()
turtle.setheading(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(50)
turtle.circle(-50,180)
turtle.left(180)
turtle.circle(-50,180)
turtle.right(90)
turtle.forward(50)

turtle.done()

2.
import turtle

# 绘制“A”字
turtle.penup()
turtle.goto(-100, 0)
turtle.pendown()
turtle.setheading(60)
turtle.forward(100)
turtle.right(120)
turtle.forward(100)
turtle.backward(50)
turtle.right(120)
turtle.forward(50)
turtle.left(120)
turtle.forward(50)

# 绘制“I”字
turtle.penup()
turtle.goto(50, 0)
turtle.pendown()
turtle.setheading(90)
turtle.forward(100)
turtle.penup()
turtle.goto(50, 50)
turtle.pendown()
turtle.backward(100)

turtle.done()

import turtle
import requests
from urllib.parse import quote
import re
 
 
# 这个函数是爬虫获取汉字的笔画坐标信息
def obtain_coordinate(target_word):  # 获取汉字的坐标
    """
    获取汉字的坐标
    :param target_word:
    :return:
    """
    url = "https://bihua.bmcx.com/web_system/bmcx_com_www/system/file/bihua/get_0/"
 
    params = {
        'font': quote(target_word).replace("%", "").lower(),
        'shi_fou_zi_dong': '1',
        'cache_sjs1': '20031914',
    }
    response = requests.get(url, params=params)
    content = response.text
    content = content.replace('hzbh.main(', '').split(');document.getElementById')[0]
    content = content.split('{')[-1].split("}")[0]
    pattern = re.compile(r'\w:\[(.+?)\]')
    result = re.split(pattern, content)
    order_xy_routine = []
    words_cnt = 0
    for r in result:
        sec = re.findall(r'\'.+?\'', r)
        if len(sec):
            orders = sec[1].split('#')
            for order in orders:
                order_str = re.findall(r'\(\d+,\d+\)', order)
                order_xy = [eval(xy) for xy in order_str]
                order_xy_routine.append(order_xy)
            words_cnt += 1
    print(order_xy_routine)
    return order_xy_routine
 
 
# 这个函数是根据汉字的笔画坐标信息,打印汉字 有笔画的轨迹
def draw_words(target_words, startx, starty, lineNum=1):  # 画汉字
    """
    画汉字
    :param target_words:
    :param startx:
    :param starty:
    :param lineNum:
    :return:
    """
    turtle.color("black", "black")  # 设置画笔颜色
    turtle.pu()  # 抬起画笔
    coordinates = obtain_coordinate(target_words)
    for index, coordinate in enumerate(coordinates):
        turtle.goto((startx + coordinate[0][0]) / 2, -(starty + coordinate[0][1]) / 2)
        turtle.pd()
        for xy in coordinate:
            x, y = xy
            turtle.goto((startx + x) / 2, -(starty + y) / 2)
        turtle.pu()
 
 
if __name__ == '__main__':
    # 画汉字
    draw_words("孙", -800, -300)
    draw_words("延", 0, -300)
 
    turtle.done()
 
 
 

```bash


```


import turtle
name = "孙延"
turtle.penup()
for i in name:
    turtle.write(i, font=("Arial",100))
    turtle.fd(150)
turtle.done()

img