Dmitrijs Griskovs / Mbed 2 deprecated el17dg

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002     ELEC2645 Embedded Systems Project
00003     School of Electronic & Electrical Engineering
00004     University of Leeds
00005     Name: Dmitrijs Griskovs
00006     Username: el17dg
00007     Student ID Number: 201160286
00008     date: start - 25/02/2019
00009 */
00010 
00011 #include "constants.h"
00012 
00013 #include "main.h"
00014 #include "game.h"
00015 #include "menu.h"
00016 #include "models.h"
00017 #include "tutorial.h"
00018 #include "gameobject.h"
00019 #include "settings.h"
00020 
00021 
00022 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
00023 
00024 AnalogIn pot(PTB2);
00025 AnalogIn x_dir(PTB11);
00026 AnalogIn y_dir(PTB10);
00027 AnalogIn rand_y(PTB3);
00028 
00029 Menu menu;
00030 Game game;
00031 Tutorial tutorial;
00032 Settings settings;
00033 Gamepad gamepad;
00034 
00035 GameObject lineOne;
00036 GameObject lineOneStars;
00037 GameObject lineTwo;
00038 GameObject lineTwoShips;
00039 GameObject lineThree;
00040 
00041 void setupIntroValues();
00042 void updateAndDrawIntroPartOne();
00043 void updateAndDrawIntroPartTwo();
00044 void introMusic(int low_frequency_music_counter);
00045 void introPartOneText();
00046 void intro();
00047 void createdByIntro();
00048 void noteToPlayer();
00049 void menuSelection();
00050 
00051 ScreenOption current_screen = ScreenOption_Menu;
00052 
00053 int main(){
00054     lcd.init();
00055     gamepad.init();
00056     #ifdef DEBUGel17dg
00057     printf("Intro starts\n");
00058     #endif 
00059     intro();
00060     createdByIntro();
00061     noteToPlayer();
00062     //Makeing the generated y position for the enemy to be trully random.
00063     srand(rand_y * 1000000);                            
00064     while(1){                                         
00065         lcd.clear();
00066         menuSelection();
00067         lcd.refresh();
00068         wait_ms(1000/fps);
00069     }
00070 }
00071 
00072 void intro(){
00073     int start_game_text_counter = 0;
00074     setupIntroValues();
00075     updateAndDrawIntroPartOne();
00076     updateAndDrawIntroPartTwo();
00077     wait(1);
00078     //Stop just a few pixels above the bottom screen border.
00079     while (!gamepad.check_event(gamepad.START_PRESSED)){
00080         lcd.clear();
00081         introPartOneText();
00082         if (start_game_text_counter >= 2){
00083             lcd.printString("Press START",10,5);
00084             if (start_game_text_counter == 4){
00085                 start_game_text_counter = 0;
00086             }  
00087         }
00088         start_game_text_counter += 1;
00089         lcd.refresh();          
00090     }
00091 }
00092 
00093 void setupIntroValues(){
00094     lineOne.pos.x = -63;                                         // The width of the sprite.    
00095     lineOne.pos.y = 1;                                           // This just will be an intro for the game.//////////////////
00096     lineOneStars.pos.x = screen_width;
00097     lineOneStars.pos.y = 1;
00098     
00099     lineTwo.pos.y = 15;
00100     lineTwo.pos.x = screen_width;
00101     lineTwoShips.pos.x = -46;                 // Starting position outside the screen limits, with the length of the sprite.
00102     lineTwoShips.pos.y = 14;                  // the height of the "The last One" and a few pixels for gaps. 
00103     
00104     lineThree.pos.x = 2;
00105     lineThree.pos.y = screen_height;          // Starting outside the screen limits on the botto - the screen's height + the sprite's height.
00106 }    
00107 
00108 void updateAndDrawIntroPartOne(){
00109     // the width of the line one + 2.
00110     for (int i = 0; i < 65; i++){           
00111         lcd.clear();
00112         lineOne.pos.x +=1;
00113         if (lineOneStars.pos.x > 70){ lineOneStars.pos.x  -= 1; }
00114         // to stop moving at the position of its width.
00115         if (lineTwo.pos.x > screen_width - 30){ lineTwo.pos.x -=1; }
00116         if (lineTwoShips.pos.x < 0){ lineTwoShips.pos.x += 1; }
00117         
00118         introPartOneText();
00119         gamepad.tone(200,2);
00120         
00121         lcd.refresh();
00122         wait(0.01);
00123     }
00124 }
00125 
00126 void updateAndDrawIntroPartTwo(){
00127     int low_frequency_music_counter = 0;
00128     for (int i = 0; i < 19 + 3; i++){
00129         lcd.clear();
00130         lineThree.pos.y -= 1;
00131         introPartOneText();
00132         drawSprite(lineThree.pos, intro_line_three_sprite);
00133         lcd.refresh();
00134         wait(0.1);
00135         introMusic(low_frequency_music_counter);
00136         low_frequency_music_counter++;
00137     } 
00138 }
00139 /**@brief
00140     * I have put the upper part of the intro into a separate function because it
00141     * it is being called several times in this file
00142     */
00143 void introPartOneText(){
00144     drawSprite(lineOne.pos, intro_line_one_sprite);
00145     drawSprite(lineOneStars.pos, intro_line_one_stars_sprite);
00146     drawSprite(lineTwo.pos, intro_line_two_sprite);
00147     drawSprite(lineTwoShips.pos, intro_line_two_ships_sprite); 
00148 }
00149 
00150 void menuSelection(){
00151     if (current_screen == ScreenOption_Game) {
00152         bool game_is_paused = game.updateAndDraw();
00153         if (game_is_paused) { current_screen = ScreenOption_Menu;}
00154         
00155     } if (current_screen == ScreenOption_Tutorial) {
00156         bool back_to_menu = tutorial.updateAndWriteTutorial();  
00157         if (back_to_menu) {
00158             current_screen = ScreenOption_Menu;
00159         }
00160     } if (current_screen == ScreenOption_Settings) {
00161         bool back_to_menu = settings.updateAndWriteSettings();
00162         if (back_to_menu) {
00163             current_screen = ScreenOption_Menu;
00164         }
00165     } else if (current_screen == ScreenOption_Menu) {
00166         bool wantsToChangeScreen = menu.updateAndDraw();
00167         if (wantsToChangeScreen) {
00168             current_screen = menu.getCurrentScreenSelection();
00169         }
00170     }
00171 }
00172 
00173 void introMusic(int low_frequency_music_counter){   
00174     if (low_frequency_music_counter == 0){ gamepad.tone(90,2);}
00175     else if (low_frequency_music_counter == 2){gamepad.tone(60,2);}
00176     else if (low_frequency_music_counter == 4){gamepad.tone(190,2);}
00177     else if (low_frequency_music_counter == 6){gamepad.tone(60,2);}
00178     else if (low_frequency_music_counter == 8){gamepad.tone(90,2);}
00179     else if (low_frequency_music_counter == 10){gamepad.tone(160,2);}
00180     else if (low_frequency_music_counter== 12){
00181         gamepad.tone(90,1);
00182         low_frequency_music_counter = 0;
00183     }
00184 }
00185 void createdByIntro(){
00186     lcd.clear();
00187     lcd.printString("Created and",0,0);
00188     lcd.printString("Developed by",0,1);
00189     lcd.printString("D Griskovs",10,3);
00190     lcd.printString("el17dg",20,4);
00191     lcd.printString("201160286",12,5);
00192     lcd.refresh();
00193     wait(2);
00194 }
00195 
00196 void noteToPlayer(){
00197     lcd.clear();
00198     lcd.printString("Please Read",0,0);
00199     lcd.printString("The TUTORIAL",10,2);
00200     lcd.printString("First!",30,4);
00201     lcd.refresh();
00202     wait(3);
00203 }