Part 4: Happy or Hungry? – Object-Oriented Programming and Conditional Logic#
Our digital pet is growing up! It's time to organize our code better and make our pet simulator more robust. We'll do this using Object-Oriented Programming (OOP), a powerful approach to software design. Don't worry, it's not as scary as it sounds!
In OOP, we create
classes, which are like blueprints for creating
objects. Think of a class as a cookie cutter, and the objects as the cookies you create using the cutter. In our case, the
DigitalPet
class will be the blueprint for creating individual digital pets. Each pet will have its own name, hunger level, and happiness level.
Let's start by defining our
DigitalPet
class. We'll add attributes (variables) for hunger and happiness, which will be
int
values, and a
name
which will be a
String. We'll also include a method (displayStatus()) to show the pet's current state. We'll use private variables (indicated by an underscore
_) to encapsulate the internal state of the pet, meaning only the class itself can directly access these variables. This approach promotes better organization and prevents accidental modification from outside the class:
class DigitalPet {
String name;
int _hunger;
int _happiness;
DigitalPet(this.name) : _hunger = 5, _happiness = 5; // Constructor initializing hunger and happiness
int get hunger => _hunger; // Getter for hunger
int get happiness => _happiness; // Getter for happiness
void displayStatus() {
print("--- ${name}'s Status ---");
print('Hunger: $_hunger');
print('Happiness: $_happiness');
print('-------------------------');
}
}
Notice the
get
methods (getters):
get hunger
and
get happiness. These provide controlled access to the private variables
_hunger
and
_happiness. This is an example of
encapsulation, a key aspect of OOP which protects the internal state of the object.
Now, let's create an instance (an object) of the
DigitalPet
class in our
main
function:
void main() {
// ... (previous code) ...
DigitalPet myPet = DigitalPet(petName); // Creating a DigitalPet object
myPet.displayStatus(); // Calling the displayStatus method
// ... (rest of your main function code) ...
}
We're creating an object named
myPet
using the
DigitalPet
class blueprint and passing the pet's name. Then, we're calling the
displayStatus()
method to see the pet's initial state.
To make the simulation more interactive, we'll use a
while
loop to keep the game running until the user decides to quit:
while (true) {
myPet.displayStatus();
// ... (rest of your game loop code) ...
}
This loop will continuously display the pet's status and allow the user to interact until they choose to exit.
Finally, we need to add some conditional logic to handle various situations:
if (myPet.hunger >= 10) {
print('${myPet.name} is starving!');
} else if (myPet.happiness <= 0) {
print('${myPet.name} is very unhappy!');
}
This code adds a simple check to alert you if your pet's needs are not being met.
Now, run your code. You'll see a more organized and robust pet simulator! We've successfully introduced the concepts of classes, objects, and conditional logic to create a more interactive and engaging experience.
In the next part, "Keeping Track!", we'll add a progress bar to visually represent your pet's stats, introduce
for
loops, and use a
switch
statement to handle user input more efficiently. Get ready for more advanced features!
