Kostadin Chakarov / Mbed 2 deprecated el17kec

Dependencies:   mbed

main.cpp

Committer:
kocemax
Date:
2019-04-10
Revision:
7:cd3cafda3dd4
Parent:
6:39bda45efeed
Child:
8:9b77eea95088

File content as of revision 7:cd3cafda3dd4:

/*
ELEC2645 Embedded Systems Project
School of Electronic & Electrical Engineering University of Leeds
Name: Kostadin Chakarov
Username: el17kec
Student ID Number:201171978 Date:11/03/2019
*/

// Breakout logo sprite
const int breakout[10][75] =   {
    { 1,0,0,1,1,0,0,0,0,0,1,0,0,1,1,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0 },
    { 1,0,1,0,0,1,0,0,1,0,1,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,1,1,1,0,0,1,0,1,1,1,1,1,1,1,1,1 },
    { 1,1,0,0,0,0,1,0,0,1,1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,1,1,1,0,0,1,0,0,0,0,0,1,0,0,0,0 },
    { 1,1,0,1,1,1,1,0,0,0,1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0 },
    { 1,0,1,0,0,1,1,0,0,0,1,1,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,1,0 },
    { 1,1,0,1,1,0,0,1,0,0,1,0,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,1,0,1,1,1,0,1,0,1,1,1 },
    { 1,1,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,1,1,1,1,1,1,1,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,1,0 },
    { 1,1,0,0,0,0,0,1,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,1,1,1,0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0 },
    { 1,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,1,1,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0 },
    { 1,0,0,1,1,1,0,0,0,0,1,0,0,0,0,0,1,0,1,1,1,1,1,1,1,1,0,1,0,0,0,1,0,0,0,1,0,1,0,0,0,0,0,0,1,0,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0 },
};

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

const int fps = 10; //sets the fps of the game doesn't change

/////////////// objects ///////////////
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);  // K64F - pwr from 3V3
Gamepad pad; // Gamepad buttons
PlayerControl cont; // Object for player
Ball ball;
Map map;

///////////// function prototypes ///////////////
void init();
void welcome();
void render();

///////////// functions ////////////////
int main() 
{   
    init();     //initialise LCD and Gamepad
    welcome();  // waiting for the user to start
    render();   //game loop
}

void init() 
{
  lcd.init();
  pad.init();
  lcd.setContrast(0.55); // setting contrast to 55% seems good
  lcd.normalMode();      // normal colour mode
  lcd.setBrightness(0.5); // put LED backlight on 50%
}   

void welcome() 
{
    lcd.printString("Welcome to",12,0);
    // x origin, y origin, rows, cols, sprite
    lcd.drawSprite(5,15,10,75,(int *)breakout);   
    lcd.printString("Press Start",9,4);
    lcd.refresh();   
    
    // flash the LEDs until start button is pressed 
    while (pad.check_event(Gamepad::START_PRESSED) == false) 
    {
        for (int i = 1; i < 7; i++) 
        {
            pad.led(i,1);
            wait(0.1);
            pad.led(i,0);
        }
    }
}


void resetGame()
{
    ball.reset();
    cont.reset();
    map.reset();
}

void checkLost()
{
    Vector2D posPad = cont.get_padPos();
    if (ball.getPos().y > posPad.y) 
    {
        while (1) 
        {
            lcd.clear();
            lcd.printString("You Lost",17,2);
            lcd.printString("Press A",20,3);
            lcd.printString("to restart",12,4);
            wait(0.1);
            lcd.refresh();
            if (pad.check_event(Gamepad::A_PRESSED) == true)
            {
                resetGame();
                break;           
            }
        }
    }
}

void checkWon() {
    if (map.checkLevel()) {
        // we have cleared a level
        ball.reset();
        cont.reset();
        
        if (map.hasWon()) {
             while (1) 
            {
                lcd.clear();
                lcd.printString("You WON!",17,2);
                lcd.printString("Press A",20,3);
                lcd.printString("to restart",12,4);
                wait(0.1);
                lcd.refresh();
                if (pad.check_event(Gamepad::A_PRESSED) == true)
                {
                    resetGame();
                    break;           
                }
            }
        }
    }
}

void render() 
{
    map.initBricks();
    
    while(1) 
    {   
        // physics, input + game logic
        cont.controlPlayer(pad); 
        cont.get_padPos(); 
        ball.getPos();
        ball.move();
        ball.hitPad(cont);
        map.checkCollision(ball);
        checkLost();
        checkWon();
        
        // rendering
        lcd.clear(); // initialise the LCD
        map.drawMap(lcd);
        cont.drawPlayer(lcd);
        ball.draw(lcd);
        lcd.refresh();
        
        wait(1.0f/fps);
    }
}