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次;

提笔;

结束;
相关推荐
He BianGu2 个月前
演示:基于WPF的DrawingVisual开发的Chart图表和表格绘制
wpf·绘图·2d·图表·chart·
present12272 个月前
Matlab中BaseZoom()函数实现曲线和图片的局部放大
开发语言·matlab·信息可视化·数据分析·绘图
Trouvaille ~2 个月前
【Python篇】matplotlib超详细教程-由入门到精通(上篇)
开发语言·python·数据分析·matplotlib·数据可视化·绘图·python3.11
巽星石3 个月前
【Godot4.3】基于纯绘图函数自定义的线框图控件
godot·gdscript·绘图·自定义控件·线框图
大福是小强3 个月前
016_Save_the_picture_in_Matlab中保存图片
开发语言·matlab·导出·绘图·图片·出图
知识分享小能手3 个月前
R 语言学习教程,从入门到精通,R 绘图饼图(22)
大数据·开发语言·python·学习·r语言·绘图·统计学
Mac分享吧3 个月前
EazyDraw for Mac 矢量图绘制设计软件
经验分享·macos·mac·软件需求·绘图·矢量图·设计软件
Fuliy963 个月前
Matplotlib库
python·plotly·matplotlib·绘图·plot·mp导出为pdf和svg
夏日恋雨3 个月前
R语言贝叶斯方法在生态环境领域技术教程
数据分析·r语言·贝叶斯·生态学·绘图·结构方程·环境科学
不勤劳的码字员4 个月前
Matlab得到无背景透明图片
matlab·绘图·图片·透明背景