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 University of Leeds 00004 Name: Kostadin Chakarov 00005 Username: el17kec 00006 Student ID Number:201171978 Date:11/03/2019 00007 */ 00008 00009 // Breakout logo sprite 00010 const int breakout[10][75] = { 00011 { 1,0,0,1,1,0,0,0,0,0,1,0,0,1,1,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0 }, 00012 { 1,0,1,0,0,1,0,0,1,0,1,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,1,1,1,0,0,1,0,1,1,1,1,1,1,1,1,1 }, 00013 { 1,1,0,0,0,0,1,0,0,1,1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,1,1,1,0,0,1,0,0,0,0,0,1,0,0,0,0 }, 00014 { 1,1,0,1,1,1,1,0,0,0,1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0 }, 00015 { 1,0,1,0,0,1,1,0,0,0,1,1,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,1,0 }, 00016 { 1,1,0,1,1,0,0,1,0,0,1,0,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,1,0,1,1,1,0,1,0,1,1,1 }, 00017 { 1,1,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,1,1,1,1,1,1,1,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,1,0 }, 00018 { 1,1,0,0,0,0,0,1,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,1,1,1,0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0 }, 00019 { 1,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,1,1,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0 }, 00020 { 1,0,0,1,1,1,0,0,0,0,1,0,0,0,0,0,1,0,1,1,1,1,1,1,1,1,0,1,0,0,0,1,0,0,0,1,0,1,0,0,0,0,0,0,1,0,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0 }, 00021 }; 00022 00023 ///////// pre-processor directives //////// 00024 #include "mbed.h" 00025 #include "N5110.h" 00026 #include "Gamepad.h" 00027 #include "Paddle.h" 00028 #include "Ball.h" 00029 #include "Map.h" 00030 00031 #define WITH_TESTING 00032 #ifdef WITH_TESTING 00033 #include "tests.h" 00034 #endif 00035 00036 const int fps = 20; // Sets the fps of the game, doesn't change 00037 00038 /////////////// objects /////////////// 00039 AnalogIn pot(PTB2); // Potentiometer to set contrast of screen 00040 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); // K64F - pwr from 3V3 00041 Gamepad pad; // Gamepad buttons 00042 Paddle paddle; // Paddle object 00043 Ball ball; // Ball object 00044 Map map; // Map object 00045 00046 ///////////// function prototypes /////////////// 00047 void init(); // Initialize LCD and Gamepad 00048 void welcome(); // Simple welcome screen displayed on start-up 00049 void render(); // Used as the game loop 00050 void contrast(); // Allows to set contrast, if not suitable 00051 00052 ///////////// functions //////////////// 00053 int main() 00054 { 00055 #ifdef WITH_TESTING 00056 int number_of_failures = run_tests(); 00057 if(number_of_failures > 0) return number_of_failures; 00058 #endif 00059 00060 init(); // Initialize LCD and Gamepad 00061 welcome(); // Waiting for the user to start 00062 render(); // Game Loop 00063 } 00064 00065 00066 void init() 00067 { 00068 lcd.init(); // Initialize the LCD 00069 pad.init(); // Initialize the Gamepad 00070 lcd.setContrast(0.55); // Initially set contrast to 55% 00071 lcd.normalMode(); // Normal color mode 00072 lcd.setBrightness(0.5); // Puts LED backlight on 50% 00073 } 00074 00075 /** Simple welcome screen displayed on start-up */ 00076 void welcome() 00077 { 00078 lcd.printString("Welcome to",12,0); 00079 lcd.drawSprite(5,15,10,75,(int *)breakout); // x origin, y origin, rows, cols, sprite 00080 lcd.printString("Press A ",2,4); 00081 lcd.printString("for Controls ",11,5); 00082 lcd.refresh(); 00083 00084 // Wait to move to next menu 00085 while (pad.check_event(Gamepad::A_PRESSED) == false) 00086 { 00087 00088 } 00089 lcd.clear(); 00090 lcd.printString("Use pot to ->",4,0); 00091 lcd.printString("set contrast",4,1); 00092 lcd.printString("Use Joystick ",6,2); 00093 lcd.printString("to move paddle ",0,3); 00094 lcd.printString("Press Start ",9,4); 00095 lcd.printString("to begin ",16,5); 00096 lcd.refresh(); 00097 // Flash LEDs until start button is pressed 00098 while (pad.check_event(Gamepad::START_PRESSED) == false) 00099 { 00100 for (int i = 1; i < 7; i++) 00101 { 00102 pad.led(i,1); 00103 wait(0.1); 00104 pad.led(i,0); 00105 } 00106 } 00107 } 00108 00109 /** Resets everything if game is won / lost */ 00110 void resetGame() 00111 { 00112 paddle.reset(); // Reset paddle 00113 ball.reset(); // Reset ball 00114 map.reset(); // Reset map 00115 map.score = 0; // Reset score 00116 } 00117 00118 /** Checks if game is lost */ 00119 void checkLost() 00120 { 00121 const Vector2D& posPad = paddle.getPos(); 00122 // Checks if ball is below paddle, i.e. lost 00123 if (ball.getPos().y > posPad.y) 00124 { 00125 while (1) 00126 { 00127 // Prints a simple 'lost' message and score and asks if you want to restart 00128 lcd.clear(); 00129 char finalscore[14]; 00130 sprintf(finalscore,"Score = %d",map.score); 00131 lcd.printString(finalscore,0,0); 00132 lcd.printString("You Lost",17,2); 00133 lcd.printString("Press A",20,3); 00134 lcd.printString("to restart",12,4); 00135 wait(0.1); 00136 lcd.refresh(); 00137 // Checks if reset button is pressed, if it is reset 00138 if (pad.check_event(Gamepad::A_PRESSED) == true) 00139 { 00140 resetGame(); 00141 break; 00142 } 00143 } 00144 } 00145 } 00146 00147 /** Checks if level / game is won */ 00148 void checkWon() 00149 { 00150 if (map.checkLevel()) { 00151 // We have cleared a level 00152 ball.reset(); // Reset ball 00153 paddle.reset(); // Reset paddle 00154 00155 if (map.hasWon()) { 00156 // We have cleared all levels 00157 while (1) 00158 { 00159 // Prints win message and score and asks if you want to restart 00160 lcd.clear(); 00161 char finalscore[14]; 00162 sprintf(finalscore,"Score = %d",map.score); 00163 lcd.printString(finalscore,0,0); 00164 lcd.printString("You WON!",17,2); 00165 lcd.printString("Press B",20,3); 00166 lcd.printString("to restart",12,4); 00167 wait(0.1); 00168 lcd.refresh(); 00169 // Checks if reset button is pressed, if it is reset 00170 if (pad.check_event(Gamepad::B_PRESSED) == true) 00171 { 00172 resetGame(); 00173 break; 00174 } 00175 } 00176 } 00177 } 00178 } 00179 00180 /** Game loop */ 00181 void render() 00182 { 00183 00184 00185 map.initBricks(); // Initialize the map 00186 00187 while(1) 00188 { 00189 00190 // Physics, movement, control input + game logic 00191 // Control Input 00192 paddle.controlPaddle(pad); 00193 00194 // Movement and physics 00195 ball.move(); 00196 paddle.move(); 00197 00198 map.checkCollision(ball, paddle); 00199 map.update(); 00200 ball.hitPad(paddle); 00201 00202 // Rendering 00203 lcd.clear(); // Initialize render 00204 contrast(); // Sets contrast 00205 map.drawMap(lcd); // Draw map objects 00206 paddle.draw(lcd); // Draws paddle 00207 ball.draw(lcd); // Draws ball 00208 00209 lcd.refresh(); // Finalize render 00210 00211 wait(1.0f/fps); // Frames per second 00212 00213 // Check Lost/Won 00214 checkLost(); 00215 checkWon(); 00216 } 00217 } 00218 00219 /** Allows to set contrast, if not suitable */ 00220 void contrast() 00221 { 00222 float contrast = pot.read(); // Read value from potentiometer(0-1) 00223 lcd.setContrast(contrast); // Set that value for the contrast 00224 } 00225
Generated on Thu Jul 14 2022 02:02:53 by
1.7.2