ELEC2645 (2018/19) / Mbed 2 deprecated el17mtu_

Dependencies:   mbed

ModeA/ModeA.cpp

Committer:
el17mtu
Date:
2019-05-09
Revision:
18:59befe1eaa56
Parent:
16:7b179005cd91
Child:
19:5e3ee84d9233

File content as of revision 18:59befe1eaa56:

#include "ModeA.h"

ModeA::ModeA() //constructor
{

}

ModeA::~ModeA() //destructor
{

}

//initialise all values 
void ModeA::initialise(N5110 &lcd)
{
    x_position = 12; //x coordinate of square
    y_position = 18; //y coordinate of square
    speed = 1; // speed of square 
    gravity = 1; //gravitational force 

    screen_width = 70; // pixel at which bar begins 
    bar_width = 10; // width of the bar

    bar_speed = 2; // spped of the bar
    score = 0; // score


    srand(time(NULL));
    size_top = rand() % 15; //15 random lengths of the top bar
    srand(time(NULL));
    size_bottom = rand() % 15; //15 random length of the bottom bar


}

//game function 
void ModeA::Game(N5110 &lcd)

{

    char buffer[14];
    sprintf(buffer,"%2d",score);
    lcd.printString(buffer,70,0);
    lcd.drawRect(x_position, y_position,6,6,FILL_BLACK);
    lcd.refresh();



    if ( pad.check_event(Gamepad::Y_PRESSED) == true) {

        speed = speed - gravity*5;

    }

    if (y_position > 44) {

        lcd.clear();
        lcd.printString("GAME OVER",6,2);
        lcd.printString("Press BACK",6,4);
        pad.tone(2000.0,0.3);
        lcd.refresh();
        bar_speed = 0;
        speed = 0;
        gravity = 0;
        y_position = 48;
        bar_width = 0;
        size_top = 0;
        size_bottom = 0;

        wait(0.5);

    }



    if (y_position < 0) {
        y_position = 0;
        speed = 0;
    }

    speed = speed + gravity;
    y_position = y_position + speed;
    wait(0.1);


    lcd.drawRect(screen_width,0,bar_width,size_top,FILL_BLACK);
    lcd.drawRect(screen_width,48-size_bottom,bar_width,size_bottom,FILL_BLACK);
    lcd.refresh();
  

    if ((screen_width == x_position) && (size_top < y_position)) {
        score = score + 1;
        lcd.clear();
        sprintf(buffer,"%2d",score);
        lcd.printString("Reset",15,2);
        lcd.refresh();
        wait(1.0);
        
      
        
 }


    if ( pad.check_event(Gamepad::X_PRESSED) == true) {

        srand(time(NULL));
        size_top = rand() % 15;
        srand(time(NULL));
        size_bottom = rand() % 15;

    }

    if ((screen_width ==  x_position)&& (size_top >  y_position))  {

        lcd.clear();
        lcd.printString("GAME OVER",6,2);
        lcd.printString("Press BACK",6,4);
        pad.tone(2000.0,0.3);
        lcd.refresh();
        bar_speed = 0;
        speed = 0;
        gravity = 0;
        y_position = 48;
        bar_width = 0;
        size_top = 0;
        size_bottom = 0;



        wait(0.5);

    }
    
       
  //  if (score == 2) {
    //    lcd.clear();
    //    lcd.printString("Press Reset",6,2);
     //   }


    screen_width = screen_width - bar_speed;
    wait(0.1);



}