所以我正在尝试使用类来进行基于文本的游戏。我已经工作了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 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
如果这个问题得到解决将会很棒。感谢您的回复和帮助我。
答案 0 :(得分:1)
欢迎来到令人兴奋的面向对象设计世界。许多冒险者死在里面。
我认为你可能对类和实例之间的区别有一点误解。如果是这样,我强烈建议您在继续之前阅读相关内容。
您在调用Player
时创建了Player.new
的新实例。你的第一个错误就是不把它放在变量中。
尝试这样的事情:
my_player = Player.new
其次,您试图在encounter
类上调用Player
,而您应该在新实例上调用它。
my_player.encounter
您使用Monster
在Player.attack
课程中执行相同的操作。
我可以告诉你如何单独解决这些问题,但我认为你将从重新设计项目的某些部分中获益更多,以便将来更容易改变。希望大多数问题能够在此过程中解决。
一般来说,方法越短越好。当您告诉Player
attack
时,就应该这样做。相反,它会做各种各样的事情,包括让怪物受到攻击!
突然间,这两个阶级有很多共同点:它们都是攻击;他们都受到伤害,他们都死了。是时候建立一个超类。 (如果你不熟悉经典继承如何工作,你应该学习 - 这确实是它的完美用例。)
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
(这些只是示例;它们不像您已有的代码那样完整。)
你看到每种方法只做一件事吗?如果将该原则应用于您编写的所有内容,则重用零碎将变得更加容易。
我希望这很有用。如果您决定坚持使用您所拥有的并且只是修复错误,请在评论中这样说,我会帮助您。
祝你好运!