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-22
Revision:
50:f538885a788b
Parent:
43:df52eec1a127
Child:
54:095eae44895b

File content as of revision 50:f538885a788b:

///////// pre-processor directives ////////
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "SpaceInvadersEngine.h"
#include "Alien.h"
#include "bullet.h"
#include "space_ship.h"

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

// structs //
struct UserInput {
    Direction d;
    float mag;
   
};

 
 Direction _d2;

int space_ship_width;
int space_ship_height;
int bullet_size;
int Alien_size;
int speed;


////////////// objects ///////////////
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad pad;

SpaceInvadersEngine space;


///////////// prototypes ///////////////
void init();
void update_game(UserInput input);
void render();
void welcome();


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,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,1,1,1,1,1,1,1,0,0,0,0},
  {0,1,1,1,1,1,1,1,1,0,0,0},
  {0,1,1,1,1,1,1,1,1,1,1,0},
  {0,1,1,1,1,1,1,1,1,0,0,0},
  {0,1,1,1,1,1,1,1,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},

};


///////////// 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;
    int x_space_ship=0;
    int y_space_ship=40;
    
    
    // game loop - read input, update the game state and render the display
    while (1) {
     
     space.read_input(pad);
        space.update(pad);
       render();
        _d2 = pad.get_direction();
           
        lcd.clear();
        
        lcd.drawSprite(x_Alien,y_Alien,12,10,(int *)Alien);
        lcd.drawSprite(x_bullet,y_bullet,4,10,(int *)bullet);
        lcd.drawSprite(x_space_ship,y_space_ship,10,12,(int *) space_ship);
         
        if (_d2==N){
             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(); 
 space.init(space_ship_width,space_ship_height,bullet_size,Alien_size,speed);
    
}
  
  // this function draws each frame on the LCD
void render(){
    // clear screen, re-draw and refresh
    lcd.clear();  
    space.draw(lcd);
    lcd.refresh();
}

// simple splash screen displayed on start-up
void welcome() {
    
    lcd.printString("SpaceInvaders!",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);
    }
 
 
 
 
}