| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2015 人关注过本帖
标题:python制作汉诺塔动画
只看楼主 加入收藏
jingxianye
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-4-30
收藏
 问题点数:0 回复次数:2 
python制作汉诺塔动画
递归我是懂了 但是请问怎么制作动画来掩饰啊 求各位大虾了!
另外在外网看见了这一一个程序,自己下载运行了下是ok的,有人知道的话能详细给我解释下么,谢谢了
from turtle import *

class Disc(Turtle):
    def __init__(self, n):
        Turtle.__init__(self, shape="square", visible=False)
        self.pu()
        self.shapesize(1.5, n*1.5, 2) # square-->rectangle
        self.fillcolor(n/6., 0, 1-n/6.)
        self.st()

class Tower(list):
    "Hanoi tower, a subclass of built-in type list"
    def __init__(self, x):
        "create an empty tower. x is x-position of peg"
        self.x = x
    def push(self, d):
        d.setx(self.x)
        d.sety(-150+34*len(self))
        self.append(d)
    def pop(self):
        d = list.pop(self)
        d.sety(150)
        return d

def hanoi(n, from_, with_, to_):
    if n > 0:
        hanoi(n-1, from_, to_, with_)
        to_.push(from_.pop())
        hanoi(n-1, with_, from_, to_)

def play():
    onkey(None,"space")
    clear()
    hanoi(6, t1, t2, t3)
    write("press STOP button to exit",
          align="center", font=("Courier", 16, "bold"))

def main():
    global t1, t2, t3
    ht(); penup(); goto(0, -225)   # writer turtle
    t1 = Tower(-250)
    t2 = Tower(0)
    t3 = Tower(250)
    # make tower of 6 discs
    for i in range(6,0,-1):
        t1.push(Disc(i))
    # prepare spartanic user interface ;-)
    write("press spacebar to start game",
          align="center", font=("Courier", 16, "bold"))
    onkey(play, "space")
    listen()
    return "EVENTLOOP"

if __name__=="__main__":
    msg = main()
    print msg
    mainloop()
搜索更多相关主题的帖子: 动画 制作 visible python import 
2013-04-30 05:39
comslogic
Rank: 1
来 自:北方
等 级:新手上路
帖 子:4
专家分:0
注 册:2013-4-23
收藏
得分:0 
帮顶吧!

Old dog play new trick????
2013-05-20 16:55
headindotcn
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-8-8
收藏
得分:0 
帮顶
2013-09-10 11:19
快速回复:python制作汉诺塔动画
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018118 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved