How do I get my game to run properly (battle system)?

时间:2021-05-24 09:16:23

As a beginner learning to program, it is extremely helpful to have such a supportive community out there!

作为一个学习编程的初学者,在那里建立这样一个支持性的社区是非常有帮助的!

I am having trouble getting this 'sample' game working. I am trying to develop a battle system where the player comes across opponents as they progress through a number of rooms. For some reason, when I run it on command prompt, it simply displays "you died" then exits. I am not sure where to go from here.

我无法让这个'示例'游戏正常运行。我正在努力开发一种战斗系统,当玩家进入许多房间时,玩家会碰到对手。出于某种原因,当我在命令提示符下运行它时,它只显示“你死了”然后退出。我不知道从哪里开始。

Any help would be greatly appreciated.

任何帮助将不胜感激。

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

2 个解决方案

#1


2  

This is breaking because Death is a class and all the code within it is in the body of the class. That means this code will be executed when the class is defined, not at the time that death is called.

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

You haven't defined a method named death.

您尚未定义名为death的方法。

Because the Death class is tiny, and it would be awkward to name a method within it that stops the game (Death.death, Death.die, Death.run, Death.execute... Not great), and you don't need any of the advantages of a class (such as multiple instances or attributes stored in instance variables), I suggest you make the death action a part of the Player class.

因为死亡级别很小,所以在其中命名一个停止游戏的方法会很尴尬(Death.death,Death.die,Death.run,Death.execute ......不是很好),而你却没有需要一个类的任何优点(例如存储在实例变量中的多个实例或属性),我建议你将死亡动作作为Player类的一部分。

class Player

  # ...

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

end 

Then when you've called death (the currently undefined method) Replace it with player.die.

然后,当你调用死亡(当前未定义的方法)时将其替换为player.die。

As noted by @Kennycoc, you'll need to define a method for the death of an enemy, too.

正如@Kennycoc所指出的那样,你也需要为敌人的死亡定义一种方法。

#2


0  

So, it seems like you're under the impression that the code in the top level of the class is run when the class is instantiated (Class.new). This is not the case! Everything in the top level of the class is run as it is defined.

所以,似乎你的印象是在实例化类(Class.new)时运行类*代码。不是这种情况!类的*中的所有内容都按照定义运行。

The simplest fix to get this running would be to add all the code in your top level of each class under a method named initialize this is what's run when the class is instantiated.

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

Also, you're using your Death class as if it were a method. You could either change it from class Death to def death or change your calls to Death.new after moving the code to the initialize method (this is not a normal pattern, but would work).

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

#1


2  

This is breaking because Death is a class and all the code within it is in the body of the class. That means this code will be executed when the class is defined, not at the time that death is called.

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

You haven't defined a method named death.

您尚未定义名为death的方法。

Because the Death class is tiny, and it would be awkward to name a method within it that stops the game (Death.death, Death.die, Death.run, Death.execute... Not great), and you don't need any of the advantages of a class (such as multiple instances or attributes stored in instance variables), I suggest you make the death action a part of the Player class.

因为死亡级别很小,所以在其中命名一个停止游戏的方法会很尴尬(Death.death,Death.die,Death.run,Death.execute ......不是很好),而你却没有需要一个类的任何优点(例如存储在实例变量中的多个实例或属性),我建议你将死亡动作作为Player类的一部分。

class Player

  # ...

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

end 

Then when you've called death (the currently undefined method) Replace it with player.die.

然后,当你调用死亡(当前未定义的方法)时将其替换为player.die。

As noted by @Kennycoc, you'll need to define a method for the death of an enemy, too.

正如@Kennycoc所指出的那样,你也需要为敌人的死亡定义一种方法。

#2


0  

So, it seems like you're under the impression that the code in the top level of the class is run when the class is instantiated (Class.new). This is not the case! Everything in the top level of the class is run as it is defined.

所以,似乎你的印象是在实例化类(Class.new)时运行类*代码。不是这种情况!类的*中的所有内容都按照定义运行。

The simplest fix to get this running would be to add all the code in your top level of each class under a method named initialize this is what's run when the class is instantiated.

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

Also, you're using your Death class as if it were a method. You could either change it from class Death to def death or change your calls to Death.new after moving the code to the initialize method (this is not a normal pattern, but would work).

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