我目前正在尝试在我正在撰写的基于文本的游戏中实施战斗系统。玩家将从一个房间走到另一个房间,有时面对多个对手。
我想:
让玩家以最大生命值开始,并随着游戏的进行而下降
预先确定每个对手的力量(最大生命值)
让玩家一次面对很多对手
这就是我到目前为止所做的,但是我在构思玩家和对手之间的互动时遇到了很多困难。另外,我怎么让玩家连续面对多个对手?
任何提示都会有很多帮助!
谢谢!
GF
代码:
class Player
attr_accessor :hit_points, :attack_power
def initialize
@hit_points = MAX_HIT_POINTS
@attack_power = rand(2 .. 15)
end
def alive?
@hit_points > 0
end
def hurt
@hit_points -= amount
end
def print_status
puts "*" * 80
puts "HP: #{@hit_points}/#{MAX_HIT_POINTS}"
puts "*" * 80
end
end
class Opponent
attr_accessor :hit_points, :attack_power
def initialize
@hit_points = MAX_HIT_POINTS
@attack_power = rand(1 .. 10)
end
def alive?
@hit_points > 0
end
def hurt
@hit_points -= amount
end
def interact(player)
player_damage_done = 0
player_damage_taken = 0
while player.alive?
hurt(player.attack_power)
player_damage_done += player.attack_power
break unless alive?
player.hurt(@attack_power)
player_damage_taken += @attack_power
end
if player.alive?
print "You took #{player_damage_taken} damage and dealt # {player_damage_done} damage, killing your opponent."
print "\n"
player.addPoints(player_damage_taken + player_damage_done)
else
print "Your opponent was too powerful and you died."
death
end
end
end
答案 0 :(得分:0)
您应该拥有某种环境类来跟踪所有角色。这可以扩展到允许移动等。超级简单的东西可能看起来像这样:
class Environment
def initialize(player, baddies)
@player = player
@baddies = baddies
end
def play_game
@baddies.each do |baddie|
baddie.interact(@player)
end
end
end
baddies = 3.times.map do
Opponent.new
end
Environment.new(Player.new, baddies).play_game
此外,您提供的代码不起作用。您的hurt
方法表现得像接受amount
参数,但您从未声明过;你调用了player.add_points,但没有定义那个方法..如果你有任何其他具体问题,请告诉我