Part 6: A Day in the Life! – Bringing it all Together#
We're in the home stretch! Your digital pet simulator is almost complete. In this final part, we'll add the finishing touch: a
timePasses()
function that simulates the passage of time, subtly changing your pet's hunger and happiness levels. This will add a dynamic element, making your pet simulation feel even more alive. We'll also refine the simulator, ensuring it handles various edge cases gracefully. Get ready to witness the culmination of your hard work!
First, let's add the
timePasses()
function to the
DigitalPet
class. This function will increment hunger and decrement happiness over time, simulating the natural progression of a pet's needs:
Future<void> timePasses() async {
print('Time passes...');
await Future.delayed(Duration(seconds: 1));
_hunger = (_hunger + 1).clamp(MIN_STAT_VALUE, MAX_STAT_VALUE);
_happiness = (_happiness - 1).clamp(MIN_STAT_VALUE, MAX_STAT_VALUE);
if (_hunger >= MAX_STAT_VALUE) {
print('$name is very hungry!');
}
if (_happiness <= MIN_STAT_VALUE) {
print('$name is very sad!');
}
}
Notice the use of
Future.delayed
again to simulate the passage of time—a one-second delay in this case. The
clamp()
method ensures that hunger and happiness stay within the defined bounds. We also added some conditional statements to alert you if your pet is extremely hungry or unhappy.
To make the game more engaging and responsive, let's add a
needsAttention()
function to our
DigitalPet
class. This function will return
true
if the pet needs immediate attention (either extremely hungry or extremely unhappy), and
false
otherwise:
bool needsAttention() {
return _hunger >= MAX_STAT_VALUE || _happiness <= MIN_STAT_VALUE;
}
This function uses boolean logic (||
meaning "or") to check if either the hunger level is at the maximum or happiness is at the minimum.
Finally, let's integrate this
timePasses()
function into our main game loop and use the
needsAttention()
function to provide warnings if our pet is in distress:
switch (choice) {
// ... (other cases) ...
case '3':
await myPet.timePasses();
break;
// ... (rest of switch statement) ...
}
await Future.delayed(Duration(milliseconds: 500));
if (myPet.needsAttention()) {
print('Warning: ${myPet.name} really needs your attention!');
}
We added a new case to the switch statement to handle the user choosing to let time pass, and we're calling
needsAttention()
to check and provide warnings accordingly.
And that's it! Congratulations! You've built a fully functional digital pet simulator. You've learned about essential Dart concepts, including variables, functions, asynchronous operations, classes, objects, conditional statements, loops, and error handling. You should be incredibly proud of what you've accomplished! Take a moment to run the complete code and interact with your digital pet. You can now feed, play with, and even let time pass for your digital companion, observing its reactions in real time.
Remember the core concepts:
main
function as the starting point,
print
and
stdin.readLineSync
for interaction,
int
and
String
for data, functions for code organization,
async
and
await
for simulating time, classes and objects for structure, conditional logic and loops for interaction, and error handling for robustness.
This project demonstrates a clear understanding of fundamental programming principles. The use of classes and functions makes your code easily maintainable and extendable. The incorporation of asynchronous operations adds a touch of realism and dynamism. Well done!
