Make Your Own Arduboy Game: Part 5 - Your First Game!

This is Part 5 in a series on learning how to program your own Arduboy game. If you have skipped the previous parts, please read over Part 1, Part 2, Part 3, and Part 4!

Your Game

I think you’ve got all the skills you need to make your first game! This game will be a number guessing game where the Arduboy picks a random number and the player has 7 attempts to guess the number. After each guess, the Arduboy will tell the player a hint. If the player guesses right, the player wins!

Starting Code

Okay, let’s start a new project. Open up the Arduino IDE and go to File > New . In the new window, add some comments at the top that include your name and the title of this game.

Whenever I make a new game, the first things I do are:

  1. Include the Arduboy library,
  2. Create a new Arduboy object,
  3. Prepare the setup() and loop() sections.

Here’s what the code looks like when we’re ready to begin:

//Jonathan Holmes (crait)
//October 24th, 2016
//Number Guessing Game

#include <Arduboy2.h>
Arduboy arduboy;

void setup() {
  arduboy.begin();
  arduboy.clear();

}

void loop() {
  arduboy.clear();

  arduboy.display();
}

Setting Up Variables

For our game, we need to set up some variables. We need one to keep track of how many attempts the players has left, what the number the player is guessing, what number the Arduboy picked, and one to know if the player has won or not.

At this point, you only know how to use int 's, so we’ll continue using them. In the next part, I’m going to teach you about more of them.

Anyway, let’s initialize the variables by adding this code before setup() :

int playerwin;
int attempts;
int guessednumber;
int randomnumber;

Inside of setup() , let’s assign these variables to 0.

playerwin = 0;
attempts = 0;
guessednumber = 0;

randomnumber isn’t supposed to be 0. Instead, let’s use the following code to assign a random number to it between 1 and 100;

arduboy.initRandomSeed();
randomnumber = random(1, 101);

Computers really cannot pick a random number so they fake it by having a built in function that incorporates ‘random’ elements, such as the time or user input, to calculate a series of numbers.
The first line of code above, ‘seeds’ the random function and forces it to calculate a new series of numbers.
If you omit this line, you will get the same series of numbers each time you play the game which sort of makes the game very predictable. However, a constant set of values can help when debugging the game!

The second line of code assigns a random number between 1 and 100 to the variable randomnumber. For some reason, the function takes two parameters that specify the lower range (inclusive) and the upper range (exclusive), hence the values 1 and 101.

Next, we’re going to put most of our game’s code between the arduboy.clear() and arduboy.display() functions. So, put a comment in between them so we can keep track of where we are. Your code should now look like this:

//Jonathan Holmes (crait)
//October 24th, 2016
//Number Guessing Game

#include <Arduboy2.h>
Arduboy2 arduboy;
int playerwin;
int attempts;
int guessednumber;
int randomnumber;

void setup() {
  arduboy.begin();
  arduboy.clear();
  arduboy.initRandomSeed();
  playerwin = 0;
  attempts = 0;
  guessednumber = 0;
  randomnumber = 0;
}

void loop() {
  arduboy.clear();
  //Gameplay code goes here
  arduboy.display();
}

Else

We want our player to know if they have won or not. So, our playerwin variable will help with that. If playerwin is equal to 0, the player has not won. If playerwin is not equal to 0, then the player has won. In our code, we can put 2 if statements in to handle this.

if (playerwin == 0) {
  //Ask the player for a number and play the game
}
if (playerwin == 1) {
  //Tell the player that they won!
}

There can be some problems from doing our code this way, so I am going to teach you a better method using the if-else statement.

What we want is for our game to run some code if the player didn’t win, yet, and run other code if they did. At the end of an if statement, we can add else and more braces for a new section of code. Confused? Take a look below:

if (playerwin == 0) {
  //Ask the player for a number and play the game
} 
else {
  //Tell the player that they won!
}

Else is just another way to say otherwise . The above code says that if playerwin is equal to 0, then we need to run some code, otherwise we need to run some other code.

