A retro gaming programme, designed for use on a portable embedded system. Incorporates power saving techniques.

Dependencies:   ConfigFile N5110 PowerControl beep mbed

Committer:
el13drt
Date:
Wed Apr 15 15:30:45 2015 +0000
Revision:
7:217810c90306
Parent:
6:fa2928bcec17
Child:
8:457183719776
post toggle

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el13drt 0:a8bc3aac5f69 1 /**
el13drt 0:a8bc3aac5f69 2 @file main.cpp
el13drt 0:a8bc3aac5f69 3 @brief Program implementation
el13drt 0:a8bc3aac5f69 4 */
el13drt 0:a8bc3aac5f69 5
el13drt 0:a8bc3aac5f69 6 #include "mbed.h"
el13drt 0:a8bc3aac5f69 7 #include "N5110.h"
el13drt 0:a8bc3aac5f69 8
el13drt 0:a8bc3aac5f69 9 #include <ctime>
el13drt 0:a8bc3aac5f69 10 #include <cstdlib>
el13drt 0:a8bc3aac5f69 11
el13drt 0:a8bc3aac5f69 12 // change this to alter tolerance of joystick direction
el13drt 0:a8bc3aac5f69 13 #define DIRECTION_TOLERANCE 0.05
el13drt 0:a8bc3aac5f69 14
el13drt 0:a8bc3aac5f69 15 // VCC,SCE,RST,D/C,MOSI,SCLK,LED - set pins for LCD
el13drt 0:a8bc3aac5f69 16 N5110 lcd(p7,p8,p9,p10,p11,p13,p22);
el13drt 0:a8bc3aac5f69 17
el13drt 0:a8bc3aac5f69 18 // navigation/action buttons
el13drt 0:a8bc3aac5f69 19 DigitalIn buttonA(p19);
el13drt 0:a8bc3aac5f69 20 DigitalIn buttonB(p20);
el13drt 0:a8bc3aac5f69 21
el13drt 3:57bab3b1c974 22 // LED indicators
el13drt 3:57bab3b1c974 23 AnalogOut ledR(p18);// RED LED
el13drt 3:57bab3b1c974 24 DigitalOut ledY(p24);// YELLOW LED
el13drt 3:57bab3b1c974 25
el13drt 0:a8bc3aac5f69 26 // connections for joystick
el13drt 3:57bab3b1c974 27 DigitalIn button(p17);
el13drt 0:a8bc3aac5f69 28 AnalogIn xPot(p15);
el13drt 0:a8bc3aac5f69 29 AnalogIn yPot(p16);
el13drt 0:a8bc3aac5f69 30
el13drt 4:c6305031f80d 31 // create local file system
el13drt 4:c6305031f80d 32 //LocalFileSytem local("local");
el13drt 4:c6305031f80d 33
el13drt 0:a8bc3aac5f69 34 // boundary conditions
el13drt 0:a8bc3aac5f69 35 int cells [84][48];
el13drt 0:a8bc3aac5f69 36
el13drt 0:a8bc3aac5f69 37 // Globabl Variables /////////////////////////
el13drt 0:a8bc3aac5f69 38
el13drt 4:c6305031f80d 39 // player's score
el13drt 4:c6305031f80d 40 int score = 0;
el13drt 4:c6305031f80d 41
el13drt 7:217810c90306 42 // difficulty - number of pixels hazards incrememnt by
el13drt 7:217810c90306 43 int fall = 2;
el13drt 7:217810c90306 44
el13drt 4:c6305031f80d 45 // global variables for movement (pixelNinja)
el13drt 0:a8bc3aac5f69 46 int a1 = 22;
el13drt 0:a8bc3aac5f69 47 int a2 = 24;
el13drt 0:a8bc3aac5f69 48 int a3 = 23;
el13drt 0:a8bc3aac5f69 49 int a4 = 25;
el13drt 0:a8bc3aac5f69 50 int a5 = 20;
el13drt 0:a8bc3aac5f69 51 int a6 = 26;
el13drt 0:a8bc3aac5f69 52 int a7 = 19;
el13drt 0:a8bc3aac5f69 53 int a8 = 21;
el13drt 0:a8bc3aac5f69 54
el13drt 0:a8bc3aac5f69 55 //global variable for random X co-ordinates
el13drt 0:a8bc3aac5f69 56 int randX1;
el13drt 0:a8bc3aac5f69 57 int randX2;
el13drt 0:a8bc3aac5f69 58 int randX3;
el13drt 0:a8bc3aac5f69 59 int randX4;
el13drt 0:a8bc3aac5f69 60 int randX5;
el13drt 0:a8bc3aac5f69 61 int randX6;
el13drt 0:a8bc3aac5f69 62
el13drt 7:217810c90306 63 // global variable for Y co-ordinates
el13drt 0:a8bc3aac5f69 64 int randY1 = 0;
el13drt 0:a8bc3aac5f69 65 int randY2 = 0;
el13drt 0:a8bc3aac5f69 66 int randY3 = 0;
el13drt 0:a8bc3aac5f69 67 int randY4 = 0;
el13drt 0:a8bc3aac5f69 68 int randY5 = 0;
el13drt 0:a8bc3aac5f69 69 int randY6 = 0;
el13drt 0:a8bc3aac5f69 70
el13drt 0:a8bc3aac5f69 71 // timer to regularly read the joystick
el13drt 0:a8bc3aac5f69 72 Ticker pollJoystick;
el13drt 1:352c50e88ffa 73
el13drt 3:57bab3b1c974 74 // serial for debug
el13drt 0:a8bc3aac5f69 75 Serial serial(USBTX,USBRX);
el13drt 0:a8bc3aac5f69 76
el13drt 0:a8bc3aac5f69 77 // create enumerated type (0,1,2,3 etc. for direction)
el13drt 0:a8bc3aac5f69 78 // could be extended for diagonals etc.
el13drt 0:a8bc3aac5f69 79 enum DirectionName {
el13drt 0:a8bc3aac5f69 80 UP,
el13drt 0:a8bc3aac5f69 81 DOWN,
el13drt 0:a8bc3aac5f69 82 LEFT,
el13drt 0:a8bc3aac5f69 83 RIGHT,
el13drt 0:a8bc3aac5f69 84 CENTRE,
el13drt 0:a8bc3aac5f69 85 UNKNOWN
el13drt 0:a8bc3aac5f69 86 };
el13drt 0:a8bc3aac5f69 87
el13drt 0:a8bc3aac5f69 88 // struct for Joystick
el13drt 0:a8bc3aac5f69 89 typedef struct JoyStick Joystick;
el13drt 0:a8bc3aac5f69 90 struct JoyStick {
el13drt 0:a8bc3aac5f69 91 float x; // current x value
el13drt 0:a8bc3aac5f69 92 float x0; // 'centred' x value
el13drt 0:a8bc3aac5f69 93 float y; // current y value
el13drt 0:a8bc3aac5f69 94 float y0; // 'centred' y value
el13drt 0:a8bc3aac5f69 95 int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
el13drt 0:a8bc3aac5f69 96 DirectionName direction; // current direction
el13drt 0:a8bc3aac5f69 97 };
el13drt 0:a8bc3aac5f69 98
el13drt 0:a8bc3aac5f69 99 // create struct variable
el13drt 0:a8bc3aac5f69 100 Joystick joystick;
el13drt 0:a8bc3aac5f69 101
el13drt 0:a8bc3aac5f69 102 int printFlag = 0;
el13drt 0:a8bc3aac5f69 103
el13drt 0:a8bc3aac5f69 104 // function prototypes
el13drt 0:a8bc3aac5f69 105 void calibrateJoystick();
el13drt 0:a8bc3aac5f69 106 void updateJoystick();
el13drt 0:a8bc3aac5f69 107
el13drt 0:a8bc3aac5f69 108 //intro screen
el13drt 0:a8bc3aac5f69 109 void welcome()
el13drt 0:a8bc3aac5f69 110 {
el13drt 0:a8bc3aac5f69 111 //bottom border
el13drt 0:a8bc3aac5f69 112 lcd.drawRect(0,44,84,2,1);
el13drt 0:a8bc3aac5f69 113
el13drt 0:a8bc3aac5f69 114 //top border
el13drt 0:a8bc3aac5f69 115 lcd.drawRect(0,1,84,2,1);
el13drt 0:a8bc3aac5f69 116
el13drt 0:a8bc3aac5f69 117 //print initials 'DRT'
el13drt 0:a8bc3aac5f69 118 lcd.printChar('D',30,14);
el13drt 0:a8bc3aac5f69 119 wait(0.6);
el13drt 0:a8bc3aac5f69 120 lcd.printChar('R',37,14);
el13drt 0:a8bc3aac5f69 121 wait(0.6);
el13drt 0:a8bc3aac5f69 122 lcd.printChar('T',44,14);
el13drt 0:a8bc3aac5f69 123 wait(0.6);
el13drt 0:a8bc3aac5f69 124
el13drt 0:a8bc3aac5f69 125 //print 'presents...'
el13drt 0:a8bc3aac5f69 126 lcd.printString("presents...",8,21);
el13drt 0:a8bc3aac5f69 127 wait(1.0);
el13drt 0:a8bc3aac5f69 128
el13drt 0:a8bc3aac5f69 129 //dramatic flashing
el13drt 0:a8bc3aac5f69 130 lcd.inverseMode();
el13drt 0:a8bc3aac5f69 131 wait(0.2);
el13drt 0:a8bc3aac5f69 132 lcd.normalMode();
el13drt 0:a8bc3aac5f69 133 wait(0.2);
el13drt 0:a8bc3aac5f69 134 lcd.inverseMode();
el13drt 0:a8bc3aac5f69 135 wait(0.2);
el13drt 0:a8bc3aac5f69 136 lcd.normalMode();
el13drt 0:a8bc3aac5f69 137 wait(1.0);
el13drt 0:a8bc3aac5f69 138
el13drt 0:a8bc3aac5f69 139 //more dramatic flashing
el13drt 0:a8bc3aac5f69 140 lcd.inverseMode();
el13drt 0:a8bc3aac5f69 141 wait(0.2);
el13drt 0:a8bc3aac5f69 142 lcd.normalMode();
el13drt 0:a8bc3aac5f69 143 wait(0.2);
el13drt 0:a8bc3aac5f69 144 lcd.inverseMode();
el13drt 0:a8bc3aac5f69 145 wait(0.2);
el13drt 0:a8bc3aac5f69 146 lcd.normalMode();
el13drt 0:a8bc3aac5f69 147 wait(0.6);
el13drt 0:a8bc3aac5f69 148 }
el13drt 0:a8bc3aac5f69 149
el13drt 0:a8bc3aac5f69 150 //selection menu///////////////////////////////////////////
el13drt 0:a8bc3aac5f69 151 void mainMenu()
el13drt 0:a8bc3aac5f69 152 {
el13drt 0:a8bc3aac5f69 153 //bottom border
el13drt 0:a8bc3aac5f69 154 lcd.drawRect(0,47,84,0,1);
el13drt 0:a8bc3aac5f69 155 //top border
el13drt 0:a8bc3aac5f69 156 lcd.drawRect(0,0,84,2,1);
el13drt 0:a8bc3aac5f69 157
el13drt 0:a8bc3aac5f69 158 //print 'Xtreme Tower'
el13drt 0:a8bc3aac5f69 159 lcd.printString("Xtreme",4,25);
el13drt 0:a8bc3aac5f69 160 lcd.printString("Tower",44,25);
el13drt 0:a8bc3aac5f69 161
el13drt 0:a8bc3aac5f69 162 //title outline
el13drt 0:a8bc3aac5f69 163 lcd.drawRect(3,6,77,10,0);
el13drt 0:a8bc3aac5f69 164
el13drt 0:a8bc3aac5f69 165 ////castle //x, y, w, h, fill//////////////////////
el13drt 0:a8bc3aac5f69 166
el13drt 0:a8bc3aac5f69 167 //castle main bulk
el13drt 0:a8bc3aac5f69 168 lcd.drawRect(59,32,21,8,1);
el13drt 0:a8bc3aac5f69 169
el13drt 0:a8bc3aac5f69 170 //left window bulk
el13drt 0:a8bc3aac5f69 171 lcd.drawRect(59,22,2,10,1);
el13drt 0:a8bc3aac5f69 172 //centre left window bulk
el13drt 0:a8bc3aac5f69 173 lcd.drawRect(65,22,2,10,1);
el13drt 0:a8bc3aac5f69 174 //centre right window bulk
el13drt 0:a8bc3aac5f69 175 lcd.drawRect(72,22,2,10,1);
el13drt 0:a8bc3aac5f69 176 //right window bulk
el13drt 0:a8bc3aac5f69 177 lcd.drawRect(78,22,2,10,1);
el13drt 0:a8bc3aac5f69 178 //central window bulk
el13drt 0:a8bc3aac5f69 179 lcd.drawRect(68,25,3,7,1);
el13drt 0:a8bc3aac5f69 180
el13drt 0:a8bc3aac5f69 181 //central window bulk
el13drt 0:a8bc3aac5f69 182 lcd.drawRect(75,28,0,0,1);
el13drt 0:a8bc3aac5f69 183 lcd.drawRect(77,28,0,0,1);
el13drt 0:a8bc3aac5f69 184 lcd.drawRect(64,28,0,0,1);
el13drt 0:a8bc3aac5f69 185 lcd.drawRect(62,28,0,0,1);
el13drt 0:a8bc3aac5f69 186
el13drt 0:a8bc3aac5f69 187 //above left window bulk
el13drt 0:a8bc3aac5f69 188 lcd.drawRect(62,25,3,2,1);
el13drt 0:a8bc3aac5f69 189 //above right window bulk
el13drt 0:a8bc3aac5f69 190 lcd.drawRect(75,25,2,2,1);
el13drt 0:a8bc3aac5f69 191
el13drt 0:a8bc3aac5f69 192 //lower right line
el13drt 0:a8bc3aac5f69 193 lcd.drawRect(71,42,9,0,1);
el13drt 0:a8bc3aac5f69 194 //upper right line
el13drt 0:a8bc3aac5f69 195 lcd.drawRect(70,41,10,0,1);
el13drt 0:a8bc3aac5f69 196
el13drt 0:a8bc3aac5f69 197 //upper left line
el13drt 0:a8bc3aac5f69 198 lcd.drawRect(59,41,10,0,1);
el13drt 0:a8bc3aac5f69 199 //lower left line
el13drt 0:a8bc3aac5f69 200 lcd.drawRect(59,42,9,0,1);
el13drt 0:a8bc3aac5f69 201
el13drt 0:a8bc3aac5f69 202 //bottom left bulk
el13drt 0:a8bc3aac5f69 203 lcd.drawRect(59,43,8,3,1);
el13drt 0:a8bc3aac5f69 204 //bottom right bulk
el13drt 0:a8bc3aac5f69 205 lcd.drawRect(72,43,8,3,1);
el13drt 0:a8bc3aac5f69 206
el13drt 0:a8bc3aac5f69 207 //option arrows - lower
el13drt 0:a8bc3aac5f69 208 lcd.drawRect(27,42,4,0,1);
el13drt 0:a8bc3aac5f69 209 lcd.drawRect(28,43,2,0,1);
el13drt 0:a8bc3aac5f69 210 lcd.drawRect(29,44,0,0,1);
el13drt 0:a8bc3aac5f69 211
el13drt 0:a8bc3aac5f69 212 //option arrows - higher
el13drt 0:a8bc3aac5f69 213 lcd.drawRect(27,29,4,0,1);
el13drt 0:a8bc3aac5f69 214 lcd.drawRect(28,28,2,0,1);
el13drt 0:a8bc3aac5f69 215 lcd.drawRect(29,27,0,0,1);
el13drt 0:a8bc3aac5f69 216 }
el13drt 0:a8bc3aac5f69 217
el13drt 0:a8bc3aac5f69 218 // static background
el13drt 0:a8bc3aac5f69 219 void backGround()
el13drt 0:a8bc3aac5f69 220 {
el13drt 0:a8bc3aac5f69 221 // x, y, w, h, fill - draw ground
el13drt 0:a8bc3aac5f69 222 lcd.drawRect(0,47,84,0,1);
el13drt 0:a8bc3aac5f69 223
el13drt 0:a8bc3aac5f69 224 // x, y, w, h, fill - draw left wall
el13drt 0:a8bc3aac5f69 225 lcd.drawRect(2,0,0,47,1);
el13drt 0:a8bc3aac5f69 226 // left wall - brick line 1
el13drt 0:a8bc3aac5f69 227 for(int x=0; x<47; x+=4)
el13drt 0:a8bc3aac5f69 228 lcd.drawLine(1,1,1,48,2);
el13drt 0:a8bc3aac5f69 229 // left wall - brick line 2
el13drt 0:a8bc3aac5f69 230 for(int x=0; x<47; x+=4)
el13drt 0:a8bc3aac5f69 231 lcd.drawLine(0,0,0,48,2);
el13drt 0:a8bc3aac5f69 232
el13drt 0:a8bc3aac5f69 233 // x, y, w, h, fill - draw right wall
el13drt 0:a8bc3aac5f69 234 lcd.drawRect(81,0,0,47,1);
el13drt 0:a8bc3aac5f69 235 // right wall - brick line 1
el13drt 0:a8bc3aac5f69 236 for(int x=0; x<WIDTH; x+=4)
el13drt 0:a8bc3aac5f69 237 lcd.drawLine(82,0,82,48,2);
el13drt 0:a8bc3aac5f69 238 // right wall - brick line 2
el13drt 0:a8bc3aac5f69 239 for(int x=0; x<WIDTH; x+=4)
el13drt 0:a8bc3aac5f69 240 lcd.drawLine(83,1,83,48,2);
el13drt 0:a8bc3aac5f69 241
el13drt 0:a8bc3aac5f69 242 lcd.refresh();
el13drt 0:a8bc3aac5f69 243 }
el13drt 0:a8bc3aac5f69 244
el13drt 0:a8bc3aac5f69 245 //pixel ninja character
el13drt 0:a8bc3aac5f69 246 void pixelNinja()
el13drt 0:a8bc3aac5f69 247 {
el13drt 0:a8bc3aac5f69 248 //x, y, w, h, fill - left leg
el13drt 0:a8bc3aac5f69 249 lcd.drawRect(a1,39,0,7,1);
el13drt 0:a8bc3aac5f69 250 //right leg
el13drt 0:a8bc3aac5f69 251 lcd.drawRect(a2,39,0,7,1);
el13drt 0:a8bc3aac5f69 252 //centre stick
el13drt 0:a8bc3aac5f69 253 lcd.drawRect(a3,37,0,7,1);
el13drt 0:a8bc3aac5f69 254 //back of the head
el13drt 0:a8bc3aac5f69 255 lcd.drawRect(a1,33,0,4,1);
el13drt 0:a8bc3aac5f69 256 //top of the head
el13drt 0:a8bc3aac5f69 257 lcd.drawRect(a1,33,4,0,1);
el13drt 0:a8bc3aac5f69 258 //jaw
el13drt 0:a8bc3aac5f69 259 lcd.drawRect(a2,38,2,0,1);
el13drt 0:a8bc3aac5f69 260 //right shoulder
el13drt 0:a8bc3aac5f69 261 lcd.drawRect(a4,40,1,0,1);
el13drt 0:a8bc3aac5f69 262 //left shoulder
el13drt 0:a8bc3aac5f69 263 lcd.drawRect(a5,40,1,0,1);
el13drt 0:a8bc3aac5f69 264 //left arm
el13drt 0:a8bc3aac5f69 265 lcd.drawRect(a5,41,0,1,1);
el13drt 0:a8bc3aac5f69 266 //right arm
el13drt 0:a8bc3aac5f69 267 lcd.drawRect(a6,41,0,1,1);
el13drt 0:a8bc3aac5f69 268 //right eye
el13drt 0:a8bc3aac5f69 269 lcd.drawRect(a6,35,0,0,1);
el13drt 0:a8bc3aac5f69 270 //mouth piece
el13drt 0:a8bc3aac5f69 271 lcd.drawRect(a6,37,0,0,1);
el13drt 0:a8bc3aac5f69 272 //left eye
el13drt 0:a8bc3aac5f69 273 lcd.drawRect(a2,35,0,0,1);
el13drt 0:a8bc3aac5f69 274 //sword handle
el13drt 0:a8bc3aac5f69 275 lcd.drawRect(a7,36,0,0,1);
el13drt 0:a8bc3aac5f69 276 lcd.drawRect(a5,37,0,0,1);
el13drt 0:a8bc3aac5f69 277 lcd.drawRect(a8,38,0,0,1);
el13drt 0:a8bc3aac5f69 278 }
el13drt 7:217810c90306 279 // stops ninja going through walls
el13drt 7:217810c90306 280 void ninjaBoundaries()
el13drt 7:217810c90306 281 {
el13drt 7:217810c90306 282 if(a6 > 79 )
el13drt 7:217810c90306 283 a6 = 79;
el13drt 7:217810c90306 284 if(a4 > 78)
el13drt 7:217810c90306 285 a4 = 78;
el13drt 7:217810c90306 286 if(a2 > 77)
el13drt 7:217810c90306 287 a2 = 77;
el13drt 7:217810c90306 288 if(a3 > 76)
el13drt 7:217810c90306 289 a3 = 76;
el13drt 7:217810c90306 290 if(a1 > 75)
el13drt 7:217810c90306 291 a1 = 75;
el13drt 7:217810c90306 292 if(a8 > 74)
el13drt 7:217810c90306 293 a8 = 74;
el13drt 7:217810c90306 294 if(a5 > 73)
el13drt 7:217810c90306 295 a5 = 73;
el13drt 7:217810c90306 296 if(a7 > 72)
el13drt 7:217810c90306 297 a7 = 72;
el13drt 7:217810c90306 298
el13drt 7:217810c90306 299 if(a6 < 11 )
el13drt 7:217810c90306 300 a6 = 11;
el13drt 7:217810c90306 301 if(a4 < 10)
el13drt 7:217810c90306 302 a4 = 10;
el13drt 7:217810c90306 303 if(a2 < 9)
el13drt 7:217810c90306 304 a2 = 9;
el13drt 7:217810c90306 305 if(a3 < 8)
el13drt 7:217810c90306 306 a3 = 8;
el13drt 7:217810c90306 307 if(a1 < 7)
el13drt 7:217810c90306 308 a1 = 7;
el13drt 7:217810c90306 309 if(a8 < 6)
el13drt 7:217810c90306 310 a8 = 6;
el13drt 7:217810c90306 311 if(a5 < 5)
el13drt 7:217810c90306 312 a5 = 5;
el13drt 7:217810c90306 313 if(a7 < 4)
el13drt 7:217810c90306 314 a7 = 4;
el13drt 7:217810c90306 315 }
el13drt 0:a8bc3aac5f69 316
el13drt 4:c6305031f80d 317 // resets back to initial values
el13drt 4:c6305031f80d 318 void resetGame()
el13drt 0:a8bc3aac5f69 319 {
el13drt 4:c6305031f80d 320 score = 0;
el13drt 0:a8bc3aac5f69 321
el13drt 4:c6305031f80d 322 a1 = 22;
el13drt 4:c6305031f80d 323 a2 = 24;
el13drt 4:c6305031f80d 324 a3 = 23;
el13drt 4:c6305031f80d 325 a4 = 25;
el13drt 4:c6305031f80d 326 a5 = 20;
el13drt 4:c6305031f80d 327 a6 = 26;
el13drt 4:c6305031f80d 328 a7 = 19;
el13drt 4:c6305031f80d 329 a8 = 21;
el13drt 0:a8bc3aac5f69 330
el13drt 4:c6305031f80d 331 // in this case the X values are given a
el13drt 4:c6305031f80d 332 // new random variable each time the player
el13drt 4:c6305031f80d 333 // dies or exits and starts a new game
el13drt 4:c6305031f80d 334 randX1 = rand() % 74 + 5;
el13drt 4:c6305031f80d 335 randX2 = rand() % 74 + 5;
el13drt 4:c6305031f80d 336 randX3 = rand() % 74 + 5;
el13drt 4:c6305031f80d 337 randX4 = rand() % 74 + 5;
el13drt 4:c6305031f80d 338 randX5 = rand() % 74 + 5;
el13drt 4:c6305031f80d 339 randX6 = rand() % 74 + 5;
el13drt 3:57bab3b1c974 340
el13drt 4:c6305031f80d 341 randY1 = 0;
el13drt 4:c6305031f80d 342 randY2 = 0;
el13drt 4:c6305031f80d 343 randY3 = 0;
el13drt 4:c6305031f80d 344 randY4 = 0;
el13drt 4:c6305031f80d 345 randY5 = 0;
el13drt 4:c6305031f80d 346 randY6 = 0;
el13drt 4:c6305031f80d 347 lcd.clear();
el13drt 0:a8bc3aac5f69 348 }
el13drt 0:a8bc3aac5f69 349
el13drt 0:a8bc3aac5f69 350 // draws falling hazards
el13drt 0:a8bc3aac5f69 351 void hazards()
el13drt 0:a8bc3aac5f69 352 {
el13drt 0:a8bc3aac5f69 353 // X, Y, radius, fill
el13drt 0:a8bc3aac5f69 354 lcd.drawCircle(randX1,randY1,2,1);
el13drt 0:a8bc3aac5f69 355 lcd.drawCircle(randX2,randY2,2,1);
el13drt 0:a8bc3aac5f69 356 lcd.drawCircle(randX3,randY3,2,1);
el13drt 0:a8bc3aac5f69 357 lcd.drawCircle(randX4,randY4,2,1);
el13drt 0:a8bc3aac5f69 358 lcd.drawCircle(randX5,randY5,2,1);
el13drt 0:a8bc3aac5f69 359 lcd.drawCircle(randX6,randY6,2,1);
el13drt 0:a8bc3aac5f69 360
el13drt 0:a8bc3aac5f69 361 lcd.refresh();
el13drt 0:a8bc3aac5f69 362 }
el13drt 0:a8bc3aac5f69 363
el13drt 0:a8bc3aac5f69 364 // makes hazards fall - randomises X axis co-ordinates
el13drt 0:a8bc3aac5f69 365 void hazardFall()
el13drt 0:a8bc3aac5f69 366 {
el13drt 0:a8bc3aac5f69 367 // increments randY1 variables
el13drt 0:a8bc3aac5f69 368 // appearing to make them fall
el13drt 7:217810c90306 369 randY1 = randY1 += fall;
el13drt 7:217810c90306 370 randY2 = randY2 += fall;
el13drt 7:217810c90306 371 randY3 = randY3 += fall;
el13drt 7:217810c90306 372 randY4 = randY4 += fall;
el13drt 7:217810c90306 373 randY5 = randY5 += fall;
el13drt 7:217810c90306 374 randY6 = randY6 += fall;
el13drt 0:a8bc3aac5f69 375
el13drt 0:a8bc3aac5f69 376 // loops the objects once they 'hit the floor'
el13drt 0:a8bc3aac5f69 377 // this imitates a new set of objects falling
el13drt 0:a8bc3aac5f69 378
el13drt 0:a8bc3aac5f69 379 if (randY1>=48)
el13drt 0:a8bc3aac5f69 380 randY1=0;
el13drt 0:a8bc3aac5f69 381
el13drt 0:a8bc3aac5f69 382 if (randY2>=48)
el13drt 0:a8bc3aac5f69 383 randY2=0;
el13drt 0:a8bc3aac5f69 384
el13drt 0:a8bc3aac5f69 385 if (randY3>=48)
el13drt 0:a8bc3aac5f69 386 randY3=0;
el13drt 0:a8bc3aac5f69 387
el13drt 0:a8bc3aac5f69 388 if (randY4>=48)
el13drt 0:a8bc3aac5f69 389 randY4=0;
el13drt 0:a8bc3aac5f69 390
el13drt 0:a8bc3aac5f69 391 if (randY5>=48)
el13drt 0:a8bc3aac5f69 392 randY5=0;
el13drt 0:a8bc3aac5f69 393
el13drt 0:a8bc3aac5f69 394 // each time the objects loop, a new pseudo random value
el13drt 0:a8bc3aac5f69 395 // is assigned to the global variables (randX) to
el13drt 0:a8bc3aac5f69 396 // randomise their positions
el13drt 0:a8bc3aac5f69 397
el13drt 0:a8bc3aac5f69 398 if (randY6>=48) {
el13drt 0:a8bc3aac5f69 399 randY6=0;
el13drt 0:a8bc3aac5f69 400
el13drt 6:fa2928bcec17 401 score = score++;//increment score by 1 after each wave of hazards
el13drt 4:c6305031f80d 402
el13drt 0:a8bc3aac5f69 403 randX1 = rand() % 74 + 5;// psuedo random number
el13drt 0:a8bc3aac5f69 404 randX2 = rand() % 74 + 5;// between 5 and 74
el13drt 0:a8bc3aac5f69 405 randX3 = rand() % 74 + 5;
el13drt 0:a8bc3aac5f69 406 randX4 = rand() % 74 + 5;
el13drt 0:a8bc3aac5f69 407 randX5 = rand() % 74 + 5;
el13drt 0:a8bc3aac5f69 408 randX6 = rand() % 74 + 5;
el13drt 0:a8bc3aac5f69 409 }
el13drt 0:a8bc3aac5f69 410 }
el13drt 0:a8bc3aac5f69 411
el13drt 0:a8bc3aac5f69 412 // clears old pixels and keeps set pixels
el13drt 0:a8bc3aac5f69 413 void startrek()
el13drt 0:a8bc3aac5f69 414 {
el13drt 0:a8bc3aac5f69 415 for (int i=3; i<81; i++)// loops through rows
el13drt 0:a8bc3aac5f69 416 for (int j=0; j<47; j++)
el13drt 0:a8bc3aac5f69 417 if (cells[i][j]) {// if there's a pixel then keep it
el13drt 0:a8bc3aac5f69 418 lcd.setPixel(i,j);
el13drt 0:a8bc3aac5f69 419 } else {
el13drt 0:a8bc3aac5f69 420 lcd.clearPixel(i,j);// else remove the old ones
el13drt 0:a8bc3aac5f69 421 }
el13drt 0:a8bc3aac5f69 422 lcd.refresh();
el13drt 0:a8bc3aac5f69 423 }
el13drt 0:a8bc3aac5f69 424
el13drt 7:217810c90306 425 // clears old pixels and keeps set pixels
el13drt 7:217810c90306 426 void refreshCursor2()
el13drt 7:217810c90306 427 {
el13drt 7:217810c90306 428 for (int i=70; i<80; i++)// loops through rows
el13drt 7:217810c90306 429 for (int j=32; j<39; j++)
el13drt 7:217810c90306 430 if (cells[i][j]) {// if there's a pixel then keep it
el13drt 7:217810c90306 431 lcd.setPixel(i,j);
el13drt 7:217810c90306 432 } else {
el13drt 7:217810c90306 433 lcd.clearPixel(i,j);// else remove the old ones
el13drt 7:217810c90306 434 }
el13drt 7:217810c90306 435 lcd.refresh();
el13drt 7:217810c90306 436 }
el13drt 7:217810c90306 437
el13drt 7:217810c90306 438 // clears old pixels and keeps set pixels
el13drt 7:217810c90306 439 void refreshCursor3()
el13drt 7:217810c90306 440 {
el13drt 7:217810c90306 441 for (int i=70; i<80; i++)// loops through rows
el13drt 7:217810c90306 442 for (int j=25; j<31; j++)
el13drt 7:217810c90306 443 if (cells[i][j]) {// if there's a pixel then keep it
el13drt 7:217810c90306 444 lcd.setPixel(i,j);
el13drt 7:217810c90306 445 } else {
el13drt 7:217810c90306 446 lcd.clearPixel(i,j);// else remove the old ones
el13drt 7:217810c90306 447 }
el13drt 7:217810c90306 448 lcd.refresh();
el13drt 7:217810c90306 449 }
el13drt 3:57bab3b1c974 450 // sound / light when buttonA is closed
el13drt 3:57bab3b1c974 451 void actionButton()
el13drt 3:57bab3b1c974 452 {
el13drt 3:57bab3b1c974 453 buttonA.mode(PullDown);
el13drt 3:57bab3b1c974 454 if (buttonA == 1) {
el13drt 3:57bab3b1c974 455 ledY = 1;
el13drt 3:57bab3b1c974 456 } else {
el13drt 3:57bab3b1c974 457 ledY = 0;
el13drt 3:57bab3b1c974 458 }
el13drt 3:57bab3b1c974 459 }
el13drt 3:57bab3b1c974 460
el13drt 3:57bab3b1c974 461 // sound / light when buttonB is closed
el13drt 3:57bab3b1c974 462 void backButton()
el13drt 3:57bab3b1c974 463 {
el13drt 3:57bab3b1c974 464 buttonB.mode(PullDown);
el13drt 3:57bab3b1c974 465 if (buttonA == 1) {
el13drt 3:57bab3b1c974 466 ledY = 1;
el13drt 3:57bab3b1c974 467 } else {
el13drt 3:57bab3b1c974 468 ledY = 0;
el13drt 3:57bab3b1c974 469 }
el13drt 3:57bab3b1c974 470 }
el13drt 3:57bab3b1c974 471
el13drt 4:c6305031f80d 472 //// MAIN /////////////////////////////////////////////////
el13drt 3:57bab3b1c974 473
el13drt 0:a8bc3aac5f69 474 int main()
el13drt 0:a8bc3aac5f69 475 {
el13drt 0:a8bc3aac5f69 476 //////// set initial values - configurate joystick /////////////////////////////////
el13drt 1:352c50e88ffa 477
el13drt 3:57bab3b1c974 478 // Power indicator - RED LED
el13drt 3:57bab3b1c974 479 ledR = 1;
el13drt 3:57bab3b1c974 480
el13drt 7:217810c90306 481 // helps return exit menu to main menu loop
el13drt 5:a0e4a67266d6 482 int exitFlag = 0;
el13drt 5:a0e4a67266d6 483
el13drt 3:57bab3b1c974 484 // initialize time seed for psuedo randomaisation
el13drt 0:a8bc3aac5f69 485 srand (time(NULL));
el13drt 0:a8bc3aac5f69 486
el13drt 0:a8bc3aac5f69 487 // generate random x co-ordinates for falling hazards
el13drt 0:a8bc3aac5f69 488 // (initial values only)
el13drt 0:a8bc3aac5f69 489 // value between 3 and 76
el13drt 0:a8bc3aac5f69 490 randX1 = rand() % 74 + 5;
el13drt 0:a8bc3aac5f69 491 randX2 = rand() % 74 + 5;
el13drt 0:a8bc3aac5f69 492 randX3 = rand() % 74 + 5;
el13drt 0:a8bc3aac5f69 493 randX4 = rand() % 74 + 5;
el13drt 0:a8bc3aac5f69 494 randX5 = rand() % 74 + 5;
el13drt 0:a8bc3aac5f69 495 randX6 = rand() % 74 + 5;
el13drt 0:a8bc3aac5f69 496
el13drt 7:217810c90306 497 calibrateJoystick();//get centred values of joystick
el13drt 7:217810c90306 498 pollJoystick.attach(&updateJoystick,1.0/10.0);//read joystick 10 times per second
el13drt 6:fa2928bcec17 499
el13drt 7:217810c90306 500 // initialise menu
el13drt 0:a8bc3aac5f69 501 lcd.init();//initialise screen
el13drt 0:a8bc3aac5f69 502 welcome();//welcome screen
el13drt 0:a8bc3aac5f69 503 lcd.clear();//clear pixels
el13drt 0:a8bc3aac5f69 504
el13drt 5:a0e4a67266d6 505 // option counters
el13drt 5:a0e4a67266d6 506 int mainOption = 0;//counter for main menu
el13drt 5:a0e4a67266d6 507 int exitOption = 0;//counter for exit menu
el13drt 7:217810c90306 508 int option = 0;//counter for options menu
el13drt 3:57bab3b1c974 509
el13drt 7:217810c90306 510 // loop 1 - Main menu
el13drt 1:352c50e88ffa 511 while(1) {
el13drt 1:352c50e88ffa 512
el13drt 4:c6305031f80d 513 mainMenu();
el13drt 5:a0e4a67266d6 514 actionButton();
el13drt 4:c6305031f80d 515
el13drt 3:57bab3b1c974 516 // joystick selection
el13drt 3:57bab3b1c974 517 if (printFlag) { //if flag set, clear flag and print joystick values to serial port
el13drt 1:352c50e88ffa 518 printFlag = 0;
el13drt 1:352c50e88ffa 519 serial.printf("x = %f y = %f button = %d ",joystick.x,joystick.y,joystick.button);
el13drt 0:a8bc3aac5f69 520
el13drt 3:57bab3b1c974 521 // option up
el13drt 2:8fac5562ed1e 522 if (joystick.direction == UP) {
el13drt 1:352c50e88ffa 523 serial.printf(" UP\n");
el13drt 5:a0e4a67266d6 524 mainOption = mainOption--;
el13drt 5:a0e4a67266d6 525 if (mainOption < 0)mainOption = 0;
el13drt 2:8fac5562ed1e 526 }
el13drt 3:57bab3b1c974 527 // option down
el13drt 2:8fac5562ed1e 528 if (joystick.direction == DOWN) {
el13drt 2:8fac5562ed1e 529 serial.printf(" DOWN\n");
el13drt 5:a0e4a67266d6 530 mainOption = mainOption++;
el13drt 5:a0e4a67266d6 531 if (mainOption > 2)mainOption = 2;
el13drt 1:352c50e88ffa 532 }
el13drt 2:8fac5562ed1e 533 // Centre / Unknown orientation
el13drt 2:8fac5562ed1e 534 if (joystick.direction == CENTRE)
el13drt 2:8fac5562ed1e 535 serial.printf(" CENTRE\n");
el13drt 2:8fac5562ed1e 536 if (joystick.direction == UNKNOWN)
el13drt 2:8fac5562ed1e 537 serial.printf(" Unsupported direction\n");
el13drt 3:57bab3b1c974 538
el13drt 3:57bab3b1c974 539 // 'Play Game' option 1
el13drt 5:a0e4a67266d6 540 if (mainOption == 0) {
el13drt 1:352c50e88ffa 541 lcd.printString("Play Game",3,4);
el13drt 1:352c50e88ffa 542 }
el13drt 4:c6305031f80d 543 // 'High Scores' option 2
el13drt 5:a0e4a67266d6 544 if (mainOption == 1) {
el13drt 4:c6305031f80d 545 lcd.printString(" Scores ",3,4);
el13drt 4:c6305031f80d 546 }
el13drt 4:c6305031f80d 547 // 'Options' option 3
el13drt 5:a0e4a67266d6 548 if (mainOption == 2) {
el13drt 1:352c50e88ffa 549 lcd.printString(" Options ",3,4);
el13drt 1:352c50e88ffa 550 }
el13drt 3:57bab3b1c974 551 }
el13drt 4:c6305031f80d 552
el13drt 3:57bab3b1c974 553 //////////// Game Loop /////////////////////////////////////////////////////////////
el13drt 3:57bab3b1c974 554
el13drt 3:57bab3b1c974 555 // 'Play Game' selected
el13drt 5:a0e4a67266d6 556 if ((mainOption == 0)&&(buttonA == 1)) {
el13drt 3:57bab3b1c974 557
el13drt 4:c6305031f80d 558 lcd.clear();
el13drt 4:c6305031f80d 559
el13drt 4:c6305031f80d 560 actionButton();
el13drt 4:c6305031f80d 561 backButton();
el13drt 3:57bab3b1c974 562 backGround();
el13drt 3:57bab3b1c974 563
el13drt 4:c6305031f80d 564 // loop 3 - Game loop
el13drt 3:57bab3b1c974 565 while(1) {
el13drt 3:57bab3b1c974 566
el13drt 6:fa2928bcec17 567 // print score - top left of display
el13drt 6:fa2928bcec17 568 char buffer[14];//create buffer for string
el13drt 6:fa2928bcec17 569 int length = sprintf(buffer,"Level:%d",score);//insert buffer
el13drt 6:fa2928bcec17 570 if (length <= 14) //ensure length is smaller than screen
el13drt 6:fa2928bcec17 571 lcd.printString(buffer,3,0);//display
el13drt 6:fa2928bcec17 572
el13drt 4:c6305031f80d 573 actionButton();
el13drt 4:c6305031f80d 574 backButton();
el13drt 4:c6305031f80d 575
el13drt 4:c6305031f80d 576 pixelNinja();//set character
el13drt 7:217810c90306 577 // hazards();//initiates hazards
el13drt 7:217810c90306 578 // hazardFall();//increments hazards towards floor
el13drt 4:c6305031f80d 579
el13drt 4:c6305031f80d 580 if (printFlag) { //if flag set, clear flag and print joystick values to serial port
el13drt 4:c6305031f80d 581 printFlag = 0;
el13drt 4:c6305031f80d 582 serial.printf("x = %f y = %f button = %d \n",joystick.x,joystick.y,joystick.button);
el13drt 3:57bab3b1c974 583
el13drt 4:c6305031f80d 584 // check joystick direction
el13drt 4:c6305031f80d 585 if (joystick.direction == LEFT) {
el13drt 4:c6305031f80d 586 serial.printf(" LEFT\n");
el13drt 4:c6305031f80d 587 a1 = a1-=2;
el13drt 4:c6305031f80d 588 a2 = a2-=2;
el13drt 4:c6305031f80d 589 a3 = a3-=2;
el13drt 4:c6305031f80d 590 a4 = a4-=2;
el13drt 4:c6305031f80d 591 a5 = a5-=2;
el13drt 4:c6305031f80d 592 a6 = a6-=2;
el13drt 4:c6305031f80d 593 a7 = a7-=2;
el13drt 4:c6305031f80d 594 a8 = a8-=2;
el13drt 7:217810c90306 595 ninjaBoundaries();
el13drt 4:c6305031f80d 596 }
el13drt 4:c6305031f80d 597 if (joystick.direction == RIGHT) {
el13drt 4:c6305031f80d 598 serial.printf(" RIGHT\n");
el13drt 4:c6305031f80d 599 a1 = a1+=2;
el13drt 4:c6305031f80d 600 a2 = a2+=2;
el13drt 4:c6305031f80d 601 a3 = a3+=2;
el13drt 4:c6305031f80d 602 a4 = a4+=2;
el13drt 4:c6305031f80d 603 a5 = a5+=2;
el13drt 4:c6305031f80d 604 a6 = a6+=2;
el13drt 4:c6305031f80d 605 a7 = a7+=2;
el13drt 4:c6305031f80d 606 a8 = a8+=2;
el13drt 7:217810c90306 607 ninjaBoundaries();
el13drt 4:c6305031f80d 608 }
el13drt 4:c6305031f80d 609 if (joystick.direction == CENTRE)
el13drt 4:c6305031f80d 610 serial.printf(" CENTRE\n");
el13drt 4:c6305031f80d 611 if (joystick.direction == UNKNOWN)
el13drt 4:c6305031f80d 612 serial.printf(" Unsupported direction\n");
el13drt 3:57bab3b1c974 613
el13drt 5:a0e4a67266d6 614 // integer to represent character being
el13drt 5:a0e4a67266d6 615 // struck by falling object
el13drt 5:a0e4a67266d6 616 int contactPoint = 0;
el13drt 4:c6305031f80d 617
el13drt 4:c6305031f80d 618 // contact points
el13drt 4:c6305031f80d 619 if(lcd.getPixel((a1+4),32))
el13drt 5:a0e4a67266d6 620 contactPoint++;
el13drt 4:c6305031f80d 621 if(lcd.getPixel((a1),32))
el13drt 5:a0e4a67266d6 622 contactPoint++;
el13drt 4:c6305031f80d 623 if(lcd.getPixel((a7),32))
el13drt 5:a0e4a67266d6 624 contactPoint++;
el13drt 3:57bab3b1c974 625
el13drt 5:a0e4a67266d6 626 // if contact point is not zero
el13drt 5:a0e4a67266d6 627 // character has been hit
el13drt 5:a0e4a67266d6 628 // and the game ends
el13drt 5:a0e4a67266d6 629 if ( contactPoint !=0) {
el13drt 6:fa2928bcec17 630 lcd.printString("Game Over",17,2);
el13drt 4:c6305031f80d 631 wait(0.5);
el13drt 4:c6305031f80d 632 lcd.inverseMode();
el13drt 4:c6305031f80d 633 wait(0.5);
el13drt 4:c6305031f80d 634 lcd.normalMode();
el13drt 4:c6305031f80d 635 wait(0.5);
el13drt 4:c6305031f80d 636 lcd.inverseMode();
el13drt 4:c6305031f80d 637 wait(0.5);
el13drt 4:c6305031f80d 638 lcd.normalMode();
el13drt 4:c6305031f80d 639 wait(0.5);
el13drt 4:c6305031f80d 640 resetGame();
el13drt 4:c6305031f80d 641 break;
el13drt 4:c6305031f80d 642 }
el13drt 4:c6305031f80d 643 startrek();//clears unset pixels, keeps set pixels
el13drt 4:c6305031f80d 644
el13drt 5:a0e4a67266d6 645 ///////////////////// Exit Menu (Back button pressed)///////////////////////////////////////////
el13drt 5:a0e4a67266d6 646
el13drt 4:c6305031f80d 647 if(buttonB == 1) {
el13drt 5:a0e4a67266d6 648
el13drt 5:a0e4a67266d6 649 // set exit menu
el13drt 5:a0e4a67266d6 650 lcd.clear();
el13drt 5:a0e4a67266d6 651 lcd.drawRect(8,6,70,30,0);//title outline
el13drt 5:a0e4a67266d6 652 lcd.printString("Exit Game?",10,25);
el13drt 5:a0e4a67266d6 653 backGround();
el13drt 5:a0e4a67266d6 654
el13drt 6:fa2928bcec17 655 // option arrow - right
el13drt 6:fa2928bcec17 656 lcd.drawRect(55,25,0,4,1);
el13drt 6:fa2928bcec17 657 lcd.drawRect(56,26,0,2,1);
el13drt 6:fa2928bcec17 658 lcd.drawRect(57,27,0,0,1);
el13drt 7:217810c90306 659
el13drt 6:fa2928bcec17 660 // option arrow - left
el13drt 6:fa2928bcec17 661 lcd.drawRect(27,25,0,4,1);
el13drt 6:fa2928bcec17 662 lcd.drawRect(26,26,0,2,1);
el13drt 6:fa2928bcec17 663 lcd.drawRect(25,27,0,0,1);
el13drt 6:fa2928bcec17 664
el13drt 7:217810c90306 665
el13drt 6:fa2928bcec17 666
el13drt 5:a0e4a67266d6 667 while(1) {
el13drt 5:a0e4a67266d6 668
el13drt 5:a0e4a67266d6 669 if (printFlag) { //if flag set, clear flag and print joystick values to serial port
el13drt 5:a0e4a67266d6 670 printFlag = 0;
el13drt 5:a0e4a67266d6 671 serial.printf("x = %f y = %f button = %d \n",joystick.x,joystick.y,joystick.button);
el13drt 5:a0e4a67266d6 672
el13drt 5:a0e4a67266d6 673 // check joystick direction
el13drt 5:a0e4a67266d6 674 if (joystick.direction == LEFT) {
el13drt 5:a0e4a67266d6 675 serial.printf(" LEFT\n");
el13drt 5:a0e4a67266d6 676 exitOption--;
el13drt 5:a0e4a67266d6 677 if(exitOption < 0)exitOption = 0;
el13drt 5:a0e4a67266d6 678 }
el13drt 5:a0e4a67266d6 679 if (joystick.direction == RIGHT) {
el13drt 5:a0e4a67266d6 680 serial.printf(" RIGHT\n");
el13drt 5:a0e4a67266d6 681 exitOption++;
el13drt 5:a0e4a67266d6 682 if(exitOption > 1)exitOption = 1;
el13drt 5:a0e4a67266d6 683 }
el13drt 5:a0e4a67266d6 684 if (joystick.direction == CENTRE)
el13drt 5:a0e4a67266d6 685 serial.printf(" CENTRE\n");
el13drt 5:a0e4a67266d6 686 if (joystick.direction == UNKNOWN)
el13drt 5:a0e4a67266d6 687 serial.printf(" Unsupported direction\n");
el13drt 5:a0e4a67266d6 688 }
el13drt 5:a0e4a67266d6 689 // draws option cursor
el13drt 5:a0e4a67266d6 690 if(exitOption == 0) {
el13drt 6:fa2928bcec17 691 lcd.printString("YES",29,27);
el13drt 5:a0e4a67266d6 692 }
el13drt 5:a0e4a67266d6 693 if(exitOption == 1) {
el13drt 6:fa2928bcec17 694 lcd.printString(" NO",29,27);
el13drt 5:a0e4a67266d6 695 }
el13drt 5:a0e4a67266d6 696
el13drt 5:a0e4a67266d6 697 // exits game
el13drt 5:a0e4a67266d6 698 if((buttonA == 1)&&(exitOption == 0)) { //returns to menu
el13drt 5:a0e4a67266d6 699 actionButton();
el13drt 5:a0e4a67266d6 700 lcd.clear();//clears screen
el13drt 5:a0e4a67266d6 701 resetGame();//resets scores/objects
el13drt 5:a0e4a67266d6 702 exitFlag = 1;//sets exit flag
el13drt 5:a0e4a67266d6 703 break;
el13drt 5:a0e4a67266d6 704 }
el13drt 5:a0e4a67266d6 705 // returns to game
el13drt 5:a0e4a67266d6 706 if((buttonA == 1)&&(exitOption == 1)) {
el13drt 5:a0e4a67266d6 707 break;
el13drt 5:a0e4a67266d6 708 }
el13drt 5:a0e4a67266d6 709 }
el13drt 5:a0e4a67266d6 710 // GAME LOOP
el13drt 5:a0e4a67266d6 711
el13drt 5:a0e4a67266d6 712 if (exitFlag!=0) { //if exit flag set
el13drt 5:a0e4a67266d6 713 exitFlag = 0;//reset flag
el13drt 5:a0e4a67266d6 714 break;//break to main menu
el13drt 5:a0e4a67266d6 715 }
el13drt 4:c6305031f80d 716 }
el13drt 4:c6305031f80d 717 serial.printf("Score: %i \n",score);
el13drt 5:a0e4a67266d6 718 }
el13drt 5:a0e4a67266d6 719 }
el13drt 5:a0e4a67266d6 720 }
el13drt 4:c6305031f80d 721
el13drt 4:c6305031f80d 722 ////////// High Scores Loop //////////////////////////////////////////////////
el13drt 4:c6305031f80d 723
el13drt 5:a0e4a67266d6 724 if((mainOption == 1)&&(buttonA == 1)) {
el13drt 3:57bab3b1c974 725
el13drt 4:c6305031f80d 726 actionButton();
el13drt 4:c6305031f80d 727 backButton();
el13drt 5:a0e4a67266d6 728
el13drt 4:c6305031f80d 729 lcd.clear();//clear screen
el13drt 4:c6305031f80d 730 backGround();//set background
el13drt 6:fa2928bcec17 731 lcd.printString("High Scores",10,0);//title
el13drt 6:fa2928bcec17 732
el13drt 6:fa2928bcec17 733 // players high scores - highest first
el13drt 6:fa2928bcec17 734 char highScore1[14];//create buffer for strings
el13drt 6:fa2928bcec17 735 char highScore2[14];
el13drt 6:fa2928bcec17 736 char highScore3[14];
el13drt 6:fa2928bcec17 737
el13drt 6:fa2928bcec17 738 int player1 = sprintf(highScore1,"1.DRT.....99",score);//insert buffer
el13drt 6:fa2928bcec17 739 int player2 = sprintf(highScore2,"2.NRG.....98",score);//insert buffer
el13drt 6:fa2928bcec17 740 int player3 = sprintf(highScore3,"3.GRT.....10",score);//insert buffer
el13drt 6:fa2928bcec17 741
el13drt 6:fa2928bcec17 742 if (player1 <= 14) //ensure length is smaller than screen
el13drt 6:fa2928bcec17 743 lcd.printString(highScore1,5,2);//display
el13drt 6:fa2928bcec17 744 if (player2 <= 14) //ensure length is smaller than screen
el13drt 6:fa2928bcec17 745 lcd.printString(highScore2,5,3);//display
el13drt 6:fa2928bcec17 746 if (player3 <= 14) //ensure length is smaller than screen
el13drt 6:fa2928bcec17 747 lcd.printString(highScore3,5,4);//display
el13drt 5:a0e4a67266d6 748
el13drt 4:c6305031f80d 749 while(1) {
el13drt 3:57bab3b1c974 750
el13drt 7:217810c90306 751 actionButton();//select
el13drt 7:217810c90306 752 backButton();//back
el13drt 4:c6305031f80d 753
el13drt 4:c6305031f80d 754 // back to menu
el13drt 4:c6305031f80d 755 if(buttonB == 1) {
el13drt 4:c6305031f80d 756 lcd.clear();
el13drt 4:c6305031f80d 757 break;
el13drt 4:c6305031f80d 758 }
el13drt 4:c6305031f80d 759 }
el13drt 4:c6305031f80d 760 }
el13drt 4:c6305031f80d 761
el13drt 4:c6305031f80d 762 ////////// OPTIONS LOOP /////////////////////////////////////////////////////
el13drt 3:57bab3b1c974 763
el13drt 5:a0e4a67266d6 764 if((mainOption == 2)&&(buttonA == 1)) {
el13drt 4:c6305031f80d 765
el13drt 4:c6305031f80d 766 actionButton();
el13drt 4:c6305031f80d 767 backButton();
el13drt 3:57bab3b1c974 768
el13drt 4:c6305031f80d 769 lcd.clear();//clear screen
el13drt 5:a0e4a67266d6 770
el13drt 7:217810c90306 771 backGround();
el13drt 4:c6305031f80d 772 lcd.drawRect(3,6,77,10,0);//title outline
el13drt 4:c6305031f80d 773 lcd.drawRect(0,47,84,0,1);//bottom border
el13drt 4:c6305031f80d 774 lcd.drawRect(0,0,84,2,1);//top border
el13drt 4:c6305031f80d 775 lcd.printString("Options",20,7);//title
el13drt 7:217810c90306 776 lcd.printString("Difficulty",3,9);
el13drt 7:217810c90306 777 lcd.printString("Sound FX",3,10);
el13drt 5:a0e4a67266d6 778
el13drt 4:c6305031f80d 779 while(1) {
el13drt 4:c6305031f80d 780
el13drt 4:c6305031f80d 781 actionButton();
el13drt 4:c6305031f80d 782 backButton();
el13drt 7:217810c90306 783 // refreshCursor();
el13drt 7:217810c90306 784
el13drt 7:217810c90306 785 // joystick selection
el13drt 7:217810c90306 786 if (printFlag) { //if flag set, clear flag and print joystick values to serial port
el13drt 7:217810c90306 787 printFlag = 0;
el13drt 7:217810c90306 788 serial.printf("x = %f y = %f button = %d ",joystick.x,joystick.y,joystick.button);
el13drt 7:217810c90306 789
el13drt 7:217810c90306 790 // option up
el13drt 7:217810c90306 791 if (joystick.direction == UP) {
el13drt 7:217810c90306 792 serial.printf(" UP\n");
el13drt 7:217810c90306 793 option = option--;
el13drt 7:217810c90306 794 if (option < 0)option = 0;
el13drt 7:217810c90306 795 }
el13drt 7:217810c90306 796 // option down
el13drt 7:217810c90306 797 if (joystick.direction == DOWN) {
el13drt 7:217810c90306 798 serial.printf(" DOWN\n");
el13drt 7:217810c90306 799 option = option++;
el13drt 7:217810c90306 800 if (option > 1)option = 1;
el13drt 7:217810c90306 801 }
el13drt 7:217810c90306 802 // Centre / Unknown orientation
el13drt 7:217810c90306 803 if (joystick.direction == CENTRE)
el13drt 7:217810c90306 804 serial.printf(" CENTRE\n");
el13drt 7:217810c90306 805 if (joystick.direction == UNKNOWN)
el13drt 7:217810c90306 806 serial.printf(" Unsupported direction\n");
el13drt 7:217810c90306 807
el13drt 7:217810c90306 808 // 'Difficulty' option 1
el13drt 7:217810c90306 809 if (option == 0) {
el13drt 7:217810c90306 810 lcd.drawCircle(72,35,2,1);
el13drt 7:217810c90306 811 refreshCursor3();
el13drt 7:217810c90306 812 }
el13drt 7:217810c90306 813 // 'Sound FX' option 2
el13drt 7:217810c90306 814 if (option == 1) {
el13drt 7:217810c90306 815 lcd.drawCircle(72,27,2,1);
el13drt 7:217810c90306 816 refreshCursor2();
el13drt 7:217810c90306 817 }
el13drt 7:217810c90306 818
el13drt 7:217810c90306 819 ////////////////////// difficulty settings ////////////////////////////////////
el13drt 7:217810c90306 820 if ((option == 1)&&(buttonA == 1)) {
el13drt 7:217810c90306 821
el13drt 7:217810c90306 822 lcd.clear();
el13drt 7:217810c90306 823 backGround();
el13drt 7:217810c90306 824 lcd.drawRect(0,47,84,0,1);//bottom border
el13drt 7:217810c90306 825 lcd.drawRect(0,0,84,2,1);//top border
el13drt 7:217810c90306 826 lcd.printString("*Difficulty*",5,7);//title
el13drt 7:217810c90306 827 lcd.printString("Easy",5,8);//title
el13drt 7:217810c90306 828 lcd.printString("Normal",5,9);//title
el13drt 7:217810c90306 829 lcd.printString("Forget It",5,10);//title
el13drt 7:217810c90306 830
el13drt 7:217810c90306 831
el13drt 7:217810c90306 832 while(1) {
el13drt 7:217810c90306 833
el13drt 7:217810c90306 834 actionButton();
el13drt 7:217810c90306 835 backButton();
el13drt 7:217810c90306 836
el13drt 7:217810c90306 837 if(buttonB == 1) {
el13drt 7:217810c90306 838 lcd.clear();
el13drt 7:217810c90306 839 break;
el13drt 7:217810c90306 840 }
el13drt 7:217810c90306 841 }
el13drt 7:217810c90306 842 }
el13drt 7:217810c90306 843 }
el13drt 4:c6305031f80d 844
el13drt 4:c6305031f80d 845 // back to menu
el13drt 4:c6305031f80d 846 if(buttonB == 1) {
el13drt 4:c6305031f80d 847 lcd.clear();
el13drt 4:c6305031f80d 848 break;
el13drt 3:57bab3b1c974 849 }
el13drt 3:57bab3b1c974 850 }
el13drt 1:352c50e88ffa 851 }
el13drt 0:a8bc3aac5f69 852 }
el13drt 3:57bab3b1c974 853 }
el13drt 1:352c50e88ffa 854
el13drt 0:a8bc3aac5f69 855 // read default positions of the joystick to calibrate later readings
el13drt 0:a8bc3aac5f69 856 void calibrateJoystick()
el13drt 0:a8bc3aac5f69 857 {
el13drt 0:a8bc3aac5f69 858 button.mode(PullDown);
el13drt 0:a8bc3aac5f69 859 // must not move during calibration
el13drt 3:57bab3b1c974 860 joystick.x0 = xPot; //initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
el13drt 0:a8bc3aac5f69 861 joystick.y0 = yPot;
el13drt 0:a8bc3aac5f69 862 }
el13drt 0:a8bc3aac5f69 863
el13drt 0:a8bc3aac5f69 864 void updateJoystick()
el13drt 0:a8bc3aac5f69 865 {
el13drt 0:a8bc3aac5f69 866 // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
el13drt 0:a8bc3aac5f69 867 joystick.x = xPot - joystick.x0;
el13drt 0:a8bc3aac5f69 868 joystick.y = yPot - joystick.y0;
el13drt 0:a8bc3aac5f69 869 // read button state
el13drt 0:a8bc3aac5f69 870 joystick.button = button;
el13drt 0:a8bc3aac5f69 871
el13drt 0:a8bc3aac5f69 872 // calculate direction depending on x,y values
el13drt 0:a8bc3aac5f69 873 // tolerance allows a little lee-way in case joystick not exactly in the stated direction
el13drt 0:a8bc3aac5f69 874 if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
el13drt 0:a8bc3aac5f69 875 joystick.direction = CENTRE;
el13drt 0:a8bc3aac5f69 876 } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
el13drt 0:a8bc3aac5f69 877 joystick.direction = UP;
el13drt 0:a8bc3aac5f69 878 } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
el13drt 0:a8bc3aac5f69 879 joystick.direction = DOWN;
el13drt 0:a8bc3aac5f69 880 } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
el13drt 0:a8bc3aac5f69 881 joystick.direction = RIGHT;
el13drt 0:a8bc3aac5f69 882 } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
el13drt 0:a8bc3aac5f69 883 joystick.direction = LEFT;
el13drt 0:a8bc3aac5f69 884 } else {
el13drt 0:a8bc3aac5f69 885 joystick.direction = UNKNOWN;
el13drt 0:a8bc3aac5f69 886 }
el13drt 0:a8bc3aac5f69 887
el13drt 0:a8bc3aac5f69 888 // set flag for printing
el13drt 0:a8bc3aac5f69 889 printFlag = 1;
el13drt 0:a8bc3aac5f69 890 }
el13drt 0:a8bc3aac5f69 891
el13drt 0:a8bc3aac5f69 892