Baby Dragon

Great! You know how to create your own classes, even some of the subtle bits, but so far you’ve really only seen a small, fluffy, toy example. Let me give you something a bit more chewy. Let’s say we want to make a simple virtual pet, a baby dragon. Like most babies, it should be able to eat, sleep, and poop, which means we will need to be able to feed it, put it to bed, and take it on walks. Internally, our dragon will need to keep track of whether it is hungry, tired, or needs to go, but we won’t be able to see that when we interact with our dragon, just like you can’t ask a human baby, “Are you hungry?” We’ll also add a few other fun ways we can interact with our baby dragon, and when he is born, we’ll give him a name. (Whatever you pass into the new method is then passed onto the initialize method for you.) OK, let’s give it a shot:

class​ Dragon
def​ initialize name
@name = name
@asleep = false
@stuff_in_belly = 10 ​# He's full.
@stuff_in_intestine = 0 ​# He doesn't need to go.
puts ​"​#{@name}​ is born."
end
def​ feed
puts ​"You feed ​#{@name}​."
@stuff_in_belly = 10
passage_of_time
end
def​ walk
puts ​"You walk ​#{@name}​."
@stuff_in_intestine = 0
passage_of_time
end
def​ put_to_bed
puts ​"You put ​#{@name}​ to bed."
@asleep = true
3.times ​do
if​ @asleep
passage_of_time
end
if​ @asleep
puts ​"​#{@name}​ snores, filling the room with smoke."
end
end
if​ @asleep
@asleep = false
puts ​"​#{@name}​ wakes up slowly."
end
end
def​ toss
puts ​"You toss ​#{@name}​ up into the air."
puts ​'He giggles, which singes your eyebrows.'
passage_of_time
end
def​ rock
puts ​"You rock ​#{@name}​ gently."
@asleep = true
puts ​'He briefly dozes off...'
passage_of_time
if​ @asleep
@asleep = false
puts ​'...but wakes when you stop.'
end
end
private
# "private" means that the methods defined here are
# methods internal to the object. (You can feed your
# dragon, but you can't ask him whether he's hungry.)
def​ hungry?
# Method names can end with "?".
# Usually, we do this only if the method
# returns true or false, like this:
@stuff_in_belly <= 2
end
def​ poopy?
@stuff_in_intestine >= 8
end
def​ passage_of_time
if​ @stuff_in_belly > 0
# Move food from belly to intestine.
@stuff_in_belly = @stuff_in_belly - 1
@stuff_in_intestine = @stuff_in_intestine + 1
else​ ​# Our dragon is starving!
if​ @asleep
@asleep = false
puts ​'He wakes up suddenly!'
end
puts ​"​#{@name}​ is starving! In desperation, he ate YOU!"
exit ​# This quits the program.
end
if​ @stuff_in_intestine >= 10
@stuff_in_intestine = 0
puts ​"Whoops! ​#{@name}​ had an accident..."
end
if​ hungry?
if​ @asleep
@asleep = false
puts ​'He wakes up suddenly!'
end
puts ​"​#{@name}​'s stomach grumbles..."
end
if​ poopy?
if​ @asleep
@asleep = false
puts ​'He wakes up suddenly!'
end
puts ​"​#{@name}​ does the potty dance..."
end
end
end
pet = Dragon.new ​'Norbert'
pet.feed
pet.toss
pet.walk
pet.put_to_bed
pet.rock
pet.put_to_bed
pet.put_to_bed
pet.put_to_bed
pet.put_to_bed
Norbert is born.
You feed Norbert.
You toss Norbert up into the air.
He giggles, which singes your eyebrows.
You walk Norbert.
You put Norbert to bed.
Norbert snores, filling the room with smoke.
Norbert snores, filling the room with smoke.
Norbert snores, filling the room with smoke.
Norbert wakes up slowly.
You rock Norbert gently.
He briefly dozes off...
...but wakes when you stop.
You put Norbert to bed.
He wakes up suddenly!
Norbert's stomach grumbles...
You put Norbert to bed.
He wakes up suddenly!
Norbert's stomach grumbles...
You put Norbert to bed.
He wakes up suddenly!
Norbert's stomach grumbles...
Norbert does the potty dance...
You put Norbert to bed.
He wakes up suddenly!
Norbert is starving! In desperation, he ate YOU!

Whew! Of course, it would be nicer if this were an interactive program…oh, I think I smell an exercise coming on.

We saw a few new things in that example. The first is the word private that we stuck right in the middle of our class definition. I could have left it out, but I wanted to enforce the idea that certain methods are things you can do to a dragon and other methods are used only within the dragon. You can think of these as being “under the hood”: unless you are an automobile mechanic, all you really need to know is the gas pedal, the brake pedal, and the steering wheel. A programmer might call those the public interface of your car. How your airbag knows when to deploy, however, is internal to the car; the typical user (driver) doesn’t need to know how that works.

Actually, for a bit more concrete example along those lines, let’s talk about how you might represent a car in a video game. First, you would want to decide what you want your public interface to look like; in other words, which methods should people be able to call on one of your car objects? Well, they need to be able to push the gas pedal and the brake pedal, but they would also need to be able to specify how hard they are pushing the pedal. (There’s a big difference between flooring it and tapping it.) They would also need to be able to steer, and again, they would need to be able to say how hard they are turning the wheel. I suppose you could go further and add a clutch, turn signals, rocket launcher, afterburner, flux capacitor, and so on…. It depends on what type of game you are making.

Internal to a car object, though, much more would need to be going on; other things a car would need are a speed, a direction, and a position (at the most basic). These attributes would be modified by pressing on the gas or brake pedals and turning the wheel, of course, but the user would not be able to set the position directly (which would be like warping). You might also want to keep track of skidding or damage, whether you have caught any air, and so on. These would all be internal to your car object (that is, not directly accessible by the player; these would be private).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.51.241