Kodeck

Lesson 2 of 7

Moving with the stick

A ship follows the analog stick over a field of falling stars.

In this lesson

  • Reading the stick: numbers from -100 to 100
  • Numbers with a fraction: float
  • Arrays: many values under one name
  • for-loops and random numbers

Stick: drag the round pad, or use the arrow keys.

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 > 02_StickShip 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 shipX = 150, shipY = 180;  // where the ship is
float maxSpeed = 5;              // pixels per frame with the stick pushed all the way

Where the ship is

float is a number with a fraction, like 2.5. The ship's position is a float so it can move by part of a pixel in one frame, which keeps slow movement smooth.

maxSpeed is the speed with the stick pushed all the way: 5 pixels per frame.

// Stars in the background: 40 points, each with its own speed
const int STARS = 40;
float starX[STARS], starY[STARS], starSpeed[STARS];

Arrays

An array keeps many values under one name. starX[0] is the first star's position across the screen, starX[39] the last one's. const means that STARS never changes, which is what C++ needs for the size of an array.

void setup() {
  kodeck.begin();
  for (int i = 0; i < STARS; i++) {
    starX[i] = random(320);
    starY[i] = random(240);
    starSpeed[i] = random(5, 30) / 10.0;  // 0.5 ... 3 pixels per frame
  }
}

Filling the arrays

A for-loop repeats its block for i = 0, 1, 2 and so on up to 39. Each star gets a place and a speed.

random(320) gives a whole number from 0 to 319. random(5, 30) gives 5 to 29; divided by 10.0 that becomes 0.5 to 2.9 pixels per frame.

void loop() {
  // 1. Move the ship
  shipX = shipX + kodeck.stick.x * maxSpeed / 100;
  shipY = shipY + kodeck.stick.y * maxSpeed / 100;
  shipX = constrain(shipX, 0, 300);  // keep it on the screen
  shipY = constrain(shipY, 24, 220);

Moving the ship

kodeck.stick.x goes from -100 (left) to 100 (right), kodeck.stick.y from -100 (up) to 100 (down). Multiplied by maxSpeed and divided by 100: pushed all the way, the ship moves 5 pixels; pushed halfway, 2.5. Let go, and it stops.

constrain(value, low, high) keeps a value between two limits, so the ship stays on the screen.

  // 2. Move the stars down; a star that leaves the screen starts again at the top
  for (int i = 0; i < STARS; i++) {
    starY[i] = starY[i] + starSpeed[i];
    if (starY[i] > 240) {
      starY[i] = 0;
      starX[i] = random(320);
    }
  }

Moving the stars

Every star moves down by its own speed. A star that leaves the bottom of the screen starts again at the top, at a new random place across. Forty stars, but only five lines of code.

  // 3. Draw everything
  kodeck.screen.fillScreen(BLACK);
  for (int i = 0; i < STARS; i++) {
    uint16_t c = starSpeed[i] > 2 ? WHITE : GREY;  // fast stars look closer and brighter
    kodeck.screen.drawPixel(starX[i], starY[i], c);
  }
  kodeck.screen.fillTriangle(shipX, shipY + 24, shipX + 10, shipY, shipX + 20, shipY + 24, CYAN);
  kodeck.screen.fillRect(shipX + 8, shipY + 24, 4, 4, ORANGE);  // engine flame

  kodeck.screen.setTextColor(GREY);
  kodeck.screen.setCursor(4, 4);
  kodeck.screen.printf("stick x %4d  y %4d", kodeck.stick.x, kodeck.stick.y);

  kodeck.show();
}

Drawing

The order matters: first the black background, then the stars, then the ship on top of them. Fast stars are drawn white and slow ones grey, so the fast ones look closer.

printf prints numbers into text: %4d is a whole number, always 4 characters wide, so the text does not jump when the number changes.

Try it yourself

  • Faster ship

    Make the ship faster or slower.

    Hint

    Change maxSpeed. Try 2 and 10.

  • Through the edge

    Instead of stopping at the left and right edges, let the ship come out on the other side.

    Hint

    Replace the constrain line for shipX with two checks: if (shipX > 320) shipX = -20; and if (shipX < -20) shipX = 320;

  • More stars

    Make a sky of 100 stars.

    Hint

    Change STARS to 100. The arrays grow with it.

  • Sideways

    Make the stars fly from right to left.

    Hint

    Move starX instead of starY, and when a star leaves the left edge, give it starX[i] = 320 and a new random starY[i].