免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 8505 | 回复: 7
打印 上一主题 下一主题

看个好玩的代码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-10-03 16:31 |只看该作者 |倒序浏览
我在aspn上看到的,有意思啊

#! /usr/local/bin/python
import time, sys

rotor  = "|/-\\|/-\\"
baloon = " .oO@@Oo. "
square = " _uUOOUu_ "
dash   = "_-=##=-_ "
rotor2 = "3WEM"

w = sys.stdout.write; f = sys.stdout.flush

def main(them):
    status = 0; n = len(them); n2 = 3*n
    while True:
        w(them[status % n]); f()
        time.sleep(0.1)
        status += 1
        if not (status%n2): w('\b.')
        else:               w('\b')

if __name__ == '__main__':
##    main(rotor)
##    main(baloon)
    main(rotor)

论坛徽章:
0
2 [报告]
发表于 2006-10-04 01:09 |只看该作者
PYTHON混乱码?

论坛徽章:
0
3 [报告]
发表于 2006-10-04 06:51 |只看该作者
此rotor非彼rotor

论坛徽章:
0
4 [报告]
发表于 2006-10-05 19:30 |只看该作者
哈哈

好玩

论坛徽章:
0
5 [报告]
发表于 2006-10-05 19:42 |只看该作者
'\b'  是不是相当于键盘上的后退键啊

论坛徽章:
0
6 [报告]
发表于 2006-10-16 10:51 |只看该作者
小弟不才改进了一下.

  1. #! /usr/bin/python
  2. import time, sys
  3. def usage():
  4.         print "Usage: %s [ rotor | baloon | square | dash | rotor2 ]" % (sys.argv[0],)
  5. if len(sys.argv) == 1:
  6.         usage()
  7.         sys.exit (1)
  8. rotor  = "|/-\\|/-\\"
  9. baloon = " .oO@@Oo. "
  10. square = " _uUOOUu_ "
  11. dash   = "_-=##=-_ "
  12. rotor2 = "3WEM"

  13. w = sys.stdout.write; f = sys.stdout.flush

  14. def main(them):
  15.     status = 0; n = len(them); n2 = 3*n
  16.     while True:
  17.         w(them[status % n]); f()
  18.         time.sleep(0.1)
  19.         status += 1
  20.         if not (status%n2): w('\b.')
  21.         else:               w('\b')

  22. if __name__ == '__main__':
  23.         main(sys.argv[1])
复制代码

刚开始学这个.以后各位请各位大哥不吝赐教!

论坛徽章:
0
7 [报告]
发表于 2006-10-16 13:07 |只看该作者
这段代码一般不是拿来单独使用的。当你在程序中将要运行某个很耗时的操作(比如大文件的拷贝),你又要告诉程序的使用者说,程序虽然暂时没有响应,但它还是在正确的运行,那么你就开个线程运行上面的代码,把
while True换成while flag,当耗时的操作完成后将flag设为false。用户看到转子在转,就知道程序没有挂掉。
有游戏经验的朋友应该记得有些游戏在即将安装完时会出现命令行窗口解压某些文件,解压时就会出现转子的转动。

论坛徽章:
0
8 [报告]
发表于 2006-10-17 18:21 |只看该作者
对"改进版本"的改进:
  由于参数是当作字符串传入,而非变量传入, 故main函数应该增加如下语句:

def main(them):
    if them == 'rotor':
        them = rotor
    elif them == 'baloon':
        them = baloon
    elif them == 'square':
        them = square
    elif them == 'dash':
        them = dash
    elif them == 'rotor2':
        them = rotor2
    status = 0; n = len(them); n2 = 3*n
    while True:
< 以下相同 >
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP