Kern Fowler / Mbed 2 deprecated Donkey_Kong_Game

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 
00006 Name: Kern Fowler
00007 Username: el17kjtf
00008 Student ID Number: 201116686
00009 Date: 08/05/2019
00010 Version: 1.4
00011 
00012 */
00013 
00014 // Includes all required classes in their respective libraries.
00015 #include "mbed.h"
00016 #include "N5110.h"
00017 #include "Gamepad.h"
00018 #include "Donkey.h"
00019 #include "Options.h"
00020 #include "Controls.h"
00021 #include "Instructions.h"
00022 #include "HighScores.h"
00023 #include "Banana.h"
00024 #include "Barrel.h" 
00025 #include "GameEngine.h"
00026 
00027 // Define all objects.
00028 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); // LCD class object- pins format required only in define. 
00029 Gamepad pad; // Gamepad class obejct.
00030 Donkey dky; // Donkey Kong class object.
00031 Barrel barrel; // Barrel class object.
00032 Banana banana; // Banana class object.
00033 GameEngine eng; // GameEngine class object.
00034 Options opt; // Options Menu class object.
00035 Controls cont; // Controls Menu class object.
00036 Instructions instr; // Instructions Menu class object.
00037 HighScores high; // HighScores Menu class object.
00038 Serial pc(USBTX, USBRX); // Used for testing.
00039 
00040 // Prototypes
00041 void init(); // 
00042 void print_menu();
00043 void welcome();
00044 void arrow_location();
00045 void arrow_select();
00046 
00047 // Intialise Menu Variables
00048 int fps = 24;
00049 int direction;
00050 int menu_option_pos = 0;
00051 int arrow_pos = 0;
00052 
00053 int menu_arrow[7][7] = { // Arrow pointer sprite used in main menu.
00054 {0,0,0,0,0,0,0,},
00055 {0,0,0,0,1,0,0,},
00056 {0,0,0,0,1,1,0,},
00057 {1,1,1,1,1,1,1,},
00058 {0,0,0,0,1,1,0,},
00059 {0,0,0,0,1,0,0,},
00060 {0,0,0,0,0,0,0,},
00061 };
00062 
00063 int menu_dk_face[36][34] =   { // Donkey Kong face sprite used in opening scene.
00064     { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0 },
00065     { 0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0 },
00066     { 0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0 },
00067     { 0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,1,0,0,0,0,0 },
00068     { 0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,1,0,0,0,0,0 },
00069     { 0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0 },
00070     { 0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,1,1,1,0,0,0,0,0 },
00071     { 0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,1,1,0,0,0,0 },
00072     { 0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,1,0,0,0 },
00073     { 0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0 },
00074     { 0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0 },
00075     { 0,0,0,1,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1,0,0 },
00076     { 0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,0 },
00077     { 0,1,1,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,1,0,1,1,0 },
00078     { 0,1,0,0,0,1,0,1,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,1,0,1,0,0,0,1,0 },
00079     { 0,1,0,0,0,1,0,0,1,0,0,1,0,1,1,1,0,1,1,1,0,1,0,0,0,1,0,0,1,0,0,0,1,0 },
00080     { 0,1,0,1,0,1,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,1,0,1,0,1,0 },
00081     { 0,1,0,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,1,1,0,0,1,0,0,1,0,0,1,0,1,0,1,0 },
00082     { 0,1,0,1,1,1,1,0,1,0,1,0,0,1,1,0,1,0,1,1,0,0,1,0,0,1,0,1,1,1,1,0,1,0 },
00083     { 0,1,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,1,0 },
00084     { 0,0,1,0,0,1,1,1,0,0,0,1,1,1,1,1,0,1,1,1,1,1,0,0,0,0,1,1,1,0,0,1,0,0 },
00085     { 0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0 },
00086     { 0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0 },
00087     { 0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0 },
00088     { 0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0 },
00089     { 0,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,0 },
00090     { 0,1,0,0,0,0,1,0,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,0,1,0,0,0,0,1,0 },
00091     { 0,1,0,0,0,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,1,0 },
00092     { 0,0,1,0,0,0,0,0,1,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,0,0,1,0,0 },
00093     { 0,0,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,0,0 },
00094     { 0,0,0,0,1,1,0,0,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,1,0,0,0,0,1,1,0,0,0,0 },
00095     { 0,0,0,0,0,1,1,0,0,0,0,1,1,1,0,0,1,0,0,1,0,1,1,0,0,0,0,1,1,0,0,0,0,0 },
00096     { 0,0,0,0,0,0,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,0,0,0,0,0,0 },
00097     { 0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0 },
00098     { 0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0 },
00099     { 0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0 },
00100     };
00101 
00102 // Functions
00103 int main() {
00104     init();  // Initialises the LCD and Gamepad.
00105     welcome();  // Displays welcome screen, has information about the creator and title screen.
00106     while(1) {  // Main menu loop
00107     arrow_location(); // Determines arrow position on screen, will go up and down with joystick commands.
00108     print_menu();  // This re-prints the menu at the start of every loop.
00109     arrow_select(); // Will select the current menu options when A button pressed.
00110     wait_ms(1.0f/fps);
00111     }
00112 }
00113 
00114 // Initialises Gamepad and LCD.
00115 void init() {
00116     lcd.init();
00117     pad.init();
00118     wait(2.5);
00119 }
00120 
00121 // Moves arrow sprite up or down depending on joystick commands. If reaches the bottom will go back to top position.
00122 void arrow_location() {
00123     direction  = pad.get_direction();
00124     if (direction == N) { // Will occur if the joystick is pushed up.
00125         menu_option_pos = menu_option_pos - 1;
00126         // printf("North Pressed");
00127         wait_ms(250);
00128     }
00129     if (direction == S) { // Will occur if the joystick is pushed down.
00130         menu_option_pos = menu_option_pos + 1;
00131         // printf("South Pressed");
00132         wait_ms(250);
00133     }
00134     if (menu_option_pos > 4) {
00135         menu_option_pos = 0;
00136     }
00137     if (menu_option_pos < 0) {
00138         menu_option_pos = 4;
00139     }
00140     arrow_pos = 8 + (menu_option_pos * 8); // Sets and offset due to sprite and font size.
00141     // printf("Option Num = %d", menu_option_pos)
00142 }
00143 
00144 // Runs the current menu option the arrow has selected when A is pressed.
00145 void arrow_select() {
00146     if (pad.check_event(Gamepad::A_PRESSED) == true) {
00147         if (menu_option_pos == 0) { // Start Game option.
00148             // printf("GameEngine");
00149             eng.gameengine_run(pad, lcd, barrel, banana, dky, opt); // Runs the main game loop in the GameEngine class.
00150             eng.gameengine_score(pad, lcd, banana, high); // When gameover is reached, runs the end game screen.
00151         }
00152         if (menu_option_pos == 1) { // Controls option.
00153             // printf("Controls");
00154             cont.controls_run(pad, lcd); // Runs the controls screen loop.
00155         }
00156         if (menu_option_pos == 2) { // Instructions option.
00157             // printf("Instructions");
00158             instr.instructions_run(pad, lcd); // Runs the instructions screen loop.
00159         }
00160         if (menu_option_pos == 3) { // Options option.
00161             // printf("Options");
00162             opt.options_run(pad, lcd); // Runs the options screen loop.
00163         }
00164         if (menu_option_pos == 4) { // High Scores option.
00165             // printf("High Scores");
00166             high.highscores_run(pad, lcd, banana); // Runs the highscores screen loop.
00167         }
00168     }
00169 }
00170 
00171 // Prints the main menu, with options and the selector arrow.
00172 void print_menu() {
00173     lcd.clear();
00174     lcd.printString("Main Menu",19,0);
00175     lcd.printString("Start Game",8,1);
00176     lcd.printString("Controls",8,2);
00177     lcd.printString("Instructions",8,3);
00178     lcd.printString("Options",8,4);
00179     lcd.printString("High Scores",8,5);
00180     lcd.drawSprite(0,arrow_pos,7,7,(int *)menu_arrow);
00181     lcd.refresh();
00182 }
00183  
00184 // Prints basic information about the game, and plays title screen.
00185 void welcome() {
00186     lcd.clear();
00187     lcd.drawSprite(24,0,36,34,(int *)menu_dk_face); // Donkey Kong sprite for main menu
00188     lcd.printString("  Donkey Kong",0,5); // Game title.
00189     lcd.refresh();
00190     wait(4.0); 
00191     lcd.clear();
00192     lcd.printString("  Created",0,0); // Information about creator.
00193     lcd.printString("  By",0,1);
00194     lcd.printString("  Kern Fowler",0,3);
00195     lcd.printString("  201116686",0,4);
00196     lcd.refresh();
00197     wait(4.0);
00198 }