Snake and Physics based jumping game with high scores and different difficulties.

Dependencies:   N5110 SDFileSystem mbed

Committer:
el14jw
Date:
Wed May 04 15:01:20 2016 +0000
Revision:
5:ae641b1d04fa
Parent:
3:02d9072a2507
Snake and Physics based jumping game

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el14jw 0:23a749719479 1 /**
el14jw 0:23a749719479 2 @file main.h
el14jw 0:23a749719479 3
el14jw 0:23a749719479 4 @brief Header file containing functions prototypes, defines and global variables.
el14jw 5:ae641b1d04fa 5 @brief Revision 1.1
el14jw 0:23a749719479 6 @author Joel W. Webb
el14jw 0:23a749719479 7 @date March 2016
el14jw 0:23a749719479 8 */
el14jw 0:23a749719479 9
el14jw 0:23a749719479 10 #ifndef MAIN_H
el14jw 0:23a749719479 11 #define MAIN_H
el14jw 0:23a749719479 12
el14jw 0:23a749719479 13 #define PI 3.14159265359
el14jw 2:80a91a737e17 14 #define DIRECTION_TOLERANCE 0.2
el14jw 0:23a749719479 15
el14jw 0:23a749719479 16 #include "mbed.h"
el14jw 0:23a749719479 17 #include "N5110.h"
el14jw 1:c4928de1f922 18 #include "SDFileSystem.h"
el14jw 0:23a749719479 19
el14jw 0:23a749719479 20
el14jw 0:23a749719479 21
el14jw 0:23a749719479 22 // GPIO Objects
el14jw 0:23a749719479 23 /*
el14jw 0:23a749719479 24 @namespace lcd
el14jw 0:23a749719479 25 @brief N5110 object for interfacing with Nokia 5110 LCD
el14jw 0:23a749719479 26 @param VCC - PTE26
el14jw 0:23a749719479 27 @param SCE - PTA0
el14jw 0:23a749719479 28 @param RST - PTC4
el14jw 0:23a749719479 29 @param D/C - PTD0
el14jw 0:23a749719479 30 @param MOSI - PTD2
el14jw 0:23a749719479 31 @param SCLK - PTD1
el14jw 0:23a749719479 32 @param LED - PTC3
el14jw 0:23a749719479 33 */
el14jw 0:23a749719479 34 N5110 lcd(PTE26, PTA0, PTC4, PTD0, PTD2, PTD1, PTC3);
el14jw 0:23a749719479 35
el14jw 1:c4928de1f922 36 /*
el14jw 1:c4928de1f922 37 @namespace sd
el14jw 1:c4928de1f922 38 @param MOSI - PTE3
el14jw 1:c4928de1f922 39 @param MISO - PTE1
el14jw 1:c4928de1f922 40 @param SCK - PTE4
el14jw 1:c4928de1f922 41 @param CS - PTE4
el14jw 1:c4928de1f922 42 */
el14jw 1:c4928de1f922 43 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
el14jw 1:c4928de1f922 44
el14jw 0:23a749719479 45 /**
el14jw 0:23a749719479 46 @namespace errorLED
el14jw 0:23a749719479 47 @brief GPIO output for error LED
el14jw 0:23a749719479 48 */
el14jw 0:23a749719479 49 DigitalOut errorLED(LED1);
el14jw 0:23a749719479 50
el14jw 0:23a749719479 51 /**
el14jw 0:23a749719479 52 @namespace buzzer
el14jw 0:23a749719479 53 @brief PWM output for buzzer
el14jw 0:23a749719479 54 */
el14jw 0:23a749719479 55 PwmOut buzzer(PTC10);
el14jw 0:23a749719479 56
el14jw 0:23a749719479 57 /**
el14jw 0:23a749719479 58 @namespace xpot
el14jw 0:23a749719479 59 @brief The joystick x axis potentiometer
el14jw 0:23a749719479 60 */
el14jw 0:23a749719479 61 AnalogIn xPot(PTB11);
el14jw 0:23a749719479 62
el14jw 0:23a749719479 63 /**
el14jw 0:23a749719479 64 @namespace ypot
el14jw 0:23a749719479 65 @brief The joystick y axis potentiometer
el14jw 0:23a749719479 66 */
el14jw 0:23a749719479 67 AnalogIn yPot(PTB10);
el14jw 0:23a749719479 68
el14jw 0:23a749719479 69 /**
el14jw 0:23a749719479 70 @namespace buttonjoy
el14jw 0:23a749719479 71 @brief Interrupt input for Joystick button
el14jw 0:23a749719479 72 */
el14jw 0:23a749719479 73 InterruptIn buttonjoy(PTB9);
el14jw 0:23a749719479 74
el14jw 0:23a749719479 75 /**
el14jw 0:23a749719479 76 @namespace buttonA
el14jw 0:23a749719479 77 @brief Interrupt input for Button A
el14jw 0:23a749719479 78 */
el14jw 1:c4928de1f922 79 InterruptIn buttonA(PTB23);
el14jw 0:23a749719479 80
el14jw 0:23a749719479 81 /**
el14jw 0:23a749719479 82 @namespace buttonB
el14jw 0:23a749719479 83 @brief Interrupt input for Button B
el14jw 0:23a749719479 84 */
el14jw 1:c4928de1f922 85 InterruptIn buttonB(PTA1);
el14jw 0:23a749719479 86
el14jw 0:23a749719479 87
el14jw 0:23a749719479 88
el14jw 0:23a749719479 89 //Data Structures
el14jw 0:23a749719479 90
el14jw 0:23a749719479 91 /// stringList struct for dealing with printing strings to LCD easier
el14jw 0:23a749719479 92 struct stringList {
el14jw 0:23a749719479 93 char str[14];
el14jw 0:23a749719479 94 int offset;
el14jw 0:23a749719479 95 };
el14jw 0:23a749719479 96 typedef stringList stringList;
el14jw 0:23a749719479 97
el14jw 0:23a749719479 98 /// cell struct for dealing with coordinates
el14jw 0:23a749719479 99 struct cell {
el14jw 0:23a749719479 100 int x;
el14jw 0:23a749719479 101 int y;
el14jw 0:23a749719479 102 };
el14jw 0:23a749719479 103 typedef cell cell;
el14jw 0:23a749719479 104
el14jw 1:c4928de1f922 105 /// vector struct for dealing with vectors
el14jw 1:c4928de1f922 106 struct vector {
el14jw 1:c4928de1f922 107 float x;
el14jw 1:c4928de1f922 108 float y;
el14jw 1:c4928de1f922 109 };
el14jw 1:c4928de1f922 110 typedef vector vector;
el14jw 1:c4928de1f922 111
el14jw 5:ae641b1d04fa 112 /// plinkvar struct is used to pass information to and from the plink functions without resorting to many global variables
el14jw 3:02d9072a2507 113 struct plinkvar {
el14jw 3:02d9072a2507 114 int difficulty;
el14jw 3:02d9072a2507 115 vector pos;
el14jw 3:02d9072a2507 116 vector vel;
el14jw 3:02d9072a2507 117 vector acc;
el14jw 3:02d9072a2507 118 int height;
el14jw 3:02d9072a2507 119 int gameOver;
el14jw 3:02d9072a2507 120 int platformWidth;
el14jw 3:02d9072a2507 121 cell powerUp;
el14jw 3:02d9072a2507 122 int powerUpHeight;
el14jw 3:02d9072a2507 123 int powerUpRadius;
el14jw 3:02d9072a2507 124 int ballRadius;
el14jw 3:02d9072a2507 125 cell platforms[20];
el14jw 3:02d9072a2507 126 float tickerDelay;
el14jw 3:02d9072a2507 127 };
el14jw 3:02d9072a2507 128 typedef plinkvar plinkvar;
el14jw 3:02d9072a2507 129
el14jw 0:23a749719479 130
el14jw 0:23a749719479 131 // Global variables
el14jw 0:23a749719479 132
el14jw 0:23a749719479 133 volatile int g_buttonA_flag; /*!< Button A flag set in ISR */
el14jw 0:23a749719479 134 volatile int g_buttonB_flag; /*!< Button B flag set in ISR */
el14jw 0:23a749719479 135 volatile int g_buttonjoy_flag; /*!< Joystick Button flag set in ISR */
el14jw 0:23a749719479 136 volatile int g_joystick_flag; /*!< Joystick x and y values have been polled in ISR */
el14jw 1:c4928de1f922 137 volatile int g_gametick_flag; /*!< gametick flag is set in gametick_isr by Ticker gametick */
el14jw 5:ae641b1d04fa 138 volatile int g_screentick_flag; /*!< screentick flag is set in screentick_isr by Ticker screentick */
el14jw 0:23a749719479 139 volatile const float* noteArray; /*!< Float pointer to next note in array. Used in sound Timeout ISR */
el14jw 0:23a749719479 140
el14jw 0:23a749719479 141 // Menu stringList arrays kept in FLASH
el14jw 1:c4928de1f922 142 const stringList menuList[] = {{"Games",20},{"Snake",0},{"Plink",0}};
el14jw 1:c4928de1f922 143 const stringList snakeList[] = {{"Snake",20},{"Start",0},{"Difficulty",0},{"Highscore",0},{"Back",0}};
el14jw 0:23a749719479 144 const stringList difficultyList[] = {{"Difficulty",10},{"Easy",0},{"Medium",0},{"Hard",0}};
el14jw 1:c4928de1f922 145 const stringList plinkList[] = {{"Plink",20},{"Start",0},{"Difficulty",0},{"Highscore",0},{"Back",0}};
el14jw 0:23a749719479 146
el14jw 0:23a749719479 147
el14jw 0:23a749719479 148
el14jw 0:23a749719479 149 // Function Prototypes
el14jw 0:23a749719479 150
el14jw 0:23a749719479 151 //ISR
el14jw 0:23a749719479 152 /**
el14jw 0:23a749719479 153 Interrupt service routine for button A
el14jw 0:23a749719479 154 @brief Sets g_buttonA_flag
el14jw 0:23a749719479 155 */
el14jw 0:23a749719479 156 void buttonA_isr();
el14jw 0:23a749719479 157 /**
el14jw 0:23a749719479 158 Interrupt service routine for button B
el14jw 0:23a749719479 159 @brief Sets g_buttonB_flag
el14jw 0:23a749719479 160 */
el14jw 0:23a749719479 161 void buttonB_isr();
el14jw 0:23a749719479 162 /**
el14jw 0:23a749719479 163 Interrupt service routine for buttonjoy
el14jw 0:23a749719479 164 @brief Set g_buttonjoy_flag
el14jw 0:23a749719479 165 */
el14jw 0:23a749719479 166 void buttonjoy_isr();
el14jw 0:23a749719479 167
el14jw 0:23a749719479 168 /**
el14jw 0:23a749719479 169 Interrupt service routine for playing the current note
el14jw 0:23a749719479 170 @brief Alters the current PWM signal to the current note frequency read from noteArray
el14jw 0:23a749719479 171 */
el14jw 0:23a749719479 172 void sound_isr();
el14jw 0:23a749719479 173
el14jw 0:23a749719479 174 /**
el14jw 0:23a749719479 175 Interrupt service routine for Ticker gametick
el14jw 0:23a749719479 176 @brief Used to control the update speed of games
el14jw 0:23a749719479 177 */
el14jw 0:23a749719479 178 void gametick_isr();
el14jw 5:ae641b1d04fa 179 /**
el14jw 5:ae641b1d04fa 180 Interrupt service routine for Ticker screentick
el14jw 5:ae641b1d04fa 181 @brief Used to control the update speed of the LCD display
el14jw 5:ae641b1d04fa 182 */
el14jw 5:ae641b1d04fa 183 void screentick_isr();
el14jw 0:23a749719479 184
el14jw 0:23a749719479 185
el14jw 0:23a749719479 186 // Snake functions
el14jw 0:23a749719479 187 /**
el14jw 0:23a749719479 188 Main Snake game function
el14jw 0:23a749719479 189 @brief The snake function is called from the main loop and contains everything needed to play the snake game
el14jw 0:23a749719479 190 @param difficulty - The difficulty is easy(0) medium(1) or hard(2) and it alters how fast the game ticker is applied
el14jw 0:23a749719479 191 */
el14jw 0:23a749719479 192 void snake(int difficulty);
el14jw 1:c4928de1f922 193
el14jw 0:23a749719479 194
el14jw 1:c4928de1f922 195 // Plink functions
el14jw 3:02d9072a2507 196 /**
el14jw 3:02d9072a2507 197 Main Plink game function
el14jw 3:02d9072a2507 198 @brief The plink function calls its sub-functions to allow the user to play a physics based jumping game
el14jw 3:02d9072a2507 199 @param difficulty - The difficulty is easy(0) medium(1) or hard(2) and it alters how long the platforms are
el14jw 3:02d9072a2507 200 */
el14jw 1:c4928de1f922 201 void plink(int difficulty);
el14jw 3:02d9072a2507 202 /**
el14jw 3:02d9072a2507 203 Initialising plink variables function
el14jw 3:02d9072a2507 204 @brief The initpvar function sets all of the intiial plink variables
el14jw 3:02d9072a2507 205 @param pvar - Struct that contains all necessary plink variables
el14jw 3:02d9072a2507 206 */
el14jw 3:02d9072a2507 207 plinkvar initpvar(plinkvar pvar);
el14jw 3:02d9072a2507 208 /**
el14jw 3:02d9072a2507 209 Scrolls the objects on the LCD display
el14jw 3:02d9072a2507 210 @brief Handles when the ball exceeds a certain height and moves all objects up in Y value
el14jw 3:02d9072a2507 211 @param pvar - Struct that contains all necessary plink variables
el14jw 3:02d9072a2507 212 */
el14jw 3:02d9072a2507 213 plinkvar plinkScroll(plinkvar pvar);
el14jw 3:02d9072a2507 214 /**
el14jw 3:02d9072a2507 215 Platform Generation function
el14jw 3:02d9072a2507 216 @brief By default only 20 platforms are saved at any one time so new platforms need to be generated when old ones are no longer needed
el14jw 3:02d9072a2507 217 @param pvar - Struct that contains all necessary plink variables
el14jw 3:02d9072a2507 218 */
el14jw 3:02d9072a2507 219 plinkvar plinkPlatGen(plinkvar pvar);
el14jw 3:02d9072a2507 220 /**
el14jw 3:02d9072a2507 221 Object collision handling function
el14jw 3:02d9072a2507 222 @brief When objects such as the ball, platforms, walls and power ups collide this function detects it and performs the corresponding action
el14jw 3:02d9072a2507 223 @param pvar - Struct that contains all necessary plink variables
el14jw 3:02d9072a2507 224 */
el14jw 3:02d9072a2507 225 plinkvar plinkCollisions(plinkvar pvar);
el14jw 3:02d9072a2507 226 /**
el14jw 3:02d9072a2507 227 Plink Physics Engine function
el14jw 3:02d9072a2507 228 @brief Handles all the physics related movements with newtons laws of motion every game iteration
el14jw 3:02d9072a2507 229 @param pvar - Struct that contains all necessary plink variables
el14jw 3:02d9072a2507 230 */
el14jw 3:02d9072a2507 231 plinkvar plinkPhysicsEngine(plinkvar pvar);
el14jw 3:02d9072a2507 232 /**
el14jw 3:02d9072a2507 233 LCD Screen drawing function
el14jw 3:02d9072a2507 234 @brief This function takes all objects and draws them on the LCD display
el14jw 3:02d9072a2507 235 @param pvar - Struct that contains all necessary plink variables
el14jw 3:02d9072a2507 236 */
el14jw 3:02d9072a2507 237 plinkvar plinkDrawScreen(plinkvar pvar);
el14jw 3:02d9072a2507 238 /**
el14jw 3:02d9072a2507 239 Game Over Function
el14jw 3:02d9072a2507 240 @brief This function handles all the high score saving and printing to LCD display
el14jw 3:02d9072a2507 241 @param pvar - Struct that contains all necessary plink variables
el14jw 3:02d9072a2507 242 */
el14jw 3:02d9072a2507 243 plinkvar plinkGameOver(plinkvar pvar);
el14jw 0:23a749719479 244
el14jw 0:23a749719479 245
el14jw 0:23a749719479 246 // Menu functions
el14jw 0:23a749719479 247 /**
el14jw 0:23a749719479 248 Menu screen Controller
el14jw 0:23a749719479 249 @brief menu is a function that handles the selection of different menu selections
el14jw 3:02d9072a2507 250 @param menuList - The pointer to the array of strings handled in the menu (Must be 6 elements or less)
el14jw 0:23a749719479 251 @param line - The number of lines in the menu list (Must be 6 or less)
el14jw 0:23a749719479 252 @returns Selected menu element (0-4)
el14jw 0:23a749719479 253 */
el14jw 0:23a749719479 254 int menu(const stringList* menuList,int lines);
el14jw 0:23a749719479 255 /**
el14jw 0:23a749719479 256 The drawString function
el14jw 0:23a749719479 257 @brief This function is used to simplify the menu function
el14jw 0:23a749719479 258 @param stringList - a const string array containing the strings needed to be written on each line
el14jw 0:23a749719479 259 @param lines - Integer representing how many strings should be written
el14jw 0:23a749719479 260 */
el14jw 0:23a749719479 261 void drawStrings(const stringList* list,int lines);
el14jw 0:23a749719479 262
el14jw 0:23a749719479 263
el14jw 0:23a749719479 264 // Sound and error functions
el14jw 0:23a749719479 265 /**
el14jw 0:23a749719479 266 Attaches Timeout to play sounds immediately
el14jw 0:23a749719479 267 @brief playSound is a function to provide ease fo use when calling sound arrays
el14jw 0:23a749719479 268 @param sound - The pointer to the noteArray
el14jw 0:23a749719479 269 */
el14jw 0:23a749719479 270 void playSound(const float* sound);
el14jw 0:23a749719479 271 /**
el14jw 0:23a749719479 272 Error function
el14jw 0:23a749719479 273 @brief Hangs while flashing errorLED
el14jw 0:23a749719479 274 */
el14jw 0:23a749719479 275 void error();
el14jw 0:23a749719479 276
el14jw 0:23a749719479 277
el14jw 0:23a749719479 278 // Initialising and joystick
el14jw 0:23a749719479 279 /**
el14jw 0:23a749719479 280 Initializes Inputs
el14jw 0:23a749719479 281 @brief Used to group code that will run only once at startup
el14jw 0:23a749719479 282 */
el14jw 0:23a749719479 283 void initInputs();
el14jw 0:23a749719479 284 /**
el14jw 0:23a749719479 285 Initializes the joystick
el14jw 0:23a749719479 286 @brief Aquires xpot and ypot default values
el14jw 0:23a749719479 287 */
el14jw 0:23a749719479 288 void calibrateJoystick();
el14jw 0:23a749719479 289 /**
el14jw 0:23a749719479 290 updateJoystick
el14jw 0:23a749719479 291 @brief Updates direction the joystick is pointing in on a Ticker event
el14jw 0:23a749719479 292 */
el14jw 0:23a749719479 293 void updateJoystick();
el14jw 0:23a749719479 294
el14jw 0:23a749719479 295
el14jw 0:23a749719479 296
el14jw 0:23a749719479 297 #include "sounds.h"
el14jw 0:23a749719479 298
el14jw 0:23a749719479 299 #endif
el14jw 0:23a749719479 300