Coding The Game

Let’s stick the above if-else statement into the game and see where we’re at:

//Jonathan Holmes (crait)
//October 24th, 2016
//Number Guessing Game

#include <Arduboy2.h>
Arduboy2 arduboy;
int playerwin;
int attempts;
int guessednumber;
int randomnumber;

void setup() {
  arduboy.begin();
  arduboy.clear();
  playerwin = 0;
  attempts = 0;
  guessednumber = 0;
  randomnumber = 0;
}

void loop() {
  arduboy.clear();
  if (playerwin == 0) {
    //Ask the player for a number and play the game
  } 
  else {
    //Tell the player that they won!
  }
  arduboy.display();
}

Now, we have two sections of code we need to finish: 1) Asking the player for numbers and 2) Telling the player that they won.

Guessing A Number

The player will have 7 attempts to guess a number. So, when their attempts is at 7, we’ll show them a game over screen instead of letting them guess, again. We’ll use another if-else statement.

if (attempts == 7) {
  //Game Over screen
} 
else {
  //Player has more attempts
}

We’ll worry about the Game Over screen in a minute. For now, let’s modify the part with players having more attempts remaining.

The first thing we want to do is handle the buttons. If a player uses the Up or Down button, the guessednumber will increase or decrease. When the player presses the A Button, the player will actually attempt to guess that number.

if (arduboy.pressed(UP_BUTTON)) {
  guessednumber = guessednumber + 1;
}
if (arduboy.pressed(DOWN_BUTTON)) {
  guessednumber = guessednumber - 1;
}
if (arduboy.pressed(A_BUTTON)) {
  //Guess number
}

This is something we’ve seen before in the previous tutorial. After we handle the button input, we want to display the number being guessed as well as how many attempts the player has left. Add this code in:

arduboy.setCursor(0, 0);
arduboy.print("Attempt: ");
arduboy.print(attempts);
arduboy.print("\n");
arduboy.print("Number to guess: ");
arduboy.print(guessednumber);

You should know what this code does. However, there is something new: arduboy.print("\n"); This line of code creates a linebreak, which is like hitting the Enter button on your keyboard to insert a new line of text.

Let’s go back and add one more if-else statement in this to handle the player making a guess! :smiley: Remember, if the player guesses the random number, the player will win, otherwise (else) the player use up an attempt.

Remember, when a player wins, we already decided that playerwin would be changed from 0 to another number! Change the A Button code to this:

if (arduboy.pressed(A_BUTTON)) {
  if (guessednumber == randomnumber) {
    playerwin = 1;
  } 
  else {
    attempts = attempts + 1;
  }
}

Makes sense, right? :slight_smile: Here’s what the code should currently look like! We’re almost done!

//Jonathan Holmes (crait)
//October 24th, 2016
//Number Guessing Game

#include <Arduboy2.h>
Arduboy2 arduboy;
int playerwin;
int attempts;
int guessednumber;
int randomnumber;

void setup() {
  arduboy.begin();
  arduboy.clear();
  arduboy.initRandomSeed();
  playerwin = 0;
  attempts = 0;
  guessednumber = 0;
  randomnumber = random(1, 101);
}

void loop() {
  arduboy.clear();
  if (playerwin == 0) {
    //Ask the player for a number and play the game
    if (attempts == 7) {
      //Game Over screen
    } 
    else {
      //Player has more attempts
      if (arduboy.pressed(UP_BUTTON)) {
        guessednumber = guessednumber + 1;
      }
      if (arduboy.pressed(DOWN_BUTTON)) {
        guessednumber = guessednumber - 1;
      }
      if (arduboy.pressed(A_BUTTON)) {
        if (guessednumber == randomnumber) {
          playerwin = 1;
        } 
        else {
          attempts = attempts + 1;
        }
      }
      arduboy.setCursor(0, 0);
      arduboy.print("Attempt: ");
      arduboy.print(attempts);
      arduboy.print("\n");
      arduboy.print("Number to guess: ");
      arduboy.print(guessednumber);
    }
  } 
  else {
    //Tell the player that they won!
  }
  arduboy.display();
}

