LogoDart Beginner Tutorials

Feeding and Playing with Your Pet

Learn about Dart functions, conditional statements ifelse, and asynchronous operations with Future and await to make your pet interactive.

Welcome to Part 3! In the previous recipes, you created your digital pet and gave it hunger and happiness levels. Now, let's make it interactive! This recipe will teach you about functions, if/else statements, and asynchronous operations using Future and await.

Problem: You want to add functionality to feed and play with your pet, updating its hunger and happiness levels accordingly. You also want to simulate the time it takes to perform these actions.

Solution: We'll create functions (feed and play) to handle feeding and playing, using if/else statements to add some logic and Future and await to simulate the time delay.

Step 1: Creating the feed function

Let's start by creating a function called feed that simulates feeding your pet. This function will decrease the hunger level and increase the happiness level.

void feed() {
  print('$petName is eating...');
  hunger = hunger - 2;
  happiness = happiness + 1;
  print('$petName finished eating! Hunger decreased, happiness increased.');
}

Notice that we're directly modifying the hunger and happiness variables within the function. We'll improve this later.

Step 2: Creating the play function

Next, let's create a play function that simulates playing with your pet. This will increase the happiness level but also slightly increase the hunger level.

void play() {
  print('$petName is playing...');
  happiness = happiness + 3;
  hunger = hunger + 1;
  print('$petName finished playing! Happiness increased, hunger increased.');
}

Step 3: Introducing Asynchronous Operations with Future and await

Feeding and playing take time! Let's add some realism using asynchronous operations with Future and await. This will make our pet interaction more engaging.

We'll use Future.delayed to pause execution for a short period. The await keyword makes our code wait for the Future to complete before proceeding.

Future<void> feed() async {
  print('$petName is eating...');
  await Future.delayed(Duration(seconds: 2)); // Simulate eating time
  hunger = hunger - 2;
  happiness = happiness + 1;
  print('$petName finished eating! Hunger decreased, happiness increased.');
}

Future<void> play() async {
  print('$petName is playing...');
  await Future.delayed(Duration(seconds: 3)); // Simulate playing time
  happiness = happiness + 3;
  hunger = hunger + 1;
  print('$petName finished playing! Happiness increased, hunger increased.');
}

Notice the async keyword before the function body and the await before Future.delayed. The Future represents the delayed operation.

Step 4: Integrating Functions and Adding Conditional Logic

Now, let’s use these functions and add a basic game loop using an if/else statement for handling user input.

void main() async {
  // ... (Previous code) ...
  while (true) {
    print('What would you like to do?');
    print('1. Feed $petName');
    print('2. Play with $petName');
    print('3. Exit');

    String? choice = stdin.readLineSync();
    if (choice == '1') {
      await feed();
    } else if (choice == '2') {
      await play();
    } else if (choice == '3') {
      print('Goodbye!');
      break; // Exit the loop
    } else {
      print('Invalid choice. Please try again.');
    }
  }
}

Congratulations! You've created interactive functions, simulated time delays with Future and await, and added if/else logic to handle user input!

Next Steps: In the next recipe, we'll add a time-passing mechanism to our game, using loops and switch statements to make it even more engaging. You'll learn about loops, the while loop, switch statements, and how to handle user input more effectively.