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 ...
Get Learn to Program, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.