ELEC2645 (2017/18) / Mbed 2 deprecated el16a2t

Dependencies:   mbed

engine/main.cpp

Committer:
el16a2t
Date:
2018-05-01
Revision:
4:a6ca44e29e97
Parent:
3:124a5eb55c77
Child:
5:cd63b0b896aa

File content as of revision 4:a6ca44e29e97:

/*
ELEC2645 Embedded Systems Project
School of Electronic & Electrical Engineering
University of Leeds
Name: Adam Thompson
Username: el16a2t
Student ID Number: 201047832
Date: 17/4/18
*/
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "sprites.h"
#include "main.h"


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

void init();
void startUp();
void newGame();
void gameOver();


int main()
{

    init();
    startUp();
    newGame();
    gameOver();
}

void init()
{

// initialise the lcd and gamepad
    lcd.init();
    lcd.setContrast(0.4);
    pad.init ();
}


void startUp()
{
    //print start screen
    lcd.drawSprite (1,1,8,20,(int*) titlescreen);
    lcd.printString ("PRESS START", 3,1);
    lcd.refresh();
    while ( pad.check_event(Gamepad::START_PRESSED) == false) {

    }
}

void newGame()
{


//set initial location conditions for the player and car(s) and score
    x_player = 24;
    y_player = 29;

    const int arrayNum[3] = {'4', '24', '44'};
    int RandIndex = rand() % 3;

    x_car = arrayNum[RandIndex];
    y_car = 0;
    


//set score
    score = 0;






//main while loop
    while (1) {

        lcd.clear();


//print the player sprite
        lcd.drawSprite(x_player,y_player,20,15,(int*) player);
//if X pressed, move player left, if B pressed, move player right
        if ( pad.check_event(Gamepad::X_PRESSED) == true) {

            x_player = x_player-20;
        }
//car cannot go further left than the left lane etc
        if (x_player <4) {
            x_player = 4;
        }
        if ( pad.check_event(Gamepad::B_PRESSED) == true) {

            x_player = x_player+20;
        }
        if (x_player >44) {
            x_player = 44;
        }

//print road lines
        lcd.drawRect(1,1,1,1500,FILL_BLACK);

        lcd.drawRect(21,1,1,1500,FILL_BLACK);

        lcd.drawRect(41,1,1,1500,FILL_BLACK);

        lcd.drawRect(61,1,1,1500,FILL_BLACK);




//cars move down the road

        lcd.drawSprite(x_car,y_car,20,15,(int*) car);
        y_car++;
        lcd.drawSprite(x_car2,y_car2,20,15,(int*) car2);
        y_car2++;
        
// re-generate cars continously
        
        if (y_car = 20){
            x_car2 = arrayNum[RandIndex];
            y_car2 = -17;  
  
            
            }
            
        if (y_car2 = 20){
            x_car = arrayNum[RandIndex];
            y_car = -17;  
     
            
            }            

//display score
        score++;

//        difficulty = read_pot();
        sprintf (str, "%d", score);
        lcd.printString(str,64,1);
        
        
//if there is a collision, display "game over" screen
        if (x_car = x_player && y_car = 12){
            printf("collision");
            y_car = 12;
            score=score;
            wait(0.5);
            gameOver();
            
            }








        lcd.refresh();
        wait(0.1);







    }
    
    
}

void gameOver(){

    lcd.clear();
    lcd.printString(str,64,1);
    lcd.printString ("GAME OVER", 3,1);
    lcd.printString ("PRESS START TO TRY AGAIN ", 3,10);
    
    if ( pad.check_event(Gamepad::START_PRESSED) == true) {
        
        newGame();
        
        }