Helios Lyons 201239214

Dependencies:   mbed

Brief

My aim for this project was to create a FRDM K64F adapted version of the classic Space Invaders by Tomohiro Nishikado. The game itself has a number of clear features to implement;

  • Left to right movement for the player 'canon'
  • A fixed amount of player lives
  • Firing mechanics for both canon and invaders (hence collision systems)
  • Random firing from remaining invaders
  • Wave based combat

My own addition to these established ideas was Boss waves, featuring a single, larger sprite which fires at a faster interval than previous waves. The addition of a movement system using a basic for loop, as opposed to a velocity based system, will enhance the nostalgic feel of the game.

https://os.mbed.com/media/uploads/helioslyons/screenshot_2020-05-27_at_06.12.00.png

Gameplay

Movement is controlled with the joystick, moving the canon left or right. Fire by pressing A. Invaders spawn at set positions, but randomly fire at a set interval, which is higher for boss waves. Time is taken during each wave, and displayed at wave intervals, and if the play wins.

Controls are shown on the Gamepad below: (attribution: Craig A. Evans, ELEC2645 University of Leeds)

https://os.mbed.com/media/uploads/helioslyons/screenshot_2020-05-27_at_06.20.18.png

Revision:
11:1fd8fca23375
Parent:
10:19b250c7de5e
Child:
13:1472c1637bfc
--- a/main.cpp	Mon May 18 18:14:35 2020 +0000
+++ b/main.cpp	Wed May 27 05:07:34 2020 +0000
@@ -11,7 +11,7 @@
 */
 
 /** Main
-* @brief Main game functions and loop
+* @brief Start screen, menu, and game loop
 * @author Helios A. Lyons
 * @date March, 2020
 */
@@ -20,85 +20,278 @@
 #include "mbed.h"
 #include "Gamepad.h"
 #include "N5110.h"
+#include "Bitmap.h"
 #include "Battle.h"
 
-#define PADDLE_WIDTH 2
-#define PADDLE_HEIGHT 8
-#define BALL_SIZE 2
-#define BALL_SPEED 3
-
 // structs
 struct UserInput {
     Direction d;
     float mag;
 };
+
 // objects
 N5110 lcd;
 Gamepad pad;
 Battle battle;
+Canon player;
+
+Timer playTime;
 
 // prototypes
 void init();
 void update_game(UserInput input);
 void render();
 void welcome();
+void wave(int n);
+void game(Gamepad &pad);
+void menu(Gamepad &pad);
 
 // functions
 int main()
