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.