Lesson 6

Your Second Game

This lesson is designed to move away from the basics of coding to toward bigger picture skills you will need to program games.

Here are the two things this lesson will focus on as we build toward our second game:

  1. Switches and how to use them to set up different game screens

  2. How look up information about coding

Game Description

  • This game will be text / story based adventure

  • Pressing different buttons will let players pick different choices in the story

  • Each branch of the story will have its own game screen

Switches

Switches are great way to help create sections of code. For example, you don't want to have to worry about handling a ball bouncing around on your screen for the player to chase while you're coding a Title Screen.

Let's make a switch called gamescreen. Start my making a variable called gamescreen before the setup() function:

int gamescreen = 0;

Now create the switch inside our loop() function. Here is how it looks:

switch (gamescreen) {


case 0:


break;


case 1:

break;


case 2:

break;

Think of our switch like a room with many doors where only one of the doors can be open at a time. When our loop() enters the room it will go through the only door open.

Right now, the only door open is "case 0"

This is because when we set up our variable gamescreen we set it = 0.

If we change the value of gamescreen, we can open and close the different doors inside our switch room.

Look at the example

Challenges!

Regular Challenge 1:Add later

The Complete Code

/* Game by Salieri Labs

* twitter @mrincorvia

*/


#include <Arduboy2.h>

Arduboy2 arduboy;




int gamescreen = 0;



void setup() {

arduboy.begin();

arduboy.clear();


}


void loop() {


//Prevent the Arduboy from running too fast

if (!arduboy.nextFrame()) {

return;

}

arduboy.clear();

arduboy.pollButtons();


switch (gamescreen) {



case 0:


arduboy.setTextSize(2);

arduboy.print("Title");



if (arduboy.justPressed(A_BUTTON)) {

gamescreen = 1;

}


break;



case 1:

arduboy.setTextSize(1);

arduboy.print("Gameplay Screen");


break;


case 2:

break;


case 3:


break;


case 4:

break;


case 5:

break;


}

arduboy.display();

}