txt写代码绘图

python 复制代码
import re
import turtle
import tkinter as tk
from tkinter import filedialog

def remove_between_hashes_and_newlines(txt_content):
    # 使用正则表达式去掉#和\n之间的内容
    pattern = re.compile(r'#.*?\n')
    result = re.sub(pattern, '', txt_content)

    # 去掉#和\n
    # result = result.replace('#', '').replace('\n', '')
    result = result.replace('#', '')

    return result

def expand_repeat_structure(input_string):
    # 定义正则表达式模式
    repeat_pattern = re.compile(r'重复{(.+?)}(\d+)次')

    # 搜索input_string中匹配模式的所有结果
    matches = re.finditer(repeat_pattern, input_string)

    # 初始化展开后的字符串
    expanded_string = input_string

    # 遍历匹配的重复结构
    for match in matches:
        content, count = match.groups()
        content = content.strip()
        count = int(count)

        # 生成展开后的重复内容
        expanded_content = f"{content};" * count
        expanded_content = expanded_content.rstrip(';')

        # 替换原字符串中的重复结构
        expanded_string = expanded_string.replace(match.group(0), expanded_content)

    return expanded_string

# 测试

def parse_and_translate(txt_content):
    # 定义正则表达式模式
    # print(txt_content)
    pattern = re.compile(r'(.*?)(?:;|$;)')


    # 搜索txt_content中匹配模式的所有结果
    matches = re.findall(pattern, txt_content)
    # print(matches)

    # 初始化Python代码字符串
    python_code = ""

    for match in matches:
        # 根据匹配的内容生成对应的Python代码
        if '开始画画' in match or '开始' in match:
            python_code += "import turtle\n\n"
        elif '抬笔' in match or '提笔' in match:
            python_code += "turtle.penup()\nturtle.hideturtle()\n"
        elif '到位置' in match:
            # 提取位置信息
            position = re.search(r'\((-?\d+),(-?\d+)\)', match)
            if position:
                x, y = position.groups()
                python_code += f"turtle.goto({x},{y})\n"
        elif '落笔' in match:
            python_code += "turtle.pendown()\nturtle.showturtle()\n"
        elif '前进' in match:
            distance = re.search(r'(\d+)', match)
            if distance:
                dist_value = distance.group()
                python_code += f"turtle.forward({dist_value})\n"
        elif '笔粗' in match or '笔粗细' in match:
            distance = re.search(r'(\d+)', match)
            if distance:
                dist_value = distance.group()
                python_code += f"turtle.pensize({dist_value})\n"
        elif '笔色' in match or '笔颜色' in match:
            distance = re.search(r'(\d+)', match)
            if distance:
                dist_value = distance.group()
                mycolor = ["black","red","green","blue","yellow","orange"]
                # print(int(dist_value))
                color1 = mycolor[int(dist_value)]
                # print(color1)
                python_code += "turtle.pencolor(\'"+color1+"\')\n"
        elif '右转' in match:
            angle = re.search(r'(\d+)', match)
            if angle:
                angle_value = angle.group()
                python_code += f"turtle.right({angle_value})\n"
        elif '左转' in match:
            angle = re.search(r'(\d+)', match)
            if angle:
                angle_value = angle.group()
                python_code += f"turtle.left({angle_value})\n"
        elif '画完' in match or '结束' in match:
            python_code += "turtle.done()\n"

    return python_code




# 解析并翻译成Python代码
# txt_content = '''
# 开始画画;
# 抬笔;
# 到位置(0,0);
# 落笔;
# 前进100米;
# 右转90度;
# 前进100米;
# 右转90度;
# 前进100米;
# 右转90度;
# 前进100米;
# 右转90度;
# 重复{前进50米;右转60度;}7次;
# 重复{前进30米;右转45度;}9次;
# 画完;
# '''

root = tk.Tk()
root.withdraw()

set_file = filedialog.askopenfilename(title="选择文件", filetypes=[("文件", "*.txt")])
# # 读取txt文件内容
with open(set_file, 'r', encoding='utf-8') as file:
    txt_content = file.read()

# a=input('按回车开始:')

txt_content = txt_content.replace(';',';')
txt_content = txt_content.replace(',',',')
txt_content = txt_content.replace(')',')')
txt_content = txt_content.replace('(','(')
txt_content = txt_content.replace('黑色','0')
txt_content = txt_content.replace('红色','1')
txt_content = txt_content.replace('绿色','2')
txt_content = txt_content.replace('蓝色','3')
txt_content = txt_content.replace('黄色','4')
txt_content = txt_content.replace('橙色','5')

txt_content3 = remove_between_hashes_and_newlines(txt_content)
txt_content2 = expand_repeat_structure(txt_content3)

print(txt_content3)
python_code_result = parse_and_translate(txt_content2)

# 打印生成的Python代码
print(python_code_result)
# a=input('按回车开始:')
try:
    exec(python_code_result)
except Exception as e:
    print(f'{e}')
# a=input('输入任意退出')

案例

python 复制代码
开始;

笔色红色;
笔粗细5;

重复{前进100米;右转72度;}5次;

提笔;

结束;
相关推荐
阿图灵19 天前
OpenCV 绘图五件套:画圆、文本、线段、矩形与椭圆
图像处理·人工智能·python·opencv·计算机视觉·绘图
尘中远2 个月前
【Qwt 7.0 系列】快速入门与核心新特性概览 —— 更现代的 Qt 数据可视化库
qt·数据可视化·绘图·qwt·科学绘图
尘中远2 个月前
【Qwt 7.0 系列】交互功能详解 —— 平移、缩放、坐标轴交互与数据拾取
qt·数据可视化·绘图·qcustomplot·qwt·科学绘图
尘中远2 个月前
【Qwt 7.0 系列】总体架构解析 —— 从单体到三库模块化的演进
qt·matplotlib·绘图·qwt·科学绘图
工程师小星星3 个月前
Mermaid-AI时代的绘图语言
绘图·图表·meimaid
Evand J4 个月前
【MATLAB绘图】三维曲面与二维映射组合图绘制,进阶教程与代码示例
开发语言·matlab·绘图
Evand J4 个月前
MATLAB绘图函数介绍:plotmatrix绘图,附MATLAB例子
开发语言·matlab·绘图
Evand J4 个月前
【MATLAB绘图教程】空间误差场图的概念及其应用,附代码
matlab·平面·绘图·定位
Evand J5 个月前
MATLAB绘图教程:figure命令的高级用法,标题、名称、窗口位置和大小等
matlab·编程·教程·绘图·窗口·figure·matlab绘图
johnny2336 个月前
绘图工具汇总:FossFLOW、LogicFlow、oxdraw、LucidChart、iCraft Editor
绘图