Python用turtle画汉字

打印汉字

如果单纯想在turtle的界面输出汉字,可以直接使用turtle.write 打印出汉字。

# 这个函数就是打印汉字 没有移动的轨迹
def writeWord(target_word, startx, starty):  # 基于坐标,打印汉字
    """
    基于坐标,打印单个汉字
    :param target_word: 目标汉字
    :param startx: 起始位置x
    :param starty: 起始位置y
    :return:
    """
    turtle.color("black", "black")  # 设置画笔颜色
    turtle.pu()  # 抬起画笔
    turtle.goto(startx, starty)  # 移动到指定位置
    turtle.pd()  # 下笔
    turtle.write(target_word, move=False, align='left', font=('汉仪程行简', 120, 'normal'))  # 打印汉字

画汉字(重要)

但是如果想画出汉字,则需要汉字的笔画坐标。这里使用了爬虫获取汉字笔画坐标的信息。

# 这个函数是爬虫获取汉字的笔画坐标信息
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
 

接下来就是将笔画信息在turtle的界面上画出来了。

#这个函数是根据汉字的笔画坐标信息,打印汉字 有笔画的轨迹
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()
 

全部代码


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


# 这个函数就是打印汉字 没有移动的轨迹
def writeWord(target_word, startx, starty):  # 基于坐标,打印汉字
    """
    基于坐标,打印单个汉字
    :param target_word: 目标汉字
    :param startx: 起始位置x
    :param starty: 起始位置y
    :return:
    """
    turtle.color("black", "black")  # 设置画笔颜色
    turtle.pu()  # 抬起画笔
    turtle.goto(startx, starty)  # 移动到指定位置
    turtle.pd()  # 下笔
    turtle.write(target_word, move=False, align='left', font=('汉仪程行简', 120, 'normal'))  # 打印汉字

# 这个函数是爬虫获取汉字的笔画坐标信息
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__':
    # 方法一  直接打印汉字
    writeWord("彩虹", -120, -60)

    # 方法二  画汉字
    draw_words("彩", -800, 0)
    draw_words("虹", 0, 0)


    turtle.done()  # 保证界面不退出 可点击右上角关闭

原文链接:Python小海龟turtle画汉字_turtle中write输出汉字-优快云博客

好的,下面是用中文回复的Python代码,可以用turtle模块出圣诞老人的图案: ```python import turtle # 设置布大小和背景颜色 turtle.setup(800, 600) turtle.bgcolor('white') # 圣诞老人的帽子 turtle.penup() turtle.goto(-200, 200) turtle.pendown() turtle.color('red') turtle.begin_fill() turtle.forward(200) turtle.left(120) turtle.forward(200) turtle.left(120) turtle.forward(200) turtle.end_fill() # 圣诞老人的脸 turtle.penup() turtle.goto(, ) turtle.pendown() turtle.color('white') turtle.begin_fill() turtle.circle(100) turtle.end_fill() # 圣诞老人的眼睛 turtle.penup() turtle.goto(-40, 50) turtle.pendown() turtle.color('black') turtle.begin_fill() turtle.circle(20) turtle.end_fill() turtle.penup() turtle.goto(40, 50) turtle.pendown() turtle.begin_fill() turtle.circle(20) turtle.end_fill() # 圣诞老人的鼻子 turtle.penup() turtle.goto(, 20) turtle.pendown() turtle.color('red') turtle.begin_fill() turtle.circle(30) turtle.end_fill() # 圣诞老人的嘴巴 turtle.penup() turtle.goto(-40, -20) turtle.pendown() turtle.color('black') turtle.right(90) turtle.circle(40, 180) # 圣诞老人的胡须 turtle.penup() turtle.goto(-40, -20) turtle.pendown() turtle.right(180) turtle.forward(60) turtle.right(180) turtle.forward(60) turtle.right(120) turtle.forward(60) turtle.right(180) turtle.forward(60) turtle.right(120) turtle.forward(60) # 圣诞老人的身体 turtle.penup() turtle.goto(-100, -200) turtle.pendown() turtle.color('red') turtle.begin_fill() turtle.forward(200) turtle.right(90) turtle.forward(300) turtle.right(90) turtle.forward(200) turtle.right(90) turtle.forward(300) turtle.end_fill() # 圣诞老人的腰带 turtle.penup() turtle.goto(-100, -100) turtle.pendown() turtle.color('black') turtle.begin_fill() turtle.forward(200) turtle.right(90) turtle.forward(50) turtle.right(90) turtle.forward(200) turtle.right(90) turtle.forward(50) turtle.end_fill() # 圣诞老人的鞋子 turtle.penup() turtle.goto(-100, -200) turtle.pendown() turtle.color('black') turtle.begin_fill() turtle.forward(50) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(50) turtle.end_fill() turtle.penup() turtle.goto(50, -200) turtle.pendown() turtle.begin_fill() turtle.forward(50) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(50) turtle.end_fill() # 完成turtle.done() ``` 运行上述代码后,就可以在turtle布上看到一个可爱的圣诞老人啦!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

火花怪怪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值