This workshop presented three notions that will come back in almost all the games that you will do later, even outside the workshops. Let's do a recap'.
The "Input, Update, Draw" structure is very useful. It allows us to better organize our code, and splits the player's actions, the game logic, and what the player sees. Constants allow us to write code with ease and arrays are a new data structure that allows us to group many variables into a list (more of their power will we unleashed in the next tutorial)!
With all of this, we built a game: TapTap. It is simple but fun. Now, it is up to you to improve it. Here's what I propose. Start by adding a highscore system. Then, to improve gameplay, add another brick or two. You can also make the game harder by tweeking the score system so that, the bigger the score, the faster it lowers per frame!
Show off your game online with #gamebuino #workshop #TapTap, we go through them all the time ;)
If you ran out of ideas, here is what we did on our side. Hope it helps :)
#include
const int LEFT = 1;
const int RIGHT = 2;
const int NO_DIRECTION = 3;
const int NUM_OF_BRICKS = 5;
int bricks[NUM_OF_BRICKS]; // Our bricks. Either LEFT or RIGHT
int arrow; // Either LEFT, RIGHT, or NO_DIRECTION
int score = 0;
int highscore = 0;
void setup() {
gb.begin();
// Shuffle bricks
bricks[0] = random(LEFT, RIGHT + 1);
bricks[1] = random(LEFT, RIGHT + 1);
bricks[2] = random(LEFT, RIGHT + 1);
bricks[3] = random(LEFT, RIGHT + 1);
bricks[4] = random(LEFT, RIGHT + 1);
arrow = NO_DIRECTION;
}
void loop() {
while (!gb.update());
// INPUTS //
if (gb.buttons.released(BUTTON_LEFT) || gb.buttons.released(BUTTON_A)) {
arrow = LEFT;
} else if (gb.buttons.released(BUTTON_RIGHT) || gb.buttons.released(BUTTON_B)) {
arrow = RIGHT;
}
// UPDATE //
// Lower score with time. The bigger the score, the faster is goes down
if (score > 0) {
score -= score / 60;
}
if (arrow != NO_DIRECTION) {
if (bricks[0] == arrow) {
score += 20;
if (score > highscore) { // Highscore ??
highscore = score;
}
// Shift down
bricks[0] = bricks[1];
bricks[1] = bricks[2];
bricks[2] = bricks[3];
bricks[3] = bricks[4];
bricks[4] = random(LEFT, RIGHT + 1); // New brick
}
else { // Lost :(
score = 0;
}
arrow = NO_DIRECTION; // User input handled
}
// DRAW //
gb.display.clear();
if (bricks[0] == LEFT) {
gb.display.fillRect(25, 40, 20, 10); // fillRect for the lower brick. drawRect for the others
} else {
gb.display.fillRect(35, 40, 20, 10);
}
if (bricks[1] == LEFT) {
gb.display.drawRect(25, 30, 20, 10);
} else {
gb.display.drawRect(35, 30, 20, 10);
}
if (bricks[2] == LEFT) {
gb.display.drawRect(25, 20, 20, 10);
} else {
gb.display.drawRect(35, 20, 20, 10);
}
if (bricks[3] == LEFT) {
gb.display.drawRect(25, 10, 20, 10);
} else {
gb.display.drawRect(35, 10, 20, 10);
}
if (bricks[4] == LEFT) {
gb.display.drawRect(25, 0, 20, 10);
} else {
gb.display.drawRect(35, 0, 20, 10);
}
// Score
gb.display.fillRect(0, gb.display.height() - (score / 5), 4, (score / 5)); // We divide the score by 5 to make it fit inside the screen
// Highscore
gb.display.drawFastHLine(0, gb.display.height() - (highscore / 5), 6);
}