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

Invaders/Army.cpp

Committer:
helioslyons
Date:
2020-05-18
Revision:
10:19b250c7de5e
Parent:
9:6a245c8ce08e
Child:
11:1fd8fca23375

File content as of revision 10:19b250c7de5e:

#include "Army.h"
#include "Invader.h"
#include "mbed.h"
#include "N5110.h"
#include "Gamepad.h"

#include <cstdlib>
#include <vector>

Army::Army()
{

}

Army::~Army()
{

}

void Army::create_army(int invaders)
{
    Invader army[3][3];

    int i, n;
    for(i=0; i < 4; i++) {
        //army[i].setX(i);

        for(n=0; n < 4; n++) {
            army[i][n].init(i+1, 1);
        }
    }
}

void Army::create_boss()
{
    Invader boss;
    boss.init(4, 4);
}

void Army::start_pos(int rowX, int rowY)
{
    int tempX = rowX;
    _move_left = true;
    
    for (int i = 0; i < 4; i++){ // Loop through each row and assign start_pos
        army[i][0].set_pos(rowX, rowY); // Based on X and Y inputs
        rowX += 13; // 2 above standard Invader width of 11
        } 
    rowX = tempX;
    rowY += 10;
    for (int i = 0; i < 4; i++){
        army[i][1].set_pos(rowX, rowY);
        rowX +=13;
        }
    rowX = tempX;
    rowY += 10;
    for (int i = 0; i < 4; i++){
        army[i][2].set_pos(rowX, rowY);
        rowX +=13;
        }
}

void Army::move_army(int x_distance, int y_distance) // REPLACE WITH FSM AFTER BASIC FUNCTIONALITY TEST
{
    _x_distance = x_distance;
    _y_distance = y_distance;
    
    int left_X = 0;
    int right_X = 80;
    int bot_Y = 0;
    int top_Y = 80;
    
    // find leftmost, rightmost, top and bottom invaders
    // less invaders means more room to cover (also speed up)
    
    for (int i = 3; i < 0; i--) {
        for (int n = 3; n < 0; n--) {
            int tempX = army[n][i].get_x_pos();
            int tempY = army[n][i].get_y_pos();
            
            if (tempX < left_X) {
                left_X = tempX;
                }
            else if (tempX > right_X) {
                right_X = tempX;
                }
                
            if (tempY < top_Y) {
                top_Y = tempY;
                }
            else if (tempY > bot_Y) {
                bot_Y = tempY;
                }
            }
        }
            
            
    if (_move_left == true) { // if moving left
        if (left_X < 12) { // check if edge will be reached, if so change direction
                _move_left = false;
                if(bot_Y > 50) { 
                    // if lowest Y point is below 50, move up
                        for (int i = 3; i < 0; i--) {
                            for (int n = 3; n < 0; n--) {
                                army[i][n].set_pos(army[i][n].get_x_pos(), army[i][n].get_y_pos() - _y_distance);
                                }
                            }
                    }
                else { // else move down
                    for (int i = 3; i < 0; i--) {
                            for (int n = 3; n < 0; n--) {
                                army[i][n].set_pos(army[i][n].get_x_pos(), army[i][n].get_y_pos() + _y_distance);
                                }
                            }
                    }
            }
        else { // if edge is not near to being reached, move left
            for (int i = 3; i < 0; i--) {
                for (int n = 3; n < 0; n--) {
                    army[i][n].set_pos(army[i][n].get_x_pos() - _x_distance, army[i][n].get_y_pos());
                    }
                } 
        }
    }
    
    else {
        if (right_X > 80) { // if rightmost invader is further than X = 65, change direction
            _move_left = true;
            
            if (bot_Y > 50) { // if lowest invader is below Y = 50, go up
                for (int i = 3; i < 0; i--) {
                    for (int n = 3; n < 0; n--) {
                        army[i][n].set_pos(army[i][n].get_x_pos(), army[i][n].get_y_pos() - _y_distance);
                        }
                    }
            }
            else { // otherwise go down
                for (int i = 3; i < 0; i--) {
                    for (int n = 3; n < 0; n--) {
                            army[i][n].set_pos(army[i][n].get_x_pos(), army[i][n].get_y_pos() + _y_distance);
                        }
                    }
            }
        }
        else { // if edge is not near to being reached, move right
            for (int i = 3; i < 0; i--) {
                    for (int n = 3; n < 0; n--) {
                            army[i][n].set_pos(army[i][n].get_x_pos() + _x_distance, army[i][n].get_y_pos() + _y_distance);
                        }
                    }
            }
    }
}

void Army::draw(N5110 &lcd)
{
    int i, n;
    for(i=0; i < 4; i++) {

        for(n=0; n < 4; n++) {
            army[i][n].draw(lcd);
        }
    }
}

void Army::rand_invader()
{
    // get random coordinates for an invader
    int rem = rand() % 5; // random column number
    int rem2 = rand()% 4; // random row number
    _fire_x = army[rem2][rem].get_x_pos(); // get position for random choices
    _fire_y = army[rem2][rem].get_y_pos(); // used to set bullet start location
}

void Army::check_col(int x, int y)
{  
    
    for (int i = 0; i < 4; i++) { // take 2 positions
        for (int n = 0; n < 4; n++) { // return match after iterating through army
            int tempX = army[n][i].get_x_pos();
            int tempY = army[n][i].get_y_pos();
            
            if (x == tempX && y == tempY) {
                _x_col = true;
                _y_col = true;
                
                army[n][i].hit();
                _sprite_pass = army[n][i].get_sprite();
                }
            }
    }
}