24 enemies
Three rows of eight make 24 enemies. Each one has a place and a flag that says whether it is still alive. waveSpeed and dir are shared by the whole wave: all enemies move together.
Lesson 5 of 7
A wave of 24 enemies moves left and right and steps down at every edge. Shoot them all before they land.
In this lesson
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 > 05_EnemyWave and press Upload.
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> const int ROWS = 3, COLS = 8; const int ENEMIES = ROWS * COLS; float enemyX[ENEMIES], enemyY[ENEMIES]; bool enemyAlive[ENEMIES]; float waveSpeed = 1; int dir = 1; int wave = 1;
Three rows of eight make 24 enemies. Each one has a place and a flag that says whether it is still alive. waveSpeed and dir are shared by the whole wave: all enemies move together.
float shipX = 150; float shotX, shotY; bool shotFlying = false; int score = 0; bool lost = false;
This time there is one shot at a time, so a single shotFlying flag is enough.
void newWave() { for (int i = 0; i < ENEMIES; i++) { enemyX[i] = 30 + (i % COLS) * 32; enemyY[i] = 30 + (i / COLS) * 24; enemyAlive[i] = true; } dir = 1; }
The enemies are stored in one list, but they are drawn as a grid. i % COLS is the remainder after dividing by 8, which is the column; i / COLS with whole numbers is the row. For enemy 10: column 2, row 1.
void setup() {
kodeck.begin();
newWave();
}Start the console and the first wave.
void loop() { if (lost) { if (kodeck.pressed(BUTTON_Y)) { lost = false; score = 0; wave = 1; waveSpeed = 1; newWave(); } } else { shipX = constrain(shipX + kodeck.stick.x * 5 / 100.0, 0, 300); if (kodeck.pressed(BUTTON_A) && !shotFlying) { shotFlying = true; shotX = shipX + 9; shotY = 200; kodeck.sound.slide(1600, 700, 60, 0.3); } if (shotFlying) { shotY = shotY - 8; if (shotY < 0) shotFlying = false; }
When the enemies have landed, Y resets everything. Otherwise the ship moves, A fires if no shot is flying, and the shot climbs 8 pixels per frame.
// 1. Is any enemy at an edge? Then all of them turn and step down. bool edge = false; for (int i = 0; i < ENEMIES; i++) { if (!enemyAlive[i]) continue; if ((dir > 0 && enemyX[i] > 300) || (dir < 0 && enemyX[i] < 4)) edge = true; } if (edge) dir = -dir;
Before anything moves, the program checks whether any living enemy has reached an edge. Only then does the whole wave turn round. If each enemy turned on its own, the grid would fall apart.
// 2. Move every enemy, check the shot and the ship int alive = 0; for (int i = 0; i < ENEMIES; i++) { if (!enemyAlive[i]) continue; enemyX[i] = enemyX[i] + waveSpeed * dir; if (edge) enemyY[i] = enemyY[i] + 8; if (shotFlying && shotX > enemyX[i] - 2 && shotX < enemyX[i] + 18 && shotY < enemyY[i] + 12 && shotY > enemyY[i]) { enemyAlive[i] = false; shotFlying = false; score = score + 10; kodeck.sound.noise(120, 0.4); continue; } if (enemyY[i] > 186) lost = true; alive = alive + 1; }
One loop moves every living enemy, steps it down if the wave turned, and checks the shot against it. A hit removes the enemy and the shot and adds 10 points; continue goes on with the next enemy.
An enemy that gets lower than 186 pixels has landed. The others are counted in alive.
if (alive == 0) { // wave cleared wave = wave + 1; waveSpeed = waveSpeed + 0.5; kodeck.sound.slide(500, 1500, 300); newWave(); } }
No enemies left: the wave number goes up, the next wave is half a pixel per frame faster, and it starts at the top again.
auto& g = kodeck.screen; g.fillScreen(BLACK); for (int i = 0; i < ENEMIES; i++) { if (!enemyAlive[i]) continue; uint16_t c = (i / COLS == 0) ? PINK : (i / COLS == 1) ? PURPLE : CYAN; // a colour per row g.fillRoundRect(enemyX[i], enemyY[i], 16, 12, 3, c); g.fillRect(enemyX[i] + 3, enemyY[i] + 4, 3, 3, BLACK); // eyes g.fillRect(enemyX[i] + 10, enemyY[i] + 4, 3, 3, BLACK); } if (shotFlying) g.fillRect(shotX, shotY, 2, 8, YELLOW); g.fillTriangle(shipX, 220, shipX + 10, 196, shipX + 20, 220, ORANGE); g.setTextColor(WHITE); g.setCursor(4, 4); g.printf("SCORE %d WAVE %d", score, wave); if (lost) { g.setTextDatum(middle_center); g.setTextSize(2); g.drawString("THEY LANDED", 160, 110); g.setTextSize(1); g.drawString("Y: try again", 160, 140); g.setTextDatum(top_left); } kodeck.show(); }
Each row has its own colour, chosen with the row number i / COLS. condition ? a : b is a short if: a when the condition is true, b when it is not. Two black squares make the eyes.
Make four rows of ten.
Set ROWS = 4 and COLS = 10. Ten enemies 32 pixels apart do not fit in 320 pixels: in newWave() use 26 instead of 32.
Make the top row worth 30 points and the others 10.
Where the score grows, use (i / COLS == 0) ? 30 : 10.
Let random enemies drop bombs.
Add bombX, bombY and bombFalling like the shot. In the enemy loop, random(1000) < 3 is true about three times in a thousand: a good moment to drop one.
Kodeck library and examples: MIT licence, source on GitHub. Lessons are in English for now; other languages will follow. Privacy