Ruby绿鞋计时器

Ruby green shoes timer

我正在为游戏构建这个弹出式倒计时计时器。问题是我不知道如何更新动画以便显示也发生变化。它到目前为止有效,但您看不到数字发生变化。它融入了 00:00。我很肯定它到目前为止能正常工作,只是在这个变化上遇到了麻烦。这是用绿鞋完成的。知道我在这里做错了什么吗?

#Timer button used for creating a countdown timer for the game.
    #Game ends when timer is zero.
    flow width: 80, height: 0.2 do
    button 'Timer', width: 1.0, height: 1.0 do
        window do
        def time_change!
           @clock = '%02d:%02d' % [@second / 60, @second % 60]
           if(@second == 0)
            alert "game is over"
            @clock = '00:00'
                close()
            para @clock, size: 50, stroke: black
           end
         end

         background whitesmoke
         @clock = '00:00'
         para @clock, size: 50, stroke: black


         @second = 10

        animate(1) do
            @second -= 1
            time_change!
            para @clock, size: 50, stroke: black

        end
    end
end

您可以替换显示时钟的当前段落的文本:

Shoes.app do
  flow do 
    button 'Timer', width: 100, height: 50 do
      window width: 200, height: 100 do  #Open a child window when the button is clicked
        seconds = 3
        tenths = 0
        clock =  '%02d:%02d'

        flow do
          @p = para clock % [seconds, tenths], #Assign the clock para to an instance variable, 
            size: 50,                          #which will make the variable visible outside this block.
            stroke: black
        end

        a = animate(10) do  #Upate the clock every 1/10 of a second.
          tenths -= 1

          if tenths < 0
            tenths  = 9
            seconds -= 1
          end

          @p.text = clock % [seconds, tenths]  #Replace the clock text.

          if seconds == 0 and tenths == 0
            a.stop
            alert("game over")
            close   #=>self.close where self is the window whose block contains this code
          end
        end #animate

      end #window
    end #button
  end #flow
end #app

要验证时钟是否实际显示每个时间增量,您可以通过将 animate(10) 更改为 animate(1) 来减慢速度。