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('输入任意退出')
案例
开始;
笔色红色;
笔粗细5;
重复{前进100米;右转72度;}5次;
提笔;
结束;