Laila Al Badwawi 200906179 SpaceInvaders I declare this my own independent work and understand the university rules on plagiarism.

Dependencies:   mbed

main.cpp

Committer:
fy14lkaa
Date:
2019-04-12
Revision:
4:13cc179c11c5
Parent:
3:985fab783b92
Child:
5:476696df08dd

File content as of revision 4:13cc179c11c5:

///////// pre-processor directives ////////
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"

#ifdef WITH_TESTING
# include "tests.h"
#endif

#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(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad pad;
//Direction _d;
//SpaceEngine space;
///////////// prototypes ///////////////
void init();
void update_game(UserInput input);
void render();
void welcome();
const int run[12][10] =   {
    { 0,0,0,0,0,1,1,1,0,0 },
    { 0,0,0,0,0,1,1,1,0,0 },
    { 0,0,0,1,1,1,1,0,0,0 },
    { 0,0,1,1,1,1,0,1,0,0 },
    { 0,1,1,0,1,1,0,1,1,1 },
    { 1,0,0,0,1,1,0,0,0,0 },
    { 0,0,0,1,1,1,1,0,0,0 },
    { 0,0,1,1,0,0,1,1,0,0 },
    { 1,1,1,1,0,0,0,1,1,0 },
    { 0,0,0,0,0,0,1,1,0,0 },
    { 0,0,0,0,0,1,1,0,0,0 },
    { 0,0,0,0,1,1,0,0,0,0 },
};
const int bullet[4][10] =   {
    { 0,0,0,0,0,1,1,1,0,0 },
    { 0,0,0,0,0,1,1,1,0,0 },
    { 0,0,0,1,1,1,1,0,0,0 },
    { 0,0,1,1,1,1,0,1,0,0 },
};
const int alien[12][10] =   {
    { 0,0,0,0,0,1,1,1,0,0 },
    { 0,0,0,0,0,1,1,1,0,0 },
    { 0,0,0,1,1,1,1,0,0,0 },
    { 0,0,1,1,1,1,0,1,0,0 },
    { 0,0,1,1,1,1,0,1,0,0 },
    { 0,0,1,1,1,1,0,1,0,0 },
    { 0,0,1,1,1,1,0,1,0,0 },
    { 0,0,1,1,1,1,0,1,0,0 },
    { 0,0,1,1,1,1,0,1,0,0 },
    { 0,0,1,1,1,1,0,1,0,0 },
    { 0,0,1,1,1,1,0,1,0,0 },
    { 0,0,1,1,1,1,0,1,0,0 },

};
/* 
const int space_ship[10][12]={
    {0,0,0,0,0,0,0,1,0,0,0,0},
    {0,0,0,0,0,0,0,1,0,0,0,0},
    {0,0,0,0,0,0,0,1,0,0,0,0},
    {0,0,0,0,0,0,0,1,0,0,0,0},
    {0,0,0,0,0,0,0,1,0,0,0,0},
    {0,0,0,0,0,0,0,1,0,0,0,0},
    {0,0,0,0,0,0,0,1,0,0,0,0},
    {0,0,0,0,0,0,0,1,0,0,0,0},
    {0,0,0,0,0,0,0,1,0,0,0,0},
    {0,0,0,0,0,0,0,1,0,0,0,0},
   
    
    };
*/
Direction _d2;
///////////// functions ////////////////
int main()
{
    /*
#ifdef WITH_TESTING
    int number_of_failures = run_all_tests();

    if(number_of_failures > 0) return number_of_failures;
#endif
*/
    int fps = 8;  // frames per second
    
    init();     // initialise and then display welcome screen...
    welcome();  // waiting for the user to start
    
    //render();  // first draw the initial frame 
    wait(1.0f/fps);  // and wait for one frame period
    
    int y=0;
    
    //define the x position of bullet from the space ship
    int x_bullet=0;
    
    //define the y position of the bullet from the space ship
    int y_bullet=0;
    int y_alien=10;
    int x_alien=70;
    int bullet_fired=0;
    // game loop - read input, update the game state and render the display
    while (1) {
        lcd.clear();
        lcd.drawSprite(0,y,12,10,(int *)run);
        
        lcd.drawSprite(x_alien,y_alien,12,10,(int *)alien);
        lcd.drawSprite(x_bullet,y_bullet,4,10,(int *)bullet);
        //space.read_input(pad);
        _d2 = pad.get_direction();
        if(_d2==N)
        //pong.update(pad);
        //render();
        y=y-2;
        else
        if(_d2==S)
        y=y+2;
        else if(_d2==E){
        bullet_fired=1;
        x_bullet=11;
        y_bullet=y;
        }
        if(y>=40)
        y=40;
        else
        if(y<=0)
        y=0;
        
        if(bullet_fired==1)
            x_bullet+=4;
            if(x_bullet>=x_alien && y_bullet >=y_alien && y_bullet <=y_alien+10)
                y=0;
        wait(1.0f/fps);
        
        lcd.refresh();
    }
}

// initialies all classes and libraries
void init()
{
    // need to initialise LCD and Gamepad 
    lcd.init();
    pad.init();
     
    // initialise the game with correct ball and paddle sizes
    //pong.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_SIZE,BALL_SPEED);

}

// this function draws each frame on the LCD
void render()
{
    // clear screen, re-draw and refresh
    lcd.clear();  
    lcd.refresh();
}

// simple splash screen displayed on start-up
void welcome() {
    
    lcd.printString("Space Invador!",0,1);  
    lcd.printString("  Press Start ",0,4);
    
    lcd.refresh();
     
    // wait flashing LEDs until start button is pressed 
    while ( pad.check_event(Gamepad::START_PRESSED) == false) {
        pad.leds_on();
        wait(0.1); //wait 0.1 seconds 
        pad.leds_off();
        wait(0.1);
    }
 
}