Game Over Screen

The Game Over screen is pretty easy. We need to put 2 things into the Game Over section: 1) Tell the player they lost and what the correct number actually was, and 2) Let them push A to restart the game. We already know how to tell the player that they lost:

arduboy.setCursor(0, 0);
arduboy.print("You lost!");
arduboy.print("\n");
arduboy.print("Correct Number: ");
arduboy.print(randomnumber);

Easy, right? When the player presses the A Button, we want the variables to reset and find a new random number. Here’s what that code looks like.

if (arduboy.pressed(A_BUTTON)) {
  randomnumber = random(1, 101);
  attempts = 0;
  playerwin = 0;
}

Win Screen

The Win screen will look almost identical to the Game Over screen. Let’s add this code to the Win screen:

arduboy.setCursor(0, 0);
arduboy.print("You won!");
arduboy.print("\n");
arduboy.print("Correct Number: ");
arduboy.print(randomnumber);

if (arduboy.pressed(A_BUTTON)) {
  randomnumber = random(1, 101);
  attempts = 0;
  playerwin = 0;
}

Uh, Oh!! There’s A Problem!

Uh, oh!!! :open_mouth: :sweat: There’s a problem… We’re almost done, but there’s a problem that I specifically avoided telling you about. Go ahead and run this code on your Arduboy and see if you can figure out what it is! If you’ve done the previous tutorials in this series, you may have already figured it out!

Here’s what the code should look like so far:

//Jonathan Holmes (crait)
//October 24th, 2016
//Number Guessing Game

#include <Arduboy2.h>
Arduboy2 arduboy;
int playerwin;
int attempts;
int guessednumber;
int randomnumber;

void setup() {
  arduboy.begin();
  arduboy.clear();
  arduboy.initRandomSeed();
  playerwin = 0;
  attempts = 0;
  guessednumber = 0;
  randomnumber = random(1, 101);
}

void loop() {
  arduboy.clear();
  if (playerwin == 0) {
    //Ask the player for a number and play the game
    if (attempts == 7) {
      //Game Over screen
      arduboy.setCursor(0, 0);
      arduboy.print("You lost!");
      arduboy.print("\n");
      arduboy.print("Correct Number: ");
      arduboy.print(randomnumber);
      if (arduboy.pressed(A_BUTTON)) {
        randomnumber = random(1, 101);
        attempts = 0;
        playerwin = 0;
      }
    } 
    else {
      //Player has more attempts
      if (arduboy.pressed(UP_BUTTON)) {
        guessednumber = guessednumber + 1;
      }
      if (arduboy.pressed(DOWN_BUTTON)) {
        guessednumber = guessednumber - 1;
      }
      if (arduboy.pressed(A_BUTTON)) {
        if (guessednumber == randomnumber) {
          playerwin = 1;
        } 
        else {
          attempts = attempts + 1;
        }
      }
      arduboy.setCursor(0, 0);
      arduboy.print("Attempt: ");
      arduboy.print(attempts);
      arduboy.print("\n");
      arduboy.print("Number to guess: ");
      arduboy.print(guessednumber);
    }
  } 
  else {
    //Tell the player that they won!
    arduboy.setCursor(0, 0);
    arduboy.print("You won!");
    arduboy.print("\n");
    arduboy.print("Correct Number: ");
    arduboy.print(randomnumber);

    if (arduboy.pressed(A_BUTTON)) {
      randomnumber = random(1, 101);
      attempts = 0;
      playerwin = 0;
    }
  }
  arduboy.display();
}

Did you figure out the problem? Yeah, it’s kinda unexpected. The Arduboy is too fast when playing a game like this! Playing the game and pressing the A button will blast through the entire menu super fast. Same with pressing Up or Down… The numbers will scroll too fast.

