Welcome to Part 3! We've established our digital pet's basic needs; now it's time to make them interactive. This recipe introduces essential programming concepts: functions,
for
loops, and object-oriented programming (OOP). These concepts are fundamental to building more complex and organized programs.
Understanding Functions
Functions are reusable blocks of code that perform specific tasks. They help organize your program, making it easier to read and maintain. A function is defined using the
void
keyword (if it doesn't return a value), a function name, parameters in parentheses, and curly braces to enclose the code:
void myFunction(parameterType parameterName) {
// Function code here
}
Understanding for Loops
for
loops are used to repeat a block of code a specific number of times. They're useful for tasks like creating visual representations, iterating through data, etc. The basic structure is:
for (int i = 0; i < 10; i++) {
// Code to be executed repeatedly
}
Introducing Object-Oriented Programming (OOP)
OOP is a powerful programming paradigm that organizes code around "objects" that contain both data (variables) and methods (functions). This approach promotes modularity, reusability, and maintainability. We'll create a
DigitalPet
class.
Step 1: Creating the DigitalPet Class
Let's start by creating a simple class to represent our digital pet. For now, it will only hold the
name,
hunger, and
happiness
variables.
class DigitalPet {
String name;
int hunger;
int happiness;
DigitalPet(this.name, this.hunger, this.happiness);
}
Step 2: Adding the feed() and play() Functions
Now, let's add
feed()
and
play()
functions to our
DigitalPet
class. These functions will modify the
hunger
and
happiness
values. We'll also add a
displayStatus
function to see their values.
void feed() {
hunger -= 2;
happiness += 1;
print("${name} is eating...");
}
void play() {
happiness += 3;
hunger += 1;
print("${name} is playing...");
}
void displayStatus() {
print("--- ${name}'s Status ---");
print("Hunger: $hunger");
print("Happiness: $happiness");
print("-------------------------");
}
Step 3: Using a for Loop for a Simple Status Bar
Let's enhance
displayStatus
with a simple visual representation of hunger and happiness using a
for
loop to create a status bar.
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 < 10; i++) {
bar += (i < value) ? '#' : '-';
}
return bar;
}
Step 4: Putting it all together in main
Finally, let's update the
main
function to use our new
DigitalPet
class and its functions.
void main() {
// ... (Your existing code for getting petName) ...
DigitalPet myPet = DigitalPet(petName, 5, 5);
myPet.feed();
myPet.displayStatus();
myPet.play();
myPet.displayStatus();
}
Now you can run your code, and see the effect of feeding and playing on your pet's status!
That's it for Part 3! We've successfully incorporated functions,
for
loops, and the basics of OOP. In Part 4, we'll add asynchronous functionality to simulate the passage of time, impacting your pet's hunger and happiness levels. We will learn about
async,
await, and
Future. Get ready for a more dynamic pet!
