Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 /* 00002 ELEC2645 Embedded Systems Project 00003 School of Electronic & Electrical Engineering 00004 University of Leeds 00005 Name: Andrew Moore 00006 Username: el16ajm 00007 Student ID Number: 201042893 00008 Date: 00009 */ 00010 00011 /** The Main class 00012 * @brief Sends and recives data from the Menu and Engine classes, alters the refresh rate and calls all drawing from other classes 00013 * @author Andrew J. Moore 00014 * @date May, 2018 00015 */ 00016 00017 #include "mbed.h" 00018 #include "Gamepad.h" 00019 #include "N5110.h" 00020 #include "Engine.h" 00021 #include "Menu.h" 00022 00023 /////////////// structs ///////////////// 00024 struct UserInput { 00025 Direction d; 00026 }; 00027 00028 /////////////// objects /////////////// 00029 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); 00030 Gamepad pad; 00031 Engine gameEngine; 00032 Menu mainMenu; 00033 00034 ///////////// prototypes /////////////// 00035 /** Initialization function */ 00036 void init(); 00037 00038 /** Initialization function */ 00039 void update_game(UserInput input); 00040 00041 /** Draws the game onto the LCD */ 00042 void renderGame(); 00043 00044 /** Draws the menu onto the LCD */ 00045 void renderMenu(); 00046 00047 /** Draws the welcome screen onto the LCD */ 00048 void welcome(); 00049 00050 /** Draws the transition screen onto the LCD */ 00051 void transition(); 00052 00053 /** Resets game and menu logic once a game over state has been reached */ 00054 void reset(); 00055 00056 /** Sets the difficulty/refresh rate */ 00057 float setDif(); 00058 00059 ///////////// functions //////////////// 00060 int main() 00061 { 00062 printf("Loaded\n"); 00063 init(); 00064 welcome(); 00065 00066 transition(); 00067 00068 // game loop - read input, update the game state and render the display 00069 while (1) { 00070 if (!mainMenu.started()) { //menu logic and rendering 00071 mainMenu.read_input(pad); 00072 mainMenu.update(); 00073 gameEngine.setLvl(mainMenu.getLvl()); 00074 renderMenu(); 00075 } else if (gameEngine.getGameOver()) { //resets the game once a game over state has been reached 00076 gameEngine.gameOverScreen(lcd); 00077 reset(); 00078 } else { //game logic and rendering 00079 gameEngine.read_input(pad); 00080 gameEngine.update(pad); 00081 renderGame(); 00082 } 00083 } 00084 } 00085 00086 void welcome() 00087 { 00088 printf("Welcome Screen\n"); 00089 00090 lcd.printString(" Snake! ",0,1); 00091 lcd.printString(" Press Start ",0,4); 00092 lcd.refresh(); 00093 00094 // wait flashing LEDs until start button is pressed 00095 while ( pad.check_event(Gamepad::START_PRESSED) == false) { 00096 pad.leds_on(); 00097 wait(0.1); 00098 pad.leds_off(); 00099 wait(0.1); 00100 } 00101 wait(0.1); 00102 00103 } 00104 00105 void init() 00106 { 00107 // need to initialise LCD and Gamepad 00108 gameEngine.init(); 00109 lcd.init(); 00110 pad.init(); 00111 mainMenu.init(); 00112 00113 printf("Initialised\n"); 00114 00115 } 00116 00117 void reset() 00118 { 00119 mainMenu.init(); //resets both the mainMenu and gameEngine 00120 gameEngine.init(); //the gameEngine will reset the food and the snake in the process 00121 00122 printf("Reset\n"); 00123 } 00124 00125 void renderGame() 00126 { 00127 // clear screen, re-draw and refresh 00128 lcd.clear(); 00129 gameEngine.draw(lcd); 00130 lcd.refresh(); 00131 wait(setDif()); //adaptive fps depending on difficulty 00132 00133 printf("Game Rending\n"); 00134 } 00135 00136 void renderMenu() 00137 { 00138 // clear screen, re-draw and refresh 00139 lcd.clear(); 00140 mainMenu.draw(lcd); 00141 lcd.refresh(); 00142 wait(1.0f/8); //fixed at 8 fps 00143 00144 printf("Menu Rending\n"); 00145 } 00146 00147 void transition() 00148 { 00149 //transition animation between modes to reduce 'ghosting' of inputs 00150 00151 printf("Transisitoning\n"); 00152 00153 for (int i = 0; i <= 11; i++) { //max values for i and j were chosen as the screen is 84x44 and the sqaures being drawn are 4x4 00154 for (int j = 0; j <= 21; j++) { 00155 lcd.drawRect(j*4,i*4,4,4,FILL_BLACK); //covers the entire screen in 4x4 squares 00156 wait(0.005); //waits a small amount of time to allow the screen to keep up 00157 lcd.refresh(); 00158 } 00159 } 00160 00161 } 00162 00163 float setDif() 00164 { 00165 int _dif = mainMenu.getDif(); 00166 00167 if (_dif == 1) { //Low difficulty 00168 return 1.0f/4; 00169 } else if (_dif == 2) { //Medium difficulty 00170 return 1.0f/8; 00171 } else if (_dif == 3) { //Hard difficulty 00172 return 1.0f/12; 00173 } else { //Adaptive difficulty 00174 float _delta = gameEngine.getScore(); 00175 00176 printf("Delta %4.2f\n", _delta); 00177 00178 if (_delta < 25) { //Limits FPS to 25 00179 return ((1.0f/(_delta + 1))); //changes the fps depending on score 00180 } else { 00181 return (1.0f/25); 00182 } 00183 } 00184 }
Generated on Sun Jul 17 2022 04:24:16 by
1.7.2