Fixing This Issue

So, we need to do 1 of 2 things to fix this speed issue. We could intentionally slow the Arduboy down, however, when making some games, they will move too slow to be enjoyable. That’s not really an option for us. The second option is to use some additional Arduboy2 library functions to make the program respond only when the user clicks a button.

As mentioned earlier, every Arduboy program has two main constructs - a setup() function that is called once and a loop() function that is called repeatedly.

The Arduboy2 library includes a variation of the arduboy.pressed(..) function that allows you to detect when a button is first pressed called arduboy.justPressed(..). It will indicate whether a button has been pressed since the last iteration of the loop and this one. To use, this function we have to include a second function at the start of the loop called arduboy.pollButtons() which simply tracks the status of buttons between loop iterations.

The code below shows how the two functions are used:

void loop() {
  arduboy.clear();
  arduboy.pollButtons();

  if (arduboy.justPressed(A_BUTTON)) {
    arduboy.setCursor(0, 0);
    arduboy.print("You pressed the A Button");
  }
  arduboy.display();
}

Inserting the code into our game results in the following:

//Jonathan Holmes (crait)
//October 24th, 2016
//Number Guessing Game

#include <Arduboy2.h>
Arduboy2 arduboy;
int playerwin;
int attempts;
int guessednumber;
int randomnumber;

void setup() {
  arduboy.begin();
  arduboy.clear();
  arduboy.initRandomSeed();
  playerwin = 0;
  attempts = 0;
  guessednumber = 0;
  randomnumber = random(1, 101);
}

void loop() {
  arduboy.clear();
  arduboy.pollButtons();
  if (playerwin == 0) {
    //Ask the player for a number and play the game
    if (attempts == 7) {
      //Game Over screen
      arduboy.setCursor(0, 0);
      arduboy.print("You lost!");
      arduboy.print("\n");
      arduboy.print("Correct Number: ");
      arduboy.print(randomnumber);
      if (arduboy.justPressed(A_BUTTON)) {
        randomnumber = random(1, 101);
        attempts = 0;
        playerwin = 0;
      }
    } 
    else {
      //Player has more attempts
      if (arduboy.justPressed(UP_BUTTON)) {
        guessednumber = guessednumber + 1;
      }
      if (arduboy.justPressed(DOWN_BUTTON)) {
        guessednumber = guessednumber - 1;
      }
      if (arduboy.justPressed(A_BUTTON)) {
        if (guessednumber == randomnumber) {
          playerwin = 1;
        } 
        else {
          attempts = attempts + 1;
        }
      }
      arduboy.setCursor(0, 0);
      arduboy.print("Attempt: ");
      arduboy.print(attempts);
      arduboy.print("\n");
      arduboy.print("Number to guess: ");
      arduboy.print(guessednumber);
    }
  } 
  else {
    //Tell the player that they won!
    arduboy.setCursor(0, 0);
    arduboy.print("You won!");
    arduboy.print("\n");
    arduboy.print("Correct Number: ");
    arduboy.print(randomnumber);

    if (arduboy.justPressed(A_BUTTON)) {
      randomnumber = random(1, 101);
      attempts = 0;
      playerwin = 0;
    }
  }
  arduboy.display();
}

Hints

After testing your game, you may have realized that it’s just too hard for someone to actually beat. I tried a many times and won only once. It’s almost impossible! :head_bandage:

Let’s make the game a little easier and a little more fun by adding in hints to the player. To start this, let’s create a new variable called lastguess and give it a value of 0.

Every time the player makes a guess, let’s set lastguess to have the same value of their guessednumber .

Look for the line of code where the player uses one of their attempts:

attempts = attempts + 1;

After that line, let’s add the following:

lastguess = guessednumber;

Add a new linebreak after with arduboy.print("\n"); and add a new if-else statement. We’re going to check the player guessed anything, yet.

