Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何让我的游戏正常运行(战斗系统)?_Ruby - Fatal编程技术网

Ruby 如何让我的游戏正常运行(战斗系统)?

Ruby 如何让我的游戏正常运行(战斗系统)?,ruby,Ruby,作为一个学习编程的初学者,拥有这样一个支持社区是非常有帮助的 我很难让这个“示例”游戏正常运行。我正在尝试开发一个战斗系统,玩家在穿越多个房间时遇到对手。出于某种原因,当我在命令提示符下运行它时,它只显示“youdead”,然后退出。我不知道从这里到哪里去 任何帮助都将不胜感激 class Player attr_accessor :hit_points, :attack_power def initialize(hit_points, attack_power) @hit_

作为一个学习编程的初学者,拥有这样一个支持社区是非常有帮助的

我很难让这个“示例”游戏正常运行。我正在尝试开发一个战斗系统,玩家在穿越多个房间时遇到对手。出于某种原因,当我在命令提示符下运行它时,它只显示“youdead”,然后退出。我不知道从这里到哪里去

任何帮助都将不胜感激

class Player 
  attr_accessor :hit_points, :attack_power

  def initialize(hit_points, attack_power)
    @hit_points = hit_points
    @attack_power = attack_power
  end 

  def alive?
    @hit_points < 1
    death
  end 

  def hurt 
    @hit_points -= Opponent.attack_power
  end 

  def print_status 
    puts "*" * 80 
    puts "HP: #{hit_points}/#{MAX_HIT_POINTS}"
    puts "*" * 80 
  end
end

class Death 
  puts "You died"
  exit(1)
end 

class Opponent 
  def initialize (hit_points, attack_power)
    @hit_points = hit_points 
    @attack_power = attack_power
    puts "you come across this awful opponent"
  end 

  def alive?
    @hit_points < 1
    death 
  end 

  def hurt 
    @hit_points -= player.attack_power
  end 

  def interact(player)
    while player.alive?
      hurt
      break if @hit_points < 1
      alive?
    end

    if player.alive?
      print "You took #{player_damage_taken} damage and dealt #{player_damage_done} damage, killing your opponent."
      room
    else 
      death
    end 
  end
end

class Room
  puts "you are now in the scary room, and you see an opponent!"

  puts "You come accross a weaker opponent. It is a fish."
  puts "Do you want to (F)ight or (L)eave?"

  action = $stdin.gets.chomp

  if action.downcase == "f"
    fish = Opponent.new(2, 1)
    fish.interact 
  else 
    death
  end 
end 

Player.new(200, 1)
Room.new

class Engine
end
职业玩家
属性访问器:生命值,攻击力
def初始化(生命值、攻击力)
@命中率=命中率
@攻击力=攻击力
结束
你还活着吗?
@命中率<1
死亡
结束
def伤害
@命中率-=对手。攻击力
结束
def打印状态
放置“*”*80
放置“HP:#{hit_points}/#{MAX_hit_points}”
放置“*”*80
结束
结束
阶级死亡
写着“你死了”
出口(1)
结束
阶级对手
def初始化(生命值、攻击力)
@命中率=命中率
@攻击力=攻击力
写着“你遇到了这个可怕的对手”
结束
你还活着吗?
@命中率<1
死亡
结束
def伤害
@命中率-=玩家。攻击力
结束
def交互(播放器)
你还活着吗?
伤害
如果@hit_points<1,则中断
活着的
结束
如果你还活着?
打印“你受到#{player_damage_take}伤害并造成#{player_damage_done}伤害,杀死你的对手。”
房间
其他的
死亡
结束
结束
结束
教室
“你现在在恐怖的房间里,你看到了一个对手!”
“你遇到了一个较弱的对手,那是一条鱼。”
放置“你想(F)光还是(L)屋檐?”
action=$stdin.gets.chomp
如果action.downcase==“f”
鱼=对手。新(2,1)
鱼肉
其他的
死亡
结束
结束
新玩家(200,1)
新房间
类引擎
结束

这是一个突破,因为死亡是一个类,其中的所有代码都在类的主体中。这意味着该代码将在定义类时执行,而不是在调用
death
时执行

您尚未定义名为
death
的方法

因为
Death
类很小,在其中命名一个停止游戏的方法(Death.Death、Death.die、Death.run、Death.execute…不是很好),并且您不需要类的任何优点(例如实例变量中存储的多个实例或属性),我建议您将
死亡
动作作为
玩家
职业的一部分

class Player

  # ...

  def die
    puts "You died"
    exit(1)
  end

end 
然后,当您调用
death
(当前未定义的方法)时,将其替换为
player.die


正如@Kennycoc所指出的,您还需要定义一个杀死敌人的方法。

因此,您似乎有这样一种印象,即类的顶层代码是在类实例化时运行的(
class.new
)。事实并非如此!类的顶级中的所有内容都按照定义运行

最简单的修复方法是在名为
initialize
的方法下添加每个类的顶层中的所有代码,这是实例化类时运行的方法


此外,您正在使用
Death
类,就好像它是一种方法一样。您可以将其从
class Death
更改为
def Death
,或者将调用更改为
Death.new
,在将代码移动到
initialize
方法后(这不是一种正常模式,但会起作用)。

我对Ruby不是特别熟悉,但是Death类在print+exit语句周围没有内部包装,因此在加载Death类时可以自动调用这些语句。我会将这两条语句封装在Death类中的一个方法中。i、 e.
def death*在此处插入put并退出*
如果没有缩进,很难理解这是在做什么。请你修改一下格式好吗?注意,你也必须对对手类做同样的修改