Ruby “未定义的方法”;攻击“;对于玩家::类
因此,我试图制作一个基于文本的游戏,其中包含作业类。我已经工作了2个小时,没有找到问题所在Ruby “未定义的方法”;攻击“;对于玩家::类,ruby,Ruby,因此,我试图制作一个基于文本的游戏,其中包含作业类。我已经工作了2个小时,没有找到问题所在 class Rankuun attr_accessor :rankuun_damage, :rankuun_health def initialize rankuun_health = 200 rankuun_damage = 100 end def monolouge puts 'Rankuun: "So, I see that you
class Rankuun
attr_accessor :rankuun_damage, :rankuun_health
def initialize
rankuun_health = 200
rankuun_damage = 100
end
def monolouge
puts 'Rankuun: "So, I see that you have lived this long. I am suprised.'
puts "Not a single libing creature has lived for this long inside my dungeon."
puts "But it's time that your endless slaughter of my brethren are halted."
puts "Now face what true fear really is!"
puts "Hoc vanitas est, et non est fere ut serves!"
puts "You see a mystical aura rise around Rankuun, and hear the shouts of agony"
puts "Rankuun has grown twice in size, and has taken the form of some kind of lich"
puts 'Rankuun: WELCOME TO DIE!"'
end
end
class Player
attr_accessor :health, :gold
def initialize
health = 100
money = 200
puts "Health: #{health}"
puts "Gold: #{money}"
end
def attack
puts "You attack the monster!"
hitmiss = 1
if hitmiss == 1
dmg = rand(5..10)
puts "You hit the monster, and do #{dmg} damage!"
monster_health = monster_health - dmg
elsif hitmiss == 2
puts "You missed!"
end
end
def guard
puts "You attempt to defend yourself"
guard = rand(1..2)
if guard == 1
counter = rand(5..10)
puts "You block the damage, and counterstrike for #{counter} damage"
monster_health = monster_health - counter
elsif guard == 2
monster_counter = rand(1..5)
puts "You try to guard, but the enemy hits harder than you expected, and you get dealt #{monster_counter}"
health = health = monster_counter
end
end
def loot
puts "You search the room and find:"
loot_item = rand (2..3)
if loot_item == 2
puts "You find some gold!"
money = money + 50
puts "Health: #{health}"
puts "Gold: #{money}"
elsif loot_item == 3
puts "You find a curious potion that seems to heal you"
health = health + 50
puts "Health: #{health}"
puts "Gold: #{money}"
end
end
def encounter
encounter = rand(1..2)
if encounter == 1
puts "A monster confronts you!"
monster = Monster.new
elsif encounter == 2
puts "There appears to be no monsters in this room"
end
end
end
class Monster
attr_accessor :monster_health, :monster_damage
def initialize
monster_health = 50
monster_damage = 10
end
def monster_attack
puts "The monster attacks you!"
end
end
puts "There has been a saying in your town for as long as you can remember:"
puts "Ne pas entrer dans le Donjon De Rankuun"
puts 'It means: "Do not enter The Dungeon of Rankuun"'
puts "Many adventurers died inside, and the only living creature in there is the man named Rankuun"
puts "He has great power over the Dungeon, reviving the dead and casting black magic"
puts "You have been selected by the village to go into the Dungeon and exterminate Rankuun"
puts "You have been given a sword, a shield, and some gold. Now you must enter:"
puts "T H E D U N G E O N O F R A N K U U N!"
puts ""
puts ""
player = Player.new
player.encounter
room1 = gets.chomp
if room1 == "attack"
player.attack
elsif room1 == "loot"
player.loot
end
如果这个问题解决了就太好了。感谢您在我的作业中给予我的回复和帮助。欢迎来到令人兴奋的面向对象设计世界。许多冒险家死在里面 我想你可能对类和实例之间的区别有点误解。如果是这样的话,我强烈建议您在继续之前阅读相关内容 调用
Player.new
时,您创建了Player
的新实例。您的第一个错误是没有将其放入变量中
试着这样做:
my_player = Player.new
其次,您正试图在Player
类上调用interface
,而您应该在新实例上调用它
my_player.encounter
你在怪物类中对玩家做同样的事情。攻击
我可以单独告诉您如何解决这些问题,但我认为您将从重新设计项目的某些部分中获益更多,以便将来更容易更改。希望大部分问题都能在这一过程中自行解决
一般来说,方法越短越好。当你让玩家
攻击时,这就是它应该做的。相反,它可以做各种事情,包括让怪物攻击
这两类人有很多共同点:他们都是攻击型的;他们都受到了伤害,都死了。是时候制作一个超类了。(如果您不熟悉经典继承的工作原理,您应该学习——这确实是一个完美的用例。)
类字符
属性访问器:运行状况
def攻击可造成伤害
可损坏的
结束
def接收损坏
健康-=伤害#等同于健康=健康-伤害
潜在死亡
结束
潜在死亡
如果死了?
死亡
结束
结束
戴夫死了?
健康在解决问题后,我得到了这个错误:未定义的方法为nil:NilClass(repl):34:in
攻击(repl):104:in`'我修复了最后一个代码(从玩家身上移除怪物攻击等)否,但是错误是关于怪物类的:类怪物属性访问器:怪物健康,:怪物伤害定义初始化怪物健康=50怪物伤害=10请您更新问题中的代码以使其更易于阅读?这是整个Monster
类吗?现在看来问题出在Monster\u health=Monster\u health-dmg
中,您没有任何方法访问Monster
实例。我可以使用遭遇类作为它们之间的桥梁。这样,您就可以使用遭遇.monster.health-=dmg
。但是,我建议您按照我的建议实施take_damage
方法,这样您就不会依赖于属性名称的知识。
class Character
attr_accessor :health
def attack damageable, damage
damageable.take_damage damage
end
def take_damage damage
health -= damage # Equivenent to health = health - damage
potential_death
end
def potential_death
if dead?
die
end
end
def dead?
health <= 0 # With random damage, it could be less than 0.
end
def die # overruled by subclass
end
end
class Monster < Character
def die
super # Call the copy of die in Character, in case it contains something important
reward killer
puts "You kill the monster..."
end
def reward rewardable
rewardable.gain_money 30
end
end
class Player < Character
def die
super # Call the copy of die in Character, in case it contains something important
puts "You died..."
game.over
end
end