Kodeck

Lesson 4 of 7

Collisions and score

Catch the falling stars with a basket. Every star is a point; three misses and the game is over.

In this lesson

  • When two rectangles touch
  • Counting points and misses
  • A game that ends and starts again
  • Text in the middle of the screen

Stick: drag the pad or use the arrow keys. Button Y: the Y key.

A browser copy of the same program, written in JavaScript, so you can try it before uploading. On Kodeck the C++ code below runs.

On the console: in the Arduino IDE open File > Examples > Kodeck > 04_CatchStars and press Upload.

First time? Setting up the Arduino IDE

The code, step by step

The whole program, from top to bottom. The comment at the top of the file repeats what this page says. The file on GitHub

#include <Kodeck.h>

float basketX = 140;
const int BASKET_Y = 214, BASKET_W = 44, BASKET_H = 12;

float starX, starY, starSpeed;
int score = 0;
int missed = 0;
bool gameOver = false;

The game's memory

The basket has a place and a size; the star has a place and a speed. score and missed count, and gameOver remembers whether the game has ended.

// true when rectangle a and rectangle b overlap
bool touching(float ax, float ay, float aw, float ah, float bx, float by, float bw, float bh) {
  return ax < bx + bw && bx < ax + aw && ay < by + bh && by < ay + ah;
}

When two rectangles touch

This function answers one question with true or false: do two rectangles overlap?

Across, they overlap when each one starts before the other one ends: ax < bx + bw and bx < ax + aw. Down, the same with y and the heights. && means “and”: all four must be true.

a: ax, ay, aw, ahb: bx, by, bw, bh ax < bx + bwbx < ax + away < by + bhby < ay + ah
The shaded part is where the two rectangles overlap. All four comparisons on the right are true.
void newStar() {
  starX = random(10, 300);
  starY = -10;
  starSpeed = 2 + score * 0.1;  // a little faster after every catch
}

void restart() {
  score = 0;
  missed = 0;
  gameOver = false;
  newStar();
}

A new star, a new game

newStar() puts a star just above the screen at a random place. Its speed grows a little with every point: 2 + score * 0.1.

restart() clears the counters and drops the first star. The same function starts the first game and every next one.

void setup() {
  kodeck.begin();
  restart();
}

setup()

Start the console and the first game.

void loop() {
  if (!gameOver) {
    basketX = constrain(basketX + kodeck.stick.x * 6 / 100.0, 0, 320 - BASKET_W);
    starY = starY + starSpeed;

    if (touching(starX, starY, 10, 10, basketX, BASKET_Y, BASKET_W, BASKET_H)) {
      score = score + 1;
      kodeck.sound.slide(900, 1800, 80);
      newStar();
    } else if (starY > 240) {
      missed = missed + 1;
      kodeck.sound.slide(400, 150, 200);
      if (missed >= 3) gameOver = true;
      newStar();
    }
  } else if (kodeck.pressed(BUTTON_Y)) {
    restart();
  }

Catch or miss

While the game runs, the basket follows the stick and the star falls. If they touch, there is a point and a new star. If the star passes the bottom edge, there is a miss, and the third miss ends the game.

When the game is over, only Y does something: it calls restart().

  auto& g = kodeck.screen;
  g.fillScreen(BLACK);
  g.fillRect(starX, starY, 10, 10, YELLOW);
  g.fillRoundRect(basketX, BASKET_Y, BASKET_W, BASKET_H, 4, CYAN);

  g.setTextColor(WHITE);
  g.setTextSize(2);
  g.setCursor(8, 8);
  g.printf("%d", score);
  g.setTextSize(1);
  for (int i = 0; i < 3; i++) {  // three marks for the misses that are left
    if (i < 3 - missed) g.fillCircle(300 - i * 14, 14, 4, ORANGE);
    else g.drawCircle(300 - i * 14, 14, 4, DARKGREY);
  }

The picture

auto& g = kodeck.screen; gives the screen a short name, so every line below is shorter.

Three circles show the misses that are left: filled orange while a miss is still allowed, an empty grey ring for each one used.

  if (gameOver) {
    g.setTextDatum(middle_center);
    g.setTextSize(2);
    g.drawString("GAME OVER", 160, 100);
    g.setTextSize(1);
    g.drawString("Y: play again", 160, 130);
    g.setTextDatum(top_left);
  }
  kodeck.show();
}

Text in the middle

setTextDatum(middle_center) makes the next text centre itself on the point given, so “GAME OVER” sits in the middle of the screen whatever its length. After that the program switches back to top_left for the score.

Try it yourself

  • Faster every five

    Make the stars speed up only after every 5 points.

    Hint

    In newStar(): starSpeed = 2 + (score / 5) * 0.5; Dividing whole numbers drops the fraction, so the speed changes in steps.

  • Wider basket

    Make the game easier with a wider basket.

    Hint

    Change BASKET_W to 70.

  • A star to avoid

    Add a red star that costs a life when caught.

    Hint

    Give it its own variables (badX, badY), move and draw it like the yellow one, and when touching is true for it, add a miss instead of a point.