On Mon, 03 Nov 2008 12:27:21 -0500, Ruairidh Mchardy
[email protected] wrote:
@hp =100
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)
end
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