初学者问题 - Gets.chomp,停止我的计时器运行,导致崩溃
你好,我一直在学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有说明您正在运行什么版本的 Ruby。此外,您的示例代码没有演示您对
gets
的使用,如果您遇到问题,这一点很重要。它也没有显示您在等待用户时如何在后台循环的任何示例。根据您的标题和时间的使用,这是我能想到的开始测试问题的最小代码:
In Ruby 1.8.7:
In Ruby 1.9.2:
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:
In Ruby 1.8.7:
In Ruby 1.9.2:
In each test I paused a couple seconds which is reflected in the Time calculations.
Besides
p
returning a value in 1.9.2 andnil
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.