Initial publish

Dependencies:   mbed

Fork of el17dg by Dmitrijs Griskovs

main/main.cpp

Committer:
Noximilien
Date:
2019-03-26
Revision:
20:557e84189a57
Parent:
19:b78fa41d04a9
Child:
21:0eb394495b8a

File content as of revision 20:557e84189a57:

/*
ELEC2645 Embedded Systems Project
School of Electronic & Electrical Engineering
University of Leeds
Name: Dmitrijs Griskovs
Username: el17dg
Student ID Number: 201160286
Date: start - 25/02/2019
*/


#include "main.h"
#include "game.h"
#include "menu.h"
#include "models.h"



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

Gamepad gamepad;

AnalogIn pot(PTB2);
AnalogIn x_dir(PTB11);
AnalogIn y_dir(PTB10);
AnalogIn rand_y(PTB3);

Menu menu;
Game game;

struct IntroMove {
    int x;
    int y;
};

IntroMove lineOne;
IntroMove lineOneStars;
IntroMove lineTwo;
IntroMove lineTwoShips;
IntroMove lineThree;


//static const unsigned int MAX_LINE_LENGTH = 14;


void intro();
void pointer(int x, int y);
void pointer_position(int menu_number);
void ship_movement();
//char line_buffer[MAX_LINE_LENGTH];



ScreenOption current_screen = ScreenOption_Menu;

void intro();

int main(){
    lcd.init();
    gamepad.init();
    //gamepad.leds_on();
    //gamepad.led(1,1.0);
    
    intro();
    srand(rand_y * 1000000);                            //Makeing the generated y position for the enemy to be trully random.
    
    while(1){                                           //Waiting for the option "start game" to be selected and for the button B to be pressed
        lcd.clear();
        
        if (current_screen == ScreenOption_Game) {
            bool game_is_paused = game.updateAndDraw();
            
            if (game_is_paused) {
                current_screen = ScreenOption_Menu;
            }
        } 
        else if (current_screen == ScreenOption_Menu) {
            bool wantsToChangeScreen = menu.updateAndDraw();
            if (wantsToChangeScreen) {
                current_screen = menu.getCurrentScreenSelection();
            }
        }
        bool game_over = game.checkGameOver();
        if (game_over){
            lcd.clear();   
            lcd.printString("GameOver",0,1);
            lcd.refresh();
            wait(1);
            lcd.printString("Press Y",0,4);
            lcd.printString("to restart",0,5);
            lcd.refresh();
            bool led_state = false;
            while (!gamepad.check_event(gamepad.Y_PRESSED)){//////////////////////////////
                gamepad.led(1,(float)led_state);
                gamepad.led(2,(float)!led_state);
                gamepad.led(3,(float)led_state);
                gamepad.led(4,(float)!led_state);
                gamepad.led(5,(float)led_state);
                gamepad.led(6,(float)!led_state);
                wait(0.5);
                led_state = !led_state;
            }
            //game.gameValuesInit();
            current_screen = ScreenOption_Menu;
        }
        
        lcd.refresh();
        wait_ms(1000/fps);
    }
}

void intro(){
    lineOne.x = -63;                                         // The width of the sprite.    
    lineOne.y = 1;                                           // This just will be an intro for the game.//////////////////
    lineOneStars.x = screen_width;
    lineOneStars.y = 1;
    
    lineTwo.y = 15;
    lineTwo.x = screen_width;
    lineTwoShips.x = -46;                                   // Starting position outside the screen limits, with the length of the sprite.
    lineTwoShips.y = 14;                                    // the height of the "The last One" and a few pixels for gaps. 
    
    lineThree.x = 2;
    lineThree.y = screen_height;                       // Starting outside the screen limits on the botto - the screen's height + the sprite's height.
    
    // the width of the lione one + 2.
    for (int i = 0; i < 65; i++){           
        lcd.clear();
        lineOne.x +=1;
        if (lineOneStars.x > 70){
            lineOneStars.x  -= 1; 
        }
        // to stop moving at the position of its width.
        if (lineTwo.x > screen_width - 30){//
          lineTwo.x -=1;          
        }
        if ( lineTwoShips.x < 0){
            lineTwoShips.x += 1;
        }
        
        lcd.drawSprite(lineOne.x, lineOne.y, 11, 63, (int*)introLineOne);
        lcd.drawSprite(lineOneStars.x, lineOneStars.y, 13, 12, (int*)introLineOneStars);
        lcd.drawSprite(lineTwo.x, lineTwo.y, 11, 30, (int*)introLineTwo);
        lcd.drawSprite(lineTwoShips.x, lineTwoShips.y, 10, 46, (int*)introLineTwoShips);
        
        lcd.refresh();
        wait(0.01);
    }
    
    //Stop just a few pixels above the bottom screen border.
    for (int i = 0; i < 19 + 3; i++){
        lcd.clear();
        
        lcd.drawSprite(lineOne.x, lineOne.y, 11, 63, (int*)introLineOne);
        lcd.drawSprite(lineOneStars.x, lineOneStars.y, 13, 12, (int*)introLineOneStars);
        lcd.drawSprite(lineTwo.x, lineTwo.y, 11, 30, (int*)introLineTwo);
        lcd.drawSprite(lineTwoShips.x, lineTwoShips.y, 10, 46, (int*)introLineTwoShips);
        
        lcd.drawSprite(lineThree.x, lineThree.y - i, 19, 78, (int*)introLineThree);
        
        lcd.refresh();
        wait(0.1);
    }
    while (!gamepad.check_event(gamepad.START_PRESSED)){   
    }
    /*lcd.printString("  StarBarians",1,1);
    lcd.refresh();
    wait(0.5);
    lcd.printString(" And the quest ",1,2);
    lcd.printString(" of the Princess Spacecate",1,3);
    lcd.printString(" Spacecate",1,4);*/
    
}