Wall dodging game utilising a joystick and Nokia 5110 LCD display

Dependencies:   N5110 mbed

Committer:
el14moh
Date:
Sat Apr 23 14:41:14 2016 +0000
Revision:
3:8890b4605a10
Parent:
2:602e9bb053a0
Child:
4:d44eace87ab3
invincibility properly implemented with indicators,; Game loops properly

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