Congratulations on making it to the final part! You've learned a lot about Dart, from basic programming concepts to object-oriented programming. In this section, we'll bring everything together to create a fully functional, interactive digital pet simulator.
Step 1: Adding a Menu-Driven Interface – User Interaction#
We'll use a
while
loop to keep the game running and a
switch
statement to handle user choices. Replace the content of your
main
function with the following:
void main() async {
stdout.write('What would you like to name your pet? ');
String? petName = stdin.readLineSync()?.trim();
petName ??= 'Buddy';
print('Hello, $petName!');
DigitalPet myPet = DigitalPet(petName);
while (true) {
myPet.displayStatus();
print('What would you like to do?');
print('1. Feed ${myPet.name}');
print('2. Play with ${myPet.name}');
print('3. Wait (time passes)');
print('4. Exit');
stdout.write('Enter your choice: ');
String? choice = stdin.readLineSync();
switch (choice) {
case '1':
await myPet.feed();
break;
case '2':
await myPet.play();
break;
case '3':
await myPet.timePasses();
break;
case '4':
print('Goodbye! Thanks for playing with ${myPet.name}.');
return;
default:
print('Invalid choice. Please try again.');
}
await Future.delayed(Duration(milliseconds: 500)); // Add a small delay
if (myPet.needsAttention()) {
print('Warning: ${myPet.name} really needs your attention!');
}
}
}
The
while (true)
loop continuously runs the game until the user chooses to exit. The
switch
statement processes user input, calling the appropriate
DigitalPet
method. The
Future.delayed
call adds a small pause between iterations.
Step 2: Adding a Progress Bar – Visual Feedback#
Let's enhance the
displayStatus
method in your
DigitalPet
class to include a visual progress bar for hunger and happiness. This adds a more engaging visual element to the simulator. Replace the existing
displayStatus
method with this improved version:
void displayStatus() {
print("--- ${name}'s Status ---");
print('Hunger: [${_getStatusBar(_hunger)}]');
print('Happiness: [${_getStatusBar(_happiness)}]');
print('-------------------------');
}
String _getStatusBar(int value) {
String bar = '';
for (int i = 0; i < MAX_STAT_VALUE; i++) {
if (i < value) {
bar += '#';
} else {
bar += '-';
}
}
return bar;
}
This utilizes a helper function
_getStatusBar
to generate a simple text-based progress bar. This significantly improves the user experience by providing clear visual feedback.
Step 3: The Complete Code#
Here is the complete, final code incorporating all the changes:
import 'dart:io';
class DigitalPet {
// ... (Your DigitalPet class code from Part 5 remains the same) ...
}
void main() async {
// ... (Your main function code from Step 1 remains the same) ...
}
Run this code! You now have a complete interactive digital pet simulator!
Congratulations!#
You've successfully created a fully functional digital pet simulator using Dart. You've mastered fundamental Dart concepts, incorporated OOP principles, and built an interactive application. You should be very proud of yourself!
There are no further parts to this tutorial. You can now experiment with adding more features, such as different pet types, more actions, or a more sophisticated user interface. The possibilities are endless!
