ELEC2645 (2018/19) / Mbed 2 deprecated el17dg

Dependencies:   mbed

Fork of el17dg by Dmitrijs Griskovs

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     DG_PRINTF("Intro starts\n");
00057     intro();
00058     createdByIntro();
00059     noteToPlayer();
00060     //Makeing the generated y position for the enemy to be trully random.
00061     srand(rand_y * 1000000);
00062     gamepad.check_event(gamepad.A_PRESSED);                            
00063     while(1){                                         
00064         lcd.clear();
00065         menuSelection();
00066         lcd.refresh();
00067         wait_ms(1000/fps);
00068     }
00069 }
00070 
00071 void intro(){
00072     int start_game_text_counter = 0;
00073     setupIntroValues();
00074     updateAndDrawIntroPartOne();
00075     updateAndDrawIntroPartTwo();
00076     wait(1);
00077     //Stop just a few pixels above the bottom screen border.
00078     while (!gamepad.check_event(gamepad.START_PRESSED)){
00079         lcd.clear();
00080         introPartOneText();
00081         if (start_game_text_counter >= 2){
00082             lcd.printString("Press START",10,5);
00083             if (start_game_text_counter == 4){
00084                 start_game_text_counter = 0;
00085             }  
00086         }
00087         start_game_text_counter += 1;
00088         lcd.refresh();          
00089     }
00090 }
00091 
00092 void setupIntroValues(){
00093     lineOne.pos.x = -63;                                         // The width of the sprite.    
00094     lineOne.pos.y = 1;                                           // This just will be an intro for the game.//////////////////
00095     lineOneStars.pos.x = screen_width;
00096     lineOneStars.pos.y = 1;
00097     
00098     lineTwo.pos.y = 15;
00099     lineTwo.pos.x = screen_width;
00100     lineTwoShips.pos.x = -46;                 // Starting position outside the screen limits, with the length of the sprite.
00101     lineTwoShips.pos.y = 14;                  // the height of the "The last One" and a few pixels for gaps. 
00102     
00103     lineThree.pos.x = 2;
00104     lineThree.pos.y = screen_height;          // Starting outside the screen limits on the botto - the screen's height + the sprite's height.
00105 }    
00106 
00107 void updateAndDrawIntroPartOne(){
00108     // the width of the line one + 2.
00109     for (int i = 0; i < 65; i++){           
00110         lcd.clear();
00111         lineOne.pos.x +=1;
00112         if (lineOneStars.pos.x > 70){ lineOneStars.pos.x  -= 1; }
00113         // to stop moving at the position of its width.
00114         if (lineTwo.pos.x > screen_width - 30){ lineTwo.pos.x -=1; }
00115         if (lineTwoShips.pos.x < 0){ lineTwoShips.pos.x += 1; }
00116         
00117         introPartOneText();
00118         gamepad.tone(200,2);
00119         
00120         lcd.refresh();
00121         wait(0.01);
00122     }
00123 }
00124 
00125 void updateAndDrawIntroPartTwo(){
00126     int low_frequency_music_counter = 0;
00127     for (int i = 0; i < 19 + 3; i++){
00128         lcd.clear();
00129         lineThree.pos.y -= 1;
00130         introPartOneText();
00131         drawSprite(lineThree.pos, intro_line_three_sprite);
00132         lcd.refresh();
00133         wait(0.1);
00134         introMusic(low_frequency_music_counter);
00135         low_frequency_music_counter++;
00136     } 
00137 }
00138 /**@brief
00139     * I have put the upper part of the intro into a separate function because it
00140     * it is being called several times in this file
00141     */
00142 void introPartOneText(){
00143     drawSprite(lineOne.pos, intro_line_one_sprite);
00144     drawSprite(lineOneStars.pos, intro_line_one_stars_sprite);
00145     drawSprite(lineTwo.pos, intro_line_two_sprite);
00146     drawSprite(lineTwoShips.pos, intro_line_two_ships_sprite); 
00147 }
00148 
00149 void menuSelection(){
00150     if (current_screen == ScreenOption_Game) {
00151         bool game_is_paused = game.updateAndDraw();
00152         if (game_is_paused) { current_screen = ScreenOption_Menu;}
00153         
00154     } if (current_screen == ScreenOption_Tutorial) {
00155         bool back_to_menu = tutorial.updateAndWriteTutorial();  
00156         if (back_to_menu) {
00157             current_screen = ScreenOption_Menu;
00158         }
00159     } if (current_screen == ScreenOption_Settings) {
00160         bool back_to_menu = settings.updateAndWriteSettings();
00161         if (back_to_menu) {
00162             current_screen = ScreenOption_Menu;
00163         }
00164     } else if (current_screen == ScreenOption_Menu) {
00165         bool wantsToChangeScreen = menu.updateAndDraw();
00166         if (wantsToChangeScreen) {
00167             current_screen = menu.getCurrentScreenSelection();
00168         }
00169     }
00170 }
00171 
00172 void introMusic(int low_frequency_music_counter){   
00173     if (low_frequency_music_counter == 0){ gamepad.tone(90,2);}
00174     else if (low_frequency_music_counter == 2){gamepad.tone(60,2);}
00175     else if (low_frequency_music_counter == 4){gamepad.tone(190,2);}
00176     else if (low_frequency_music_counter == 6){gamepad.tone(60,2);}
00177     else if (low_frequency_music_counter == 8){gamepad.tone(90,2);}
00178     else if (low_frequency_music_counter == 10){gamepad.tone(160,2);}
00179     else if (low_frequency_music_counter== 12){
00180         gamepad.tone(90,1);
00181         low_frequency_music_counter = 0;
00182     }
00183 }
00184 void createdByIntro(){
00185     lcd.clear();
00186     lcd.printString("Created and",0,0);
00187     lcd.printString("Developed by",0,1);
00188     lcd.printString("D Griskovs",10,3);
00189     lcd.printString("el17dg",20,4);
00190     lcd.printString("201160286",12,5);
00191     lcd.refresh();
00192     wait(2);
00193 }
00194 
00195 void noteToPlayer(){
00196     lcd.clear();
00197     lcd.printString("Please Read",0,0);
00198     lcd.printString("The TUTORIAL",10,2);
00199     lcd.printString("First!",30,4);
00200     lcd.refresh();
00201     wait(3);
00202 }