ELEC2645 (2015/16) / Mbed 2 deprecated Dodgemania

Dependencies:   N5110 mbed

Committer:
el14moh
Date:
Sun Apr 24 00:42:45 2016 +0000
Revision:
8:b49e0cc362f2
Parent:
7:ca18e8775d8d
Child:
9:ca800196baeb
Buzzer succesfully implemented

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el14moh 0:6b29f9c29a2a 1 /* Joystick
el14moh 0:6b29f9c29a2a 2 Max Hamilton
el14moh 0:6b29f9c29a2a 3
el14moh 0:6b29f9c29a2a 4 My project will be designing a game in which the player must avoid a number
el14moh 0:6b29f9c29a2a 5 of obstacles coming from different locations for as long as possible
el14moh 0:6b29f9c29a2a 6
el14moh 0:6b29f9c29a2a 7 Week 19 Code - Initial test of mbed screen and initilisations
el14moh 0:6b29f9c29a2a 8 Week 20 Code - Add code to move a player controlled ball around the screen
el14moh 0:6b29f9c29a2a 9 Week 21 Code - Add code to generate and move an obstacle down the screen
el14moh 0:6b29f9c29a2a 10
el14moh 0:6b29f9c29a2a 11 Week 22 Code - Significant progress over easter holiday
el14moh 0:6b29f9c29a2a 12 - Diagonal directions added to joystick
el14moh 0:6b29f9c29a2a 13 - Collisions implemented. Game over triggers if character hits a wall
el14moh 0:6b29f9c29a2a 14 - Walls/obstacles developed greatly
el14moh 0:6b29f9c29a2a 15 - Obstacle extended into full wall. Wall structs created and walls travel from all sides of the screen
el14moh 0:6b29f9c29a2a 16 - Walls start of appearing from one side of the screen. As time goes on, more will appear from differnt sides, with multiple walls on screen at once
el14moh 0:6b29f9c29a2a 17 - Cooldown added to walls to ensure a new one will not instantly appear from a side that another wall has just travelled to.
el14moh 0:6b29f9c29a2a 18 - visual warning on screen when walls are about to appear from a new direction
el14moh 0:6b29f9c29a2a 19 - Game now keeps track of score and displays it at the end of the game
el14moh 0:6b29f9c29a2a 20 - Intro screen added
el14moh 0:6b29f9c29a2a 21
el14moh 3:8890b4605a10 22 Week 23 Code - Brief invincibility added by pressing joybutton
el14moh 3:8890b4605a10 23 - invincibilty limit added, indicators added to the side
el14moh 3:8890b4605a10 24 - game loops succesfully
el14moh 7:ca18e8775d8d 25 - Menu added with 4 options
el14moh 7:ca18e8775d8d 26 - DodgeMILD, start the game easy with one wall
el14moh 7:ca18e8775d8d 27 - DodgeMANIA, start the game hard with all four walls
el14moh 7:ca18e8775d8d 28 - Help, get instructions on the game
el14moh 7:ca18e8775d8d 29 - Scores, see the top 3 highscore, will be used for saved scores if SD card is implemented
el14moh 3:8890b4605a10 30
el14moh 3:8890b4605a10 31 NOTES - top wall collision seems to be off
el14moh 7:ca18e8775d8d 32 - horizontal walls still seem to go beyond the left border occasinanly
el14moh 2:602e9bb053a0 33
el14moh 0:6b29f9c29a2a 34 */
el14moh 0:6b29f9c29a2a 35
el14moh 0:6b29f9c29a2a 36 #include "mbed.h"
el14moh 0:6b29f9c29a2a 37 #include "N5110.h"
el14moh 0:6b29f9c29a2a 38 #include "tones.h"
el14moh 0:6b29f9c29a2a 39
el14moh 0:6b29f9c29a2a 40 #define DIRECTION_TOLERANCE 0.05 // tolerance of joystick direction
el14moh 0:6b29f9c29a2a 41 #define PLAYERRADIUS 2 // size of player ball
el14moh 8:b49e0cc362f2 42 #define MUTE 0 // 0 - buzzer plays, 1 - buzzer muted
el14moh 0:6b29f9c29a2a 43
el14moh 0:6b29f9c29a2a 44 // VCC, SCE, RST, D/C, MOSI, SCLK, LED
el14moh 0:6b29f9c29a2a 45 N5110 lcd (PTD3 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3);
el14moh 0:6b29f9c29a2a 46
el14moh 5:c64d8ba051e9 47 AnalogIn backlight(PTB2); // pot to control brightness
el14moh 0:6b29f9c29a2a 48 DigitalIn button(PTB3); // joystick button object
el14moh 0:6b29f9c29a2a 49 AnalogIn xPot(PTB11); // joystick x direction object
el14moh 0:6b29f9c29a2a 50 AnalogIn yPot(PTB10); // joystick y direction object
el14moh 8:b49e0cc362f2 51 PwmOut buzzer(PTA2); // buzzer object
el14moh 2:602e9bb053a0 52 InterruptIn flick (PTB3); // interruptin instance of button
el14moh 0:6b29f9c29a2a 53
el14moh 0:6b29f9c29a2a 54 Ticker pollJoystick; // timer to regularly read the joystick
el14moh 6:153680563027 55 Ticker menu_pollJoystick; // slower ticker to move joystick through menu
el14moh 0:6b29f9c29a2a 56 Ticker game_timer; // timer to regularly update the screen
el14moh 0:6b29f9c29a2a 57
el14moh 0:6b29f9c29a2a 58 Serial serial(USBTX,USBRX); // Serial for debug
el14moh 0:6b29f9c29a2a 59
el14moh 0:6b29f9c29a2a 60 // create enumerated type (0,1,2,3 etc. for direction)
el14moh 0:6b29f9c29a2a 61 enum DirectionName {
el14moh 0:6b29f9c29a2a 62 UP,
el14moh 0:6b29f9c29a2a 63 DOWN,
el14moh 0:6b29f9c29a2a 64 LEFT,
el14moh 0:6b29f9c29a2a 65 RIGHT,
el14moh 0:6b29f9c29a2a 66 UPRIGHT, // Diagonally up + right
el14moh 0:6b29f9c29a2a 67 UPLEFT, // Diagonally up + left
el14moh 0:6b29f9c29a2a 68 DOWNRIGHT, // Diagonally down + right
el14moh 0:6b29f9c29a2a 69 DOWNLEFT, // Diagonally down + left
el14moh 0:6b29f9c29a2a 70 CENTRE,
el14moh 0:6b29f9c29a2a 71 UNKNOWN
el14moh 0:6b29f9c29a2a 72 };
el14moh 0:6b29f9c29a2a 73
el14moh 0:6b29f9c29a2a 74 typedef struct JoyStick Joystick; // struct for Joystick
el14moh 0:6b29f9c29a2a 75 typedef struct Wall Wall; // struct for Walls
el14moh 6:153680563027 76 typedef const struct State STyp;
el14moh 6:153680563027 77 typedef struct Highscore Highscore;
el14moh 0:6b29f9c29a2a 78
el14moh 0:6b29f9c29a2a 79 struct JoyStick {
el14moh 0:6b29f9c29a2a 80 float x; // current x value
el14moh 0:6b29f9c29a2a 81 float x0; // 'centred' x value
el14moh 0:6b29f9c29a2a 82 float y; // current y value
el14moh 0:6b29f9c29a2a 83 float y0; // 'centred' y value
el14moh 0:6b29f9c29a2a 84 int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
el14moh 0:6b29f9c29a2a 85 DirectionName direction; // current direction
el14moh 0:6b29f9c29a2a 86 };
el14moh 0:6b29f9c29a2a 87
el14moh 0:6b29f9c29a2a 88 struct Wall {
el14moh 0:6b29f9c29a2a 89 int x; // x-coordinate of wall (realtive to centre of the gap)
el14moh 0:6b29f9c29a2a 90 int y; // y-coordinate of wall (relative to centre of the gap)
el14moh 0:6b29f9c29a2a 91 DirectionName direction; // Direction the wall travels in
el14moh 0:6b29f9c29a2a 92 int random; // randomly generated integer to determine when a wall begins to travel
el14moh 0:6b29f9c29a2a 93 int cooldown; // stops a wall respawning before a certain amount of time
el14moh 1:26ebbb94cf36 94 volatile bool moveFlag; // flag to determine if wall is on screen
el14moh 1:26ebbb94cf36 95 volatile bool genFlag; // flag to determine if wall has been generated
el14moh 0:6b29f9c29a2a 96 };
el14moh 0:6b29f9c29a2a 97
el14moh 6:153680563027 98 struct State {
el14moh 6:153680563027 99 int output; // output value for current state
el14moh 6:153680563027 100 int next_state[3]; // next state (depending on direction 0 - UP, 1 - DOWN)
el14moh 6:153680563027 101 };
el14moh 6:153680563027 102
el14moh 6:153680563027 103 struct Highscore {
el14moh 6:153680563027 104 int one;
el14moh 6:153680563027 105 int two;
el14moh 6:153680563027 106 int three;
el14moh 6:153680563027 107 };
el14moh 6:153680563027 108
el14moh 6:153680563027 109 STyp fsm[4] = {
el14moh 6:153680563027 110 {1,{0,1,0}},
el14moh 6:153680563027 111 {2,{1,2,0}},
el14moh 6:153680563027 112 {4,{2,3,1}},
el14moh 6:153680563027 113 {8,{3,3,2}}
el14moh 6:153680563027 114 };
el14moh 6:153680563027 115
el14moh 0:6b29f9c29a2a 116 // struct variable for joystick
el14moh 0:6b29f9c29a2a 117 Joystick joystick;
el14moh 0:6b29f9c29a2a 118 // struct variable for moving walls
el14moh 0:6b29f9c29a2a 119 Wall leftwall;
el14moh 0:6b29f9c29a2a 120 Wall rightwall;
el14moh 0:6b29f9c29a2a 121 Wall downwall;
el14moh 0:6b29f9c29a2a 122 Wall upwall;
el14moh 0:6b29f9c29a2a 123
el14moh 6:153680563027 124 Highscore hiscore;
el14moh 6:153680563027 125
el14moh 0:6b29f9c29a2a 126 void calibrateJoystick(); // read default positions of the joystick to calibrate later readings
el14moh 0:6b29f9c29a2a 127 void updateJoystick(); // reads direction the joystick has been moved
el14moh 0:6b29f9c29a2a 128 void initDisplay(); // initialises the LCD display
el14moh 0:6b29f9c29a2a 129 void game_timer_isr(); // sets flag for timer interrupt
el14moh 7:ca18e8775d8d 130 void initGame();
el14moh 0:6b29f9c29a2a 131 void moveBall(); // reads joystick direction and moves position of the player
el14moh 0:6b29f9c29a2a 132 void moveWall(); // moves walls along the screen
el14moh 2:602e9bb053a0 133 void invincible(); // makes player briefly invincible
el14moh 2:602e9bb053a0 134 void checkWallCollision(); // checks for any collisions with wall
el14moh 2:602e9bb053a0 135 void checkBorderCollision(); // checks for any collisions with border
el14moh 0:6b29f9c29a2a 136 void updateScreen(); // refreshes the screen, redraws player and walls
el14moh 0:6b29f9c29a2a 137 void warning(); // flashes screen when a new wall is ready to appear
el14moh 7:ca18e8775d8d 138 void initSerial(); // sets baud rate for serial
el14moh 0:6b29f9c29a2a 139 void debug(); // prints for debug purposes
el14moh 2:602e9bb053a0 140 void button_isr();
el14moh 6:153680563027 141 void blink(); // function to make screen flash (do not put in isr unless breaking the loop)
el14moh 6:153680563027 142 void printHelp(); // prints game help
el14moh 6:153680563027 143 void printScores();
el14moh 6:153680563027 144 void draw_border();
el14moh 6:153680563027 145 void calculateHighscores();
el14moh 6:153680563027 146 void initHiscores();
el14moh 8:b49e0cc362f2 147 void playNote(int freq, float time);
el14moh 0:6b29f9c29a2a 148
el14moh 6:153680563027 149 //float refresh_rate = 20; // how often to update display (Hz)
el14moh 6:153680563027 150 //float g_dt = 1.0F/refresh_rate; // global to store time step (F makes it a float, gets rid of compiler warning)
el14moh 0:6b29f9c29a2a 151 volatile int g_timer_flag = 0; // flag for timer interrupt
el14moh 2:602e9bb053a0 152 volatile int game_over_flag = 0; // flag to signal game over
el14moh 2:602e9bb053a0 153 volatile int g_button_flag = 0;
el14moh 0:6b29f9c29a2a 154 int printFlag = 0; // flag for printing
el14moh 6:153680563027 155 volatile int game_start_flag = 0; // flag to start the game
el14moh 3:8890b4605a10 156 int i; // x-coordinate value of player
el14moh 3:8890b4605a10 157 int j; // y-coordinate value of player
el14moh 0:6b29f9c29a2a 158 int counter = 0; // number of times code has looped
el14moh 2:602e9bb053a0 159 volatile bool mortal = 1;
el14moh 2:602e9bb053a0 160 int invun_cool = 0;
el14moh 2:602e9bb053a0 161 int saves = 5;
el14moh 6:153680563027 162 int joyspeed = 5;
el14moh 7:ca18e8775d8d 163 int score = 0;
el14moh 0:6b29f9c29a2a 164
el14moh 6:153680563027 165 int direction;
el14moh 6:153680563027 166 int state;
el14moh 6:153680563027 167 int output;
el14moh 0:6b29f9c29a2a 168
el14moh 0:6b29f9c29a2a 169 int main()
el14moh 0:6b29f9c29a2a 170 {
el14moh 7:ca18e8775d8d 171 initSerial();
el14moh 8:b49e0cc362f2 172 srand(time(NULL)); // generate seed for random number generation
el14moh 0:6b29f9c29a2a 173 calibrateJoystick();
el14moh 0:6b29f9c29a2a 174 initDisplay();
el14moh 6:153680563027 175 initHiscores();
el14moh 0:6b29f9c29a2a 176
el14moh 0:6b29f9c29a2a 177 wait (1.0);
el14moh 0:6b29f9c29a2a 178
el14moh 6:153680563027 179 flick.rise(&button_isr);
el14moh 6:153680563027 180 flick.mode(PullDown);
el14moh 0:6b29f9c29a2a 181
el14moh 3:8890b4605a10 182 lcd.printString("Dodgemania",13,2); // Print game title on screen
el14moh 3:8890b4605a10 183 wait (2.5);
el14moh 3:8890b4605a10 184 for (int z=0; z<88; z++) {
el14moh 3:8890b4605a10 185 lcd.drawCircle(z,20,4,1);
el14moh 0:6b29f9c29a2a 186
el14moh 3:8890b4605a10 187 lcd.clearPixel(z-3,16);
el14moh 3:8890b4605a10 188 lcd.clearPixel(z-4,17);
el14moh 3:8890b4605a10 189 lcd.clearPixel(z-4,18);
el14moh 3:8890b4605a10 190 lcd.clearPixel(z-5,19);
el14moh 3:8890b4605a10 191 lcd.clearPixel(z-5,20);
el14moh 3:8890b4605a10 192 lcd.clearPixel(z-5,21);
el14moh 3:8890b4605a10 193 lcd.clearPixel(z-4,22);
el14moh 3:8890b4605a10 194 lcd.clearPixel(z-4,23);
el14moh 3:8890b4605a10 195 lcd.clearPixel(z-3,24);
el14moh 3:8890b4605a10 196 lcd.refresh();
el14moh 3:8890b4605a10 197 wait(0.01);
el14moh 3:8890b4605a10 198 }
el14moh 0:6b29f9c29a2a 199
el14moh 3:8890b4605a10 200 lcd.clear();
el14moh 3:8890b4605a10 201 wait(0.5);
el14moh 0:6b29f9c29a2a 202
el14moh 6:153680563027 203 blink();
el14moh 3:8890b4605a10 204
el14moh 3:8890b4605a10 205 while(1) {
el14moh 0:6b29f9c29a2a 206
el14moh 6:153680563027 207 joyspeed = 10;
el14moh 6:153680563027 208 game_timer.detach();
el14moh 6:153680563027 209 game_timer.attach(&game_timer_isr,1.0/joyspeed);
el14moh 6:153680563027 210
el14moh 6:153680563027 211 pollJoystick.detach();
el14moh 6:153680563027 212 pollJoystick.attach(&updateJoystick,1.0/joyspeed); // read joystick (JOYSPEED) times per second
el14moh 6:153680563027 213
el14moh 6:153680563027 214 state = 0;
el14moh 6:153680563027 215
el14moh 0:6b29f9c29a2a 216
el14moh 6:153680563027 217 while(game_start_flag == 0) // ticker interrupt
el14moh 6:153680563027 218 if (g_timer_flag) {
el14moh 6:153680563027 219 g_timer_flag = 0; // clear flag
el14moh 6:153680563027 220 if (joystick.direction == UP) {
el14moh 6:153680563027 221 direction = 2;
el14moh 6:153680563027 222 } else if (joystick.direction == DOWN) {
el14moh 6:153680563027 223 direction = 1;
el14moh 6:153680563027 224 } else {
el14moh 6:153680563027 225 direction = 0;
el14moh 6:153680563027 226 }
el14moh 6:153680563027 227 output = fsm[state].output;
el14moh 6:153680563027 228 state = fsm[state].next_state[direction];
el14moh 0:6b29f9c29a2a 229
el14moh 6:153680563027 230 lcd.clear();
el14moh 6:153680563027 231
el14moh 6:153680563027 232 lcd.printString("Dodgemild",12,1);
el14moh 6:153680563027 233 lcd.printString("DodgeMANIA!",12,2);
el14moh 6:153680563027 234 lcd.printString("Help",12,3);
el14moh 6:153680563027 235 lcd.printString("Scores",12,4);
el14moh 0:6b29f9c29a2a 236
el14moh 6:153680563027 237 if (output == 1) {
el14moh 6:153680563027 238 lcd.drawCircle(6,11,2,1);
el14moh 6:153680563027 239 } else if (output == 2) {
el14moh 6:153680563027 240 lcd.drawCircle(6,19,2,1);
el14moh 6:153680563027 241 } else if (output == 4) {
el14moh 6:153680563027 242 lcd.drawCircle(6,27,2,1);
el14moh 6:153680563027 243 } else {
el14moh 6:153680563027 244 lcd.drawCircle(6,35,2,1);
el14moh 6:153680563027 245 }
el14moh 6:153680563027 246 lcd.refresh();
el14moh 0:6b29f9c29a2a 247
el14moh 6:153680563027 248 if (g_button_flag) {
el14moh 6:153680563027 249 g_button_flag = 0;
el14moh 6:153680563027 250 if (output == 1) {
el14moh 7:ca18e8775d8d 251 initGame();
el14moh 6:153680563027 252 game_start_flag = 1;
el14moh 6:153680563027 253 } else if (output == 2) {
el14moh 7:ca18e8775d8d 254 initGame();
el14moh 6:153680563027 255 counter = 1501;
el14moh 6:153680563027 256 game_start_flag = 1;
el14moh 6:153680563027 257 } else if (output == 4) {
el14moh 6:153680563027 258 blink();
el14moh 6:153680563027 259 lcd.clear();
el14moh 6:153680563027 260 printHelp();
el14moh 6:153680563027 261 } else if (output == 8) {
el14moh 6:153680563027 262 blink();
el14moh 6:153680563027 263 lcd.clear();
el14moh 6:153680563027 264 printScores();
el14moh 6:153680563027 265 }
el14moh 6:153680563027 266 }
el14moh 6:153680563027 267 }
el14moh 6:153680563027 268 blink();
el14moh 6:153680563027 269 lcd.clear();
el14moh 0:6b29f9c29a2a 270
el14moh 0:6b29f9c29a2a 271
el14moh 6:153680563027 272 // Draw game border
el14moh 6:153680563027 273 draw_border();
el14moh 6:153680563027 274
el14moh 6:153680563027 275 // Countdown
el14moh 0:6b29f9c29a2a 276 wait(0.5);
el14moh 0:6b29f9c29a2a 277 lcd.printString("3",40,2);
el14moh 8:b49e0cc362f2 278 playNote(NOTE_C4,0.2);
el14moh 8:b49e0cc362f2 279 wait(0.3);
el14moh 0:6b29f9c29a2a 280 lcd.drawRect(10,10,64,28,2);
el14moh 0:6b29f9c29a2a 281 lcd.refresh();
el14moh 0:6b29f9c29a2a 282 wait(0.5);
el14moh 0:6b29f9c29a2a 283 lcd.printString("2",40,2);
el14moh 8:b49e0cc362f2 284 playNote(NOTE_C4,0.2);
el14moh 8:b49e0cc362f2 285 wait(0.3);
el14moh 0:6b29f9c29a2a 286 lcd.drawRect(10,10,64,28,2);
el14moh 0:6b29f9c29a2a 287 lcd.refresh();
el14moh 0:6b29f9c29a2a 288 wait(0.5);
el14moh 0:6b29f9c29a2a 289 lcd.printString("1",40,2);
el14moh 8:b49e0cc362f2 290 playNote(NOTE_C4,0.2);
el14moh 8:b49e0cc362f2 291 wait(0.3);
el14moh 0:6b29f9c29a2a 292 lcd.drawRect(10,10,64,28,2);
el14moh 0:6b29f9c29a2a 293 lcd.refresh();
el14moh 0:6b29f9c29a2a 294 wait(0.5);
el14moh 0:6b29f9c29a2a 295 lcd.drawRect(10,10,64,28,2);
el14moh 0:6b29f9c29a2a 296 lcd.refresh();
el14moh 0:6b29f9c29a2a 297 lcd.printString("Go!",36,2);
el14moh 8:b49e0cc362f2 298 playNote(NOTE_G4,0.5);
el14moh 0:6b29f9c29a2a 299 lcd.drawRect(10,10,64,28,2);
el14moh 0:6b29f9c29a2a 300 lcd.refresh();
el14moh 0:6b29f9c29a2a 301
el14moh 6:153680563027 302 joyspeed = 20;
el14moh 6:153680563027 303 game_timer.detach();
el14moh 6:153680563027 304 game_timer.attach(&game_timer_isr,1.0/joyspeed);
el14moh 6:153680563027 305 pollJoystick.detach();
el14moh 6:153680563027 306 pollJoystick.attach(&updateJoystick,1.0/joyspeed);
el14moh 3:8890b4605a10 307
el14moh 0:6b29f9c29a2a 308 while (game_over_flag == 0) {
el14moh 0:6b29f9c29a2a 309
el14moh 0:6b29f9c29a2a 310 if ( g_timer_flag ) { // ticker interrupt
el14moh 0:6b29f9c29a2a 311 g_timer_flag = 0; // clear flag
el14moh 0:6b29f9c29a2a 312 moveWall();
el14moh 0:6b29f9c29a2a 313 moveBall();
el14moh 2:602e9bb053a0 314 invincible();
el14moh 2:602e9bb053a0 315 checkBorderCollision();
el14moh 2:602e9bb053a0 316 if (mortal) {
el14moh 2:602e9bb053a0 317 checkWallCollision();
el14moh 2:602e9bb053a0 318 }
el14moh 0:6b29f9c29a2a 319 updateScreen();
el14moh 0:6b29f9c29a2a 320 warning();
el14moh 0:6b29f9c29a2a 321 debug();
el14moh 0:6b29f9c29a2a 322
el14moh 0:6b29f9c29a2a 323 counter++; // increment counter each cycle (approx. 20 points a second)
el14moh 7:ca18e8775d8d 324 score++; // this is seperate from counter for the purposes of keeping score 0 while all walls are present on DodgeMANIA
el14moh 0:6b29f9c29a2a 325
el14moh 0:6b29f9c29a2a 326 // wall cooldowns increased. N.B these are set to 0 when a wall finishes moving
el14moh 0:6b29f9c29a2a 327 leftwall.cooldown++;
el14moh 0:6b29f9c29a2a 328 rightwall.cooldown++;
el14moh 0:6b29f9c29a2a 329 downwall.cooldown++;
el14moh 0:6b29f9c29a2a 330 upwall.cooldown++;
el14moh 0:6b29f9c29a2a 331 }
el14moh 0:6b29f9c29a2a 332 sleep();
el14moh 0:6b29f9c29a2a 333 }
el14moh 0:6b29f9c29a2a 334
el14moh 3:8890b4605a10 335 game_over_flag = 0;
el14moh 3:8890b4605a10 336
el14moh 6:153680563027 337 blink();
el14moh 8:b49e0cc362f2 338 playNote(NOTE_E4,0.1);
el14moh 8:b49e0cc362f2 339 playNote(NOTE_C4,0.1);
el14moh 8:b49e0cc362f2 340 playNote(NOTE_A3,0.1);
el14moh 8:b49e0cc362f2 341 playNote(NOTE_A2,0.3);
el14moh 8:b49e0cc362f2 342 wait(0.8);
el14moh 0:6b29f9c29a2a 343 lcd.clear();
el14moh 0:6b29f9c29a2a 344 wait(0.2);
el14moh 0:6b29f9c29a2a 345
el14moh 6:153680563027 346 calculateHighscores();
el14moh 6:153680563027 347
el14moh 6:153680563027 348 lcd.printString("Game Over!",14,0);
el14moh 0:6b29f9c29a2a 349 lcd.refresh();
el14moh 0:6b29f9c29a2a 350
el14moh 0:6b29f9c29a2a 351 wait(1.0);
el14moh 6:153680563027 352
el14moh 6:153680563027 353 lcd.printString("HiScore:",3,3);
el14moh 6:153680563027 354 lcd.printString("Score:",3,2);
el14moh 6:153680563027 355
el14moh 6:153680563027 356 char score_buffer[14];
el14moh 6:153680563027 357 char hiscore_buffer[14];
el14moh 7:ca18e8775d8d 358 int length_one = sprintf(score_buffer,"%d",score);
el14moh 6:153680563027 359 int length_two = sprintf(hiscore_buffer,"%d",hiscore.one);
el14moh 7:ca18e8775d8d 360 if (score <= 9999) { // 9999 is highest number that fits on screen
el14moh 6:153680563027 361 lcd.printString(score_buffer,54,2);
el14moh 6:153680563027 362 lcd.printString(hiscore_buffer,54,3);
el14moh 0:6b29f9c29a2a 363 } else {
el14moh 6:153680563027 364 lcd.printString ("Wow!",54,2); // if score is too large to fit in box
el14moh 6:153680563027 365 lcd.printString ("Wow!",54,3);
el14moh 0:6b29f9c29a2a 366 }
el14moh 6:153680563027 367
el14moh 0:6b29f9c29a2a 368 lcd.refresh();
el14moh 0:6b29f9c29a2a 369
el14moh 7:ca18e8775d8d 370 if (score >= hiscore.one) {
el14moh 6:153680563027 371 wait(1.0);
el14moh 6:153680563027 372 lcd.printString("HIGHSCORE!",13,5);
el14moh 8:b49e0cc362f2 373 playNote(NOTE_E3,0.1);
el14moh 8:b49e0cc362f2 374 wait(0.1);
el14moh 8:b49e0cc362f2 375 playNote(NOTE_C3,0.05);
el14moh 8:b49e0cc362f2 376 wait(0.05);
el14moh 8:b49e0cc362f2 377 playNote(NOTE_C3,0.05);
el14moh 8:b49e0cc362f2 378 wait(0.05);
el14moh 8:b49e0cc362f2 379 playNote(NOTE_C3,0.05);
el14moh 8:b49e0cc362f2 380 wait(0.15);
el14moh 8:b49e0cc362f2 381 playNote(NOTE_G3,0.1);
el14moh 8:b49e0cc362f2 382 wait(0.1);
el14moh 8:b49e0cc362f2 383 playNote(NOTE_E3,0.1);
el14moh 8:b49e0cc362f2 384 wait(0.1);
el14moh 8:b49e0cc362f2 385 playNote(NOTE_G3,0.1);
el14moh 8:b49e0cc362f2 386 wait(0.1);
el14moh 8:b49e0cc362f2 387 playNote(NOTE_C4,0.5);
el14moh 8:b49e0cc362f2 388 wait(0.1);
el14moh 6:153680563027 389 lcd.printString("CONGRATS!!",13,5);
el14moh 8:b49e0cc362f2 390 playNote(NOTE_E2,0.2);
el14moh 8:b49e0cc362f2 391 wait(1.5);
el14moh 8:b49e0cc362f2 392
el14moh 6:153680563027 393 } else {
el14moh 8:b49e0cc362f2 394 playNote(NOTE_A2,0.05);
el14moh 8:b49e0cc362f2 395 wait(0.05);
el14moh 8:b49e0cc362f2 396 playNote(NOTE_A2,0.05);
el14moh 8:b49e0cc362f2 397 wait(0.05);
el14moh 8:b49e0cc362f2 398 playNote(NOTE_A2,0.05);
el14moh 8:b49e0cc362f2 399 wait(0.05);
el14moh 8:b49e0cc362f2 400 playNote(NOTE_A2,0.3);
el14moh 6:153680563027 401 wait(5.0);
el14moh 6:153680563027 402 }
el14moh 6:153680563027 403 blink();
el14moh 0:6b29f9c29a2a 404 lcd.clear();
el14moh 6:153680563027 405 game_start_flag = 0;
el14moh 0:6b29f9c29a2a 406
el14moh 6:153680563027 407 //return 0;
el14moh 0:6b29f9c29a2a 408 }
el14moh 0:6b29f9c29a2a 409
el14moh 0:6b29f9c29a2a 410 }
el14moh 0:6b29f9c29a2a 411
el14moh 2:602e9bb053a0 412 void moveBall() // reads joystick direction and moves position of the player
el14moh 0:6b29f9c29a2a 413 {
el14moh 0:6b29f9c29a2a 414 if (joystick.direction == UP) {
el14moh 0:6b29f9c29a2a 415 j-=1;
el14moh 0:6b29f9c29a2a 416 } else if (joystick.direction == DOWN) {
el14moh 0:6b29f9c29a2a 417 j+=1;
el14moh 0:6b29f9c29a2a 418 } else if (joystick.direction == LEFT) {
el14moh 0:6b29f9c29a2a 419 i-=1;
el14moh 0:6b29f9c29a2a 420 } else if (joystick.direction == RIGHT) {
el14moh 0:6b29f9c29a2a 421 i+=1;
el14moh 0:6b29f9c29a2a 422 } else if (joystick.direction == UPRIGHT) {
el14moh 0:6b29f9c29a2a 423 j-=1;
el14moh 0:6b29f9c29a2a 424 i+=1;
el14moh 0:6b29f9c29a2a 425 } else if (joystick.direction == UPLEFT) {
el14moh 0:6b29f9c29a2a 426 j-=1;
el14moh 0:6b29f9c29a2a 427 i-=1;
el14moh 0:6b29f9c29a2a 428 } else if (joystick.direction == DOWNRIGHT) {
el14moh 0:6b29f9c29a2a 429 j+=1;
el14moh 0:6b29f9c29a2a 430 i+=1;
el14moh 0:6b29f9c29a2a 431 } else if (joystick.direction == DOWNLEFT) {
el14moh 0:6b29f9c29a2a 432 j+=1;
el14moh 0:6b29f9c29a2a 433 i-=1;
el14moh 0:6b29f9c29a2a 434 }
el14moh 0:6b29f9c29a2a 435 }
el14moh 0:6b29f9c29a2a 436
el14moh 2:602e9bb053a0 437 void moveWall() // moves walls along the screen
el14moh 0:6b29f9c29a2a 438 {
el14moh 0:6b29f9c29a2a 439 leftwall.random = rand()%20;
el14moh 0:6b29f9c29a2a 440 rightwall.random = rand()%20;
el14moh 0:6b29f9c29a2a 441 downwall.random = rand()%50;
el14moh 0:6b29f9c29a2a 442 upwall.random = rand()%50;
el14moh 0:6b29f9c29a2a 443
el14moh 0:6b29f9c29a2a 444 // LEFT WALL
el14moh 0:6b29f9c29a2a 445 if (leftwall.moveFlag == 1) { // if wall is moving
el14moh 0:6b29f9c29a2a 446 leftwall.x-=1; // move wall left
el14moh 3:8890b4605a10 447 if (leftwall.x<8) { // if wall hits a border
el14moh 0:6b29f9c29a2a 448 leftwall.moveFlag = 0; // stop wall moving
el14moh 0:6b29f9c29a2a 449 leftwall.cooldown = 0; // reset the cooldown for the wall
el14moh 0:6b29f9c29a2a 450 }
el14moh 0:6b29f9c29a2a 451 } else { // if wall has stopped moving
el14moh 0:6b29f9c29a2a 452 if (leftwall.genFlag == 0) { // if a new wall HASN'T been generated
el14moh 0:6b29f9c29a2a 453 leftwall.y = rand() % 27+8; // make new random y-coordinate
el14moh 0:6b29f9c29a2a 454 leftwall.x = 82; // reset x-coordinate to rightmost position
el14moh 0:6b29f9c29a2a 455 leftwall.genFlag = 1; // wall has been generated
el14moh 0:6b29f9c29a2a 456 } else { // if a new wall HAS been generated
el14moh 0:6b29f9c29a2a 457 if (leftwall.cooldown > 80) { // if a new wall hasnt started moving in 4 seconds, force it to move
el14moh 0:6b29f9c29a2a 458 leftwall.moveFlag = 1;
el14moh 0:6b29f9c29a2a 459 leftwall.genFlag = 0;
el14moh 0:6b29f9c29a2a 460 } else if ((leftwall.random == 1)&&(rightwall.cooldown > 60)) { // if wall starts moving again
el14moh 0:6b29f9c29a2a 461 leftwall.moveFlag = 1; // start wall moving
el14moh 0:6b29f9c29a2a 462 leftwall.genFlag = 0; // clear 'wall generated' flag
el14moh 0:6b29f9c29a2a 463 } else { // else if wall has not started moving again
el14moh 0:6b29f9c29a2a 464 leftwall.moveFlag = 0; // wall is stopped
el14moh 0:6b29f9c29a2a 465 }
el14moh 0:6b29f9c29a2a 466 }
el14moh 0:6b29f9c29a2a 467 }
el14moh 0:6b29f9c29a2a 468
el14moh 0:6b29f9c29a2a 469 // RIGHT WALL
el14moh 0:6b29f9c29a2a 470 if (counter > 200) {
el14moh 0:6b29f9c29a2a 471 if (rightwall.moveFlag == 1) {
el14moh 0:6b29f9c29a2a 472 rightwall.x+=1;
el14moh 0:6b29f9c29a2a 473 if (rightwall.x>80) {
el14moh 0:6b29f9c29a2a 474 rightwall.moveFlag = 0;
el14moh 0:6b29f9c29a2a 475 rightwall.cooldown = 0;
el14moh 0:6b29f9c29a2a 476 }
el14moh 0:6b29f9c29a2a 477 } else {
el14moh 0:6b29f9c29a2a 478 if ((rightwall.genFlag == 0)) {
el14moh 0:6b29f9c29a2a 479 rightwall.y = rand() % 27+8;
el14moh 3:8890b4605a10 480 rightwall.x = 6;
el14moh 0:6b29f9c29a2a 481 rightwall.genFlag = 1;
el14moh 0:6b29f9c29a2a 482 } else {
el14moh 0:6b29f9c29a2a 483 if (rightwall.cooldown > 80) {
el14moh 0:6b29f9c29a2a 484 rightwall.moveFlag = 1;
el14moh 0:6b29f9c29a2a 485 rightwall.genFlag = 0;
el14moh 0:6b29f9c29a2a 486 } else if ((rightwall.random == 1) && (leftwall.cooldown > 60)) {
el14moh 0:6b29f9c29a2a 487 rightwall.moveFlag = 1;
el14moh 0:6b29f9c29a2a 488 rightwall.genFlag = 0;
el14moh 0:6b29f9c29a2a 489 } else {
el14moh 0:6b29f9c29a2a 490 rightwall.moveFlag = 0;
el14moh 0:6b29f9c29a2a 491 }
el14moh 0:6b29f9c29a2a 492 }
el14moh 0:6b29f9c29a2a 493 }
el14moh 0:6b29f9c29a2a 494 }
el14moh 0:6b29f9c29a2a 495
el14moh 0:6b29f9c29a2a 496 // DOWN WALL
el14moh 0:6b29f9c29a2a 497 if (counter > 600) {
el14moh 0:6b29f9c29a2a 498 if (downwall.moveFlag == 1) {
el14moh 0:6b29f9c29a2a 499 if (upwall.cooldown > 60) {
el14moh 0:6b29f9c29a2a 500 if (counter % 2 == 1) { // horizontal walls move half the speed of vertical walls
el14moh 0:6b29f9c29a2a 501 downwall.y+=1;
el14moh 0:6b29f9c29a2a 502 }
el14moh 0:6b29f9c29a2a 503 }
el14moh 0:6b29f9c29a2a 504 if (downwall.y>44) {
el14moh 0:6b29f9c29a2a 505 downwall.moveFlag = 0;
el14moh 0:6b29f9c29a2a 506 }
el14moh 0:6b29f9c29a2a 507 } else {
el14moh 0:6b29f9c29a2a 508 if (downwall.genFlag == 0) {
el14moh 4:d44eace87ab3 509 downwall.x = rand() % 58+13;
el14moh 0:6b29f9c29a2a 510 downwall.y = 1;
el14moh 0:6b29f9c29a2a 511 downwall.genFlag = 1;
el14moh 0:6b29f9c29a2a 512 } else {
el14moh 0:6b29f9c29a2a 513 if (downwall.cooldown > 80) {
el14moh 0:6b29f9c29a2a 514 downwall.moveFlag = 1;
el14moh 0:6b29f9c29a2a 515 downwall.genFlag = 0;
el14moh 0:6b29f9c29a2a 516 } else if ((downwall.random == 1)&&(upwall.cooldown > 60)) {
el14moh 0:6b29f9c29a2a 517 downwall.moveFlag = 1;
el14moh 0:6b29f9c29a2a 518 downwall.genFlag = 0;
el14moh 0:6b29f9c29a2a 519 } else {
el14moh 0:6b29f9c29a2a 520 downwall.moveFlag = 0;
el14moh 0:6b29f9c29a2a 521 }
el14moh 0:6b29f9c29a2a 522 }
el14moh 0:6b29f9c29a2a 523 }
el14moh 0:6b29f9c29a2a 524 }
el14moh 0:6b29f9c29a2a 525
el14moh 0:6b29f9c29a2a 526 // UP WALL
el14moh 0:6b29f9c29a2a 527 if (counter > 1500) {
el14moh 0:6b29f9c29a2a 528 if (upwall.moveFlag == 1) {
el14moh 0:6b29f9c29a2a 529 if (downwall.cooldown > 60) {
el14moh 0:6b29f9c29a2a 530 if (counter % 2 == 1) { // horizontal walls move half the speed of vertical walls
el14moh 0:6b29f9c29a2a 531 upwall.y-=1;
el14moh 0:6b29f9c29a2a 532 }
el14moh 0:6b29f9c29a2a 533 }
el14moh 0:6b29f9c29a2a 534 if (upwall.y<3) {
el14moh 0:6b29f9c29a2a 535 upwall.moveFlag = 0;
el14moh 0:6b29f9c29a2a 536 }
el14moh 0:6b29f9c29a2a 537 } else {
el14moh 0:6b29f9c29a2a 538 if (upwall.genFlag == 0) {
el14moh 4:d44eace87ab3 539 upwall.x = rand() % 58+13;
el14moh 0:6b29f9c29a2a 540 upwall.y = 46;
el14moh 0:6b29f9c29a2a 541 upwall.genFlag = 1;
el14moh 0:6b29f9c29a2a 542 } else {
el14moh 0:6b29f9c29a2a 543 if (upwall.cooldown > 80) {
el14moh 0:6b29f9c29a2a 544 upwall.moveFlag = 1;
el14moh 0:6b29f9c29a2a 545 upwall.genFlag = 0;
el14moh 0:6b29f9c29a2a 546 } else if ((upwall.random == 1)&&(downwall.cooldown > 60)) {
el14moh 0:6b29f9c29a2a 547 upwall.moveFlag = 1;
el14moh 0:6b29f9c29a2a 548 upwall.genFlag = 0;
el14moh 0:6b29f9c29a2a 549 } else {
el14moh 0:6b29f9c29a2a 550 upwall.moveFlag = 0;
el14moh 0:6b29f9c29a2a 551 }
el14moh 0:6b29f9c29a2a 552 }
el14moh 0:6b29f9c29a2a 553 }
el14moh 0:6b29f9c29a2a 554 }
el14moh 0:6b29f9c29a2a 555 }
el14moh 2:602e9bb053a0 556 void checkBorderCollision()
el14moh 0:6b29f9c29a2a 557 {
el14moh 0:6b29f9c29a2a 558 // if floor
el14moh 0:6b29f9c29a2a 559 if ( j >= 47 - (PLAYERRADIUS+3)) {
el14moh 0:6b29f9c29a2a 560 j = 47 - (PLAYERRADIUS+3);
el14moh 0:6b29f9c29a2a 561 }
el14moh 0:6b29f9c29a2a 562
el14moh 0:6b29f9c29a2a 563 // if roof
el14moh 0:6b29f9c29a2a 564 if ( j <= (PLAYERRADIUS+3)) {
el14moh 0:6b29f9c29a2a 565 j = (PLAYERRADIUS+3);
el14moh 0:6b29f9c29a2a 566 }
el14moh 0:6b29f9c29a2a 567
el14moh 0:6b29f9c29a2a 568 // if right wall
el14moh 0:6b29f9c29a2a 569 if ( i >= 83 - (PLAYERRADIUS+3)) {
el14moh 0:6b29f9c29a2a 570 i = 83 - (PLAYERRADIUS+3);
el14moh 0:6b29f9c29a2a 571 }
el14moh 0:6b29f9c29a2a 572
el14moh 0:6b29f9c29a2a 573 // if left wall
el14moh 3:8890b4605a10 574 if ( i <= (PLAYERRADIUS+8)) {
el14moh 3:8890b4605a10 575 i = (PLAYERRADIUS+8);
el14moh 0:6b29f9c29a2a 576 }
el14moh 2:602e9bb053a0 577 }
el14moh 2:602e9bb053a0 578
el14moh 2:602e9bb053a0 579 void invincible()
el14moh 2:602e9bb053a0 580 {
el14moh 2:602e9bb053a0 581 if (g_button_flag) {
el14moh 2:602e9bb053a0 582 g_button_flag = 0;
el14moh 2:602e9bb053a0 583 if ((saves > 0) && (mortal == true)) { // saves are available and not currently used
el14moh 2:602e9bb053a0 584 invun_cool=0;
el14moh 2:602e9bb053a0 585 mortal = false;
el14moh 2:602e9bb053a0 586 invun_cool++;
el14moh 2:602e9bb053a0 587 saves--;
el14moh 2:602e9bb053a0 588 }
el14moh 2:602e9bb053a0 589 }
el14moh 2:602e9bb053a0 590 if (mortal == false) {
el14moh 2:602e9bb053a0 591 invun_cool++;
el14moh 5:c64d8ba051e9 592 if (invun_cool > 30) { // ticker called 20 times per second, therefore 1.5s of invincibility
el14moh 2:602e9bb053a0 593 mortal = true;
el14moh 2:602e9bb053a0 594 invun_cool=0;
el14moh 2:602e9bb053a0 595 }
el14moh 2:602e9bb053a0 596 }
el14moh 2:602e9bb053a0 597 }
el14moh 2:602e9bb053a0 598
el14moh 2:602e9bb053a0 599 void checkWallCollision() // checks for any collisions (i.e. player has hit a wall or side of the screen)
el14moh 2:602e9bb053a0 600 {
el14moh 0:6b29f9c29a2a 601
el14moh 0:6b29f9c29a2a 602 // LEFT WALL
el14moh 0:6b29f9c29a2a 603 if ((((i - PLAYERRADIUS) <= leftwall.x) && (leftwall.x <= (i + PLAYERRADIUS))) && (j > (leftwall.y+3) || j < (leftwall.y-3))) {
el14moh 0:6b29f9c29a2a 604 game_over_flag = 1;
el14moh 0:6b29f9c29a2a 605 }
el14moh 0:6b29f9c29a2a 606
el14moh 0:6b29f9c29a2a 607 // RIGHT WALL
el14moh 0:6b29f9c29a2a 608 if ((((i - PLAYERRADIUS) <= rightwall.x) && (rightwall.x <= (i + PLAYERRADIUS))) && (j > (rightwall.y+3) || j < (rightwall.y-3))) {
el14moh 0:6b29f9c29a2a 609 game_over_flag = 1;
el14moh 0:6b29f9c29a2a 610 }
el14moh 0:6b29f9c29a2a 611
el14moh 0:6b29f9c29a2a 612 // DOWN WALL
el14moh 0:6b29f9c29a2a 613 if ((((j - PLAYERRADIUS) <= downwall.y) && (downwall.y <= (j - PLAYERRADIUS))) && (i > (downwall.x+9) || i < (downwall.x-9))) { // if player x is 4 or more than wall x, it has missed the gap
el14moh 0:6b29f9c29a2a 614 game_over_flag = 1;
el14moh 0:6b29f9c29a2a 615 }
el14moh 0:6b29f9c29a2a 616
el14moh 0:6b29f9c29a2a 617 // UP WALL
el14moh 0:6b29f9c29a2a 618 if (((j + PLAYERRADIUS) == upwall.y || (j - PLAYERRADIUS) == upwall.y) && (i > (upwall.x+9) || i < (upwall.x-9))) { // if player x is 4 or more than wall x, it has missed the gap
el14moh 0:6b29f9c29a2a 619 game_over_flag = 1;
el14moh 0:6b29f9c29a2a 620 }
el14moh 0:6b29f9c29a2a 621 }
el14moh 0:6b29f9c29a2a 622
el14moh 0:6b29f9c29a2a 623 void updateScreen() // refreshes the screen, redraws player and walls
el14moh 0:6b29f9c29a2a 624 {
el14moh 0:6b29f9c29a2a 625 lcd.clear();
el14moh 2:602e9bb053a0 626 if (mortal) {
el14moh 2:602e9bb053a0 627 lcd.drawCircle(i,j,PLAYERRADIUS,1);
el14moh 2:602e9bb053a0 628 } else {
el14moh 2:602e9bb053a0 629 if (counter % 2 == 0) {
el14moh 2:602e9bb053a0 630 lcd.drawCircle(i,j,PLAYERRADIUS,1);
el14moh 2:602e9bb053a0 631 }
el14moh 2:602e9bb053a0 632 }
el14moh 0:6b29f9c29a2a 633 lcd.refresh(); // update display
el14moh 0:6b29f9c29a2a 634
el14moh 3:8890b4605a10 635 if (saves > 0) {
el14moh 3:8890b4605a10 636 lcd.drawCircle(2,7,1,1);
el14moh 3:8890b4605a10 637 }
el14moh 3:8890b4605a10 638 if (saves > 1) {
el14moh 3:8890b4605a10 639 lcd.drawCircle(2,15,1,1);
el14moh 3:8890b4605a10 640 }
el14moh 3:8890b4605a10 641 if (saves > 2) {
el14moh 3:8890b4605a10 642 lcd.drawCircle(2,23,1,1);
el14moh 3:8890b4605a10 643 }
el14moh 3:8890b4605a10 644 if (saves > 3) {
el14moh 3:8890b4605a10 645 lcd.drawCircle(2,31,1,1);
el14moh 3:8890b4605a10 646 }
el14moh 3:8890b4605a10 647 if (saves > 4) {
el14moh 3:8890b4605a10 648 lcd.drawCircle(2,39,1,1);
el14moh 3:8890b4605a10 649 }
el14moh 3:8890b4605a10 650
el14moh 2:602e9bb053a0 651
el14moh 0:6b29f9c29a2a 652 // draw Border
el14moh 6:153680563027 653 draw_border();
el14moh 0:6b29f9c29a2a 654
el14moh 0:6b29f9c29a2a 655 // draw walls
el14moh 0:6b29f9c29a2a 656 // LEFT WALL
el14moh 0:6b29f9c29a2a 657 lcd.drawLine(leftwall.x,leftwall.y+5,leftwall.x,44,1);
el14moh 0:6b29f9c29a2a 658 lcd.drawLine(leftwall.x,leftwall.y-5,leftwall.x,3,1);
el14moh 0:6b29f9c29a2a 659 lcd.drawLine(leftwall.x+1,leftwall.y+5,leftwall.x+1,44,1);
el14moh 0:6b29f9c29a2a 660 lcd.drawLine(leftwall.x+1,leftwall.y-5,leftwall.x+1,3,1);
el14moh 0:6b29f9c29a2a 661
el14moh 0:6b29f9c29a2a 662 // RIGHT WALL
el14moh 0:6b29f9c29a2a 663 if (counter > 200) {
el14moh 0:6b29f9c29a2a 664 lcd.drawLine(rightwall.x,rightwall.y+5,rightwall.x,44,1);
el14moh 0:6b29f9c29a2a 665 lcd.drawLine(rightwall.x,rightwall.y-5,rightwall.x,3,1);
el14moh 0:6b29f9c29a2a 666 lcd.drawLine(rightwall.x-1,rightwall.y+5,rightwall.x-1,44,1);
el14moh 0:6b29f9c29a2a 667 lcd.drawLine(rightwall.x-1,rightwall.y-5,rightwall.x-1,3,1);
el14moh 0:6b29f9c29a2a 668 }
el14moh 0:6b29f9c29a2a 669
el14moh 0:6b29f9c29a2a 670 // DOWN WALL
el14moh 0:6b29f9c29a2a 671 if (counter > 600) {
el14moh 0:6b29f9c29a2a 672 lcd.drawLine(downwall.x+11,downwall.y,80,downwall.y,1);
el14moh 3:8890b4605a10 673 lcd.drawLine(downwall.x-11,downwall.y,8,downwall.y,1);
el14moh 0:6b29f9c29a2a 674 lcd.drawLine(downwall.x+11,downwall.y-1,80,downwall.y-1,1);
el14moh 3:8890b4605a10 675 lcd.drawLine(downwall.x-11,downwall.y-1,8,downwall.y-1,1);
el14moh 0:6b29f9c29a2a 676 }
el14moh 0:6b29f9c29a2a 677
el14moh 0:6b29f9c29a2a 678 // UP WALL
el14moh 0:6b29f9c29a2a 679 if (counter > 1500) {
el14moh 0:6b29f9c29a2a 680 lcd.drawLine(upwall.x+11,upwall.y,80,upwall.y,1);
el14moh 3:8890b4605a10 681 lcd.drawLine(upwall.x-11,upwall.y,8,upwall.y,1);
el14moh 0:6b29f9c29a2a 682 lcd.drawLine(upwall.x+11,upwall.y+1,80,upwall.y+1,1);
el14moh 3:8890b4605a10 683 lcd.drawLine(upwall.x-11,upwall.y+1,8,upwall.y+1,1);
el14moh 0:6b29f9c29a2a 684 }
el14moh 0:6b29f9c29a2a 685
el14moh 0:6b29f9c29a2a 686 lcd.refresh();
el14moh 0:6b29f9c29a2a 687 }
el14moh 0:6b29f9c29a2a 688
el14moh 0:6b29f9c29a2a 689 void warning ()
el14moh 0:6b29f9c29a2a 690 {
el14moh 0:6b29f9c29a2a 691 if (counter == 170) {
el14moh 0:6b29f9c29a2a 692 lcd.inverseMode();
el14moh 0:6b29f9c29a2a 693 } else if (counter == 570) {
el14moh 0:6b29f9c29a2a 694 lcd.inverseMode();
el14moh 0:6b29f9c29a2a 695 } else if (counter == 1470) {
el14moh 0:6b29f9c29a2a 696 lcd.inverseMode();
el14moh 0:6b29f9c29a2a 697 } else {
el14moh 0:6b29f9c29a2a 698 lcd.normalMode();
el14moh 0:6b29f9c29a2a 699 }
el14moh 0:6b29f9c29a2a 700 }
el14moh 0:6b29f9c29a2a 701
el14moh 0:6b29f9c29a2a 702 void game_timer_isr() // sets flag for timer interrupt
el14moh 0:6b29f9c29a2a 703 {
el14moh 0:6b29f9c29a2a 704 g_timer_flag = 1;
el14moh 0:6b29f9c29a2a 705 }
el14moh 0:6b29f9c29a2a 706
el14moh 2:602e9bb053a0 707 void button_isr()
el14moh 2:602e9bb053a0 708 {
el14moh 2:602e9bb053a0 709 g_button_flag = 1;
el14moh 2:602e9bb053a0 710 }
el14moh 2:602e9bb053a0 711
el14moh 0:6b29f9c29a2a 712 void initDisplay() // initialises the LCD display
el14moh 0:6b29f9c29a2a 713 {
el14moh 0:6b29f9c29a2a 714 lcd.init();
el14moh 0:6b29f9c29a2a 715 lcd.normalMode();
el14moh 0:6b29f9c29a2a 716 lcd.setBrightness(1.0F-backlight); // brightness pot on PCB is soldered in the wrong direction, (1.0F-backlight) inverts the reading
el14moh 0:6b29f9c29a2a 717 }
el14moh 0:6b29f9c29a2a 718
el14moh 0:6b29f9c29a2a 719 void calibrateJoystick() // read default positions of the joystick to calibrate later readings
el14moh 0:6b29f9c29a2a 720 {
el14moh 0:6b29f9c29a2a 721 button.mode(PullDown);
el14moh 0:6b29f9c29a2a 722 // must not move during calibration
el14moh 0:6b29f9c29a2a 723 joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
el14moh 0:6b29f9c29a2a 724 joystick.y0 = yPot;
el14moh 0:6b29f9c29a2a 725 }
el14moh 0:6b29f9c29a2a 726 void updateJoystick() // reads direction the joystick has been moved
el14moh 0:6b29f9c29a2a 727 {
el14moh 0:6b29f9c29a2a 728 // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
el14moh 0:6b29f9c29a2a 729 joystick.x = xPot - joystick.x0;
el14moh 0:6b29f9c29a2a 730 joystick.y = yPot - joystick.y0;
el14moh 0:6b29f9c29a2a 731 // read button state
el14moh 0:6b29f9c29a2a 732 joystick.button = button;
el14moh 0:6b29f9c29a2a 733
el14moh 0:6b29f9c29a2a 734 // calculate direction depending on x,y values
el14moh 0:6b29f9c29a2a 735 // tolerance allows a little lee-way in case joystick not exactly in the stated direction
el14moh 0:6b29f9c29a2a 736 if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 737 joystick.direction = CENTRE;
el14moh 0:6b29f9c29a2a 738 } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 739 joystick.direction = UP;
el14moh 0:6b29f9c29a2a 740 } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 741 joystick.direction = DOWN;
el14moh 0:6b29f9c29a2a 742 } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 743 joystick.direction = LEFT;
el14moh 0:6b29f9c29a2a 744 } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 745 joystick.direction = RIGHT;
el14moh 0:6b29f9c29a2a 746 } else if ( joystick.y > DIRECTION_TOLERANCE && joystick.x > DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 747 joystick.direction = UPLEFT;
el14moh 0:6b29f9c29a2a 748 } else if ( joystick.y > DIRECTION_TOLERANCE && joystick.x < DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 749 joystick.direction = UPRIGHT;
el14moh 0:6b29f9c29a2a 750 } else if ( joystick.x > DIRECTION_TOLERANCE && joystick.y < DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 751 joystick.direction = DOWNLEFT;
el14moh 0:6b29f9c29a2a 752 } else if ( joystick.x < DIRECTION_TOLERANCE && joystick.y < DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 753 joystick.direction = DOWNRIGHT;
el14moh 0:6b29f9c29a2a 754 }
el14moh 0:6b29f9c29a2a 755
el14moh 0:6b29f9c29a2a 756 else {
el14moh 0:6b29f9c29a2a 757 joystick.direction = UNKNOWN;
el14moh 0:6b29f9c29a2a 758 }
el14moh 0:6b29f9c29a2a 759
el14moh 0:6b29f9c29a2a 760
el14moh 0:6b29f9c29a2a 761 printFlag = 1; // set flag for printing
el14moh 0:6b29f9c29a2a 762 }
el14moh 0:6b29f9c29a2a 763
el14moh 0:6b29f9c29a2a 764 void debug() // prints for debug purposes
el14moh 0:6b29f9c29a2a 765 {
el14moh 0:6b29f9c29a2a 766 if (printFlag) { // if flag set, clear flag and print joystick values to serial port
el14moh 0:6b29f9c29a2a 767 printFlag = 0;
el14moh 0:6b29f9c29a2a 768 /*
el14moh 0:6b29f9c29a2a 769 if (joystick.direction == UP)
el14moh 0:6b29f9c29a2a 770 serial.printf(" UP\n");
el14moh 0:6b29f9c29a2a 771 if (joystick.direction == DOWN)
el14moh 0:6b29f9c29a2a 772 serial.printf(" DOWN\n");
el14moh 0:6b29f9c29a2a 773 if (joystick.direction == LEFT)
el14moh 0:6b29f9c29a2a 774 serial.printf(" LEFT\n");
el14moh 0:6b29f9c29a2a 775 if (joystick.direction == RIGHT)
el14moh 0:6b29f9c29a2a 776 serial.printf(" RIGHT\n");
el14moh 0:6b29f9c29a2a 777 if (joystick.direction == UPRIGHT)
el14moh 0:6b29f9c29a2a 778 serial.printf(" UPRIGHT\n"); // Diagonally up + right
el14moh 0:6b29f9c29a2a 779 if (joystick.direction == UPLEFT)
el14moh 0:6b29f9c29a2a 780 serial.printf(" UPLEFT\n"); // Diagonally up + left
el14moh 0:6b29f9c29a2a 781 if (joystick.direction == DOWNRIGHT)
el14moh 0:6b29f9c29a2a 782 serial.printf(" DOWNRIGHT\n"); // Diagonally down + right
el14moh 0:6b29f9c29a2a 783 if (joystick.direction == DOWNLEFT)
el14moh 0:6b29f9c29a2a 784 serial.printf(" DOWNLEFT\n"); // Diagonally down + left
el14moh 0:6b29f9c29a2a 785 if (joystick.direction == CENTRE)
el14moh 0:6b29f9c29a2a 786 serial.printf(" CENTRE\n");
el14moh 0:6b29f9c29a2a 787 if (joystick.direction == UNKNOWN)
el14moh 0:6b29f9c29a2a 788 serial.printf(" Unsupported direction\n");
el14moh 0:6b29f9c29a2a 789 */
el14moh 2:602e9bb053a0 790 serial.printf("saves = %d \n",saves);
el14moh 2:602e9bb053a0 791 //serial.printf("Right-wall Cooldown = %d \n",rightwall.cooldown);
el14moh 2:602e9bb053a0 792 //serial.printf("counter = %d \n",counter);
el14moh 0:6b29f9c29a2a 793
el14moh 0:6b29f9c29a2a 794 }
el14moh 0:6b29f9c29a2a 795 }
el14moh 0:6b29f9c29a2a 796
el14moh 7:ca18e8775d8d 797 void initSerial() // sets baud rate for serial
el14moh 0:6b29f9c29a2a 798 {
el14moh 0:6b29f9c29a2a 799 // set to highest baud - ensure terminal software matches
el14moh 0:6b29f9c29a2a 800 serial.baud(115200);
el14moh 0:6b29f9c29a2a 801 }
el14moh 7:ca18e8775d8d 802 void initGame()
el14moh 3:8890b4605a10 803 {
el14moh 0:6b29f9c29a2a 804
el14moh 3:8890b4605a10 805 g_button_flag = 0;
el14moh 3:8890b4605a10 806 leftwall.y = rand() % 27+8;
el14moh 3:8890b4605a10 807 leftwall.x = 0;
el14moh 6:153680563027 808 rightwall.y = rand() % 58+13;
el14moh 6:153680563027 809 rightwall.x = 6;
el14moh 3:8890b4605a10 810 downwall.x = rand() % 27+8;
el14moh 3:8890b4605a10 811 downwall.y = 0;
el14moh 6:153680563027 812 upwall.x = rand() % 5814;
el14moh 3:8890b4605a10 813 upwall.y = 0;
el14moh 3:8890b4605a10 814 rightwall.cooldown = 61;
el14moh 3:8890b4605a10 815 counter = 0;
el14moh 7:ca18e8775d8d 816 score = 0;
el14moh 3:8890b4605a10 817
el14moh 3:8890b4605a10 818 saves = 5;
el14moh 3:8890b4605a10 819
el14moh 3:8890b4605a10 820 i = 42;
el14moh 3:8890b4605a10 821 j = 24;
el14moh 3:8890b4605a10 822
el14moh 3:8890b4605a10 823 }
el14moh 6:153680563027 824
el14moh 8:b49e0cc362f2 825 void blink() // command for brief flash
el14moh 6:153680563027 826 {
el14moh 6:153680563027 827 lcd.inverseMode();
el14moh 6:153680563027 828 wait(0.2);
el14moh 6:153680563027 829 lcd.normalMode();
el14moh 6:153680563027 830 }
el14moh 6:153680563027 831
el14moh 6:153680563027 832 void printHelp()
el14moh 6:153680563027 833 {
el14moh 6:153680563027 834 lcd.printString("Try and",0,1);
el14moh 6:153680563027 835 lcd.printString("survive for",0,2);
el14moh 6:153680563027 836 lcd.printString("as long as ",0,3);
el14moh 6:153680563027 837 lcd.printString("possible!",0,4);
el14moh 6:153680563027 838 wait(4);
el14moh 6:153680563027 839
el14moh 6:153680563027 840 blink();
el14moh 6:153680563027 841 lcd.clear();
el14moh 6:153680563027 842
el14moh 6:153680563027 843 lcd.printString("Get through",0,1);
el14moh 6:153680563027 844 lcd.printString("the holes in",0,2);
el14moh 6:153680563027 845 lcd.printString("the walls with",0,3);
el14moh 6:153680563027 846 lcd.printString("the joystick",0,4);
el14moh 6:153680563027 847 wait(4);
el14moh 6:153680563027 848
el14moh 6:153680563027 849 blink();
el14moh 6:153680563027 850 lcd.clear();
el14moh 6:153680563027 851
el14moh 6:153680563027 852 lcd.printString("Press the",0,1);
el14moh 6:153680563027 853 lcd.printString("stick to",0,2);
el14moh 6:153680563027 854 lcd.printString("be briefly",0,3);
el14moh 6:153680563027 855 lcd.printString("untouchable",0,4);
el14moh 6:153680563027 856 wait(4);
el14moh 6:153680563027 857
el14moh 6:153680563027 858 blink();
el14moh 6:153680563027 859 lcd.clear();
el14moh 6:153680563027 860
el14moh 6:153680563027 861 lcd.printString("But don't use",0,1);
el14moh 6:153680563027 862 lcd.printString("it too much!",0,2);
el14moh 6:153680563027 863 lcd.printString("you can only",0,3);
el14moh 6:153680563027 864 lcd.printString("do it 5 times",0,4);
el14moh 6:153680563027 865 wait(4);
el14moh 6:153680563027 866
el14moh 6:153680563027 867 blink();
el14moh 6:153680563027 868 lcd.clear();
el14moh 6:153680563027 869
el14moh 6:153680563027 870 lcd.printString("Lastly, keep",0,1);
el14moh 6:153680563027 871 lcd.printString("an eye on the",0,2);
el14moh 6:153680563027 872 lcd.printString("borders of",0,3);
el14moh 6:153680563027 873 lcd.printString("the screen...",0,4);
el14moh 6:153680563027 874 wait(4);
el14moh 6:153680563027 875
el14moh 6:153680563027 876 blink();
el14moh 6:153680563027 877 lcd.clear();
el14moh 6:153680563027 878
el14moh 6:153680563027 879 lcd.printString("See where the",0,1);
el14moh 6:153680563027 880 lcd.printString("holes are",0,2);
el14moh 6:153680563027 881 lcd.printString("before the",0,3);
el14moh 6:153680563027 882 lcd.printString("walls move!",0,4);
el14moh 6:153680563027 883 wait(4);
el14moh 6:153680563027 884
el14moh 6:153680563027 885 blink();
el14moh 6:153680563027 886 lcd.clear();
el14moh 6:153680563027 887 }
el14moh 6:153680563027 888
el14moh 6:153680563027 889 void printScores()
el14moh 6:153680563027 890 {
el14moh 6:153680563027 891 lcd.printString("Highscores",13,0);
el14moh 6:153680563027 892 lcd.printString("1st:",6,2);
el14moh 6:153680563027 893 lcd.printString("2nd:",6,3);
el14moh 6:153680563027 894 lcd.printString("3rd:",6,4);
el14moh 6:153680563027 895 char first[14];
el14moh 6:153680563027 896 char second[14];
el14moh 6:153680563027 897 char third[14];
el14moh 6:153680563027 898 int one = sprintf(first,"%d",hiscore.one);
el14moh 6:153680563027 899 int two = sprintf(second,"%d",hiscore.two);
el14moh 6:153680563027 900 int three = sprintf(third,"%d",hiscore.three);
el14moh 8:b49e0cc362f2 901
el14moh 8:b49e0cc362f2 902 lcd.printString(first,30,2);
el14moh 8:b49e0cc362f2 903
el14moh 8:b49e0cc362f2 904
el14moh 8:b49e0cc362f2 905 lcd.printString(second,30,3);
el14moh 8:b49e0cc362f2 906
el14moh 8:b49e0cc362f2 907
el14moh 8:b49e0cc362f2 908 lcd.printString(third,30,4);
el14moh 8:b49e0cc362f2 909
el14moh 6:153680563027 910 lcd.refresh();
el14moh 6:153680563027 911 wait(4);
el14moh 6:153680563027 912 blink();
el14moh 6:153680563027 913 lcd.clear();
el14moh 6:153680563027 914
el14moh 6:153680563027 915 }
el14moh 6:153680563027 916
el14moh 8:b49e0cc362f2 917 void draw_border() // draws game border
el14moh 6:153680563027 918 {
el14moh 6:153680563027 919 lcd.drawLine(7,2,81,2,1);
el14moh 6:153680563027 920 lcd.drawLine(7,2,7,45,1);
el14moh 6:153680563027 921 lcd.drawLine(81,2,81,45,1);
el14moh 6:153680563027 922 lcd.drawLine(7,45,81,45,1);
el14moh 6:153680563027 923
el14moh 6:153680563027 924 lcd.drawLine(5,0,83,0,1);
el14moh 6:153680563027 925 lcd.drawLine(5,0,5,47,1);
el14moh 6:153680563027 926 lcd.drawLine(83,0,83,47,1);
el14moh 6:153680563027 927 lcd.drawLine(5,47,83,47,1);
el14moh 6:153680563027 928
el14moh 6:153680563027 929 lcd.refresh();
el14moh 6:153680563027 930 }
el14moh 6:153680563027 931
el14moh 6:153680563027 932 void calculateHighscores()
el14moh 6:153680563027 933 {
el14moh 7:ca18e8775d8d 934 if (score > hiscore.one) {
el14moh 6:153680563027 935 hiscore.three = hiscore.two;
el14moh 6:153680563027 936 hiscore.two = hiscore.one;
el14moh 7:ca18e8775d8d 937 hiscore.one = score;
el14moh 7:ca18e8775d8d 938 } else if ((hiscore.one > score)&&(score > hiscore.two)) {
el14moh 6:153680563027 939 hiscore.three = hiscore.two;
el14moh 7:ca18e8775d8d 940 hiscore.two = score;
el14moh 7:ca18e8775d8d 941 } else if ((hiscore.two > score)&&(score > hiscore.three)) {
el14moh 7:ca18e8775d8d 942 hiscore.three = score;
el14moh 6:153680563027 943 }
el14moh 6:153680563027 944 }
el14moh 6:153680563027 945
el14moh 6:153680563027 946 void initHiscores()
el14moh 6:153680563027 947 {
el14moh 6:153680563027 948 hiscore.one = 0;
el14moh 6:153680563027 949 hiscore.two = 0;
el14moh 6:153680563027 950 hiscore.three = 0;
el14moh 8:b49e0cc362f2 951 }
el14moh 8:b49e0cc362f2 952
el14moh 8:b49e0cc362f2 953 void playNote(int freq, float time)
el14moh 8:b49e0cc362f2 954 {
el14moh 8:b49e0cc362f2 955 if(!MUTE) { // no sound if MUTE
el14moh 8:b49e0cc362f2 956 buzzer.period(1.0/freq); // 1 KHz
el14moh 8:b49e0cc362f2 957 buzzer.write(0.0);
el14moh 8:b49e0cc362f2 958 buzzer.write(0.5);
el14moh 8:b49e0cc362f2 959 wait(time);
el14moh 8:b49e0cc362f2 960 buzzer.write(0.0);
el14moh 8:b49e0cc362f2 961 } else {
el14moh 8:b49e0cc362f2 962 wait(time); // wait included so that function still takes the same time to execute
el14moh 8:b49e0cc362f2 963 }
el14moh 6:153680563027 964 }