Wall dodging game utilising a joystick and Nokia 5110 LCD display

Dependencies:   N5110 mbed

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