FreeG
FreeG

Reputation: 11

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

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

Upvotes: 1

Views: 68

Answers (2)

AJFaraday
AJFaraday

Reputation: 2450

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.

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.

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.

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

Upvotes: 2

iCodeSometime
iCodeSometime

Reputation: 1643

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.

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.

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).

Upvotes: 0

Related Questions