Hi,

Zoe Phoenix wrote:
> Okay, I took someone's advice in an earlier post I made on how to poison
> a character in a test game I wanted to make by making a thread.  I'm not
> sure if this is correct or not, but this is what I have:
> 
> def poison
>   poisoned = thread.new
>     poisoned do
>           Character.hp - (rand(Character.hp * (10 - 5 + 1)/100) +
> Character.hp*5/100)
>           puts 'Your HP dropped!' + Character.hp + '/' + Character.maxhp
>           sleep 20  #thread sleeps for 20 seconds
>     end
> end
> 
> 
> Can I do it this way, so "Character.poison" would start the thread?  If
> this would work, how would I use an "antidote" item to stop the thread?

Here is a working code:

def poison
  $poisoned = Thread.new {
    hp = 100
    maxhp = 300
    loop {
      Thread.stop if Thread.current["state"]=="stop"
          hp -= (rand(hp * (10 - 5 + 1)/100) + hp*5/100)
          puts 'Your HP dropped! ' + hp.to_s + '/' + maxhp.to_s
          sleep 1
        }
  }
end
def antidote
  puts "call antidote"
  $poisoned["state"] = "stop"
end
def antiantidote
  puts "call antiantidote"
  $poisoned["state"] = "run"
  $poisoned.run
end

poison
sleep 10
antidote
sleep 10
antiantidote
sleep 10


Regards,
Park Heesob
-- 
Posted via http://www.ruby-forum.com/.