-{
-    int fps = 6;  // frames per second
-
-    init();     // initialise and then display welcome screen...
-    welcome();  // waiting for the user to start
+{        
+    //lcd.clear();
+    init(); // initialise LCD and gamepad
+    
+    welcome(); // display welcome screen
+    
+    Menu_Point: // goto point after victory/defeat
+    menu(pad);
+    
+    battle.reset_life(); // start player lives at 3
+    int n = 0;
     
-    render();  // first draw the initial frame 
-    wait(1.0f/fps);  // and wait for one frame period
-
-
-    // game loop - read input, update the game state and render the display
-    while (1) {
-        //battle.read_input(pad);
-        //battle.update(pad);
-        render();
-        wait(1.0f/fps);
-    }
+    while (n < 9) {
+            n++;
+            if (battle.life() < 1) { break; } // break condition for when player dies
+            switch(n) {
+                case 1: // initialise entities for the wave
+                    battle.init(1,3,4,3,0,3); // row,column,speed,firing interval,boss,bossNum
+                    battle.clock(pad); // start the invader firing pattern
+                    wave(n); // print wave #
+                    playTime.start(); // start timer before game starts
+                    game(pad); // start the main game loop
+                    playTime.stop(); // stop timer
+                break;
+                
+                case 2:
+                    battle.init(3,3,4,3,0,3); // init function is used this way to provide
+                    battle.clock(pad); // easy options to vary invader formation and number
+                    wave(n); // in wave
+                    playTime.start();
+                    game(pad);
+                    playTime.stop();
+                break;
+                
+                case 3:
+                    battle.init(1,1,3,2,1,1);
+                    battle.clock(pad);
+                    wave(n);
+                    playTime.start();
+                    game(pad);
+                    playTime.stop();
+                break;
+                
+                case 4:
+                    battle.init(2,3,3,3,0,1);
+                    battle.clock(pad);
+                    wave(n);
+                    playTime.start();
+                    game(pad);
+                    playTime.stop();
+                break;
+                
+                case 5:
+                    battle.init(3,3,3,3,0,1);
+                    battle.clock(pad);
+                    wave(n);
+                    playTime.start();
+                    game(pad);
+                    playTime.stop();
+                break;
+                
+                case 6:
+                    battle.init(1,1,2,1,1,2);
+                    battle.clock(pad);
+                    wave(n);
+                    playTime.start();
+                    game(pad);
+                    playTime.stop();
+                break;
+                
+                case 7:
+                    battle.init(3,4,2,3,0,2);
+                    battle.clock(pad);
+                    wave(n);
+                    playTime.start();
+                    game(pad);
+                    playTime.stop();
+                break;
+                
+                case 8:
+                    battle.init(3,6,3,2,0,2);
+                    battle.clock(pad);
+                    wave(n);
+                    playTime.start();
+                    game(pad);
+                    playTime.stop();
+                break;
+                
+                case 9:
+                    battle.init(1,1,1,1,1,3);
+                    battle.clock(pad);
+                    wave(n);
+                    playTime.start();
+                    game(pad);
+                    playTime.stop();
+                break;
+                } // end of switch statement
+        }
+        
+        if (battle.life() < 1) { // if player failed display game over
+            lcd.printString("Game Over",16,2);
+            lcd.refresh();
+            wait(8.0);
+            goto Menu_Point; // return to main menu after wait
+            }
+        else { // if waves are completed display time and victory screen
+            lcd.printString("You won!",16,2);
+            char buffer4[14];
+            float time = sprintf(buffer4,"Time:%.2f",playTime.read());
+            lcd.printString(buffer4,16,3);
+            lcd.refresh();
+            wait(8.0);
+            goto Menu_Point; // return to main menu
+            }
 }
 
-// initialies all classes and libraries
-void init()
+void init() // initialises LCD and Gamepad
 {
-    // need to initialise LCD and Gamepad 
     lcd.init();
     pad.init();
-     
-    // initialise the game
-    //battle.init();
-
 }
 
-// this function draws each frame on the LCD
-void render()
-{
-    // clear screen, re-draw and refresh
-    lcd.clear();  
-    //battle.draw(lcd);
+void render() {  // clears screen and re-draws content (+ current lives left) 
+    lcd.clear(); // called in the main game() loop
+    battle.draw(lcd);
+    lcd.refresh();
+    
+    char buffer[4];
+    int lives = sprintf(buffer,"%2d",battle.life()); 
+    lcd.printString(buffer,0,5);
     lcd.refresh();
 }
 
-// simple splash screen displayed on start-up
-void welcome() {
+void game(Gamepad &pad) { // main game loop, reads input and updates bullet / canon positions
+    while (1) { 
+        int fps = 6;
+        battle.read_input(pad);
+        battle.update(pad);
+        render();
+        wait(1.0f/fps);
+        if (battle.end() || battle.life() < 1 ) { break; }
+        }
+    lcd.clear();
+    }
+   
+void wave(int n) {  // prints wave number and time taken so far
+        lcd.clear();
+        char buffer[14];
+        char buffer3[3];
+        int number = sprintf(buffer,"Wave%2d",n);
+        float time = sprintf(buffer3,"%.2f",playTime.read());
+        lcd.printString(buffer,24,1);
+        lcd.printString(buffer3,27,2);
+        
+        lcd.refresh();
+        wait(3.0); 
+    }
+
+void welcome() { // bitmap logo and title for start screen
+    static int logo[] = {
+    0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,
+    0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,
+    0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,
+    0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,
+    0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,
+    0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,
+    0,0,1,1,1,1,0,0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,
+    0,0,1,1,1,1,0,0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,
+    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+    1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,
+    1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,
+    1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,
+    1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,
+    0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,
+    0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0
+    };
     
-    lcd.printString("Space Invaders",0,1);  
-    lcd.printString("  Press Start ",0,4);
+    Bitmap canon_sprite(logo, 16, 22);
+    canon_sprite.render(lcd,30,4);
+    
+    lcd.printString("Space Invaders",0,3);
+    lcd.printString("Press Start",8,4);
     lcd.refresh();
-     
-    // wait flashing LEDs until start button is pressed 
-    while ( pad.start_pressed() == false) {
-        lcd.setContrast( pad.read_pot1());
+    
+    while (pad.start_pressed() == false) { // LEDs flash until start is pressed
+        lcd.setContrast(pad.read_pot1());
         pad.leds_on();
         wait(0.1);
         pad.leds_off();
         wait(0.1);
     }
- 
-}
\ No newline at end of file
+}
+
+void menu(Gamepad &pad) {
+    while(1) {
+        pad.reset_buttons();
+        lcd.clear();
+        lcd.printString("(A) Play",0,1);
+        lcd.printString("(X) How To",0,2);
+        lcd.printString("(B) Credits",0,3);
+        lcd.refresh();
+    
+        if (pad.X_pressed()) {
+            lcd.clear();
+            lcd.printString("Press A",0,2);
+            lcd.printString("to shoot",0,3);
+            lcd.refresh();
+            wait(5.0);
+            
+            lcd.clear();
+            lcd.printString("Use the",0,1);
+            lcd.printString("joystick to",0,2);
+            lcd.printString("move left",0,3);
+            lcd.printString("or right",0,4);
+            lcd.refresh();
+            wait(5.0);
+            
+            lcd.clear();
+            lcd.printString("Remaining ",0,1);
+            lcd.printString("lives are on",0,2);
+            lcd.printString("the bottom",0,3);
+            lcd.printString("left",0,4);
+            lcd.refresh();
+            wait(5.0);
+            
+            lcd.clear();
+            lcd.printString("Elapsed time",0,1);
+            lcd.printString("is shown at",0,2);
+            lcd.printString("each wave",0,3);
+            lcd.printString("interval",0,4);
+            lcd.refresh();
+            wait(5.0);
+            }
+        else if (pad.A_pressed()) {
+            return;
+            }
+        else if (pad.B_pressed()) {
+            lcd.clear();
+            lcd.printString("Game by",0,2);
+            lcd.printString("H.A. Lyons",0,3);
+            lcd.refresh();
+            wait(5.0);
+            }
+    }
+    
+} 
\ No newline at end of file