我想制作一个计数器,如果发生某种情况,将重置为0。看起来模糊不清:
counter = 0
如果发生了某些事情
counter += 1
elsif 其他事情发生
*counter is set back to 0*
端
非常感谢!
修改
def attack(cookie)
cookie.changeDefense()
@offense = Random.new.rand(0..100)
win_count = 0
puts "#{cookie.name}'s health is #{cookie.health}"
puts "The foe's health is #{@health}"
puts win_count
puts "Do you want to fight?"
response = gets.chomp
if response == "yes"
if @offense > cookie.defense
cookie.health -= 10
puts "The foe has defeated you!"
win_count += 1
else
puts "You win! The foe has lost health!"
@health -= 10
win_count = 0
number = Random.new.rand(0..100)
if number.between?(15, 50)
puts "The enemy has dropped a health item!"
cookie.health += 5
elsif number.between?(60, 95)
puts "The enemy has dropped a fighting upgrade! You
have a hightened chance of winning!"
cookie.lowDefense += 10
else
end
end
这是代码,它来自基于文本的游戏,所以不仅仅是那里的计数器。
另一个编辑
我已经通过在此处未显示的部分代码中使用全局变量来解决它,问题无法删除,但不需要更多帮助。谢谢!
答案 0 :(得分:1)
只要把'elseif("其他事情发生"){ counter = 0 }
这应该使计数器再次等于0
答案 1 :(得分:0)
在else
puts "You win! The foe has lost health!"
@health -= 10
win_count = 0
中设置win_count = 0
部分为win_count - = 1