# -*- coding: utf-8 -*-
"""
This is a visual solution to the hanoi tower problom
"""
#引入所需要的库
import turtle as t #引入turtle,起简名为t
import random #引入random库
#更新屏幕
def clear_screen():
t.pensize(1) #设置画笔大小
t.pencolor('black') #设置画笔颜色
t.clear() #清屏
#屏幕划分,隐藏画笔,快速绘制
t.setup(screenwidth,screenheight) #移动画笔到指定位置
t.tracer(False) #取消绘画延迟
t.hideturtle() #隐藏画笔
t.penup() #抬起画笔
t.setpos(-screenwidth/2,0) #移动画笔到指定位置
t.pendown() #放下画笔
t.fd(screenwidth) #向前移动指定距离
t.tracer(True) #打开绘画延迟
#设置画笔初始位置
def pen_seat(area):
global screenwidth,screenheight #全局变量
#判断绘制区域
if area == 0:
t.penup() #抬起画笔
t.setpos(0,screenheight/4) #移动画笔到指定位置
t.seth(0) #奖画笔指向右(即正东,上北下南左西右东)
t.pendown() #放下画笔
elif area == 1:
t.penup() #抬起画笔
t.setpos(0,-screenheight/4) #移动画笔到指定位置
t.seth(0) #奖画笔指向右(即正东,上北下南左西右东)
t.pendown() #放下画笔
#绘制底物
def draw_substrate1(n,src,dst,area):
global count,screenwidth,screenheight #全局变量
pen_seat(area) #设置画笔初始位置
t.pensize(5) #设置画笔大小
t.pencolor('black') #设置画笔颜色
t.hideturtle() #隐藏画笔
t.tracer(False) #取消绘画延迟
#绘制底座
t.penup() #抬起画笔
t.setpos(-screenwidth/2,screenheight/4-screenheight/6) #移动画笔到指定位置
t.pendown() #放下画笔
t.fd(screenwidth) #向前移动画笔指定距离
#绘制A柱
t.penup() #抬起画笔
t.setpos(-(screenwidth/2-screenwidth/4),screenheight/4-screenheight/6) #移动画笔到指定位置
t.left(90) #奖画笔指向左转90度
t.pendown() #放下画笔
t.fd(screenheight/2-(screenheight/4-screenheight/6)) #向前移动画笔指定距离
t.penup() #抬起画笔
t.setpos(-(screenwidth/2-screenwidth/4),(screenheight/4-screenheight/6)-20) #移动画笔到指定位置
t.write("A",False) #书写”A"
#绘制B柱
t.setpos(0,screenheight/4-scre
可视化“汉诺塔问题”
最新推荐文章于 2024-05-15 06:04:15 发布