请写一段代买在python turtle中实现:round dance, clock, minimal_hanoi
时间: 2024-02-24 19:59:43 浏览: 131
以下是在Python Turtle中实现round dance、clock和minimal_hanoi的代码:
Round Dance:
```python
import turtle
import random
turtle.speed(0)
while True:
turtle.right(random.randint(0, 360))
turtle.forward(random.randint(50, 150))
turtle.stamp()
turtle.goto(0,0)
```
Clock:
```python
import turtle
import time
turtle.speed(0)
def draw_clock():
turtle.clear()
turtle.pu()
turtle.goto(0, -200)
turtle.pd()
turtle.circle(200)
turtle.pu()
turtle.goto(0, 0)
for i in range(12):
turtle.pu()
turtle.fd(170)
turtle.pd()
turtle.fd(30)
turtle.pu()
turtle.fd(20)
turtle.write(i+1, align="center", font=("Arial", 12, "normal"))
turtle.bk(220)
turtle.right(30)
turtle.pu()
turtle.goto(0, 0)
turtle.pd()
while True:
draw_clock()
now = time.localtime()
hour = now.tm_hour % 12
minute = now.tm_min
second = now.tm_sec
turtle.pensize(10)
# 绘制时针
turtle.color("red")
turtle.goto(0, 0)
turtle.seth(90)
turtle.rt(hour * 30 + minute / 2)
turtle.pd()
turtle.fd(80)
turtle.pu()
# 绘制分针
turtle.color("green")
turtle.goto(0, 0)
turtle.seth(90)
turtle.rt(minute * 6)
turtle.pd()
turtle.fd(120)
turtle.pu()
# 绘制秒针
turtle.color("blue")
turtle.goto(0, 0)
turtle.seth(90)
turtle.rt(second * 6)
turtle.pd()
turtle.fd(150)
turtle.pu()
time.sleep(1)
```
Minimal Hanoi:
```python
import turtle
turtle.speed(0)
def hanoi(n, a, b, c):
if n == 1:
turtle.pu()
turtle.goto(a, 0)
turtle.pd()
turtle.goto(b, 0)
turtle.pu()
else:
hanoi(n-1, a, c, b)
hanoi(1, a, b, c)
hanoi(n-1, c, b, a)
n = 5
a = -150
b = 150
c = 0
for i in range(n):
turtle.pu()
turtle.goto(a, i*15)
turtle.pd()
turtle.goto(b, i*15)
turtle.pu()
hanoi(n, a, b, c)
turtle.done()
```
阅读全文