Wall dodging game utilising a joystick and Nokia 5110 LCD display

Dependencies:   N5110 mbed

Committer:
el14moh
Date:
Sat Apr 23 17:50:30 2016 +0000
Revision:
5:c64d8ba051e9
Parent:
4:d44eace87ab3
Child:
6:153680563027
about to try implement menu

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 3:8890b4605a10 25
el14moh 3:8890b4605a10 26 NOTES - top wall collision seems to be off
el14moh 3:8890b4605a10 27
el14moh 2:602e9bb053a0 28
el14moh 2:602e9bb053a0 29
el14moh 0:6b29f9c29a2a 30 */
el14moh 0:6b29f9c29a2a 31
el14moh 0:6b29f9c29a2a 32 #include "mbed.h"
el14moh 0:6b29f9c29a2a 33 #include "N5110.h"
el14moh 0:6b29f9c29a2a 34 #include "tones.h"
el14moh 0:6b29f9c29a2a 35
el14moh 0:6b29f9c29a2a 36 #define DIRECTION_TOLERANCE 0.05 // tolerance of joystick direction
el14moh 0:6b29f9c29a2a 37 #define PLAYERRADIUS 2 // size of player ball
el14moh 0:6b29f9c29a2a 38 #define GAMESPEED 20 // game timer speed
el14moh 0:6b29f9c29a2a 39 #define JOYSPEED 20 // rate at which the joystick is read
el14moh 0:6b29f9c29a2a 40
el14moh 0:6b29f9c29a2a 41 // VCC, SCE, RST, D/C, MOSI, SCLK, LED
el14moh 0:6b29f9c29a2a 42 N5110 lcd (PTD3 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3);
el14moh 0:6b29f9c29a2a 43
el14moh 5:c64d8ba051e9 44 AnalogIn backlight(PTB2); // pot to control brightness
el14moh 0:6b29f9c29a2a 45 DigitalIn button(PTB3); // joystick button object
el14moh 0:6b29f9c29a2a 46 AnalogIn xPot(PTB11); // joystick x direction object
el14moh 0:6b29f9c29a2a 47 AnalogIn yPot(PTB10); // joystick y direction object
el14moh 0:6b29f9c29a2a 48 DigitalOut buzzer(PTA2); // buzzer object
el14moh 2:602e9bb053a0 49 InterruptIn flick (PTB3); // interruptin instance of button
el14moh 0:6b29f9c29a2a 50
el14moh 0:6b29f9c29a2a 51 Ticker pollJoystick; // timer to regularly read the joystick
el14moh 5:c64d8ba051e9 52 Ticker menu_pollJoystick // slower ticker to move joystick through menu
el14moh 0:6b29f9c29a2a 53 Ticker game_timer; // timer to regularly update the screen
el14moh 0:6b29f9c29a2a 54
el14moh 0:6b29f9c29a2a 55 Serial serial(USBTX,USBRX); // Serial for debug
el14moh 0:6b29f9c29a2a 56
el14moh 0:6b29f9c29a2a 57 // create enumerated type (0,1,2,3 etc. for direction)
el14moh 0:6b29f9c29a2a 58 enum DirectionName {
el14moh 0:6b29f9c29a2a 59 UP,
el14moh 0:6b29f9c29a2a 60 DOWN,
el14moh 0:6b29f9c29a2a 61 LEFT,
el14moh 0:6b29f9c29a2a 62 RIGHT,
el14moh 0:6b29f9c29a2a 63 UPRIGHT, // Diagonally up + right
el14moh 0:6b29f9c29a2a 64 UPLEFT, // Diagonally up + left
el14moh 0:6b29f9c29a2a 65 DOWNRIGHT, // Diagonally down + right
el14moh 0:6b29f9c29a2a 66 DOWNLEFT, // Diagonally down + left
el14moh 0:6b29f9c29a2a 67 CENTRE,
el14moh 0:6b29f9c29a2a 68 UNKNOWN
el14moh 0:6b29f9c29a2a 69 };
el14moh 0:6b29f9c29a2a 70
el14moh 0:6b29f9c29a2a 71 typedef struct JoyStick Joystick; // struct for Joystick
el14moh 0:6b29f9c29a2a 72 typedef struct Wall Wall; // struct for Walls
el14moh 0:6b29f9c29a2a 73
el14moh 0:6b29f9c29a2a 74 struct JoyStick {
el14moh 0:6b29f9c29a2a 75 float x; // current x value
el14moh 0:6b29f9c29a2a 76 float x0; // 'centred' x value
el14moh 0:6b29f9c29a2a 77 float y; // current y value
el14moh 0:6b29f9c29a2a 78 float y0; // 'centred' y value
el14moh 0:6b29f9c29a2a 79 int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
el14moh 0:6b29f9c29a2a 80 DirectionName direction; // current direction
el14moh 0:6b29f9c29a2a 81 };
el14moh 0:6b29f9c29a2a 82
el14moh 0:6b29f9c29a2a 83 struct Wall {
el14moh 0:6b29f9c29a2a 84 int x; // x-coordinate of wall (realtive to centre of the gap)
el14moh 0:6b29f9c29a2a 85 int y; // y-coordinate of wall (relative to centre of the gap)
el14moh 0:6b29f9c29a2a 86 DirectionName direction; // Direction the wall travels in
el14moh 0:6b29f9c29a2a 87 int random; // randomly generated integer to determine when a wall begins to travel
el14moh 0:6b29f9c29a2a 88 int cooldown; // stops a wall respawning before a certain amount of time
el14moh 1:26ebbb94cf36 89 volatile bool moveFlag; // flag to determine if wall is on screen
el14moh 1:26ebbb94cf36 90 volatile bool genFlag; // flag to determine if wall has been generated
el14moh 0:6b29f9c29a2a 91 };
el14moh 0:6b29f9c29a2a 92
el14moh 0:6b29f9c29a2a 93 // struct variable for joystick
el14moh 0:6b29f9c29a2a 94 Joystick joystick;
el14moh 0:6b29f9c29a2a 95 // struct variable for moving walls
el14moh 0:6b29f9c29a2a 96 Wall leftwall;
el14moh 0:6b29f9c29a2a 97 Wall rightwall;
el14moh 0:6b29f9c29a2a 98 Wall downwall;
el14moh 0:6b29f9c29a2a 99 Wall upwall;
el14moh 0:6b29f9c29a2a 100
el14moh 0:6b29f9c29a2a 101 void calibrateJoystick(); // read default positions of the joystick to calibrate later readings
el14moh 0:6b29f9c29a2a 102 void updateJoystick(); // reads direction the joystick has been moved
el14moh 0:6b29f9c29a2a 103 void initDisplay(); // initialises the LCD display
el14moh 0:6b29f9c29a2a 104 void game_timer_isr(); // sets flag for timer interrupt
el14moh 3:8890b4605a10 105 void init_game();
el14moh 0:6b29f9c29a2a 106 void moveBall(); // reads joystick direction and moves position of the player
el14moh 0:6b29f9c29a2a 107 void moveWall(); // moves walls along the screen
el14moh 2:602e9bb053a0 108 void invincible(); // makes player briefly invincible
el14moh 2:602e9bb053a0 109 void checkWallCollision(); // checks for any collisions with wall
el14moh 2:602e9bb053a0 110 void checkBorderCollision(); // checks for any collisions with border
el14moh 0:6b29f9c29a2a 111 void updateScreen(); // refreshes the screen, redraws player and walls
el14moh 0:6b29f9c29a2a 112 void warning(); // flashes screen when a new wall is ready to appear
el14moh 0:6b29f9c29a2a 113 void init_serial(); // sets baud rate for serial
el14moh 0:6b29f9c29a2a 114 void debug(); // prints for debug purposes
el14moh 2:602e9bb053a0 115 void button_isr();
el14moh 0:6b29f9c29a2a 116
el14moh 0:6b29f9c29a2a 117 float refresh_rate = GAMESPEED; // how often to update display (Hz)
el14moh 0:6b29f9c29a2a 118 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 119 volatile int g_timer_flag = 0; // flag for timer interrupt
el14moh 2:602e9bb053a0 120 volatile int game_over_flag = 0; // flag to signal game over
el14moh 2:602e9bb053a0 121 volatile int g_button_flag = 0;
el14moh 0:6b29f9c29a2a 122 int printFlag = 0; // flag for printing
el14moh 0:6b29f9c29a2a 123 int game_start_flag = 0; // flag to start the game
el14moh 3:8890b4605a10 124 int i; // x-coordinate value of player
el14moh 3:8890b4605a10 125 int j; // y-coordinate value of player
el14moh 0:6b29f9c29a2a 126 int counter = 0; // number of times code has looped
el14moh 2:602e9bb053a0 127 volatile bool mortal = 1;
el14moh 2:602e9bb053a0 128 int invun_cool = 0;
el14moh 2:602e9bb053a0 129 int saves = 5;
el14moh 2:602e9bb053a0 130
el14moh 0:6b29f9c29a2a 131
el14moh 0:6b29f9c29a2a 132
el14moh 0:6b29f9c29a2a 133
el14moh 0:6b29f9c29a2a 134 int main()
el14moh 0:6b29f9c29a2a 135 {
el14moh 0:6b29f9c29a2a 136 init_serial();
el14moh 0:6b29f9c29a2a 137 srand(time(NULL)); // generate seed for random number generation
el14moh 0:6b29f9c29a2a 138 calibrateJoystick();
el14moh 0:6b29f9c29a2a 139 pollJoystick.attach(&updateJoystick,1.0/JOYSPEED); // read joystick (JOYSPEED) times per second
el14moh 0:6b29f9c29a2a 140 initDisplay();
el14moh 0:6b29f9c29a2a 141
el14moh 0:6b29f9c29a2a 142
el14moh 0:6b29f9c29a2a 143 wait (1.0);
el14moh 0:6b29f9c29a2a 144
el14moh 0:6b29f9c29a2a 145 game_timer.attach(&game_timer_isr,g_dt);
el14moh 0:6b29f9c29a2a 146
el14moh 2:602e9bb053a0 147
el14moh 3:8890b4605a10 148
el14moh 0:6b29f9c29a2a 149
el14moh 3:8890b4605a10 150 lcd.printString("Dodgemania",13,2); // Print game title on screen
el14moh 3:8890b4605a10 151 wait (2.5);
el14moh 3:8890b4605a10 152 for (int z=0; z<88; z++) {
el14moh 3:8890b4605a10 153 lcd.drawCircle(z,20,4,1);
el14moh 0:6b29f9c29a2a 154
el14moh 3:8890b4605a10 155 lcd.clearPixel(z-3,16);
el14moh 3:8890b4605a10 156 lcd.clearPixel(z-4,17);
el14moh 3:8890b4605a10 157 lcd.clearPixel(z-4,18);
el14moh 3:8890b4605a10 158 lcd.clearPixel(z-5,19);
el14moh 3:8890b4605a10 159 lcd.clearPixel(z-5,20);
el14moh 3:8890b4605a10 160 lcd.clearPixel(z-5,21);
el14moh 3:8890b4605a10 161 lcd.clearPixel(z-4,22);
el14moh 3:8890b4605a10 162 lcd.clearPixel(z-4,23);
el14moh 3:8890b4605a10 163 lcd.clearPixel(z-3,24);
el14moh 3:8890b4605a10 164 lcd.refresh();
el14moh 3:8890b4605a10 165 wait(0.01);
el14moh 3:8890b4605a10 166 }
el14moh 0:6b29f9c29a2a 167
el14moh 3:8890b4605a10 168 lcd.clear();
el14moh 3:8890b4605a10 169 wait(0.5);
el14moh 0:6b29f9c29a2a 170
el14moh 3:8890b4605a10 171 lcd.inverseMode();
el14moh 3:8890b4605a10 172 wait(0.2);
el14moh 3:8890b4605a10 173 lcd.normalMode();
el14moh 3:8890b4605a10 174
el14moh 3:8890b4605a10 175 while(1) {
el14moh 0:6b29f9c29a2a 176
el14moh 0:6b29f9c29a2a 177 lcd.printString("Press button",6,2);
el14moh 0:6b29f9c29a2a 178 lcd.printString("to start",18,3);
el14moh 0:6b29f9c29a2a 179
el14moh 2:602e9bb053a0 180 while(1) { // replace with interrupt?
el14moh 2:602e9bb053a0 181 if (button) {
el14moh 2:602e9bb053a0 182 break;
el14moh 0:6b29f9c29a2a 183 }
el14moh 0:6b29f9c29a2a 184 }
el14moh 0:6b29f9c29a2a 185
el14moh 2:602e9bb053a0 186 lcd.clear();
el14moh 2:602e9bb053a0 187 wait(0.5);
el14moh 0:6b29f9c29a2a 188 lcd.inverseMode();
el14moh 0:6b29f9c29a2a 189 wait(0.2);
el14moh 0:6b29f9c29a2a 190 lcd.normalMode();
el14moh 0:6b29f9c29a2a 191
el14moh 0:6b29f9c29a2a 192 // Draw game border
el14moh 3:8890b4605a10 193 lcd.drawLine(7,2,81,2,1);
el14moh 3:8890b4605a10 194 lcd.drawLine(7,2,7,45,1);
el14moh 0:6b29f9c29a2a 195 lcd.drawLine(81,2,81,45,1);
el14moh 3:8890b4605a10 196 lcd.drawLine(7,45,81,45,1);
el14moh 0:6b29f9c29a2a 197
el14moh 3:8890b4605a10 198 lcd.drawLine(5,0,83,0,1);
el14moh 3:8890b4605a10 199 lcd.drawLine(5,0,5,47,1);
el14moh 0:6b29f9c29a2a 200 lcd.drawLine(83,0,83,47,1);
el14moh 3:8890b4605a10 201 lcd.drawLine(5,47,83,47,1);
el14moh 0:6b29f9c29a2a 202
el14moh 0:6b29f9c29a2a 203 lcd.refresh();
el14moh 0:6b29f9c29a2a 204
el14moh 0:6b29f9c29a2a 205 // Countdown
el14moh 0:6b29f9c29a2a 206 wait(0.5);
el14moh 0:6b29f9c29a2a 207 lcd.printString("3",40,2);
el14moh 0:6b29f9c29a2a 208 wait(0.5);
el14moh 0:6b29f9c29a2a 209 lcd.drawRect(10,10,64,28,2);
el14moh 0:6b29f9c29a2a 210 lcd.refresh();
el14moh 0:6b29f9c29a2a 211 wait(0.5);
el14moh 0:6b29f9c29a2a 212 lcd.printString("2",40,2);
el14moh 0:6b29f9c29a2a 213 wait(0.5);
el14moh 0:6b29f9c29a2a 214 lcd.drawRect(10,10,64,28,2);
el14moh 0:6b29f9c29a2a 215 lcd.refresh();
el14moh 0:6b29f9c29a2a 216 wait(0.5);
el14moh 0:6b29f9c29a2a 217 lcd.printString("1",40,2);
el14moh 0:6b29f9c29a2a 218 wait(0.5);
el14moh 0:6b29f9c29a2a 219 lcd.drawRect(10,10,64,28,2);
el14moh 0:6b29f9c29a2a 220 lcd.refresh();
el14moh 0:6b29f9c29a2a 221 wait(0.5);
el14moh 0:6b29f9c29a2a 222 lcd.drawRect(10,10,64,28,2);
el14moh 0:6b29f9c29a2a 223 lcd.refresh();
el14moh 0:6b29f9c29a2a 224 lcd.printString("Go!",36,2);
el14moh 0:6b29f9c29a2a 225 wait(0.5);
el14moh 0:6b29f9c29a2a 226 lcd.drawRect(10,10,64,28,2);
el14moh 0:6b29f9c29a2a 227 lcd.refresh();
el14moh 0:6b29f9c29a2a 228
el14moh 2:602e9bb053a0 229 flick.rise(&button_isr);
el14moh 2:602e9bb053a0 230 flick.mode(PullDown);
el14moh 2:602e9bb053a0 231
el14moh 3:8890b4605a10 232 init_game();
el14moh 3:8890b4605a10 233
el14moh 0:6b29f9c29a2a 234 while (game_over_flag == 0) {
el14moh 0:6b29f9c29a2a 235
el14moh 0:6b29f9c29a2a 236 if ( g_timer_flag ) { // ticker interrupt
el14moh 0:6b29f9c29a2a 237 g_timer_flag = 0; // clear flag
el14moh 0:6b29f9c29a2a 238 moveWall();
el14moh 0:6b29f9c29a2a 239 moveBall();
el14moh 2:602e9bb053a0 240 invincible();
el14moh 2:602e9bb053a0 241 checkBorderCollision();
el14moh 2:602e9bb053a0 242 if (mortal) {
el14moh 2:602e9bb053a0 243 checkWallCollision();
el14moh 2:602e9bb053a0 244 }
el14moh 0:6b29f9c29a2a 245 updateScreen();
el14moh 0:6b29f9c29a2a 246 warning();
el14moh 0:6b29f9c29a2a 247 debug();
el14moh 0:6b29f9c29a2a 248
el14moh 0:6b29f9c29a2a 249 counter++; // increment counter each cycle (approx. 20 points a second)
el14moh 0:6b29f9c29a2a 250
el14moh 0:6b29f9c29a2a 251 // wall cooldowns increased. N.B these are set to 0 when a wall finishes moving
el14moh 0:6b29f9c29a2a 252 leftwall.cooldown++;
el14moh 0:6b29f9c29a2a 253 rightwall.cooldown++;
el14moh 0:6b29f9c29a2a 254 downwall.cooldown++;
el14moh 0:6b29f9c29a2a 255 upwall.cooldown++;
el14moh 0:6b29f9c29a2a 256 }
el14moh 0:6b29f9c29a2a 257 sleep();
el14moh 0:6b29f9c29a2a 258 }
el14moh 0:6b29f9c29a2a 259
el14moh 3:8890b4605a10 260 game_over_flag = 0;
el14moh 3:8890b4605a10 261
el14moh 0:6b29f9c29a2a 262 lcd.inverseMode();
el14moh 0:6b29f9c29a2a 263 wait(0.1);
el14moh 0:6b29f9c29a2a 264 lcd.normalMode();
el14moh 0:6b29f9c29a2a 265 wait(2.0);
el14moh 0:6b29f9c29a2a 266 lcd.clear();
el14moh 0:6b29f9c29a2a 267 wait(0.2);
el14moh 0:6b29f9c29a2a 268
el14moh 0:6b29f9c29a2a 269 lcd.drawRect(11,5,62,11,0);
el14moh 0:6b29f9c29a2a 270 lcd.printString("Game Over!",14,1);
el14moh 0:6b29f9c29a2a 271 lcd.refresh();
el14moh 0:6b29f9c29a2a 272
el14moh 0:6b29f9c29a2a 273 wait(1.0);
el14moh 0:6b29f9c29a2a 274 lcd.drawRect(11,21,62,21,0);
el14moh 0:6b29f9c29a2a 275 lcd.printString("Score:",14,3);
el14moh 0:6b29f9c29a2a 276 char buffer[14];
el14moh 0:6b29f9c29a2a 277 int length = sprintf(buffer,"%d",counter);
el14moh 0:6b29f9c29a2a 278 if (counter <= 999999999) { // 999999999 is highest number that fits in score display box
el14moh 0:6b29f9c29a2a 279 lcd.printString(buffer,14,4);
el14moh 0:6b29f9c29a2a 280 } else {
el14moh 0:6b29f9c29a2a 281 lcd.printString ("Too High!",14,4); // if score is too large to fit in box (unlikely!)
el14moh 0:6b29f9c29a2a 282 }
el14moh 0:6b29f9c29a2a 283 lcd.refresh();
el14moh 0:6b29f9c29a2a 284
el14moh 0:6b29f9c29a2a 285 wait(5.0);
el14moh 0:6b29f9c29a2a 286 lcd.inverseMode();
el14moh 0:6b29f9c29a2a 287 wait(0.2);
el14moh 0:6b29f9c29a2a 288 lcd.normalMode();
el14moh 0:6b29f9c29a2a 289 lcd.clear();
el14moh 0:6b29f9c29a2a 290
el14moh 3:8890b4605a10 291 //return 0;
el14moh 0:6b29f9c29a2a 292 }
el14moh 0:6b29f9c29a2a 293
el14moh 0:6b29f9c29a2a 294 }
el14moh 0:6b29f9c29a2a 295
el14moh 2:602e9bb053a0 296 void moveBall() // reads joystick direction and moves position of the player
el14moh 0:6b29f9c29a2a 297 {
el14moh 0:6b29f9c29a2a 298 if (joystick.direction == UP) {
el14moh 0:6b29f9c29a2a 299 j-=1;
el14moh 0:6b29f9c29a2a 300 } else if (joystick.direction == DOWN) {
el14moh 0:6b29f9c29a2a 301 j+=1;
el14moh 0:6b29f9c29a2a 302 } else if (joystick.direction == LEFT) {
el14moh 0:6b29f9c29a2a 303 i-=1;
el14moh 0:6b29f9c29a2a 304 } else if (joystick.direction == RIGHT) {
el14moh 0:6b29f9c29a2a 305 i+=1;
el14moh 0:6b29f9c29a2a 306 } else if (joystick.direction == UPRIGHT) {
el14moh 0:6b29f9c29a2a 307 j-=1;
el14moh 0:6b29f9c29a2a 308 i+=1;
el14moh 0:6b29f9c29a2a 309 } else if (joystick.direction == UPLEFT) {
el14moh 0:6b29f9c29a2a 310 j-=1;
el14moh 0:6b29f9c29a2a 311 i-=1;
el14moh 0:6b29f9c29a2a 312 } else if (joystick.direction == DOWNRIGHT) {
el14moh 0:6b29f9c29a2a 313 j+=1;
el14moh 0:6b29f9c29a2a 314 i+=1;
el14moh 0:6b29f9c29a2a 315 } else if (joystick.direction == DOWNLEFT) {
el14moh 0:6b29f9c29a2a 316 j+=1;
el14moh 0:6b29f9c29a2a 317 i-=1;
el14moh 0:6b29f9c29a2a 318 }
el14moh 0:6b29f9c29a2a 319 }
el14moh 0:6b29f9c29a2a 320
el14moh 2:602e9bb053a0 321 void moveWall() // moves walls along the screen
el14moh 0:6b29f9c29a2a 322 {
el14moh 0:6b29f9c29a2a 323 leftwall.random = rand()%20;
el14moh 0:6b29f9c29a2a 324 rightwall.random = rand()%20;
el14moh 0:6b29f9c29a2a 325 downwall.random = rand()%50;
el14moh 0:6b29f9c29a2a 326 upwall.random = rand()%50;
el14moh 0:6b29f9c29a2a 327
el14moh 0:6b29f9c29a2a 328 // LEFT WALL
el14moh 0:6b29f9c29a2a 329 if (leftwall.moveFlag == 1) { // if wall is moving
el14moh 0:6b29f9c29a2a 330 leftwall.x-=1; // move wall left
el14moh 3:8890b4605a10 331 if (leftwall.x<8) { // if wall hits a border
el14moh 0:6b29f9c29a2a 332 leftwall.moveFlag = 0; // stop wall moving
el14moh 0:6b29f9c29a2a 333 leftwall.cooldown = 0; // reset the cooldown for the wall
el14moh 0:6b29f9c29a2a 334 }
el14moh 0:6b29f9c29a2a 335 } else { // if wall has stopped moving
el14moh 0:6b29f9c29a2a 336 if (leftwall.genFlag == 0) { // if a new wall HASN'T been generated
el14moh 0:6b29f9c29a2a 337 leftwall.y = rand() % 27+8; // make new random y-coordinate
el14moh 0:6b29f9c29a2a 338 leftwall.x = 82; // reset x-coordinate to rightmost position
el14moh 0:6b29f9c29a2a 339 leftwall.genFlag = 1; // wall has been generated
el14moh 0:6b29f9c29a2a 340 } else { // if a new wall HAS been generated
el14moh 0:6b29f9c29a2a 341 if (leftwall.cooldown > 80) { // if a new wall hasnt started moving in 4 seconds, force it to move
el14moh 0:6b29f9c29a2a 342 leftwall.moveFlag = 1;
el14moh 0:6b29f9c29a2a 343 leftwall.genFlag = 0;
el14moh 0:6b29f9c29a2a 344 } else if ((leftwall.random == 1)&&(rightwall.cooldown > 60)) { // if wall starts moving again
el14moh 0:6b29f9c29a2a 345 leftwall.moveFlag = 1; // start wall moving
el14moh 0:6b29f9c29a2a 346 leftwall.genFlag = 0; // clear 'wall generated' flag
el14moh 0:6b29f9c29a2a 347 } else { // else if wall has not started moving again
el14moh 0:6b29f9c29a2a 348 leftwall.moveFlag = 0; // wall is stopped
el14moh 0:6b29f9c29a2a 349 }
el14moh 0:6b29f9c29a2a 350 }
el14moh 0:6b29f9c29a2a 351 }
el14moh 0:6b29f9c29a2a 352
el14moh 0:6b29f9c29a2a 353 // RIGHT WALL
el14moh 0:6b29f9c29a2a 354 if (counter > 200) {
el14moh 0:6b29f9c29a2a 355 if (rightwall.moveFlag == 1) {
el14moh 0:6b29f9c29a2a 356 rightwall.x+=1;
el14moh 0:6b29f9c29a2a 357 if (rightwall.x>80) {
el14moh 0:6b29f9c29a2a 358 rightwall.moveFlag = 0;
el14moh 0:6b29f9c29a2a 359 rightwall.cooldown = 0;
el14moh 0:6b29f9c29a2a 360 }
el14moh 0:6b29f9c29a2a 361 } else {
el14moh 0:6b29f9c29a2a 362 if ((rightwall.genFlag == 0)) {
el14moh 0:6b29f9c29a2a 363 rightwall.y = rand() % 27+8;
el14moh 3:8890b4605a10 364 rightwall.x = 6;
el14moh 0:6b29f9c29a2a 365 rightwall.genFlag = 1;
el14moh 0:6b29f9c29a2a 366 } else {
el14moh 0:6b29f9c29a2a 367 if (rightwall.cooldown > 80) {
el14moh 0:6b29f9c29a2a 368 rightwall.moveFlag = 1;
el14moh 0:6b29f9c29a2a 369 rightwall.genFlag = 0;
el14moh 0:6b29f9c29a2a 370 } else if ((rightwall.random == 1) && (leftwall.cooldown > 60)) {
el14moh 0:6b29f9c29a2a 371 rightwall.moveFlag = 1;
el14moh 0:6b29f9c29a2a 372 rightwall.genFlag = 0;
el14moh 0:6b29f9c29a2a 373 } else {
el14moh 0:6b29f9c29a2a 374 rightwall.moveFlag = 0;
el14moh 0:6b29f9c29a2a 375 }
el14moh 0:6b29f9c29a2a 376 }
el14moh 0:6b29f9c29a2a 377 }
el14moh 0:6b29f9c29a2a 378 }
el14moh 0:6b29f9c29a2a 379
el14moh 0:6b29f9c29a2a 380 // DOWN WALL
el14moh 0:6b29f9c29a2a 381 if (counter > 600) {
el14moh 0:6b29f9c29a2a 382 if (downwall.moveFlag == 1) {
el14moh 0:6b29f9c29a2a 383 if (upwall.cooldown > 60) {
el14moh 0:6b29f9c29a2a 384 if (counter % 2 == 1) { // horizontal walls move half the speed of vertical walls
el14moh 0:6b29f9c29a2a 385 downwall.y+=1;
el14moh 0:6b29f9c29a2a 386 }
el14moh 0:6b29f9c29a2a 387 }
el14moh 0:6b29f9c29a2a 388 if (downwall.y>44) {
el14moh 0:6b29f9c29a2a 389 downwall.moveFlag = 0;
el14moh 0:6b29f9c29a2a 390 }
el14moh 0:6b29f9c29a2a 391 } else {
el14moh 0:6b29f9c29a2a 392 if (downwall.genFlag == 0) {
el14moh 4:d44eace87ab3 393 downwall.x = rand() % 58+13;
el14moh 0:6b29f9c29a2a 394 downwall.y = 1;
el14moh 0:6b29f9c29a2a 395 downwall.genFlag = 1;
el14moh 0:6b29f9c29a2a 396 } else {
el14moh 0:6b29f9c29a2a 397 if (downwall.cooldown > 80) {
el14moh 0:6b29f9c29a2a 398 downwall.moveFlag = 1;
el14moh 0:6b29f9c29a2a 399 downwall.genFlag = 0;
el14moh 0:6b29f9c29a2a 400 } else if ((downwall.random == 1)&&(upwall.cooldown > 60)) {
el14moh 0:6b29f9c29a2a 401 downwall.moveFlag = 1;
el14moh 0:6b29f9c29a2a 402 downwall.genFlag = 0;
el14moh 0:6b29f9c29a2a 403 } else {
el14moh 0:6b29f9c29a2a 404 downwall.moveFlag = 0;
el14moh 0:6b29f9c29a2a 405 }
el14moh 0:6b29f9c29a2a 406 }
el14moh 0:6b29f9c29a2a 407 }
el14moh 0:6b29f9c29a2a 408 }
el14moh 0:6b29f9c29a2a 409
el14moh 0:6b29f9c29a2a 410 // UP WALL
el14moh 0:6b29f9c29a2a 411 if (counter > 1500) {
el14moh 0:6b29f9c29a2a 412 if (upwall.moveFlag == 1) {
el14moh 0:6b29f9c29a2a 413 if (downwall.cooldown > 60) {
el14moh 0:6b29f9c29a2a 414 if (counter % 2 == 1) { // horizontal walls move half the speed of vertical walls
el14moh 0:6b29f9c29a2a 415 upwall.y-=1;
el14moh 0:6b29f9c29a2a 416 }
el14moh 0:6b29f9c29a2a 417 }
el14moh 0:6b29f9c29a2a 418 if (upwall.y<3) {
el14moh 0:6b29f9c29a2a 419 upwall.moveFlag = 0;
el14moh 0:6b29f9c29a2a 420 }
el14moh 0:6b29f9c29a2a 421 } else {
el14moh 0:6b29f9c29a2a 422 if (upwall.genFlag == 0) {
el14moh 4:d44eace87ab3 423 upwall.x = rand() % 58+13;
el14moh 0:6b29f9c29a2a 424 upwall.y = 46;
el14moh 0:6b29f9c29a2a 425 upwall.genFlag = 1;
el14moh 0:6b29f9c29a2a 426 } else {
el14moh 0:6b29f9c29a2a 427 if (upwall.cooldown > 80) {
el14moh 0:6b29f9c29a2a 428 upwall.moveFlag = 1;
el14moh 0:6b29f9c29a2a 429 upwall.genFlag = 0;
el14moh 0:6b29f9c29a2a 430 } else if ((upwall.random == 1)&&(downwall.cooldown > 60)) {
el14moh 0:6b29f9c29a2a 431 upwall.moveFlag = 1;
el14moh 0:6b29f9c29a2a 432 upwall.genFlag = 0;
el14moh 0:6b29f9c29a2a 433 } else {
el14moh 0:6b29f9c29a2a 434 upwall.moveFlag = 0;
el14moh 0:6b29f9c29a2a 435 }
el14moh 0:6b29f9c29a2a 436 }
el14moh 0:6b29f9c29a2a 437 }
el14moh 0:6b29f9c29a2a 438 }
el14moh 0:6b29f9c29a2a 439 }
el14moh 2:602e9bb053a0 440 void checkBorderCollision()
el14moh 0:6b29f9c29a2a 441 {
el14moh 0:6b29f9c29a2a 442 // if floor
el14moh 0:6b29f9c29a2a 443 if ( j >= 47 - (PLAYERRADIUS+3)) {
el14moh 0:6b29f9c29a2a 444 j = 47 - (PLAYERRADIUS+3);
el14moh 0:6b29f9c29a2a 445 }
el14moh 0:6b29f9c29a2a 446
el14moh 0:6b29f9c29a2a 447 // if roof
el14moh 0:6b29f9c29a2a 448 if ( j <= (PLAYERRADIUS+3)) {
el14moh 0:6b29f9c29a2a 449 j = (PLAYERRADIUS+3);
el14moh 0:6b29f9c29a2a 450 }
el14moh 0:6b29f9c29a2a 451
el14moh 0:6b29f9c29a2a 452 // if right wall
el14moh 0:6b29f9c29a2a 453 if ( i >= 83 - (PLAYERRADIUS+3)) {
el14moh 0:6b29f9c29a2a 454 i = 83 - (PLAYERRADIUS+3);
el14moh 0:6b29f9c29a2a 455 }
el14moh 0:6b29f9c29a2a 456
el14moh 0:6b29f9c29a2a 457 // if left wall
el14moh 3:8890b4605a10 458 if ( i <= (PLAYERRADIUS+8)) {
el14moh 3:8890b4605a10 459 i = (PLAYERRADIUS+8);
el14moh 0:6b29f9c29a2a 460 }
el14moh 2:602e9bb053a0 461 }
el14moh 2:602e9bb053a0 462
el14moh 2:602e9bb053a0 463 void invincible()
el14moh 2:602e9bb053a0 464 {
el14moh 2:602e9bb053a0 465 if (g_button_flag) {
el14moh 2:602e9bb053a0 466 g_button_flag = 0;
el14moh 2:602e9bb053a0 467 if ((saves > 0) && (mortal == true)) { // saves are available and not currently used
el14moh 2:602e9bb053a0 468 invun_cool=0;
el14moh 2:602e9bb053a0 469 mortal = false;
el14moh 2:602e9bb053a0 470 invun_cool++;
el14moh 2:602e9bb053a0 471 saves--;
el14moh 2:602e9bb053a0 472 }
el14moh 2:602e9bb053a0 473 }
el14moh 2:602e9bb053a0 474 if (mortal == false) {
el14moh 2:602e9bb053a0 475 invun_cool++;
el14moh 5:c64d8ba051e9 476 if (invun_cool > 30) { // ticker called 20 times per second, therefore 1.5s of invincibility
el14moh 2:602e9bb053a0 477 mortal = true;
el14moh 2:602e9bb053a0 478 invun_cool=0;
el14moh 2:602e9bb053a0 479 }
el14moh 2:602e9bb053a0 480 }
el14moh 2:602e9bb053a0 481 }
el14moh 2:602e9bb053a0 482
el14moh 2:602e9bb053a0 483 void checkWallCollision() // checks for any collisions (i.e. player has hit a wall or side of the screen)
el14moh 2:602e9bb053a0 484 {
el14moh 0:6b29f9c29a2a 485
el14moh 0:6b29f9c29a2a 486 // LEFT WALL
el14moh 0:6b29f9c29a2a 487 if ((((i - PLAYERRADIUS) <= leftwall.x) && (leftwall.x <= (i + PLAYERRADIUS))) && (j > (leftwall.y+3) || j < (leftwall.y-3))) {
el14moh 0:6b29f9c29a2a 488 game_over_flag = 1;
el14moh 0:6b29f9c29a2a 489 }
el14moh 0:6b29f9c29a2a 490
el14moh 0:6b29f9c29a2a 491 // RIGHT WALL
el14moh 0:6b29f9c29a2a 492 if ((((i - PLAYERRADIUS) <= rightwall.x) && (rightwall.x <= (i + PLAYERRADIUS))) && (j > (rightwall.y+3) || j < (rightwall.y-3))) {
el14moh 0:6b29f9c29a2a 493 game_over_flag = 1;
el14moh 0:6b29f9c29a2a 494 }
el14moh 0:6b29f9c29a2a 495
el14moh 0:6b29f9c29a2a 496 // DOWN WALL
el14moh 0:6b29f9c29a2a 497 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 498 game_over_flag = 1;
el14moh 0:6b29f9c29a2a 499 }
el14moh 0:6b29f9c29a2a 500
el14moh 0:6b29f9c29a2a 501 // UP WALL
el14moh 0:6b29f9c29a2a 502 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 503 game_over_flag = 1;
el14moh 0:6b29f9c29a2a 504 }
el14moh 0:6b29f9c29a2a 505 }
el14moh 0:6b29f9c29a2a 506
el14moh 0:6b29f9c29a2a 507 void updateScreen() // refreshes the screen, redraws player and walls
el14moh 0:6b29f9c29a2a 508 {
el14moh 0:6b29f9c29a2a 509 lcd.clear();
el14moh 2:602e9bb053a0 510 if (mortal) {
el14moh 2:602e9bb053a0 511 lcd.drawCircle(i,j,PLAYERRADIUS,1);
el14moh 2:602e9bb053a0 512 } else {
el14moh 2:602e9bb053a0 513 if (counter % 2 == 0) {
el14moh 2:602e9bb053a0 514 lcd.drawCircle(i,j,PLAYERRADIUS,1);
el14moh 2:602e9bb053a0 515 }
el14moh 2:602e9bb053a0 516 }
el14moh 0:6b29f9c29a2a 517 lcd.refresh(); // update display
el14moh 0:6b29f9c29a2a 518
el14moh 3:8890b4605a10 519 if (saves > 0) {
el14moh 3:8890b4605a10 520 lcd.drawCircle(2,7,1,1);
el14moh 3:8890b4605a10 521 }
el14moh 3:8890b4605a10 522 if (saves > 1) {
el14moh 3:8890b4605a10 523 lcd.drawCircle(2,15,1,1);
el14moh 3:8890b4605a10 524 }
el14moh 3:8890b4605a10 525 if (saves > 2) {
el14moh 3:8890b4605a10 526 lcd.drawCircle(2,23,1,1);
el14moh 3:8890b4605a10 527 }
el14moh 3:8890b4605a10 528 if (saves > 3) {
el14moh 3:8890b4605a10 529 lcd.drawCircle(2,31,1,1);
el14moh 3:8890b4605a10 530 }
el14moh 3:8890b4605a10 531 if (saves > 4) {
el14moh 3:8890b4605a10 532 lcd.drawCircle(2,39,1,1);
el14moh 3:8890b4605a10 533 }
el14moh 3:8890b4605a10 534
el14moh 2:602e9bb053a0 535
el14moh 0:6b29f9c29a2a 536 // draw Border
el14moh 3:8890b4605a10 537 lcd.drawLine(7,2,81,2,1);
el14moh 3:8890b4605a10 538 lcd.drawLine(7,2,7,45,1);
el14moh 0:6b29f9c29a2a 539 lcd.drawLine(81,2,81,45,1);
el14moh 3:8890b4605a10 540 lcd.drawLine(7,45,81,45,1);
el14moh 0:6b29f9c29a2a 541
el14moh 3:8890b4605a10 542 lcd.drawLine(5,0,83,0,1);
el14moh 3:8890b4605a10 543 lcd.drawLine(5,0,5,47,1);
el14moh 0:6b29f9c29a2a 544 lcd.drawLine(83,0,83,47,1);
el14moh 3:8890b4605a10 545 lcd.drawLine(5,47,83,47,1);
el14moh 0:6b29f9c29a2a 546
el14moh 0:6b29f9c29a2a 547 // draw walls
el14moh 0:6b29f9c29a2a 548 // LEFT WALL
el14moh 0:6b29f9c29a2a 549 lcd.drawLine(leftwall.x,leftwall.y+5,leftwall.x,44,1);
el14moh 0:6b29f9c29a2a 550 lcd.drawLine(leftwall.x,leftwall.y-5,leftwall.x,3,1);
el14moh 0:6b29f9c29a2a 551 lcd.drawLine(leftwall.x+1,leftwall.y+5,leftwall.x+1,44,1);
el14moh 0:6b29f9c29a2a 552 lcd.drawLine(leftwall.x+1,leftwall.y-5,leftwall.x+1,3,1);
el14moh 0:6b29f9c29a2a 553
el14moh 0:6b29f9c29a2a 554 // RIGHT WALL
el14moh 0:6b29f9c29a2a 555 if (counter > 200) {
el14moh 0:6b29f9c29a2a 556 lcd.drawLine(rightwall.x,rightwall.y+5,rightwall.x,44,1);
el14moh 0:6b29f9c29a2a 557 lcd.drawLine(rightwall.x,rightwall.y-5,rightwall.x,3,1);
el14moh 0:6b29f9c29a2a 558 lcd.drawLine(rightwall.x-1,rightwall.y+5,rightwall.x-1,44,1);
el14moh 0:6b29f9c29a2a 559 lcd.drawLine(rightwall.x-1,rightwall.y-5,rightwall.x-1,3,1);
el14moh 0:6b29f9c29a2a 560 }
el14moh 0:6b29f9c29a2a 561
el14moh 0:6b29f9c29a2a 562 // DOWN WALL
el14moh 0:6b29f9c29a2a 563 if (counter > 600) {
el14moh 0:6b29f9c29a2a 564 lcd.drawLine(downwall.x+11,downwall.y,80,downwall.y,1);
el14moh 3:8890b4605a10 565 lcd.drawLine(downwall.x-11,downwall.y,8,downwall.y,1);
el14moh 0:6b29f9c29a2a 566 lcd.drawLine(downwall.x+11,downwall.y-1,80,downwall.y-1,1);
el14moh 3:8890b4605a10 567 lcd.drawLine(downwall.x-11,downwall.y-1,8,downwall.y-1,1);
el14moh 0:6b29f9c29a2a 568 }
el14moh 0:6b29f9c29a2a 569
el14moh 0:6b29f9c29a2a 570 // UP WALL
el14moh 0:6b29f9c29a2a 571 if (counter > 1500) {
el14moh 0:6b29f9c29a2a 572 lcd.drawLine(upwall.x+11,upwall.y,80,upwall.y,1);
el14moh 3:8890b4605a10 573 lcd.drawLine(upwall.x-11,upwall.y,8,upwall.y,1);
el14moh 0:6b29f9c29a2a 574 lcd.drawLine(upwall.x+11,upwall.y+1,80,upwall.y+1,1);
el14moh 3:8890b4605a10 575 lcd.drawLine(upwall.x-11,upwall.y+1,8,upwall.y+1,1);
el14moh 0:6b29f9c29a2a 576 }
el14moh 0:6b29f9c29a2a 577
el14moh 0:6b29f9c29a2a 578 lcd.refresh();
el14moh 0:6b29f9c29a2a 579 }
el14moh 0:6b29f9c29a2a 580
el14moh 0:6b29f9c29a2a 581 void warning ()
el14moh 0:6b29f9c29a2a 582 {
el14moh 0:6b29f9c29a2a 583 if (counter == 170) {
el14moh 0:6b29f9c29a2a 584 lcd.inverseMode();
el14moh 0:6b29f9c29a2a 585 } else if (counter == 570) {
el14moh 0:6b29f9c29a2a 586 lcd.inverseMode();
el14moh 0:6b29f9c29a2a 587 } else if (counter == 1470) {
el14moh 0:6b29f9c29a2a 588 lcd.inverseMode();
el14moh 0:6b29f9c29a2a 589 } else {
el14moh 0:6b29f9c29a2a 590 lcd.normalMode();
el14moh 0:6b29f9c29a2a 591 }
el14moh 0:6b29f9c29a2a 592 }
el14moh 0:6b29f9c29a2a 593
el14moh 0:6b29f9c29a2a 594 void game_timer_isr() // sets flag for timer interrupt
el14moh 0:6b29f9c29a2a 595 {
el14moh 0:6b29f9c29a2a 596 g_timer_flag = 1;
el14moh 0:6b29f9c29a2a 597 }
el14moh 0:6b29f9c29a2a 598
el14moh 2:602e9bb053a0 599 void button_isr()
el14moh 2:602e9bb053a0 600 {
el14moh 2:602e9bb053a0 601 g_button_flag = 1;
el14moh 2:602e9bb053a0 602 }
el14moh 2:602e9bb053a0 603
el14moh 0:6b29f9c29a2a 604 void initDisplay() // initialises the LCD display
el14moh 0:6b29f9c29a2a 605 {
el14moh 0:6b29f9c29a2a 606 lcd.init();
el14moh 0:6b29f9c29a2a 607 lcd.normalMode();
el14moh 0:6b29f9c29a2a 608 lcd.setBrightness(1.0F-backlight); // brightness pot on PCB is soldered in the wrong direction, (1.0F-backlight) inverts the reading
el14moh 0:6b29f9c29a2a 609 }
el14moh 0:6b29f9c29a2a 610
el14moh 0:6b29f9c29a2a 611 void calibrateJoystick() // read default positions of the joystick to calibrate later readings
el14moh 0:6b29f9c29a2a 612 {
el14moh 0:6b29f9c29a2a 613 button.mode(PullDown);
el14moh 0:6b29f9c29a2a 614 // must not move during calibration
el14moh 0:6b29f9c29a2a 615 joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
el14moh 0:6b29f9c29a2a 616 joystick.y0 = yPot;
el14moh 0:6b29f9c29a2a 617 }
el14moh 0:6b29f9c29a2a 618 void updateJoystick() // reads direction the joystick has been moved
el14moh 0:6b29f9c29a2a 619 {
el14moh 0:6b29f9c29a2a 620 // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
el14moh 0:6b29f9c29a2a 621 joystick.x = xPot - joystick.x0;
el14moh 0:6b29f9c29a2a 622 joystick.y = yPot - joystick.y0;
el14moh 0:6b29f9c29a2a 623 // read button state
el14moh 0:6b29f9c29a2a 624 joystick.button = button;
el14moh 0:6b29f9c29a2a 625
el14moh 0:6b29f9c29a2a 626 // calculate direction depending on x,y values
el14moh 0:6b29f9c29a2a 627 // tolerance allows a little lee-way in case joystick not exactly in the stated direction
el14moh 0:6b29f9c29a2a 628 if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 629 joystick.direction = CENTRE;
el14moh 0:6b29f9c29a2a 630 } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 631 joystick.direction = UP;
el14moh 0:6b29f9c29a2a 632 } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 633 joystick.direction = DOWN;
el14moh 0:6b29f9c29a2a 634 } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 635 joystick.direction = LEFT;
el14moh 0:6b29f9c29a2a 636 } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 637 joystick.direction = RIGHT;
el14moh 0:6b29f9c29a2a 638 } else if ( joystick.y > DIRECTION_TOLERANCE && joystick.x > DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 639 joystick.direction = UPLEFT;
el14moh 0:6b29f9c29a2a 640 } else if ( joystick.y > DIRECTION_TOLERANCE && joystick.x < DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 641 joystick.direction = UPRIGHT;
el14moh 0:6b29f9c29a2a 642 } else if ( joystick.x > DIRECTION_TOLERANCE && joystick.y < DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 643 joystick.direction = DOWNLEFT;
el14moh 0:6b29f9c29a2a 644 } else if ( joystick.x < DIRECTION_TOLERANCE && joystick.y < DIRECTION_TOLERANCE) {
el14moh 0:6b29f9c29a2a 645 joystick.direction = DOWNRIGHT;
el14moh 0:6b29f9c29a2a 646 }
el14moh 0:6b29f9c29a2a 647
el14moh 0:6b29f9c29a2a 648 else {
el14moh 0:6b29f9c29a2a 649 joystick.direction = UNKNOWN;
el14moh 0:6b29f9c29a2a 650 }
el14moh 0:6b29f9c29a2a 651
el14moh 0:6b29f9c29a2a 652
el14moh 0:6b29f9c29a2a 653 printFlag = 1; // set flag for printing
el14moh 0:6b29f9c29a2a 654 }
el14moh 0:6b29f9c29a2a 655
el14moh 0:6b29f9c29a2a 656 void debug() // prints for debug purposes
el14moh 0:6b29f9c29a2a 657 {
el14moh 0:6b29f9c29a2a 658 if (printFlag) { // if flag set, clear flag and print joystick values to serial port
el14moh 0:6b29f9c29a2a 659 printFlag = 0;
el14moh 0:6b29f9c29a2a 660 /*
el14moh 0:6b29f9c29a2a 661 if (joystick.direction == UP)
el14moh 0:6b29f9c29a2a 662 serial.printf(" UP\n");
el14moh 0:6b29f9c29a2a 663 if (joystick.direction == DOWN)
el14moh 0:6b29f9c29a2a 664 serial.printf(" DOWN\n");
el14moh 0:6b29f9c29a2a 665 if (joystick.direction == LEFT)
el14moh 0:6b29f9c29a2a 666 serial.printf(" LEFT\n");
el14moh 0:6b29f9c29a2a 667 if (joystick.direction == RIGHT)
el14moh 0:6b29f9c29a2a 668 serial.printf(" RIGHT\n");
el14moh 0:6b29f9c29a2a 669 if (joystick.direction == UPRIGHT)
el14moh 0:6b29f9c29a2a 670 serial.printf(" UPRIGHT\n"); // Diagonally up + right
el14moh 0:6b29f9c29a2a 671 if (joystick.direction == UPLEFT)
el14moh 0:6b29f9c29a2a 672 serial.printf(" UPLEFT\n"); // Diagonally up + left
el14moh 0:6b29f9c29a2a 673 if (joystick.direction == DOWNRIGHT)
el14moh 0:6b29f9c29a2a 674 serial.printf(" DOWNRIGHT\n"); // Diagonally down + right
el14moh 0:6b29f9c29a2a 675 if (joystick.direction == DOWNLEFT)
el14moh 0:6b29f9c29a2a 676 serial.printf(" DOWNLEFT\n"); // Diagonally down + left
el14moh 0:6b29f9c29a2a 677 if (joystick.direction == CENTRE)
el14moh 0:6b29f9c29a2a 678 serial.printf(" CENTRE\n");
el14moh 0:6b29f9c29a2a 679 if (joystick.direction == UNKNOWN)
el14moh 0:6b29f9c29a2a 680 serial.printf(" Unsupported direction\n");
el14moh 0:6b29f9c29a2a 681 */
el14moh 2:602e9bb053a0 682 serial.printf("saves = %d \n",saves);
el14moh 2:602e9bb053a0 683 //serial.printf("Right-wall Cooldown = %d \n",rightwall.cooldown);
el14moh 2:602e9bb053a0 684 //serial.printf("counter = %d \n",counter);
el14moh 0:6b29f9c29a2a 685
el14moh 0:6b29f9c29a2a 686 }
el14moh 0:6b29f9c29a2a 687 }
el14moh 0:6b29f9c29a2a 688
el14moh 0:6b29f9c29a2a 689 void init_serial() // sets baud rate for serial
el14moh 0:6b29f9c29a2a 690 {
el14moh 0:6b29f9c29a2a 691 // set to highest baud - ensure terminal software matches
el14moh 0:6b29f9c29a2a 692 serial.baud(115200);
el14moh 0:6b29f9c29a2a 693 }
el14moh 3:8890b4605a10 694 void init_game()
el14moh 3:8890b4605a10 695 {
el14moh 0:6b29f9c29a2a 696
el14moh 3:8890b4605a10 697 g_button_flag = 0;
el14moh 3:8890b4605a10 698 leftwall.y = rand() % 27+8;
el14moh 3:8890b4605a10 699 leftwall.x = 0;
el14moh 3:8890b4605a10 700 rightwall.y = rand() % 27+8;
el14moh 3:8890b4605a10 701 rightwall.x = 0;
el14moh 3:8890b4605a10 702 downwall.x = rand() % 27+8;
el14moh 3:8890b4605a10 703 downwall.y = 0;
el14moh 3:8890b4605a10 704 upwall.x = rand() % 27+8;
el14moh 3:8890b4605a10 705 upwall.y = 0;
el14moh 3:8890b4605a10 706 rightwall.cooldown = 61;
el14moh 3:8890b4605a10 707 counter = 0;
el14moh 3:8890b4605a10 708
el14moh 3:8890b4605a10 709 saves = 5;
el14moh 3:8890b4605a10 710
el14moh 3:8890b4605a10 711 i = 42;
el14moh 3:8890b4605a10 712 j = 24;
el14moh 3:8890b4605a10 713
el14moh 3:8890b4605a10 714 }