初学者问题 - Gets.chomp,停止我的计时器运行,导致崩溃

发布于 2024-10-03 18:51:16 字数 800 浏览 6 评论 0原文

你好,我一直在学习 Chris Pine 的《红宝石编程》一书,自从完成后,我一直在尝试编写一个文本冒险游戏,从按位文章“红宝石冒险”中汲取一些想法。

对于战斗,我放入了一个 while 语句,当玩家的生命值或敌人的生命值达到 0 时,该语句就会中断。有一个计时器每 2 秒运行一次伤害数学运算。我想要某种方式允许玩家在战斗期间以不中断计时器的方式输入命令,但是当要求代码调用 gets.chomp 时,如果玩家花费超过 2 秒的时间,计时器崩溃。这是删除了大部分无用位的代码:-

while true
    start_time = Time.now
    hp[1] = hp[1] - (enemy.stats[0] - rand(enemy.stats[0]))
    enemy.hp = enemy.hp.to_i - (mystats[0] - rand(mystats[0]))
    if hp[1] <= 0 or enemy.hp <= 0
        break
    end
    puts " "
    puts "(hp: #{hp[1]} mp: #{mp[1]} st: #{st[1]})"
    #enemy conditions go here
    puts "Enemy condition: " + condition
    puts " "
    total_time = Time.now - start_time 
    sleep(2.0 - total_time)
end

我可以做到的方法是让另一个计时器每 2 秒刷新一次 gets.chomp,但我不认为这样做。

任何帮助我将不胜感激。

Hello I've been learning from the Chris Pine's, learn to program book on ruby and since finishing I have been trying to write a text adventure game, taking some ideas from the bitwise article 'adventures in ruby.'

For combat, I have put in a while statement which breaks when either the player's hit points or the enemy's hit points reach 0. There is a timer which runs the math for damage every 2 seconds. I'd like some way of allowing the player to type in commands during combat in a way that doesn't interrupt the timer, but when asking the code to call a gets.chomp, if the player takes more than 2 seconds, the timer crashes. Heres the code with most of the useless bits taken out:-

while true
    start_time = Time.now
    hp[1] = hp[1] - (enemy.stats[0] - rand(enemy.stats[0]))
    enemy.hp = enemy.hp.to_i - (mystats[0] - rand(mystats[0]))
    if hp[1] <= 0 or enemy.hp <= 0
        break
    end
    puts " "
    puts "(hp: #{hp[1]} mp: #{mp[1]} st: #{st[1]})"
    #enemy conditions go here
    puts "Enemy condition: " + condition
    puts " "
    total_time = Time.now - start_time 
    sleep(2.0 - total_time)
end

The way I can thing it might be done is to have another timer refresh gets.chomp every 2 seconds but I can't think to do that.

Any help would me much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如歌彻婉言 2024-10-10 18:51:16

您没有说明您正在运行什么版本的 Ruby。此外,您的示例代码没有演示您对 gets 的使用,如果您遇到问题,这一点很重要。它也没有显示您在等待用户时如何在后台循环的任何示例。

根据您的标题和时间的使用,这是我能想到的开始测试问题的最小代码:

time = Time.now
asdf = STDIN.gets
p Time.now - time

In Ruby 1.8.7:

test.rb(main):004:0> greg-mbp-wireless:Desktop greg$ rvm 1.8.7; rvm list

rvm rubies

=> ruby-1.8.7-p302 [ x86_64 ]
   ruby-1.9.2-head [ x86_64 ]
   ruby-1.9.2-p0 [ x86_64 ]

greg-mbp-wireless:Desktop greg$ irb -f test.rb
test.rb(main):001:0> time = Time.now
=> Sun Nov 21 13:24:58 -0700 2010
test.rb(main):002:0> asdf = STDIN.gets

=> "\n"
test.rb(main):003:0> p Time.now - time
3.802123
=> nil

In Ruby 1.9.2:

test.rb(main):004:0> greg-mbp-wireless:Desktop greg$ rvm 1.9.2; rvm list

rvm rubies

   ruby-1.8.7-p302 [ x86_64 ]
   ruby-1.9.2-head [ x86_64 ]
=> ruby-1.9.2-p0 [ x86_64 ]

greg-mbp-wireless:Desktop greg$ irb -f test.rb
test.rb(main):001:0> time = Time.now
=> 2010-11-21 13:25:37 -0700
test.rb(main):002:0> asdf = STDIN.gets

=> "\n"
test.rb(main):003:0> p Time.now - time
3.578869
=> 3.578869

In every test I PATCH 几秒钟,这反映了在时间计算中。

除了 1.9.2 中的 p 返回值和 1.8.7 中的 nil 之外,我没有看到任何真正的区别。显示一些如何在后台循环进行计算的示例,我可以扩展测试代码。

You don't say what version of Ruby you're running. Also, your sample code doesn't demonstrate your use of gets, which is important if you're having a problem with it. Nor does it show any sample of how you're trying to loop in the background as it waits for the user.

Based on your title and the use of Time, this is the minimum code I could think of that would start to test the problem:

time = Time.now
asdf = STDIN.gets
p Time.now - time

In Ruby 1.8.7:

test.rb(main):004:0> greg-mbp-wireless:Desktop greg$ rvm 1.8.7; rvm list

rvm rubies

=> ruby-1.8.7-p302 [ x86_64 ]
   ruby-1.9.2-head [ x86_64 ]
   ruby-1.9.2-p0 [ x86_64 ]

greg-mbp-wireless:Desktop greg$ irb -f test.rb
test.rb(main):001:0> time = Time.now
=> Sun Nov 21 13:24:58 -0700 2010
test.rb(main):002:0> asdf = STDIN.gets

=> "\n"
test.rb(main):003:0> p Time.now - time
3.802123
=> nil

In Ruby 1.9.2:

test.rb(main):004:0> greg-mbp-wireless:Desktop greg$ rvm 1.9.2; rvm list

rvm rubies

   ruby-1.8.7-p302 [ x86_64 ]
   ruby-1.9.2-head [ x86_64 ]
=> ruby-1.9.2-p0 [ x86_64 ]

greg-mbp-wireless:Desktop greg$ irb -f test.rb
test.rb(main):001:0> time = Time.now
=> 2010-11-21 13:25:37 -0700
test.rb(main):002:0> asdf = STDIN.gets

=> "\n"
test.rb(main):003:0> p Time.now - time
3.578869
=> 3.578869

In each test I paused a couple seconds which is reflected in the Time calculations.

Besides p returning a value in 1.9.2 and nil in 1.8.7, I don't see any real difference. Show some sample of how you're looping in the background to do your calculations and I can expand the test code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文