首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未定义Player::Class的方法"attack“

未定义Player::Class的方法"attack“
EN

Stack Overflow用户
提问于 2017-02-22 00:35:25
回答 1查看 52关注 0票数 1

因此,我正在尝试制作一个基于文本的游戏,其中有一个任务类。我已经工作了2个小时,找不到问题所在。

代码语言:javascript
复制
    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

如果这个问题得到解决,那就太好了。感谢您的回复,并在我的作业中给予帮助。

EN

回答 1

Stack Overflow用户

发布于 2017-02-22 02:03:31

欢迎来到令人兴奋的面向对象设计世界。许多冒险家死在里面。

我想您可能对类和实例之间的区别有一点误解。如果是这样,我强烈建议您在继续之前先阅读相关内容。

在调用Player.new时,您创建了一个新的Player实例。您的第一个错误是没有将其放入变量中。

尝试如下所示:

代码语言:javascript
复制
my_player = Player.new

其次,您正尝试在Player类上调用encounter,而您应该在新实例上调用它。

代码语言:javascript
复制
my_player.encounter

您可以使用Player.attackMonster类中执行相同的操作。

我可以告诉你如何单独解决这些问题,但我认为重新设计项目的某些部分,以便将来更容易更改,会让你受益更多。希望大多数问题都能在此过程中自行解决。

一般来说,方法越短越好。当您告诉attackPlayer时,它应该做的就是这些。相反,它会做各种各样的事情,包括让怪物攻击!

突然间很明显,这两个职业有相当多的共同点:他们都攻击;他们都受到伤害,他们都死了。是时候创建一个超类了。(如果您不熟悉经典继承是如何工作的,那么您应该学习一下--这确实是它的完美用例。)

代码语言:javascript
复制
 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

这样做的最大好处是你只需要在一个地方编写代码,它就可以工作在所有地方。如果你改变了对设计决定的想法,你可以在一个地方改变它,并且知道一切都会被调整。

您可以创建一个类似以下内容的子类:

代码语言:javascript
复制
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

(这些只是示例;它们并不像您已有的代码那样完整。)

你看到每个方法只做一件事了吗?如果您将该原则应用于您编写的所有内容,那么重用零碎代码就会变得容易得多。

我希望这是有用的。如果你决定坚持你已有的,只是修复错误,只要在评论中说出来,我会帮助你的。

祝好运!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42372972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档