Wall dodging game utilising a joystick and Nokia 5110 LCD display

Dependencies:   N5110 mbed

Committer:
el14moh
Date:
Sat Apr 23 23:38:53 2016 +0000
Revision:
7:ca18e8775d8d
Parent:
6:153680563027
Child:
8:b49e0cc362f2
previous code updated to have score seperate from counter

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 6:153680563027 42 //#define GAMESPEED 20 // game timer speed
el14moh 6:153680563027 43 //#define JOYSPEED 20 // rate at which the joystick is read
el14moh 0:6b29f9c29a2a 44
el14moh 0:6b29f9c29a2a 45 // VCC, SCE, RST, D/C, MOSI, SCLK, LED
el14moh 0:6b29f9c29a2a 46 N5110 lcd (PTD3 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3);
el14moh 0:6b29f9c29a2a 47
el14moh 5:c64d8ba051e9 48 AnalogIn backlight(PTB2); // pot to control brightness
el14moh 0:6b29f9c29a2a 49 DigitalIn button(PTB3); // joystick button object
el14moh 0:6b29f9c29a2a 50 AnalogIn xPot(PTB11); // joystick x direction object
el14moh 0:6b29f9c29a2a 51 AnalogIn yPot(PTB10); // joystick y direction object
el14moh 0:6b29f9c29a2a 52 DigitalOut buzzer(PTA2); // buzzer object
el14moh 2:602e9bb053a0 53 InterruptIn flick (PTB3); // interruptin instance of button
el14moh 0:6b29f9c29a2a 54
el14moh 0:6b29f9c29a2a 55 Ticker pollJoystick; // timer to regularly read the joystick
el14moh 6:153680563027 56 Ticker menu_pollJoystick; // slower ticker to move joystick through menu
el14moh 0:6b29f9c29a2a 57 Ticker game_timer; // timer to regularly update the screen
el14moh 0:6b29f9c29a2a 58
el14moh 0:6b29f9c29a2a 59 Serial serial(USBTX,USBRX); // Serial for debug
el14moh 0:6b29f9c29a2a 60
el14moh 0:6b29f9c29a2a 61 // create enumerated type (0,1,2,3 etc. for direction)
el14moh 0:6b29f9c29a2a 62 enum DirectionName {
el14moh 0:6b29f9c29a2a 63 UP,
el14moh 0:6b29f9c29a2a 64 DOWN,
el14moh 0:6b29f9c29a2a 65 LEFT,
el14moh 0:6b29f9c29a2a 66 RIGHT,
el14moh 0:6b29f9c29a2a 67 UPRIGHT, // Diagonally up + right
el14moh 0:6b29f9c29a2a 68 UPLEFT, // Diagonally up + left
el14moh 0:6b29f9c29a2a 69 DOWNRIGHT, // Diagonally down + right
el14moh 0:6b29f9c29a2a 70 DOWNLEFT, // Diagonally down + left
el14moh 0:6b29f9c29a2a 71 CENTRE,
el14moh 0:6b29f9c29a2a 72 UNKNOWN
el14moh 0:6b29f9c29a2a 73 };
el14moh 0:6b29f9c29a2a 74
el14moh 0:6b29f9c29a2a 75 typedef struct JoyStick Joystick; // struct for Joystick
el14moh 0:6b29f9c29a2a 76 typedef struct Wall Wall; // struct for Walls
el14moh 6:153680563027 77 typedef const struct State STyp;
el14moh 6:153680563027 78 typedef struct Highscore Highscore;
el14moh 0:6b29f9c29a2a 79
el14moh 0:6b29f9c29a2a 80 struct JoyStick {
el14moh 0:6b29f9c29a2a 81 float x; // current x value
el14moh 0:6b29f9c29a2a 82 float x0; // 'centred' x value
el14moh 0:6b29f9c29a2a 83 float y; // current y value
el14moh 0:6b29f9c29a2a 84 float y0; // 'centred' y value
el14moh 0:6b29f9c29a2a 85 int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
el14moh 0:6b29f9c29a2a 86 DirectionName direction; // current direction
el14moh 0:6b29f9c29a2a 87 };
el14moh 0:6b29f9c29a2a 88
el14moh 0:6b29f9c29a2a 89 struct Wall {
el14moh 0:6b29f9c29a2a 90 int x; // x-coordinate of wall (realtive to centre of the gap)
el14moh 0:6b29f9c29a2a 91 int y; // y-coordinate of wall (relative to centre of the gap)
el14moh 0:6b29f9c29a2a 92 DirectionName direction; // Direction the wall travels in
el14moh 0:6b29f9c29a2a 93 int random; // randomly generated integer to determine when a wall begins to travel
el14moh 0:6b29f9c29a2a 94 int cooldown; // stops a wall respawning before a certain amount of time
el14moh 1:26ebbb94cf36 95 volatile bool moveFlag; // flag to determine if wall is on screen
el14moh 1:26ebbb94cf36 96 volatile bool genFlag; // flag to determine if wall has been generated
el14moh 0:6b29f9c29a2a 97 };
el14moh 0:6b29f9c29a2a 98
el14moh 6:153680563027 99 struct State {
el14moh 6:153680563027 100 int output; // output value for current state
el14moh 6:153680563027 101 int next_state[3]; // next state (depending on direction 0 - UP, 1 - DOWN)
el14moh 6:153680563027 102 };
el14moh 6:153680563027 103
el14moh 6:153680563027 104 struct Highscore {
el14moh 6:153680563027 105 int one;
el14moh 6:153680563027 106 int two;
el14moh 6:153680563027 107 int three;
el14moh 6:153680563027 108 };
el14moh 6:153680563027 109
el14moh 6:153680563027 110 STyp fsm[4] = {
el14moh 6:153680563027 111 {1,{0,1,0}},
el14moh 6:153680563027 112 {2,{1,2,0}},
el14moh 6:153680563027 113 {4,{2,3,1}},
el14moh 6:153680563027 114 {8,{3,3,2}}
el14moh 6:153680563027 115 };
el14moh 6:153680563027 116
el14moh 0:6b29f9c29a2a 117 // struct variable for joystick
el14moh 0:6b29f9c29a2a 118 Joystick joystick;
el14moh 0:6b29f9c29a2a 119 // struct variable for moving walls
el14moh 0:6b29f9c29a2a 120 Wall leftwall;
el14moh 0:6b29f9c29a2a 121 Wall rightwall;
el14moh 0:6b29f9c29a2a 122 Wall downwall;
el14moh 0:6b29f9c29a2a 123 Wall upwall;
el14moh 0:6b29f9c29a2a 124
el14moh 6:153680563027 125 Highscore hiscore;
el14moh 6:153680563027 126
el14moh 0:6b29f9c29a2a 127 void calibrateJoystick(); // read default positions of the joystick to calibrate later readings
el14moh 0:6b29f9c29a2a 128 void updateJoystick(); // reads direction the joystick has been moved
el14moh 0:6b29f9c29a2a 129 void initDisplay(); // initialises the LCD display
el14moh 0:6b29f9c29a2a 130 void game_timer_isr(); // sets flag for timer interrupt
el14moh 7:ca18e8775d8d 131 void initGame();
el14moh 0:6b29f9c29a2a 132 void moveBall(); // reads joystick direction and moves position of the player
el14moh 0:6b29f9c29a2a 133 void moveWall(); // moves walls along the screen
el14moh 2:602e9bb053a0 134 void invincible(); // makes player briefly invincible
el14moh 2:602e9bb053a0 135 void checkWallCollision(); // checks for any collisions with wall
el14moh 2:602e9bb053a0 136 void checkBorderCollision(); // checks for any collisions with border
el14moh 0:6b29f9c29a2a 137 void updateScreen(); // refreshes the screen, redraws player and walls
el14moh 0:6b29f9c29a2a 138 void warning(); // flashes screen when a new wall is ready to appear
el14moh 7:ca18e8775d8d 139 void initSerial(); // sets baud rate for serial
el14moh 0:6b29f9c29a2a 140 void debug(); // prints for debug purposes
el14moh 2:602e9bb053a0 141 void button_isr();
el14moh 6:153680563027 142 void blink(); // function to make screen flash (do not put in isr unless breaking the loop)
el14moh 6:153680563027 143 void printHelp(); // prints game help
el14moh 6:153680563027 144 void printScores();
el14moh 6:153680563027 145 void draw_border();
el14moh 6:153680563027 146 void calculateHighscores();
el14moh 6:153680563027 147 void initHiscores();
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 0:6b29f9c29a2a 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 0:6b29f9c29a2a 278 wait(0.5);
el14moh 0:6b29f9c29a2a 279 lcd.drawRect(10,10,64,28,2);
el14moh 0:6b29f9c29a2a 280 lcd.refresh();
el14moh 0:6b29f9c29a2a 281 wait(0.5);
el14moh 0:6b29f9c29a2a 282 lcd.printString("2",40,2);
el14moh 0:6b29f9c29a2a 283 wait(0.5);
el14moh 0:6b29f9c29a2a 284 lcd.drawRect(10,10,64,28,2);
el14moh 0:6b29f9c29a2a 285 lcd.refresh();
el14moh 0:6b29f9c29a2a 286 wait(0.5);
el14moh 0:6b29f9c29a2a 287 lcd.printString("1",40,2);
el14moh 0:6b29f9c29a2a 288 wait(0.5);
el14moh 0:6b29f9c29a2a 289 lcd.drawRect(10,10,64,28,2);
el14moh 0:6b29f9c29a2a 290 lcd.refresh();
el14moh 0:6b29f9c29a2a 291 wait(0.5);
el14moh 0:6b29f9c29a2a 292 lcd.drawRect(10,10,64,28,2);
el14moh 0:6b29f9c29a2a 293 lcd.refresh();
el14moh 0:6b29f9c29a2a 294 lcd.printString("Go!",36,2);
el14moh 0:6b29f9c29a2a 295 wait(0.5);
el14moh 0:6b29f9c29a2a 296 lcd.drawRect(10,10,64,28,2);
el14moh 0:6b29f9c29a2a 297 lcd.refresh();
el14moh 0:6b29f9c29a2a 298
el14moh 6:153680563027 299 joyspeed = 20;
el14moh 6:153680563027 300 game_timer.detach();
el14moh 6:153680563027 301 game_timer.attach(&game_timer_isr,1.0/joyspeed);
el14moh 6:153680563027 302 pollJoystick.detach();
el14moh 6:153680563027 303 pollJoystick.attach(&updateJoystick,1.0/joyspeed);
el14moh 3:8890b4605a10 304
el14moh 0:6b29f9c29a2a 305 while (game_over_flag == 0) {
el14moh 0:6b29f9c29a2a 306
el14moh 0:6b29f9c29a2a 307 if ( g_timer_flag ) { // ticker interrupt
el14moh 0:6b29f9c29a2a 308 g_timer_flag = 0; // clear flag
el14moh 0:6b29f9c29a2a 309 moveWall();
el14moh 0:6b29f9c29a2a 310 moveBall();
el14moh 2:602e9bb053a0 311 invincible();
el14moh 2:602e9bb053a0 312 checkBorderCollision();
el14moh 2:602e9bb053a0 313 if (mortal) {
el14moh 2:602e9bb053a0 314 checkWallCollision();
el14moh 2:602e9bb053a0 315 }
el14moh 0:6b29f9c29a2a 316 updateScreen();
el14moh 0:6b29f9c29a2a 317 warning();
el14moh 0:6b29f9c29a2a 318 debug();
el14moh 0:6b29f9c29a2a 319
el14moh 0:6b29f9c29a2a 320 counter++; // increment counter each cycle (approx. 20 points a second)
el14moh 7:ca18e8775d8d 321 score++; // this is seperate from counter for the purposes of keeping score 0 while all walls are present on DodgeMANIA
el14moh 0:6b29f9c29a2a 322
el14moh 0:6b29f9c29a2a 323 // wall cooldowns increased. N.B these are set to 0 when a wall finishes moving
el14moh 0:6b29f9c29a2a 324 leftwall.cooldown++;
el14moh 0:6b29f9c29a2a 325 rightwall.cooldown++;
el14moh 0:6b29f9c29a2a 326 downwall.cooldown++;
el14moh 0:6b29f9c29a2a 327 upwall.cooldown++;
el14moh 0:6b29f9c29a2a 328 }
el14moh 0:6b29f9c29a2a 329 sleep();
el14moh 0:6b29f9c29a2a 330 }
el14moh 0:6b29f9c29a2a 331
el14moh 3:8890b4605a10 332 game_over_flag = 0;
el14moh 3:8890b4605a10 333
el14moh 6:153680563027 334 blink();
el14moh 0:6b29f9c29a2a 335 wait(2.0);
el14moh 0:6b29f9c29a2a 336 lcd.clear();
el14moh 0:6b29f9c29a2a 337 wait(0.2);
el14moh 0:6b29f9c29a2a 338
el14moh 6:153680563027 339 calculateHighscores();
el14moh 6:153680563027 340
el14moh 6:153680563027 341 lcd.printString("Game Over!",14,0);
el14moh 0:6b29f9c29a2a 342 lcd.refresh();
el14moh 0:6b29f9c29a2a 343
el14moh 0:6b29f9c29a2a 344 wait(1.0);
el14moh 6:153680563027 345
el14moh 6:153680563027 346 lcd.printString("HiScore:",3,3);
el14moh 6:153680563027 347 lcd.printString("Score:",3,2);
el14moh 6:153680563027 348
el14moh 6:153680563027 349 char score_buffer[14];
el14moh 6:153680563027 350 char hiscore_buffer[14];
el14moh 7:ca18e8775d8d 351 int length_one = sprintf(score_buffer,"%d",score);
el14moh 6:153680563027 352 int length_two = sprintf(hiscore_buffer,"%d",hiscore.one);
el14moh 7:ca18e8775d8d 353 if (score <= 9999) { // 9999 is highest number that fits on screen
el14moh 6:153680563027 354 lcd.printString(score_buffer,54,2);
el14moh 6:153680563027 355 lcd.printString(hiscore_buffer,54,3);
el14moh 0:6b29f9c29a2a 356 } else {
el14moh 6:153680563027 357 lcd.printString ("Wow!",54,2); // if score is too large to fit in box
el14moh 6:153680563027 358 lcd.printString ("Wow!",54,3);
el14moh 0:6b29f9c29a2a 359 }
el14moh 6:153680563027 360
el14moh 0:6b29f9c29a2a 361 lcd.refresh();
el14moh 0:6b29f9c29a2a 362
el14moh 7:ca18e8775d8d 363 if (score >= hiscore.one) {
el14moh 6:153680563027 364 wait(1.0);
el14moh 6:153680563027 365 lcd.printString("HIGHSCORE!",13,5);
el14moh 6:153680563027 366 wait(2.0);
el14moh 6:153680563027 367 lcd.printString("CONGRATS!!",13,5);
el14moh 6:153680563027 368 wait(2.0);
el14moh 6:153680563027 369 } else {
el14moh 6:153680563027 370 wait(5.0);
el14moh 6:153680563027 371 }
el14moh 6:153680563027 372 blink();
el14moh 0:6b29f9c29a2a 373 lcd.clear();
el14moh 6:153680563027 374 game_start_flag = 0;
el14moh 0:6b29f9c29a2a 375
el14moh 6:153680563027 376 //return 0;
el14moh 0:6b29f9c29a2a 377 }
el14moh 0:6b29f9c29a2a 378
el14moh 0:6b29f9c29a2a 379 }
el14moh 0:6b29f9c29a2a 380
el14moh 2:602e9bb053a0 381 void moveBall() // reads joystick direction and moves position of the player
el14moh 0:6b29f9c29a2a 382 {
el14moh 0:6b29f9c29a2a 383 if (joystick.direction == UP) {
el14moh 0:6b29f9c29a2a 384 j-=1;
el14moh 0:6b29f9c29a2a 385 } else if (joystick.direction == DOWN) {
el14moh 0:6b29f9c29a2a 386 j+=1;
el14moh 0:6b29f9c29a2a 387 } else if (joystick.direction == LEFT) {
el14moh 0:6b29f9c29a2a 388 i-=1;
el14moh 0:6b29f9c29a2a 389 } else if (joystick.direction == RIGHT) {
el14moh 0:6b29f9c29a2a 390 i+=1;
el14moh 0:6b29f9c29a2a 391 } else if (joystick.direction == UPRIGHT) {
el14moh 0:6b29f9c29a2a 392 j-=1;
el14moh 0:6b29f9c29a2a 393 i+=1;
el14moh 0:6b29f9c29a2a 394 } else if (joystick.direction == UPLEFT) {
el14moh 0:6b29f9c29a2a 395 j-=1;
el14moh 0:6b29f9c29a2a 396 i-=1;
el14moh 0:6b29f9c29a2a 397 } else if (joystick.direction == DOWNRIGHT) {
el14moh 0:6b29f9c29a2a 398 j+=1;
el14moh 0:6b29f9c29a2a 399 i+=1;
el14moh 0:6b29f9c29a2a 400 } else if (joystick.direction == DOWNLEFT) {
el14moh 0:6b29f9c29a2a 401 j+=1;
el14moh 0:6b29f9c29a2a 402 i-=1;
el14moh 0:6b29f9c29a2a 403 }
el14moh 0:6b29f9c29a2a 404 }
el14moh 0:6b29f9c29a2a 405
el14moh 2:602e9bb053a0 406 void moveWall() // moves walls along the screen
el14moh 0:6b29f9c29a2a 407 {
el14moh 0:6b29f9c29a2a 408 leftwall.random = rand()%20;
el14moh 0:6b29f9c29a2a 409 rightwall.random = rand()%20;
el14moh 0:6b29f9c29a2a 410 downwall.random = rand()%50;
el14moh 0:6b29f9c29a2a 411 upwall.random = rand()%50;
el14moh 0:6b29f9c29a2a 412
el14moh 0:6b29f9c29a2a 413 // LEFT WALL
el14moh 0:6b29f9c29a2a 414 if (leftwall.moveFlag == 1) { // if wall is moving
el14moh 0:6b29f9c29a2a 415 leftwall.x-=1; // move wall left
el14moh 3:8890b4605a10 416 if (leftwall.x<8) { // if wall hits a border
el14moh 0:6b29f9c29a2a 417 leftwall.moveFlag = 0; // stop wall moving
el14moh 0:6b29f9c29a2a 418 leftwall.cooldown = 0; // reset the cooldown for the wall
el14moh 0:6b29f9c29a2a 419 }
el14moh 0:6b29f9c29a2a 420 } else { // if wall has stopped moving
el14moh 0:6b29f9c29a2a 421 if (leftwall.genFlag == 0) { // if a new wall HASN'T been generated
el14moh 0:6b29f9c29a2a 422 leftwall.y = rand() % 27+8; // make new random y-coordinate
el14moh 0:6b29f9c29a2a 423 leftwall.x = 82; // reset x-coordinate to rightmost position
el14moh 0:6b29f9c29a2a 424 leftwall.genFlag = 1; // wall has been generated
el14moh 0:6b29f9c29a2a 425 } else { // if a new wall HAS been generated
el14moh 0:6b29f9c29a2a 426 if (leftwall.cooldown > 80) { // if a new wall hasnt started moving in 4 seconds, force it to move
el14moh 0:6b29f9c29a2a 427 leftwall.moveFlag = 1;
el14moh 0:6b29f9c29a2a 428 leftwall.genFlag = 0;
el14moh 0:6b29f9c29a2a 429 } else if ((leftwall.random == 1)&&(rightwall.cooldown > 60)) { // if wall starts moving again
el14moh 0:6b29f9c29a2a 430 leftwall.moveFlag = 1; // start wall moving
el14moh 0:6b29f9c29a2a 431 leftwall.genFlag = 0; // clear 'wall generated' flag
el14moh 0:6b29f9c29a2a 432 } else { // else if wall has not started moving again
el14moh 0:6b29f9c29a2a 433 leftwall.moveFlag = 0; // wall is stopped
el14moh 0:6b29f9c29a2a 434 }
el14moh 0:6b29f9c29a2a 435 }
el14moh 0:6b29f9c29a2a 436 }
el14moh 0:6b29f9c29a2a 437
el14moh 0:6b29f9c29a2a 438 // RIGHT WALL
el14moh 0:6b29f9c29a2a 439 if (counter > 200) {
el14moh 0:6b29f9c29a2a 440 if (rightwall.moveFlag == 1) {
el14moh 0:6b29f9c29a2a 441 rightwall.x+=1;
el14moh 0:6b29f9c29a2a 442 if (rightwall.x>80) {
el14moh 0:6b29f9c29a2a 443 rightwall.moveFlag = 0;
el14moh 0:6b29f9c29a2a 444 rightwall.cooldown = 0;
el14moh 0:6b29f9c29a2a 445 }
el14moh 0:6b29f9c29a2a 446 } else {
el14moh 0:6b29f9c29a2a 447 if ((rightwall.genFlag == 0)) {
el14moh 0:6b29f9c29a2a 448 rightwall.y = rand() % 27+8;
el14moh 3:8890b4605a10 449 rightwall.x = 6;
el14moh 0:6b29f9c29a2a 450 rightwall.genFlag = 1;
el14moh 0:6b29f9c29a2a 451 } else {
el14moh 0:6b29f9c29a2a 452 if (rightwall.cooldown > 80) {
el14moh 0:6b29f9c29a2a 453 rightwall.moveFlag = 1;
el14moh 0:6b29f9c29a2a 454 rightwall.genFlag = 0;
el14moh 0:6b29f9c29a2a 455 } else if ((rightwall.random == 1) && (leftwall.cooldown > 60)) {
el14moh 0:6b29f9c29a2a 456 rightwall.moveFlag = 1;
el14moh 0:6b29f9c29a2a 457 rightwall.genFlag = 0;
el14moh 0:6b29f9c29a2a 458 } else {
el14moh 0:6b29f9c29a2a 459 rightwall.moveFlag = 0;
el14moh 0:6b29f9c29a2a 460 }
el14moh 0:6b29f9c29a2a 461 }
el14moh 0:6b29f9c29a2a 462 }
el14moh 0:6b29f9c29a2a 463 }
el14moh 0:6b29f9c29a2a 464
el14moh 0:6b29f9c29a2a 465 // DOWN WALL
el14moh 0:6b29f9c29a2a 466 if (counter > 600) {
el14moh 0:6b29f9c29a2a 467 if (downwall.moveFlag == 1) {
el14moh 0:6b29f9c29a2a 468 if (upwall.cooldown > 60) {
el14moh 0:6b29f9c29a2a 469 if (counter % 2 == 1) { // horizontal walls move half the speed of vertical walls
el14moh 0:6b29f9c29a2a 470 downwall.y+=1;
el14moh 0:6b29f9c29a2a 471 }
el14moh 0:6b29f9c29a2a 472 }
el14moh 0:6b29f9c29a2a 473 if (downwall.y>44) {
el14moh 0:6b29f9c29a2a 474 downwall.moveFlag = 0;
el14moh 0:6b29f9c29a2a 475 }
el14moh 0:6b29f9c29a2a 476 } else {
el14moh 0:6b29f9c29a2a 477 if (downwall.genFlag == 0) {
el14moh 4:d44eace87ab3 478 downwall.x = rand() % 58+13;
el14moh 0:6b29f9c29a2a 479 downwall.y = 1;
el14moh 0:6b29f9c29a2a 480 downwall.genFlag = 1;
el14moh 0:6b29f9c29a2a 481 } else {
el14moh 0:6b29f9c29a2a 482 if (downwall.cooldown > 80) {
el14moh 0:6b29f9c29a2a 483 downwall.moveFlag = 1;
el14moh 0:6b29f9c29a2a 484 downwall.genFlag = 0;
el14moh 0:6b29f9c29a2a 485 } else if ((downwall.random == 1)&&(upwall.cooldown > 60)) {
el14moh 0:6b29f9c29a2a 486 downwall.moveFlag = 1;
el14moh 0:6b29f9c29a2a 487 downwall.genFlag = 0;
el14moh 0:6b29f9c29a2a 488 } else {
el14moh 0:6b29f9c29a2a 489 downwall.moveFlag = 0;
el14moh 0:6b29f9c29a2a 490 }
el14moh 0:6b29f9c29a2a 491 }
el14moh 0:6b29f9c29a2a 492 }
el14moh 0:6b29f9c29a2a 493 }
el14moh 0:6b29f9c29a2a 494
el14moh 0:6b29f9c29a2a 495 // UP WALL
el14moh 0:6b29f9c29a2a 496 if (counter > 1500) {
el14moh 0:6b29f9c29a2a 497 if (upwall.moveFlag == 1) {
el14moh 0:6b29f9c29a2a 498 if (downwall.cooldown > 60) {
el14moh 0:6b29f9c29a2a 499 if (counter % 2 == 1) { // horizontal walls move half the speed of vertical walls
el14moh 0:6b29f9c29a2a 500 upwall.y-=1;
el14moh 0:6b29f9c29a2a 501 }
el14moh 0:6b29f9c29a2a 502 }
el14moh 0:6b29f9c29a2a 503 if (upwall.y<3) {
el14moh 0:6b29f9c29a2a 504 upwall.moveFlag = 0;
el14moh 0:6b29f9c29a2a 505 }
el14moh 0:6b29f9c29a2a 506 } else {
el14moh 0:6b29f9c29a2a 507 if (upwall.genFlag == 0) {
el14moh 4:d44eace87ab3 508 upwall.x = rand() % 58+13;
el14moh 0:6b29f9c29a2a 509 upwall.y = 46;
el14moh 0:6b29f9c29a2a 510 upwall.genFlag = 1;
el14moh 0:6b29f9c29a2a 511 } else {
el14moh 0:6b29f9c29a2a 512 if (upwall.cooldown > 80) {
el14moh 0:6b29f9c29a2a 513 upwall.moveFlag = 1;
el14moh 0:6b29f9c29a2a 514 upwall.genFlag = 0;
el14moh 0:6b29f9c29a2a 515 } else if ((upwall.random == 1)&&(downwall.cooldown > 60)) {
el14moh 0:6b29f9c29a2a 516 upwall.moveFlag = 1;
el14moh 0:6b29f9c29a2a 517 upwall.genFlag = 0;
el14moh 0:6b29f9c29a2a 518 } else {
el14moh 0:6b29f9c29a2a 519 upwall.moveFlag = 0;
el14moh 0:6b29f9c29a2a 520 }
el14moh 0:6b29f9c29a2a 521 }
el14moh 0:6b29f9c29a2a 522 }
el14moh 0:6b29f9c29a2a 523 }
el14moh 0:6b29f9c29a2a 524 }
el14moh 2:602e9bb053a0 525 void checkBorderCollision()
el14moh 0:6b29f9c29a2a 526 {
el14moh 0:6b29f9c29a2a 527 // if floor
el14moh 0:6b29f9c29a2a 528 if ( j >= 47 - (PLAYERRADIUS+3)) {
el14moh 0:6b29f9c29a2a 529 j = 47 - (PLAYERRADIUS+3);
el14moh 0:6b29f9c29a2a 530 }
el14moh 0:6b29f9c29a2a 531
el14moh 0:6b29f9c29a2a 532 // if roof
el14moh 0:6b29f9c29a2a 533 if ( j <= (PLAYERRADIUS+3)) {
el14moh 0:6b29f9c29a2a 534 j = (PLAYERRADIUS+3);
el14moh 0:6b29f9c29a2a 535 }
el14moh 0:6b29f9c29a2a 536
el14moh 0:6b29f9c29a2a 537 // if right wall
el14moh 0:6b29f9c29a2a 538 if ( i >= 83 - (PLAYERRADIUS+3)) {
el14moh 0:6b29f9c29a2a 539 i = 83 - (PLAYERRADIUS+3);
el14moh 0:6b29f9c29a2a 540 }
el14moh 0:6b29f9c29a2a 541
el14moh 0:6b29f9c29a2a 542 // if left wall
el14moh 3:8890b4605a10 543 if ( i <= (PLAYERRADIUS+8)) {
el14moh 3:8890b4605a10 544 i = (PLAYERRADIUS+8);
el14moh 0:6b29f9c29a2a 545 }
el14moh 2:602e9bb053a0 546 }
el14moh 2:602e9bb053a0 547
el14moh 2:602e9bb053a0 548 void invincible()
el14moh 2:602e9bb053a0 549 {
el14moh 2:602e9bb053a0 550 if (g_button_flag) {
el14moh 2:602e9bb053a0 551 g_button_flag = 0;
el14moh 2:602e9bb053a0 552 if ((saves > 0) && (mortal == true)) { // saves are available and not currently used
el14moh 2:602e9bb053a0 553 invun_cool=0;
el14moh 2:602e9bb053a0 554 mortal = false;
el14moh 2:602e9bb053a0 555 invun_cool++;
el14moh 2:602e9bb053a0 556 saves--;
el14moh 2:602e9bb053a0 557 }
el14moh 2:602e9bb053a0 558 }
el14moh 2:602e9bb053a0 559 if (mortal == false) {
el14moh 2:602e9bb053a0 560 invun_cool++;
el14moh 5:c64d8ba051e9 561 if (invun_cool > 30) { // ticker called 20 times per second, therefore 1.5s of invincibility
el14moh 2:602e9bb053a0 562 mortal = true;
el14moh 2:602e9bb053a0 563 invun_cool=0;
el14moh 2:602e9bb053a0 564 }
el14moh 2:602e9bb053a0 565 }
el14moh 2:602e9bb053a0 566 }
el14moh 2:602e9bb053a0 567
el14moh 2:602e9bb053a0 568 void checkWallCollision() // checks for any collisions (i.e. player has hit a wall or side of the screen)
el14moh 2:602e9bb053a0 569 {
el14moh 0:6b29f9c29a2a 570
el14moh 0:6b29f9c29a2a 571 // LEFT WALL
el14moh 0:6b29f9c29a2a 572 if ((((i - PLAYERRADIUS) <= leftwall.x) && (leftwall.x <= (i + PLAYERRADIUS))) && (j > (leftwall.y+3) || j < (leftwall.y-3))) {
el14moh 0:6b29f9c29a2a 573 game_over_flag = 1;
el14moh 0:6b29f9c29a2a 574 }
el14moh 0:6b29f9c29a2a 575
el14moh 0:6b29f9c29a2a 576 // RIGHT WALL
el14moh 0:6b29f9c29a2a 577 if ((((i - PLAYERRADIUS) <= rightwall.x) && (rightwall.x <= (i + PLAYERRADIUS))) && (j > (rightwall.y+3) || j < (rightwall.y-3))) {
el14moh 0:6b29f9c29a2a 578 game_over_flag = 1;
el14moh 0:6b29f9c29a2a 579 }
el14moh 0:6b29f9c29a2a 580
el14moh 0:6b29f9c29a2a 581 // DOWN WALL
el14moh 0:6b29f9c29a2a 582 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 583 game_over_flag = 1;
el14moh 0:6b29f9c29a2a 584 }
el14moh 0:6b29f9c29a2a 585
el14moh 0:6b29f9c29a2a 586 // UP WALL
el14moh 0:6b29f9c29a2a 587 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 588 game_over_flag = 1;
el14moh 0:6b29f9c29a2a 589 }
el14moh 0:6b29f9c29a2a 590 }
el14moh 0:6b29f9c29a2a 591
el14moh 0:6b29f9c29a2a 592 void updateScreen() // refreshes the screen, redraws player and walls
el14moh 0:6b29f9c29a2a 593 {
el14moh 0:6b29f9c29a2a 594 lcd.clear();
el14moh 2:602e9bb053a0 595 if (mortal) {
el14moh 2:602e9bb053a0 596 lcd.drawCircle(i,j,PLAYERRADIUS,1);
el14moh 2:602e9bb053a0 597 } else {
el14moh 2:602e9bb053a0 598 if (counter % 2 == 0) {
el14moh 2:602e9bb053a0 599 lcd.drawCircle(i,j,PLAYERRADIUS,1);
el14moh 2:602e9bb053a0 600 }
el14moh 2:602e9bb053a0 601 }
el14moh 0:6b29f9c29a2a 602 lcd.refresh(); // update display
el14moh 0:6b29f9c29a2a 603
el14moh 3:8890b4605a10 604 if (saves > 0) {
el14moh 3:8890b4605a10 605 lcd.drawCircle(2,7,1,1);
el14moh 3:8890b4605a10 606 }
el14moh 3:8890b4605a10 607 if (saves > 1) {
el14moh 3:8890b4605a10 608 lcd.drawCircle(2,15,1,1);
el14moh 3:8890b4605a10 609 }
el14moh 3:8890b4605a10 610 if (saves > 2) {
el14moh 3:8890b4605a10 611 lcd.drawCircle(2,23,1,1);
el14moh 3:8890b4605a10 612 }
el14moh 3:8890b4605a10 613 if (saves > 3) {
el14moh 3:8890b4605a10 614 lcd.drawCircle(2,31,1,1);
el14moh 3:8890b4605a10 615 }
el14moh 3:8890b4605a10 616 if (saves > 4) {
el14moh 3:8890b4605a10 617 lcd.drawCircle(2,39,1,1);
el14moh 3:8890b4605a10 618 }
el14moh 3:8890b4605a10 619
el14moh 2:602e9bb053a0 620
el14moh 0:6b29f9c29a2a 621 // draw Border
el14moh 6:153680563027 622 draw_border();
el14moh 0:6b29f9c29a2a 623
el14moh 0:6b29f9c29a2a 624 // draw walls
el14moh 0:6b29f9c29a2a 625 // LEFT WALL
el14moh 0:6b29f9c29a2a 626 lcd.drawLine(leftwall.x,leftwall.y+5,leftwall.x,44,1);
el14moh 0:6b29f9c29a2a 627 lcd.drawLine(leftwall.x,leftwall.y-5,leftwall.x,3,1);
el14moh 0:6b29f9c29a2a 628 lcd.drawLine(leftwall.x+1,leftwall.y+5,leftwall.x+1,44,1);
el14moh 0:6b29f9c29a2a 629 lcd.drawLine(leftwall.x+1,leftwall.y-5,leftwall.x+1,3,1);
el14moh 0:6b29f9c29a2a 630
el14moh 0:6b29f9c29a2a 631 // RIGHT WALL
el14moh 0:6b29f9c29a2a 632 if (counter > 200) {
el14moh 0:6b29f9c29a2a 633 lcd.drawLine(rightwall.x,rightwall.y+5,rightwall.x,44,1);
el14moh 0:6b29f9c29a2a 634 lcd.drawLine(rightwall.x,rightwall.y-5,rightwall.x,3,1);
el14moh 0:6b29f9c29a2a 635 lcd.drawLine(rightwall.x-1,rightwall.y+5,rightwall.x-1,44,1);
el14moh 0:6b29f9c29a2a 636 lcd.drawLine(rightwall.x-1,rightwall.y-5,rightwall.x-1,3,1);
el14moh 0:6b29f9c29a2a 637 }
el14moh 0:6b29f9c29a2a 638
el14moh 0:6b29f9c29a2a 639 // DOWN WALL
el14moh 0:6b29f9c29a2a 640 if (counter > 600) {
el14moh 0:6b29f9c29a2a 641 lcd.drawLine(downwall.x+11,downwall.y,80,downwall.y,1);
el14moh 3:8890b4605a10 642 lcd.drawLine(downwall.x-11,downwall.y,8,downwall.y,1);
el14moh 0:6b29f9c29a2a 643 lcd.drawLine(downwall.x+11,downwall.y-1,80,downwall.y-1,1);
el14moh 3:8890b4605a10 644 lcd.drawLine(downwall.x-11,downwall.y-1,8,downwall.y-1,1);
el14moh 0:6b29f9c29a2a 645 }
el14moh 0:6b29f9c29a2a 646
el14moh 0:6b29f9c29a2a 647 // UP WALL
el14moh 0:6b29f9c29a2a 648 if (counter > 1500) {
el14moh 0:6b29f9c29a2a 649 lcd.drawLine(upwall.x+11,upwall.y,80,upwall.y,1);
el14moh 3:8890b4605a10 650 lcd.drawLine(upwall.x-11,upwall.y,8,upwall.y,1);
el14moh 0:6b29f9c29a2a 651 lcd.drawLine(upwall.x+11,upwall.y+1,80,upwall.y+1,1);
el14moh 3:8890b4605a10 652 lcd.drawLine(upwall.x-11,upwall.y+1,8,upwall.y+1,1);
el14moh 0:6b29f9c29a2a 653 }
el14moh 0:6b29f9c29a2a 654
el14moh 0:6b29f9c29a2a 655 lcd.refresh();
el14moh 0:6b29f9c29a2a 656 }
el14moh 0:6b29f9c29a2a 657
el14moh 0:6b29f9c29a2a 658 void warning ()
el14moh 0:6b29f9c29a2a 659 {
el14moh 0:6b29f9c29a2a 660 if (counter == 170) {
el14moh 0:6b29f9c29a2a 661 lcd.inverseMode();
el14moh 0:6b29f9c29a2a 662 } else if (counter == 570) {
el14moh 0:6b29f9c29a2a 663 lcd.inverseMode();
el14moh 0:6b29f9c29a2a 664 } else if (counter == 1470) {
el14moh 0:6b29f9c29a2a 665 lcd.inverseMode();
el14moh 0:6b29f9c29a2a 666 } else {
el14moh 0:6b29f9c29a2a 667 lcd.normalMode();
el14moh 0:6b29f9c29a2a 668 }
el14moh 0:6b29f9c29a2a 669 }
el14moh 0:6b29f9c29a2a 670
el14moh 0:6b29f9c29a2a 671 void game_timer_isr() // sets flag for timer interrupt
el14moh 0:6b29f9c29a2a 672 {
el14moh 0:6b29f9c29a2a 673 g_timer_flag = 1;
el14moh 0:6b29f9c29a2a 674 }
el14moh 0:6b29f9c29a2a 675
el14moh 2:602e9bb053a0 676 void button_isr()
el14moh 2:602e9bb053a0 677 {
el14moh 2:602e9bb053a0 678 g_button_flag = 1;
el14moh 2:602e9bb053a0 679 }
el14moh 2:602e9bb053a0 680
el14moh 0:6b29f9c29a2a 681 void initDisplay() // initialises the LCD display
el14moh 0:6b29f9c29a2a 682 {
el14moh 0:6b29f9c29a2a 683 lcd.init();
el14moh 0:6b29f9c29a2a 684 lcd.normalMode();
el14moh 0:6b29f9c29a2a 685 lcd.setBrightness(1.0F-backlight); // brightness pot on PCB is soldered in the wrong direction, (1.0F-backlight) inverts the reading
el14moh 0:6b29f9c29a2a 686 }
el14moh 0:6b29f9c29a2a 687
el14moh 0:6b29f9c29a2a 688 void calibrateJoystick() // read default positions of the joystick to calibrate later readings
el14moh 0:6b29f9c29a2a 689 {
el14moh 0:6b29f9c29a2a 690 button.mode(PullDown);
el14moh 0:6b29f9c29a2a 691 // must not move during calibration
el14moh 0:6b29f9c29a2a 692 joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
el14moh 0:6b29f9c29a2a 693 joystick.y0 = yPot;
el14moh 0:6b29f9c29a2a 694 }
el14moh 0:6b29f9c29a2a 695 void updateJoystick() // reads direction the joystick has been moved
el14moh 0:6b29f9c29a2a 696 {
el14moh 0:6b29f9c29a2a 697 // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
el14moh 0:6b29f9c29a2a 698 joystick.x = xPot - joystick.x0;
el14moh 0:6b29f9c29a2a 699 joystick.y = yPot - joystick.y0;
el14moh 0:6b29f9c29a2a 700 // read button state
el14moh 0:6b29f9c29a2a 701 joystick.button = button;
el14moh 0:6b29f9c29a2a 702
el14moh 0:6b29f9c29a2a 703 // calculate direction depending on x,y values
el14moh 0:6b29f9c29a2a 704 // tolerance allows a little lee-way in case joystick not exactly in the stated direction
el14moh 0:6b29f9c29a2a 705 if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 706 joystick.direction = CENTRE;
el14moh 0:6b29f9c29a2a 707 } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 708 joystick.direction = UP;
el14moh 0:6b29f9c29a2a 709 } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 710 joystick.direction = DOWN;
el14moh 0:6b29f9c29a2a 711 } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 712 joystick.direction = LEFT;
el14moh 0:6b29f9c29a2a 713 } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 714 joystick.direction = RIGHT;
el14moh 0:6b29f9c29a2a 715 } else if ( joystick.y > DIRECTION_TOLERANCE && joystick.x > DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 716 joystick.direction = UPLEFT;
el14moh 0:6b29f9c29a2a 717 } else if ( joystick.y > DIRECTION_TOLERANCE && joystick.x < DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 718 joystick.direction = UPRIGHT;
el14moh 0:6b29f9c29a2a 719 } else if ( joystick.x > DIRECTION_TOLERANCE && joystick.y < DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 720 joystick.direction = DOWNLEFT;
el14moh 0:6b29f9c29a2a 721 } else if ( joystick.x < DIRECTION_TOLERANCE && joystick.y < DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 722 joystick.direction = DOWNRIGHT;
el14moh 0:6b29f9c29a2a 723 }
el14moh 0:6b29f9c29a2a 724
el14moh 0:6b29f9c29a2a 725 else {
el14moh 0:6b29f9c29a2a 726 joystick.direction = UNKNOWN;
el14moh 0:6b29f9c29a2a 727 }
el14moh 0:6b29f9c29a2a 728
el14moh 0:6b29f9c29a2a 729
el14moh 0:6b29f9c29a2a 730 printFlag = 1; // set flag for printing
el14moh 0:6b29f9c29a2a 731 }
el14moh 0:6b29f9c29a2a 732
el14moh 0:6b29f9c29a2a 733 void debug() // prints for debug purposes
el14moh 0:6b29f9c29a2a 734 {
el14moh 0:6b29f9c29a2a 735 if (printFlag) { // if flag set, clear flag and print joystick values to serial port
el14moh 0:6b29f9c29a2a 736 printFlag = 0;
el14moh 0:6b29f9c29a2a 737 /*
el14moh 0:6b29f9c29a2a 738 if (joystick.direction == UP)
el14moh 0:6b29f9c29a2a 739 serial.printf(" UP\n");
el14moh 0:6b29f9c29a2a 740 if (joystick.direction == DOWN)
el14moh 0:6b29f9c29a2a 741 serial.printf(" DOWN\n");
el14moh 0:6b29f9c29a2a 742 if (joystick.direction == LEFT)
el14moh 0:6b29f9c29a2a 743 serial.printf(" LEFT\n");
el14moh 0:6b29f9c29a2a 744 if (joystick.direction == RIGHT)
el14moh 0:6b29f9c29a2a 745 serial.printf(" RIGHT\n");
el14moh 0:6b29f9c29a2a 746 if (joystick.direction == UPRIGHT)
el14moh 0:6b29f9c29a2a 747 serial.printf(" UPRIGHT\n"); // Diagonally up + right
el14moh 0:6b29f9c29a2a 748 if (joystick.direction == UPLEFT)
el14moh 0:6b29f9c29a2a 749 serial.printf(" UPLEFT\n"); // Diagonally up + left
el14moh 0:6b29f9c29a2a 750 if (joystick.direction == DOWNRIGHT)
el14moh 0:6b29f9c29a2a 751 serial.printf(" DOWNRIGHT\n"); // Diagonally down + right
el14moh 0:6b29f9c29a2a 752 if (joystick.direction == DOWNLEFT)
el14moh 0:6b29f9c29a2a 753 serial.printf(" DOWNLEFT\n"); // Diagonally down + left
el14moh 0:6b29f9c29a2a 754 if (joystick.direction == CENTRE)
el14moh 0:6b29f9c29a2a 755 serial.printf(" CENTRE\n");
el14moh 0:6b29f9c29a2a 756 if (joystick.direction == UNKNOWN)
el14moh 0:6b29f9c29a2a 757 serial.printf(" Unsupported direction\n");
el14moh 0:6b29f9c29a2a 758 */
el14moh 2:602e9bb053a0 759 serial.printf("saves = %d \n",saves);
el14moh 2:602e9bb053a0 760 //serial.printf("Right-wall Cooldown = %d \n",rightwall.cooldown);
el14moh 2:602e9bb053a0 761 //serial.printf("counter = %d \n",counter);
el14moh 0:6b29f9c29a2a 762
el14moh 0:6b29f9c29a2a 763 }
el14moh 0:6b29f9c29a2a 764 }
el14moh 0:6b29f9c29a2a 765
el14moh 7:ca18e8775d8d 766 void initSerial() // sets baud rate for serial
el14moh 0:6b29f9c29a2a 767 {
el14moh 0:6b29f9c29a2a 768 // set to highest baud - ensure terminal software matches
el14moh 0:6b29f9c29a2a 769 serial.baud(115200);
el14moh 0:6b29f9c29a2a 770 }
el14moh 7:ca18e8775d8d 771 void initGame()
el14moh 3:8890b4605a10 772 {
el14moh 0:6b29f9c29a2a 773
el14moh 3:8890b4605a10 774 g_button_flag = 0;
el14moh 3:8890b4605a10 775 leftwall.y = rand() % 27+8;
el14moh 3:8890b4605a10 776 leftwall.x = 0;
el14moh 6:153680563027 777 rightwall.y = rand() % 58+13;
el14moh 6:153680563027 778 rightwall.x = 6;
el14moh 3:8890b4605a10 779 downwall.x = rand() % 27+8;
el14moh 3:8890b4605a10 780 downwall.y = 0;
el14moh 6:153680563027 781 upwall.x = rand() % 5814;
el14moh 3:8890b4605a10 782 upwall.y = 0;
el14moh 3:8890b4605a10 783 rightwall.cooldown = 61;
el14moh 3:8890b4605a10 784 counter = 0;
el14moh 7:ca18e8775d8d 785 score = 0;
el14moh 3:8890b4605a10 786
el14moh 3:8890b4605a10 787 saves = 5;
el14moh 3:8890b4605a10 788
el14moh 3:8890b4605a10 789 i = 42;
el14moh 3:8890b4605a10 790 j = 24;
el14moh 3:8890b4605a10 791
el14moh 3:8890b4605a10 792 }
el14moh 6:153680563027 793
el14moh 6:153680563027 794 void blink() // command for brief flash
el14moh 6:153680563027 795 {
el14moh 6:153680563027 796 lcd.inverseMode();
el14moh 6:153680563027 797 wait(0.2);
el14moh 6:153680563027 798 lcd.normalMode();
el14moh 6:153680563027 799 }
el14moh 6:153680563027 800
el14moh 6:153680563027 801 void printHelp()
el14moh 6:153680563027 802 {
el14moh 6:153680563027 803 lcd.printString("Try and",0,1);
el14moh 6:153680563027 804 lcd.printString("survive for",0,2);
el14moh 6:153680563027 805 lcd.printString("as long as ",0,3);
el14moh 6:153680563027 806 lcd.printString("possible!",0,4);
el14moh 6:153680563027 807 wait(4);
el14moh 6:153680563027 808
el14moh 6:153680563027 809 blink();
el14moh 6:153680563027 810 lcd.clear();
el14moh 6:153680563027 811
el14moh 6:153680563027 812 lcd.printString("Get through",0,1);
el14moh 6:153680563027 813 lcd.printString("the holes in",0,2);
el14moh 6:153680563027 814 lcd.printString("the walls with",0,3);
el14moh 6:153680563027 815 lcd.printString("the joystick",0,4);
el14moh 6:153680563027 816 wait(4);
el14moh 6:153680563027 817
el14moh 6:153680563027 818 blink();
el14moh 6:153680563027 819 lcd.clear();
el14moh 6:153680563027 820
el14moh 6:153680563027 821 lcd.printString("Press the",0,1);
el14moh 6:153680563027 822 lcd.printString("stick to",0,2);
el14moh 6:153680563027 823 lcd.printString("be briefly",0,3);
el14moh 6:153680563027 824 lcd.printString("untouchable",0,4);
el14moh 6:153680563027 825 wait(4);
el14moh 6:153680563027 826
el14moh 6:153680563027 827 blink();
el14moh 6:153680563027 828 lcd.clear();
el14moh 6:153680563027 829
el14moh 6:153680563027 830 lcd.printString("But don't use",0,1);
el14moh 6:153680563027 831 lcd.printString("it too much!",0,2);
el14moh 6:153680563027 832 lcd.printString("you can only",0,3);
el14moh 6:153680563027 833 lcd.printString("do it 5 times",0,4);
el14moh 6:153680563027 834 wait(4);
el14moh 6:153680563027 835
el14moh 6:153680563027 836 blink();
el14moh 6:153680563027 837 lcd.clear();
el14moh 6:153680563027 838
el14moh 6:153680563027 839 lcd.printString("Lastly, keep",0,1);
el14moh 6:153680563027 840 lcd.printString("an eye on the",0,2);
el14moh 6:153680563027 841 lcd.printString("borders of",0,3);
el14moh 6:153680563027 842 lcd.printString("the screen...",0,4);
el14moh 6:153680563027 843 wait(4);
el14moh 6:153680563027 844
el14moh 6:153680563027 845 blink();
el14moh 6:153680563027 846 lcd.clear();
el14moh 6:153680563027 847
el14moh 6:153680563027 848 lcd.printString("See where the",0,1);
el14moh 6:153680563027 849 lcd.printString("holes are",0,2);
el14moh 6:153680563027 850 lcd.printString("before the",0,3);
el14moh 6:153680563027 851 lcd.printString("walls move!",0,4);
el14moh 6:153680563027 852 wait(4);
el14moh 6:153680563027 853
el14moh 6:153680563027 854 blink();
el14moh 6:153680563027 855 lcd.clear();
el14moh 6:153680563027 856 }
el14moh 6:153680563027 857
el14moh 6:153680563027 858 void printScores()
el14moh 6:153680563027 859 {
el14moh 6:153680563027 860 lcd.printString("Highscores",13,0);
el14moh 6:153680563027 861 lcd.printString("1st:",6,2);
el14moh 6:153680563027 862 lcd.printString("2nd:",6,3);
el14moh 6:153680563027 863 lcd.printString("3rd:",6,4);
el14moh 6:153680563027 864 char first[14];
el14moh 6:153680563027 865 char second[14];
el14moh 6:153680563027 866 char third[14];
el14moh 6:153680563027 867 int one = sprintf(first,"%d",hiscore.one);
el14moh 6:153680563027 868 int two = sprintf(second,"%d",hiscore.two);
el14moh 6:153680563027 869 int three = sprintf(third,"%d",hiscore.three);
el14moh 6:153680563027 870
el14moh 6:153680563027 871 lcd.printString(first,30,2);
el14moh 6:153680563027 872
el14moh 6:153680563027 873
el14moh 6:153680563027 874 lcd.printString(second,30,3);
el14moh 6:153680563027 875
el14moh 6:153680563027 876
el14moh 6:153680563027 877 lcd.printString(third,30,4);
el14moh 6:153680563027 878
el14moh 6:153680563027 879 lcd.refresh();
el14moh 6:153680563027 880 wait(4);
el14moh 6:153680563027 881 blink();
el14moh 6:153680563027 882 lcd.clear();
el14moh 6:153680563027 883
el14moh 6:153680563027 884 }
el14moh 6:153680563027 885
el14moh 6:153680563027 886 void draw_border() // draws game border
el14moh 6:153680563027 887 {
el14moh 6:153680563027 888 lcd.drawLine(7,2,81,2,1);
el14moh 6:153680563027 889 lcd.drawLine(7,2,7,45,1);
el14moh 6:153680563027 890 lcd.drawLine(81,2,81,45,1);
el14moh 6:153680563027 891 lcd.drawLine(7,45,81,45,1);
el14moh 6:153680563027 892
el14moh 6:153680563027 893 lcd.drawLine(5,0,83,0,1);
el14moh 6:153680563027 894 lcd.drawLine(5,0,5,47,1);
el14moh 6:153680563027 895 lcd.drawLine(83,0,83,47,1);
el14moh 6:153680563027 896 lcd.drawLine(5,47,83,47,1);
el14moh 6:153680563027 897
el14moh 6:153680563027 898 lcd.refresh();
el14moh 6:153680563027 899 }
el14moh 6:153680563027 900
el14moh 6:153680563027 901 void calculateHighscores()
el14moh 6:153680563027 902 {
el14moh 7:ca18e8775d8d 903 if (score > hiscore.one) {
el14moh 6:153680563027 904 hiscore.three = hiscore.two;
el14moh 6:153680563027 905 hiscore.two = hiscore.one;
el14moh 7:ca18e8775d8d 906 hiscore.one = score;
el14moh 7:ca18e8775d8d 907 } else if ((hiscore.one > score)&&(score > hiscore.two)) {
el14moh 6:153680563027 908 hiscore.three = hiscore.two;
el14moh 7:ca18e8775d8d 909 hiscore.two = score;
el14moh 7:ca18e8775d8d 910 } else if ((hiscore.two > score)&&(score > hiscore.three)) {
el14moh 7:ca18e8775d8d 911 hiscore.three = score;
el14moh 6:153680563027 912 }
el14moh 6:153680563027 913 }
el14moh 6:153680563027 914
el14moh 6:153680563027 915 void initHiscores()
el14moh 6:153680563027 916 {
el14moh 6:153680563027 917 hiscore.one = 0;
el14moh 6:153680563027 918 hiscore.two = 0;
el14moh 6:153680563027 919 hiscore.three = 0;
el14moh 6:153680563027 920 }