arduboy.print("\n");
if (attempts == 0) {
	//No last guess
} 
else {
	//Last guess was wrong
}

If there have been 0 attempts so far, then let’s print out “ Good luck! ” Otherwise, let’s display the last guess. That code should now look like:

arduboy.print("\n");
if (attempts == 0) {
  arduboy.print("Good luck!");
} 
else {
  arduboy.print(lastguess);
}

Our hint will actually to tell the user if lastguess is less than < or greater than > the randomnumber and print that out. That code should now look like:

arduboy.print("\n");
if (attempts == 0) {
  arduboy.print("Good luck!");
}
else {
  arduboy.print(lastguess);
  if (lastguess > randomnumber) {
    arduboy.print(" is too high!");
  }
  if( lastguess < randomnumber ) {
    arduboy.print(" is too low!");
  }
}

You’ve seen all of this code before, except for checking if a number is less than < or greater than > another number.

Full Code

Yes! :laughing: You’re finally done!! Test out this code and have fun!

#include <Arduboy2.h>
Arduboy2 arduboy;
int playerwin;
int attempts;
int guessednumber;
int randomnumber;
int lastguess;

void setup() {
  arduboy.begin();
  arduboy.clear();
  arduboy.initRandomSeed();
  playerwin = 0;
  attempts = 0;
  guessednumber = 0;
  randomnumber = random(1, 101);
}

void loop() {
  arduboy.clear();
  arduboy.pollButtons();
  if (playerwin == 0) {
    //Ask the player for a number and play the game
    if (attempts == 7) {
      //Game Over screen
      arduboy.setCursor(0, 0);
      arduboy.print("You lost!");
      arduboy.print("\n");
      arduboy.print("Correct Number: ");
      arduboy.print(randomnumber);
      if (arduboy.justPressed(A_BUTTON)) {
        randomnumber = random(1, 101);
        attempts = 0;
        playerwin = 0;
      }
    } 
    else {
      //Player has more attempts
      if (arduboy.justPressed(UP_BUTTON)) {
        guessednumber = guessednumber + 1;
      }
      if (arduboy.justPressed(DOWN_BUTTON)) {
        guessednumber = guessednumber - 1;
      }
      if (arduboy.justPressed(A_BUTTON)) {
        if (guessednumber == randomnumber) {
          playerwin = 1;
        } 
        else {
          attempts = attempts + 1;
					lastguess = guessednumber;
        }
      }
      arduboy.setCursor(0, 0);
      arduboy.print("Attempt: ");
      arduboy.print(attempts);
      arduboy.print("\n");
      arduboy.print("Number to guess: ");
      arduboy.print(guessednumber);
      arduboy.print("\n");
      if (attempts == 0) {
        arduboy.print("Good luck!");
      } 
      else {
        arduboy.print(lastguess);
        if (lastguess > randomnumber) {
          arduboy.print(" is too high!");
        }
        if( lastguess < randomnumber ) {
          arduboy.print(" is too low!");
        }
      }
    }
  } 
  else {
    //Tell the player that they won!
    arduboy.setCursor(0, 0);
    arduboy.print("You won!");
    arduboy.print("\n");
    arduboy.print("Correct Number: ");
    arduboy.print(randomnumber);

    if (arduboy.justPressed(A_BUTTON)) {
      randomnumber = random(1, 101);
      attempts = 0;
      playerwin = 0;
    }
  }
  arduboy.display();
}

For anyone wanting a graphical representation of this game, here’s a flow chart. :slight_smile:

What’s Next?

Wow! That was a lot! There is still a lot of stuff you need to learn to be a great programmer. I’ll teach you that stuff later, though. The next tutorial is going to be fun! You’ll learn how to make games with images!

Credits

I wrote this tutorial in order to give back to the programming community that taught me to get into it about 10 years ago. If you’d like to follow me on Twitter, please do so at http://www.twitter.com/crait . I’d greatly appreciate that. :smile:

3 Likes