0

这是一个程序,它显示一个简单的动画来向用户显示程序正在等待输入,或者正在做某事,并且它没有崩溃:

    require "curses"
        include Curses

        chars = ["   ","*  ","** ","***","***","** ","*  ","   "]

          four = Window.new(3,20,10,30)
          four.box('|', '-')
          four.setpos 1, 1
          four.addstr "Hello"
          while ( true )
            four.setpos 1, 6
            four.addstr chars[0]
            four.refresh
            sleep 0.1
            chars.push chars.shift
          end

在 while 循环中,每次循环时,光标都会重新定位到第 1 行第 6 列。这样星星就会被空格覆盖,并且一切正常。

但是,尝试将“Hello”字符串更改为“Hello Everyone”

如您所见,星形动画现在出现在该字符串的中间。动画并没有被“分流”。有没有办法自动将动画附加到字符串的末尾?

还是我需要以编程方式定位它?求hello字符串的长度,加1,用它来定位col坐标?

4

1 回答 1

1

Ruby Curses 模块不提供getyx. 所以你应该自己计算位置。

另一种选择是编写"Hello""Hello Everyone"后跟chars[0]内部循环。

require "curses"
include Curses

chars = ["   ","*  ","** ","***","***","** ","*  ","   "]
four = Window.new(3,20,10,30)
four.box('|', '-')

loop do
  four.setpos 1, 1
  four.addstr 'Hello'
  four.addstr chars[0]
  four.refresh
  sleep 0.1
  chars.push chars.shift
end

参考:以下是使用getyx.

import curses
import itertools
import time

chars = itertools.cycle(["   ", "*  ", "** ", "***", "***", "** ", "*  ", "   "])
curses.initscr()
four = curses.newwin(3, 20, 10, 30)
four.box()
four.addstr(1, 1, 'Hello Everyone')
y, x = four.getyx()
while True:
    four.addstr(1, x, next(chars))
    four.refresh()
    time.sleep(0.1)
于 2013-10-15T03:48:56.963 回答