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.
Dependencies: N5110 PinDetect PowerControl mbed
BrickBreaker.h
00001 /** 00002 * @file BrickBreaker.h 00003 * 00004 * @brief Header file containg function prototypes and variables. 00005 * @brief The program runs a simple version of brickbreaker on a nokia N5110 display. 00006 * @brief The game is controlled by a 2-axis joystick and buttons. 00007 * 00008 * @brief Credits to Craig A. Evans for his implementation of the 2-axis Joystick and the basis for the N5110 library. 00009 * 00010 * @brief Credits to Andy Kirkham for his PinDetect library 00011 * 00012 * @brief Credits to Toyomasa Watarai for his PowerControl Library 00013 * 00014 * @author Calum L. Johnston 00015 * @date 11.05.2015 00016 */ 00017 00018 00019 #include "mbed.h" 00020 #include "N5110.h" 00021 #include "PinDetect.h" 00022 #include "PowerControl/PowerControl.h" 00023 #include "PowerControl/EthernetPowerControl.h" 00024 00025 #define USR_POWERDOWN (0x104) 00026 00027 // change this to alter tolerance of joystick direction 00028 #define DIRECTION_TOLERANCE 0.05 00029 00030 //included for power control 00031 int semihost_powerdown() { 00032 00033 uint32_t arg; 00034 return __semihost(USR_POWERDOWN, &arg); 00035 } 00036 00037 00038 00039 00040 // connections for joystick 00041 00042 /** Joystick Button 00043 * 00044 * Button for menu navigation and other controls. PinDetect was used to overcome the inherent debounce of the buttons 00045 */ 00046 PinDetect button(p17); 00047 00048 /** Joystick x-axis 00049 * 00050 * Horizontal motion of the joystick, read as a potentiometer. 00051 */ 00052 AnalogIn xPot(p15); 00053 00054 /** Joystick y-axis 00055 * 00056 * Veritcal motion of the joystick, read as a potentiometer. 00057 */ 00058 AnalogIn yPot(p16); 00059 00060 //connections for other controls 00061 00062 /** Play/Pause 00063 * 00064 * Button to pause and resume the game. PinDetect was used to overcome the inherent debounce of the buttons 00065 */ 00066 PinDetect playPause(p12); 00067 00068 /** Volume 00069 * 00070 * Potentiometer to control the volume of the buzzer 00071 */ 00072 AnalogIn volPot(p20); 00073 00074 /** Noise Pin 00075 * 00076 * Pin to create noise for the randomisation of the levels 00077 */ 00078 AnalogIn noise(p19); 00079 00080 00081 /** Local File System 00082 * 00083 * Create Local filesystem to store the highscores 00084 */ 00085 00086 LocalFileSystem local("local"); 00087 00088 /** Direction Name 00089 * 00090 * Enum type to store the names of the joystick directions 00091 */ 00092 enum DirectionName { 00093 UP, 00094 DOWN, 00095 LEFT, 00096 RIGHT, 00097 CENTRE, 00098 UPLEFT, 00099 UPRIGHT, 00100 DOWNLEFT, 00101 DOWNRIGHT, 00102 UNKNOWN 00103 }; 00104 00105 /** Joystick 00106 * 00107 * Struct for the Joystick 00108 */ 00109 typedef struct JoyStick Joystick; 00110 struct JoyStick { 00111 float x; // current x value 00112 float x0; // 'centred' x value 00113 float y; // current y value 00114 float y0; // 'centred' y value 00115 int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed) 00116 DirectionName direction; // current direction 00117 }; 00118 00119 Joystick joystick; 00120 00121 /** High Score 1 00122 * 00123 * Variable to store the highscores from the memory while the program is running. 00124 * This is compared with the player's score at the end of the game and overwritten if necessary. 00125 */ 00126 int highscore1; 00127 00128 /** High Score 2 00129 * 00130 * Variable to store the highscores from the memory while the program is running. 00131 * This is compared with the player's score at the end of the game and overwritten if necessary. 00132 */ 00133 int highscore2; 00134 00135 /** High Score 3 00136 * 00137 * Variable to store the highscores from the memory while the program is running. 00138 * This is compared with the player's score at the end of the game and overwritten if necessary. 00139 */ 00140 int highscore3; 00141 00142 00143 /** Score Flag 00144 * 00145 * Set to 1 on initial conditions, set to 0 if a score overwrites a high score. 00146 * This is to stop the same score from overwriting multiple entries in the high scores. 00147 */ 00148 int scoreFlag = 1; 00149 00150 /** Ball x-Position 00151 * 00152 * Stores the x-Position of the ball for passing to lcd.drawRect() 00153 */ 00154 int bx = 42; 00155 00156 /** Ball y-Position 00157 * 00158 * Stores the y-position of the ball for passing to lcd.drawRect() 00159 */ 00160 int by = 30; 00161 00162 /** Ball Width 00163 * 00164 * Stores the width of the ball for passing to lcd.drawRect() 00165 */ 00166 int bw = 1; 00167 00168 /** Ball Height 00169 * 00170 * Stores the Height of the ball for passing to lcd.drawRect() 00171 */ 00172 int bh = 1; 00173 00174 /** Ball Fill 00175 * 00176 * Stores the Fill State of the ball for passing to lcd.drawRect() 00177 */ 00178 int bf = 1; 00179 00180 /** Ball Direction 00181 * 00182 * Direction of the ball, quantised into 16 directions of motion 00183 */ 00184 int d = 0; 00185 00186 /** Paddle x-Position 00187 * 00188 * Stores the x-Position of the paddle for passing to lcd.drawRect() 00189 */ 00190 int px = 38; 00191 00192 /** Paddle y-Position 00193 * 00194 * Stores the y-position of the paddle for passing to lcd.drawRect() 00195 */ 00196 int py = 40; 00197 00198 /** Paddle Width 00199 * 00200 * Stores the width of the paddle for passing to lcd.drawRect() 00201 */ 00202 int pw = 8; 00203 00204 /** Paddle Height 00205 * 00206 * Stores the Height of the paddle for passing to lcd.drawRect() 00207 */ 00208 int ph = 2; 00209 00210 00211 /** Ball Surround 00212 * 00213 * Array to store the active pixels around the ball 00214 */ 00215 int surround[12] = {}; 00216 00217 /** Touch Flag 00218 * 00219 * Set when the ball touches any set pixel on the screen to flag it for setAngle() 00220 */ 00221 int touchFlag = 0; 00222 00223 /** Bricks x-Coordinates 00224 * 00225 * Array to index the x-positions of the bricks 00226 */ 00227 int bricksx[8] = {3, 12, 21, 30, 39, 48, 57, 66}; 00228 00229 /** Bricks y-Coordinates 00230 * 00231 * Array to index the y-positions of the bricks 00232 */ 00233 int bricksy[4] = {3, 8, 13, 18}; 00234 00235 /** State of Bricks 00236 * 00237 * Array to index the state of the bricks: 1 is set, 0 is cleared 00238 */ 00239 int bricks[4][8]; 00240 00241 /** Display Menu 00242 * 00243 * Display the menu - for passing to the main() function 00244 * One of several variables to control what is displayed on the screen 00245 */ 00246 int displayMenu = 1; 00247 00248 /** Display Help 00249 * 00250 * Display the Help - for passing to the main() function 00251 * One of several variables to control what is displayed on the screen 00252 */ 00253 int displayHelp = 0; 00254 00255 /** Display High Scores 00256 * 00257 * Display the High Scores - for passing to the main() function 00258 * One of several variables to control what is displayed on the screen 00259 */ 00260 int displayHighScores = 0; 00261 00262 /** Display Game 00263 * 00264 * Display the Game - for passing to the main() function 00265 * One of several variables to control what is displayed on the screen 00266 */ 00267 int displayGame = 0; 00268 00269 /** Display Game Over 00270 * 00271 * Display the Game Over - for passing to the main() function 00272 * One of several variables to control what is displayed on the screen 00273 */ 00274 int displayGameOver = 0; 00275 00276 /** Menu Select 00277 * 00278 * Used to control the position of the cursor on the screen 00279 */ 00280 int menuSelect = 1; 00281 00282 /** Help Screen 00283 * 00284 * Used to select which of the help pages to load 00285 */ 00286 int helpScreen = 0; 00287 00288 /** Help Select 00289 * 00290 * Used to control the position of the cursor on the help screen 00291 */ 00292 int helpSelect = 35; 00293 00294 /** Level 00295 * 00296 * Used to set the level - for passing to initBricks() 00297 */ 00298 int level = 0; 00299 00300 /** Start/Stop 00301 * 00302 * Variable to determine whether the game is paused - 1 is start, 0 is stop. 00303 */ 00304 int startStop = 1; 00305 00306 /** Lives 00307 * 00308 * Variable to store the remaining number of player lives 00309 */ 00310 int lives = 3; 00311 00312 /** Score 00313 * 00314 * Variable to store the current player score 00315 */ 00316 int score = 0; 00317 00318 /** Border Flag 00319 * 00320 * Set when the borders have been drawn to stop multiple attempts 00321 */ 00322 int borderFlag = 0; 00323 00324 /** Brick Clear Flag 00325 * 00326 * Set to 1 for the bricks to be cleared, set to 0 once cleared to stop blank space from being repeatedly cleared 00327 */ 00328 int clearFlag[4][8]; 00329 00330 /** Brick Draw Flag 00331 * 00332 * Set to 1 for the bricks to be drawn, set to 0 once drawn to stop unnecessary overwriting 00333 */ 00334 int brickDrawFlag[4][8]; 00335 00336 /** Poll Joystick 00337 * 00338 * Timer to regularly update the game joystick values 00339 */ 00340 Ticker pollJoystick; 00341 00342 /** Move Paddle 00343 * 00344 * Timer to regularly move the paddle 00345 */ 00346 Ticker movePaddle; 00347 00348 /** Select 00349 * 00350 * Timer to update the cursor throughout the game menus 00351 */ 00352 Ticker select; 00353 00354 /** Read High Scores 00355 * 00356 * Reads the current values of the high scores from the internal memory 00357 */ 00358 void readHighScores(); 00359 00360 /** Write High Scores 00361 * 00362 * Writes the updated values of the high scores to the internal memory 00363 */ 00364 void writeHighScores(); 00365 00366 00367 /** Initialise the Bricks 00368 * 00369 * Populate the array storing the state of the bricks for drawing 00370 * 00371 * @param l - The game level to be initialised. Predefined when l<2 and random afterwards. 00372 */ 00373 void initBricks(int l); 00374 00375 /** Do the Bricks 00376 * 00377 * Does getBrickTouch() on all bricks, draws any that are set to be drawn and clears any that are set to be cleared 00378 */ 00379 void doBricks(); 00380 00381 /** Get Brick Touch 00382 * 00383 * Checks to see if any of the pixels surrounding a given brick are set - i.e. if something is touching one 00384 * 00385 * @param x - the horizontal index of the brick check 00386 * @param y - the vertical index of the brick to check 00387 */ 00388 void getBrickTouch(int x, int y); 00389 00390 /** Clear Bricks 00391 * 00392 * Loops through all bricks and clears any that are set to be cleared 00393 */ 00394 void clearBricks(); 00395 00396 /** Calibrate Joystick 00397 * 00398 * Initialises the built in button in the joystick in PullUp mode. Also reads the initial values of the 00399 * joystick for reference. 00400 */ 00401 void calibrateJoystick(); 00402 00403 /** Update Joystick 00404 * 00405 * Checks the x and y values of the potentiometers and assigns the joystick a direction based on that. 00406 */ 00407 void updateJoystick(); 00408 00409 /** Menu 00410 * 00411 * Displays the menu screen and moves the cursor to the current position 00412 */ 00413 void menu(); 00414 00415 /** Get Menu Select 00416 * 00417 * Checks the joystick direction and assigns the cursor a new position based on that 00418 */ 00419 void getMenuSelect(); 00420 00421 /** New Screen 00422 * 00423 * Called on a click of the joystick from the menu, changes the information displayed on the screen. 00424 * Depends on the position of the cursor 00425 */ 00426 void newScreen(); 00427 00428 /** Help 00429 * 00430 * Displays the various pages of help available, moves the cursor to the current position 00431 */ 00432 void help(); 00433 00434 /** High Scores 00435 * 00436 * Pulls the high score data from the internal memory and displays them on the screen 00437 */ 00438 void highScores(); 00439 00440 /** Game 00441 * 00442 * Pulls together various other functions to run the game 00443 * 00444 * @param g - If true, game runs. If false, game stops. Used to pause the game 00445 */ 00446 void game(int g); 00447 00448 /** Ball 00449 * 00450 * @brief Move the ball 00451 * 00452 * 2-step movement for the ball - combines moveBall1() and moveBall2() with setAngle() and doBricks() to ensure game updates properly. 00453 * 00454 * NB: wait() functions were included to slow down the motion of the ball to a reasonable speed. A timer would have been preferable but the game 00455 * ceased to function if this implementation was used. 00456 */ 00457 void ball(); 00458 00459 /** Paddle 00460 * 00461 * @brief Move the paddle left and right 00462 * 00463 * Checks the position of the joystick and updates the postion of the paddle based on that. 00464 * 00465 * NB: The entire row is cleared when in theory only the prior position should need to be. In practise this does not work and the row does not 00466 * properly cleared. 00467 */ 00468 void paddle(); 00469 00470 /** Get Number of Bricks 00471 * 00472 * @return - Number of remaining bricks 00473 * 00474 * Finds the number of bricks remaining on the screen 00475 */ 00476 int getNumBricks(); 00477 00478 /** New Level 00479 * 00480 * @brief Updates the level and redraws the bricks 00481 * 00482 * Called when getNumBricks() returns 0 00483 */ 00484 void newLevel(); 00485 00486 /** Pause 00487 * 00488 * Reverses the value of StartStop to Pause/Unpause the game 00489 */ 00490 void pause(); 00491 00492 /** Move Ball (1) 00493 * 00494 * First iteration of ball motion 00495 * 00496 * @param d - The direction to move the ball in - quantised into 16 separate directions 00497 */ 00498 void moveBall1(int d); 00499 00500 /** Move Ball (2) 00501 * 00502 * Second iteration of ball motion 00503 * 00504 * @param d - The direction to move the ball in - quantised into 16 separate directions 00505 */ 00506 void moveBall2(int d); 00507 00508 /** Initialise the Borders 00509 * 00510 * Draws the boundaries so that the ball cannot leave the screen through the sides or top. 00511 * Also draws some graphics for containing the scores and remaining lives. 00512 */ 00513 void borderInit(); 00514 00515 /** Get Ball Touch Flag 00516 * 00517 * Check to see if the ball is touching anything 00518 */ 00519 void getTouchFlag(); 00520 00521 /** Set Ball Angle 00522 * 00523 * @return d The direction that the ball is to move in 00524 * 00525 * Set the new direction of the ball based on what the edges of the ball are touching and the current angle of the ball 00526 */ 00527 int setAngle(); 00528 00529 /** Displays Score 00530 * 00531 * Displays the current score on the screen. The score is displayed vertical but the score is often multiple 00532 * digits so modulus was used to split up the digits. 00533 */ 00534 void dispScore(); 00535 00536 00537 /** Display Lives 00538 * 00539 * Displays the current number of remaining lives on the screen 00540 */ 00541 void dispLives(); 00542 00543 /** Lost Life 00544 * 00545 * @return 1 - If a life is lost. I think this was for debugging CHECK THIS LATER 00546 */ 00547 int lifeLost(); 00548 00549 /** Game Over 00550 * 00551 * Displays the dreaded Game Over screen. If the score is greater than any of the high scores they will be updated accordingly and the player informed. 00552 */ 00553 void gameOver();
Generated on Tue Aug 23 2022 21:03:02 by
1.7.2