Argh ok having problems with this. My code has just become very messy
because I can't get my head clear on this one.
I'm trying to structure the enemy classes as per your advice and so have
Grunty laid out in a manner similar to this:
class Grunty < Enemy
def initialize(hp,attack, defense, weapon)
super(hp, attack, defense)
@weapon = "AK47"
@hp =100
if Grunto.hp == 0
puts "Grunty is dead!"
else
puts "Grunty is wounded but can still fight!"
end
randy = rand(100)
health = Grunto.hp - randy
puts "Grunty's hp is #{Health}"
end
This throws up no errors and although the hit assignment is poor, it
remains functional.
The check of Grunto's health shouldn't be at initialization (unless you'll be potentially initializing enemies that are already dead). And you certainly shouldn't be printing from the initialization method.
I rather envisioned Enemy having set methods for:
- affecting health
- returning a health string (e.g. your "Grunty is staggered but still ready for action." kind of statements)
Something like (and this is really rough)
class Enemy
def initialize(hp)
@hp = hp
end
def hit(damage)
@hp -= damage
end
def status
if @hp < 50
return "He looks really rough."
else
return "He is in a fighting mood."
end
end
end
grunt = Enemy.new(50)
puts grunt.status
# >> He is in a fighting mood.
grunt.hit(70)
puts grunt.status
# >> He looks really rough.
You'll probably want to track max hit points and current hit points, so you can set the health statements by percentage thresholds (e.g. current health is less than 25% of max) in addition or instead of explicit point limits.
Also you should probably just go ahead and create a player class and get out of the habit of hard coding things straight into your main program tests. Ideally you'll want to completely avoid any hardcoding (e.g. how you hardcode "AK47" in your test attack, that should be something like #{player.weapon}). You also have "AK47" hardcoded in your Grunt class instead of using the weapon argument.
Here's an idea of how Player and Enemy could interact.
class Player
def attack
# this should be random, and based off of the current weapon
return 75
end
end
grunt.hit(player.attack)
I then continue to test with an example scenario battle hard coded into
the script:
Grunty = Grunt.new("hp", 300, 300, "Ak47")
puts "Grunty is battling a kitten!"
puts "The kitten attacks Grunty with an AK47 and inflicts #{randy}
points of damage!"
puts "Grunty lost #{randy} health!"
puts "Grunty's hp is #{Health} "
end
In theory this should work but get the following errors:
/home/roo/dev/RubyRPG/RubyRPG/lib/main.rb:19:in `initialize':
uninitialized constant Enemy::Grunt::Grunto (NameError)
from /home/roo/dev/RubyRPG/RubyRPG/lib/main.rb:31:in `new'
from /home/roo/dev/RubyRPG/RubyRPG/lib/main.rb:31
from :1
I'm not too sure why these errors are being thrown up, I assume it's not
liking the way the class has been edited?
It looks like your syntax error results from declaring a "Grunty" class, but then calling Grunt.new, but without seeing the entire code base I can't be sure.
Thanks for all the help so far Stephen, it's helped show me the
direction I should be taking!
No problem, always ready to help with games. 
-- Stephen
···
On Mon, 03 Nov 2008 12:27:21 -0500, Ruairidh Mchardy <ruairidh82@gmail.com> wrote: