![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Racing Cars game using N5110 LCD and thumb Joystick
Dependencies: N5110 PowerControl beep mbed
main.cpp@8:699055e89c7d, 2015-05-11 (annotated)
- Committer:
- el13gs
- Date:
- Mon May 11 16:23:55 2015 +0000
- Revision:
- 8:699055e89c7d
- Parent:
- 7:edf0f1fcb16b
Handheld Arcade Game "Racing Cars"
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el13gs | 5:243718c3cd8b | 1 | /** |
el13gs | 5:243718c3cd8b | 2 | @file main.cpp |
el13gs | 5:243718c3cd8b | 3 | @brief Contains all functions implementations, and main program |
el13gs | 5:243718c3cd8b | 4 | */ |
el13gs | 5:243718c3cd8b | 5 | #include "main.h" |
el13gs | 4:f9a899c6ac50 | 6 | |
el13gs | 4:f9a899c6ac50 | 7 | int main() |
el13gs | 4:f9a899c6ac50 | 8 | |
el13gs | 4:f9a899c6ac50 | 9 | { |
el13gs | 8:699055e89c7d | 10 | |
el13gs | 8:699055e89c7d | 11 | Peripheral_PowerDown(0xFFFFFFFF); //turn off all peripherals |
el13gs | 8:699055e89c7d | 12 | /* turn on only the required peripherals */ |
el13gs | 8:699055e89c7d | 13 | Peripheral_PowerUp(0x800000); |
el13gs | 8:699055e89c7d | 14 | Peripheral_PowerUp(0x200000); |
el13gs | 8:699055e89c7d | 15 | Peripheral_PowerUp(0x40); |
el13gs | 8:699055e89c7d | 16 | Peripheral_PowerUp(0x8000); |
el13gs | 8:699055e89c7d | 17 | Peripheral_PowerUp(0x1000); |
el13gs | 8:699055e89c7d | 18 | |
el13gs | 7:edf0f1fcb16b | 19 | sprintf(soundString,"%s","YES"); //set the Sound Initial String to YES |
el13gs | 5:243718c3cd8b | 20 | |
el13gs | 4:f9a899c6ac50 | 21 | calibrateJoystick(); // get centred values of joystick |
el13gs | 8:699055e89c7d | 22 | pollJoystick.attach(&updateJoystick,1.0/20.0); // read joystick 30 times per second |
el13gs | 5:243718c3cd8b | 23 | |
el13gs | 5:243718c3cd8b | 24 | lcd.setXYAddress(0,0);//set the XY address to 0.0 as initialising condition |
el13gs | 5:243718c3cd8b | 25 | initTable();//initialise the array table |
el13gs | 5:243718c3cd8b | 26 | lcd.init();//initialise LCD screen |
el13gs | 4:f9a899c6ac50 | 27 | lcd.refresh(); |
el13gs | 8:699055e89c7d | 28 | /* PRINT WELCOMING MESSAGES */ |
el13gs | 8:699055e89c7d | 29 | lcd.printString("RACING CARS",9,1); |
el13gs | 8:699055e89c7d | 30 | wait(0.5); |
el13gs | 8:699055e89c7d | 31 | lcd.printString("GAME",29,2); |
el13gs | 8:699055e89c7d | 32 | wait(0.5); |
el13gs | 8:699055e89c7d | 33 | lcd.printString("by Giorgos",14,4); |
el13gs | 8:699055e89c7d | 34 | wait(0.5); |
el13gs | 8:699055e89c7d | 35 | lcd.printString("Savvides",16,5); |
el13gs | 8:699055e89c7d | 36 | wait(2); |
el13gs | 8:699055e89c7d | 37 | lcd.clear(); |
el13gs | 8:699055e89c7d | 38 | lcd.refresh(); |
el13gs | 8:699055e89c7d | 39 | lcd.printString("USE JOYSTICK",0,0); |
el13gs | 8:699055e89c7d | 40 | lcd.printString("AND BUTTONS ",0,1); |
el13gs | 8:699055e89c7d | 41 | lcd.printString("ON THE RIGHT",0,2); |
el13gs | 8:699055e89c7d | 42 | lcd.printString("TO NAVIGATE",0,3); |
el13gs | 8:699055e89c7d | 43 | lcd.printString("HAVE FUN!!!",12,5); |
el13gs | 8:699055e89c7d | 44 | wait(3.5); |
el13gs | 7:edf0f1fcb16b | 45 | start.rise(&startButtonPressed); // Call function startButtonPressed when START button pressed |
el13gs | 7:edf0f1fcb16b | 46 | reset.rise(&resetButtonPressed); // Call function resetButtonPressed when RESET button pressed |
el13gs | 4:f9a899c6ac50 | 47 | |
el13gs | 4:f9a899c6ac50 | 48 | MainMenu: |
el13gs | 7:edf0f1fcb16b | 49 | /* INITIAL DISPLAY IN MAIN MENU */ |
el13gs | 8:699055e89c7d | 50 | |
el13gs | 7:edf0f1fcb16b | 51 | int menuPointer=1; // set the Pointer in Main Menu initially to 1 |
el13gs | 7:edf0f1fcb16b | 52 | startButtonFlag=0; // reset the pointing flags |
el13gs | 7:edf0f1fcb16b | 53 | resetButtonFlag=0; // reset the pointing flags |
el13gs | 7:edf0f1fcb16b | 54 | clearCells(84,48); // clear the whole Screen Cells |
el13gs | 7:edf0f1fcb16b | 55 | lcd.refresh(); // refresh are done after and before setting or clearing Pixels |
el13gs | 7:edf0f1fcb16b | 56 | lcd.drawCircle(22,19,2,1); //initial condition draw pointer around Play submenu |
el13gs | 7:edf0f1fcb16b | 57 | lcd.drawCircle(53,19,2,1); //initial condition draw pointer around Play submenu |
el13gs | 7:edf0f1fcb16b | 58 | |
el13gs | 4:f9a899c6ac50 | 59 | lcd.printString("MAIN MENU",15,0); //print title |
el13gs | 4:f9a899c6ac50 | 60 | lcd.printString("Play",26,2); //print submenu title |
el13gs | 4:f9a899c6ac50 | 61 | lcd.printString("Options",20,3); //print submenu title |
el13gs | 4:f9a899c6ac50 | 62 | lcd.drawLine(15,9,65,9,1);//underline main menu title |
el13gs | 8:699055e89c7d | 63 | wait(0.1); |
el13gs | 7:edf0f1fcb16b | 64 | /* While start Button is not pressed user can navigate through main Menu Submenus */ |
el13gs | 6:289f237b8d90 | 65 | while(startButtonFlag==0) { |
el13gs | 4:f9a899c6ac50 | 66 | |
el13gs | 7:edf0f1fcb16b | 67 | /* when joystick moves down, pointer selects Options subMenu */ |
el13gs | 4:f9a899c6ac50 | 68 | if (joystick.direction==DOWN) { |
el13gs | 4:f9a899c6ac50 | 69 | lcd.drawCircle(16,27,2,1); //draw pointer around Options submenu when selected |
el13gs | 4:f9a899c6ac50 | 70 | lcd.drawCircle(64,27,2,1); //draw pointer around Options submenu when selected |
el13gs | 4:f9a899c6ac50 | 71 | |
el13gs | 7:edf0f1fcb16b | 72 | lcd.clearCircle(53,19,2,1); //clear pointer around Play submenu |
el13gs | 7:edf0f1fcb16b | 73 | lcd.clearCircle(22,19,2,1); //clear pointer around Play submenu |
el13gs | 4:f9a899c6ac50 | 74 | |
el13gs | 7:edf0f1fcb16b | 75 | menuPointer=2; //set Pointer to 2 |
el13gs | 7:edf0f1fcb16b | 76 | } |
el13gs | 4:f9a899c6ac50 | 77 | |
el13gs | 7:edf0f1fcb16b | 78 | /* when joystick moves up, pointer selects Options subMenu */ |
el13gs | 4:f9a899c6ac50 | 79 | if(joystick.direction==UP) { |
el13gs | 4:f9a899c6ac50 | 80 | lcd.drawCircle(22,19,2,1); //draw pointer around Play submenu when selected |
el13gs | 4:f9a899c6ac50 | 81 | lcd.drawCircle(53,19,2,1); //draw pointer around Play submenu when selected |
el13gs | 4:f9a899c6ac50 | 82 | |
el13gs | 7:edf0f1fcb16b | 83 | lcd.clearCircle(16,27,2,1); //clear pointer around Options submenu |
el13gs | 7:edf0f1fcb16b | 84 | lcd.clearCircle(64,27,2,1); //clear pointer around Options submenu |
el13gs | 7:edf0f1fcb16b | 85 | menuPointer=1; //set Pointer to 1 |
el13gs | 4:f9a899c6ac50 | 86 | } |
el13gs | 8:699055e89c7d | 87 | sleep(); |
el13gs | 4:f9a899c6ac50 | 88 | } |
el13gs | 4:f9a899c6ac50 | 89 | |
el13gs | 7:edf0f1fcb16b | 90 | /* SELECT POINTER CONDITION */ |
el13gs | 5:243718c3cd8b | 91 | switch(menuPointer) { |
el13gs | 4:f9a899c6ac50 | 92 | |
el13gs | 7:edf0f1fcb16b | 93 | /* Case when Play was selected */ |
el13gs | 4:f9a899c6ac50 | 94 | case 1: |
el13gs | 7:edf0f1fcb16b | 95 | /*INITIAL GAME CONDITIONS */ |
el13gs | 7:edf0f1fcb16b | 96 | startButtonFlag=0; //reset start Button Flag |
el13gs | 7:edf0f1fcb16b | 97 | gamePlays=1; // Begin the game with pointer set to 1 |
el13gs | 7:edf0f1fcb16b | 98 | lives=5; // set lives to 5 |
el13gs | 7:edf0f1fcb16b | 99 | round=1; // set round number to 1 |
el13gs | 7:edf0f1fcb16b | 100 | coins=0; // set coin number to 0 |
el13gs | 7:edf0f1fcb16b | 101 | clearCells(84,48); //clear the whole LCD screen |
el13gs | 8:699055e89c7d | 102 | |
el13gs | 8:699055e89c7d | 103 | lcd.refresh(); |
el13gs | 8:699055e89c7d | 104 | lcd.printString("READY?",20,3); |
el13gs | 8:699055e89c7d | 105 | wait(0.75); |
el13gs | 8:699055e89c7d | 106 | lcd.printString("GET SET",20,3); |
el13gs | 8:699055e89c7d | 107 | wait(1); |
el13gs | 8:699055e89c7d | 108 | lcd.printString(" GO !!!",20,3); |
el13gs | 8:699055e89c7d | 109 | wait(1); |
el13gs | 8:699055e89c7d | 110 | lcd.clear(); |
el13gs | 8:699055e89c7d | 111 | lcd.refresh(); |
el13gs | 8:699055e89c7d | 112 | |
el13gs | 8:699055e89c7d | 113 | |
el13gs | 7:edf0f1fcb16b | 114 | gameReset(); // set and draw initial player and enemies position |
el13gs | 7:edf0f1fcb16b | 115 | timer.attach(&movePlayer,0.1);// attach timer to call function that moves the Player |
el13gs | 5:243718c3cd8b | 116 | |
el13gs | 4:f9a899c6ac50 | 117 | |
el13gs | 5:243718c3cd8b | 118 | while(gamePlays) { |
el13gs | 4:f9a899c6ac50 | 119 | |
el13gs | 7:edf0f1fcb16b | 120 | //print Round number on buffer and display |
el13gs | 7:edf0f1fcb16b | 121 | sprintf(roundBuffer,"%d",round); |
el13gs | 7:edf0f1fcb16b | 122 | lcd.printString(roundBuffer,74,3); |
el13gs | 8:699055e89c7d | 123 | lcd.refresh(); |
el13gs | 4:f9a899c6ac50 | 124 | |
el13gs | 7:edf0f1fcb16b | 125 | //print Lives number on buffer and display |
el13gs | 7:edf0f1fcb16b | 126 | sprintf(livesBuffer,"%d",lives); |
el13gs | 7:edf0f1fcb16b | 127 | lcd.printString(livesBuffer,74,1); |
el13gs | 8:699055e89c7d | 128 | lcd.refresh(); |
el13gs | 6:289f237b8d90 | 129 | |
el13gs | 7:edf0f1fcb16b | 130 | //print Coins number on buffer and display |
el13gs | 7:edf0f1fcb16b | 131 | sprintf(coinsBuffer,"%d",coins); |
el13gs | 7:edf0f1fcb16b | 132 | lcd.printString(coinsBuffer,72,5); |
el13gs | 4:f9a899c6ac50 | 133 | |
el13gs | 8:699055e89c7d | 134 | lcd.refresh(); |
el13gs | 7:edf0f1fcb16b | 135 | enemy1MovesDown(); //move enemy 1 downwards |
el13gs | 8:699055e89c7d | 136 | |
el13gs | 7:edf0f1fcb16b | 137 | checkPlayerPos(); //check player position frequently |
el13gs | 8:699055e89c7d | 138 | if (gamePlays==0) { // if gameplay is 0 returns to initial MAIN MENU |
el13gs | 7:edf0f1fcb16b | 139 | goto MainMenu; |
el13gs | 7:edf0f1fcb16b | 140 | } |
el13gs | 4:f9a899c6ac50 | 141 | |
el13gs | 8:699055e89c7d | 142 | lcd.refresh(); |
el13gs | 7:edf0f1fcb16b | 143 | enemy2MovesDown(); //move enemy 2 downwards |
el13gs | 8:699055e89c7d | 144 | |
el13gs | 4:f9a899c6ac50 | 145 | checkPlayerPos(); |
el13gs | 7:edf0f1fcb16b | 146 | if (gamePlays==0) { |
el13gs | 7:edf0f1fcb16b | 147 | goto MainMenu; |
el13gs | 7:edf0f1fcb16b | 148 | } |
el13gs | 7:edf0f1fcb16b | 149 | |
el13gs | 8:699055e89c7d | 150 | lcd.refresh(); |
el13gs | 7:edf0f1fcb16b | 151 | enemy3MovesDown(); //move enemy 3 downwards |
el13gs | 5:243718c3cd8b | 152 | |
el13gs | 8:699055e89c7d | 153 | checkPlayerPos(); |
el13gs | 8:699055e89c7d | 154 | if (gamePlays==0) { |
el13gs | 8:699055e89c7d | 155 | goto MainMenu; |
el13gs | 8:699055e89c7d | 156 | } |
el13gs | 7:edf0f1fcb16b | 157 | coinMoves(); // move coin downwards |
el13gs | 8:699055e89c7d | 158 | lcd.refresh(); |
el13gs | 5:243718c3cd8b | 159 | checkPlayerPos(); |
el13gs | 7:edf0f1fcb16b | 160 | if (gamePlays==0) { |
el13gs | 7:edf0f1fcb16b | 161 | goto MainMenu; |
el13gs | 7:edf0f1fcb16b | 162 | } |
el13gs | 4:f9a899c6ac50 | 163 | } |
el13gs | 7:edf0f1fcb16b | 164 | |
el13gs | 4:f9a899c6ac50 | 165 | break; |
el13gs | 4:f9a899c6ac50 | 166 | |
el13gs | 4:f9a899c6ac50 | 167 | case 2: |
el13gs | 4:f9a899c6ac50 | 168 | |
el13gs | 7:edf0f1fcb16b | 169 | int optionsMenu=1; // set options Menu enabled |
el13gs | 8:699055e89c7d | 170 | optionsPointer=1; |
el13gs | 7:edf0f1fcb16b | 171 | startButtonFlag=0; //reset START Button Flag |
el13gs | 4:f9a899c6ac50 | 172 | |
el13gs | 7:edf0f1fcb16b | 173 | clearCells(84,48); // clear all screen Cells |
el13gs | 4:f9a899c6ac50 | 174 | lcd.refresh(); |
el13gs | 4:f9a899c6ac50 | 175 | wait(0.02); |
el13gs | 7:edf0f1fcb16b | 176 | /* PRINT OPTIONS SUBMENU DISPLAY */ |
el13gs | 4:f9a899c6ac50 | 177 | lcd.printString("OPTIONS",18,0); |
el13gs | 4:f9a899c6ac50 | 178 | lcd.printString("Exit",23,5); |
el13gs | 4:f9a899c6ac50 | 179 | lcd.printString("Brightness",6,3); |
el13gs | 4:f9a899c6ac50 | 180 | lcd.printString("Sound",6,2); |
el13gs | 7:edf0f1fcb16b | 181 | lcd.drawLine(15,8,61,8,1); //underline Options Menu title |
el13gs | 4:f9a899c6ac50 | 182 | |
el13gs | 7:edf0f1fcb16b | 183 | /* Print brightness value ranged from 1 to 10 to user */ |
el13gs | 7:edf0f1fcb16b | 184 | sprintf(brightnessBuffer,"%d",brightnessDisplay); |
el13gs | 7:edf0f1fcb16b | 185 | lcd.printString(brightnessBuffer,68,3); |
el13gs | 4:f9a899c6ac50 | 186 | |
el13gs | 7:edf0f1fcb16b | 187 | /* Print Sound string value , "YES" or "NO"*/ |
el13gs | 4:f9a899c6ac50 | 188 | lcd.printString(soundString,40,2); |
el13gs | 4:f9a899c6ac50 | 189 | |
el13gs | 7:edf0f1fcb16b | 190 | /* USER NAVIGATES WHILE IN OPTIONS MENU */ |
el13gs | 4:f9a899c6ac50 | 191 | while(optionsMenu) { |
el13gs | 4:f9a899c6ac50 | 192 | |
el13gs | 7:edf0f1fcb16b | 193 | /* If direction is up pointer value decrease by 1 (cannot decrease to less than 1) */ |
el13gs | 4:f9a899c6ac50 | 194 | if (joystick.direction==UP) { |
el13gs | 4:f9a899c6ac50 | 195 | optionsPointer--; |
el13gs | 4:f9a899c6ac50 | 196 | if(optionsPointer==0) { |
el13gs | 4:f9a899c6ac50 | 197 | optionsPointer=1; |
el13gs | 4:f9a899c6ac50 | 198 | } |
el13gs | 4:f9a899c6ac50 | 199 | } |
el13gs | 8:699055e89c7d | 200 | wait(0.05); |
el13gs | 4:f9a899c6ac50 | 201 | |
el13gs | 8:699055e89c7d | 202 | /* If direction is down, pointer value increase by 1 (cannot increase to more than 3) */ |
el13gs | 4:f9a899c6ac50 | 203 | if(joystick.direction==DOWN) { |
el13gs | 4:f9a899c6ac50 | 204 | optionsPointer++; |
el13gs | 4:f9a899c6ac50 | 205 | if(optionsPointer>3) { |
el13gs | 4:f9a899c6ac50 | 206 | optionsPointer=3; |
el13gs | 4:f9a899c6ac50 | 207 | } |
el13gs | 4:f9a899c6ac50 | 208 | } |
el13gs | 7:edf0f1fcb16b | 209 | /* CASES FOR EACH POINTER VALUE */ |
el13gs | 7:edf0f1fcb16b | 210 | /* when it is 1, Sound submenu is selected */ |
el13gs | 4:f9a899c6ac50 | 211 | if (optionsPointer==1) { |
el13gs | 7:edf0f1fcb16b | 212 | /* print Circle pointer around Sound submenu */ |
el13gs | 4:f9a899c6ac50 | 213 | lcd.drawCircle(2,19,2,1); |
el13gs | 4:f9a899c6ac50 | 214 | lcd.clearCircle(2,27,2,1); |
el13gs | 4:f9a899c6ac50 | 215 | lcd.clearCircle(19,44,2,1); |
el13gs | 7:edf0f1fcb16b | 216 | /* if pointer is 1 and joystick direction is left, sound is enabled (=1)*/ |
el13gs | 4:f9a899c6ac50 | 217 | if(joystick.direction==LEFT) { |
el13gs | 4:f9a899c6ac50 | 218 | sounds=1; |
el13gs | 4:f9a899c6ac50 | 219 | lcd.refresh(); |
el13gs | 4:f9a899c6ac50 | 220 | sprintf(soundString,"%s","YES"); |
el13gs | 4:f9a899c6ac50 | 221 | lcd.printString(soundString,40,2); |
el13gs | 4:f9a899c6ac50 | 222 | } |
el13gs | 7:edf0f1fcb16b | 223 | /* if pointer is 1 and joystick direction is right, sound is disabled (=0)*/ |
el13gs | 4:f9a899c6ac50 | 224 | if(joystick.direction==RIGHT) { |
el13gs | 4:f9a899c6ac50 | 225 | sounds=0; |
el13gs | 4:f9a899c6ac50 | 226 | lcd.refresh(); |
el13gs | 7:edf0f1fcb16b | 227 | sprintf(soundString,"%s","NO "); //print string in buffer |
el13gs | 7:edf0f1fcb16b | 228 | lcd.printString(soundString,40,2); //display value of buffer |
el13gs | 4:f9a899c6ac50 | 229 | } |
el13gs | 8:699055e89c7d | 230 | sleep(); |
el13gs | 4:f9a899c6ac50 | 231 | } |
el13gs | 7:edf0f1fcb16b | 232 | /* when it is 2, brightness submenu is selected */ |
el13gs | 4:f9a899c6ac50 | 233 | if (optionsPointer==2) { |
el13gs | 7:edf0f1fcb16b | 234 | /* print Circle pointer around Brightness submenu */ |
el13gs | 4:f9a899c6ac50 | 235 | lcd.drawCircle(2,27,2,1); |
el13gs | 4:f9a899c6ac50 | 236 | lcd.clearCircle(2,19,2,1); |
el13gs | 4:f9a899c6ac50 | 237 | lcd.clearCircle(19,44,2,1); |
el13gs | 4:f9a899c6ac50 | 238 | |
el13gs | 7:edf0f1fcb16b | 239 | /* if pointer is 2 and joystick direction is left, brightness decreases by 0.1*/ |
el13gs | 4:f9a899c6ac50 | 240 | if(joystick.direction==LEFT) { |
el13gs | 4:f9a899c6ac50 | 241 | brightness=brightness-.1; |
el13gs | 7:edf0f1fcb16b | 242 | brightnessDisplay=brightnessDisplay-1; // user displayed value also decrease |
el13gs | 7:edf0f1fcb16b | 243 | if(brightness<0.1||brightnessDisplay<0) { // brightnessDisplay and brightness never go below 0 and 0.1 respectively |
el13gs | 4:f9a899c6ac50 | 244 | brightnessDisplay=1; |
el13gs | 4:f9a899c6ac50 | 245 | brightness=0.1; |
el13gs | 4:f9a899c6ac50 | 246 | } |
el13gs | 7:edf0f1fcb16b | 247 | lcd.setBrightness(brightness); //sets the brightness |
el13gs | 4:f9a899c6ac50 | 248 | |
el13gs | 7:edf0f1fcb16b | 249 | /* print brightness Display on screen */ |
el13gs | 7:edf0f1fcb16b | 250 | sprintf(brightnessBuffer,"%d",brightnessDisplay); |
el13gs | 7:edf0f1fcb16b | 251 | lcd.printString(brightnessBuffer,68,3); |
el13gs | 4:f9a899c6ac50 | 252 | } |
el13gs | 4:f9a899c6ac50 | 253 | |
el13gs | 7:edf0f1fcb16b | 254 | /* if pointer is 2 and joystick direction is right, brightness increases by 0.1*/ |
el13gs | 4:f9a899c6ac50 | 255 | if(joystick.direction==RIGHT) { |
el13gs | 4:f9a899c6ac50 | 256 | brightness=brightness+.1; |
el13gs | 7:edf0f1fcb16b | 257 | brightnessDisplay=brightnessDisplay+1; // user displayed value also increase |
el13gs | 7:edf0f1fcb16b | 258 | if(brightness>0.9||brightnessDisplay>9) {// brightnessDisplay and brightness never go above 9 and 0.9 respectively |
el13gs | 4:f9a899c6ac50 | 259 | brightnessDisplay=9; |
el13gs | 4:f9a899c6ac50 | 260 | brightness=0.9; |
el13gs | 4:f9a899c6ac50 | 261 | } |
el13gs | 7:edf0f1fcb16b | 262 | lcd.setBrightness(brightness); //sets the brightness |
el13gs | 4:f9a899c6ac50 | 263 | |
el13gs | 7:edf0f1fcb16b | 264 | /* print brightness Display on screen */ |
el13gs | 7:edf0f1fcb16b | 265 | sprintf(brightnessBuffer,"%d",brightnessDisplay); |
el13gs | 7:edf0f1fcb16b | 266 | lcd.printString(brightnessBuffer,68,3); |
el13gs | 4:f9a899c6ac50 | 267 | } |
el13gs | 8:699055e89c7d | 268 | sleep(); |
el13gs | 4:f9a899c6ac50 | 269 | } |
el13gs | 4:f9a899c6ac50 | 270 | if (optionsPointer==3) { |
el13gs | 7:edf0f1fcb16b | 271 | /* print Circle pointer around Exit submenu */ |
el13gs | 4:f9a899c6ac50 | 272 | lcd.drawCircle(19,44,2,1); |
el13gs | 4:f9a899c6ac50 | 273 | lcd.clearCircle(2,19,2,1); |
el13gs | 4:f9a899c6ac50 | 274 | lcd.clearCircle(2,27,2,1); |
el13gs | 4:f9a899c6ac50 | 275 | |
el13gs | 7:edf0f1fcb16b | 276 | /* if START button is press while pointer is on Exit goes back to Main Menu label */ |
el13gs | 6:289f237b8d90 | 277 | if(startButtonFlag) { |
el13gs | 6:289f237b8d90 | 278 | startButtonFlag=0; |
el13gs | 8:699055e89c7d | 279 | wait(0.5); |
el13gs | 7:edf0f1fcb16b | 280 | optionsMenu=0; |
el13gs | 4:f9a899c6ac50 | 281 | goto MainMenu; |
el13gs | 4:f9a899c6ac50 | 282 | } |
el13gs | 8:699055e89c7d | 283 | sleep(); |
el13gs | 4:f9a899c6ac50 | 284 | } |
el13gs | 8:699055e89c7d | 285 | wait(0.05); |
el13gs | 8:699055e89c7d | 286 | sleep(); |
el13gs | 4:f9a899c6ac50 | 287 | |
el13gs | 4:f9a899c6ac50 | 288 | } |
el13gs | 4:f9a899c6ac50 | 289 | break; |
el13gs | 4:f9a899c6ac50 | 290 | } |
el13gs | 4:f9a899c6ac50 | 291 | } |
el13gs | 4:f9a899c6ac50 | 292 | |
el13gs | 6:289f237b8d90 | 293 | void startButtonPressed() |
el13gs | 4:f9a899c6ac50 | 294 | { |
el13gs | 6:289f237b8d90 | 295 | startButtonFlag=!startButtonFlag; |
el13gs | 4:f9a899c6ac50 | 296 | } |
el13gs | 4:f9a899c6ac50 | 297 | |
el13gs | 6:289f237b8d90 | 298 | void resetButtonPressed() |
el13gs | 6:289f237b8d90 | 299 | { |
el13gs | 6:289f237b8d90 | 300 | resetButtonFlag=!resetButtonFlag; |
el13gs | 6:289f237b8d90 | 301 | } |
el13gs | 6:289f237b8d90 | 302 | |
el13gs | 4:f9a899c6ac50 | 303 | void movePlayer() |
el13gs | 4:f9a899c6ac50 | 304 | { |
el13gs | 4:f9a899c6ac50 | 305 | if (gamePlays) { |
el13gs | 8:699055e89c7d | 306 | /* if direction is RIGHT clears the previous Car shape and draws the next one by increasing the x position */ |
el13gs | 4:f9a899c6ac50 | 307 | if (joystick.direction==RIGHT&&x+w<58) { |
el13gs | 4:f9a899c6ac50 | 308 | |
el13gs | 5:243718c3cd8b | 309 | clearRect(x,v,w,h); |
el13gs | 5:243718c3cd8b | 310 | lcd.refresh(); |
el13gs | 4:f9a899c6ac50 | 311 | x=x+4; |
el13gs | 4:f9a899c6ac50 | 312 | lcd.drawRect(x,v,w,h,0); |
el13gs | 4:f9a899c6ac50 | 313 | lcd.refresh(); |
el13gs | 4:f9a899c6ac50 | 314 | } |
el13gs | 8:699055e89c7d | 315 | /* if direction is LEFT clears the previous Car shape and draws the next one by decreasing the x position */ |
el13gs | 4:f9a899c6ac50 | 316 | if (joystick.direction==LEFT&&x+w>10) { |
el13gs | 4:f9a899c6ac50 | 317 | |
el13gs | 5:243718c3cd8b | 318 | clearRect(x,v,w,h); |
el13gs | 5:243718c3cd8b | 319 | lcd.refresh(); |
el13gs | 4:f9a899c6ac50 | 320 | x=x-4; |
el13gs | 5:243718c3cd8b | 321 | |
el13gs | 4:f9a899c6ac50 | 322 | lcd.drawRect(x,v,w,h,0); |
el13gs | 5:243718c3cd8b | 323 | lcd.refresh(); |
el13gs | 4:f9a899c6ac50 | 324 | } |
el13gs | 8:699055e89c7d | 325 | /* if direction is UP clears the previous Car shape and draws the next one by decreasing the y position */ |
el13gs | 4:f9a899c6ac50 | 326 | if (joystick.direction==UP && v>2) { |
el13gs | 4:f9a899c6ac50 | 327 | |
el13gs | 5:243718c3cd8b | 328 | clearRect(x,v,w,h); |
el13gs | 5:243718c3cd8b | 329 | lcd.refresh(); |
el13gs | 4:f9a899c6ac50 | 330 | v=v-4; |
el13gs | 4:f9a899c6ac50 | 331 | lcd.drawRect(x,v,w,h,0); |
el13gs | 5:243718c3cd8b | 332 | lcd.refresh(); |
el13gs | 4:f9a899c6ac50 | 333 | } |
el13gs | 8:699055e89c7d | 334 | /* if direction is DOWN clears the previous Car shape and draws the next one by increasing the y position */ |
el13gs | 4:f9a899c6ac50 | 335 | if (joystick.direction==DOWN && v<32) { |
el13gs | 5:243718c3cd8b | 336 | clearRect(x,v,w,h); |
el13gs | 5:243718c3cd8b | 337 | lcd.refresh(); |
el13gs | 4:f9a899c6ac50 | 338 | v=v+4; |
el13gs | 4:f9a899c6ac50 | 339 | lcd.drawRect(x,v,w,h,0); |
el13gs | 5:243718c3cd8b | 340 | lcd.refresh(); |
el13gs | 4:f9a899c6ac50 | 341 | } |
el13gs | 4:f9a899c6ac50 | 342 | |
el13gs | 4:f9a899c6ac50 | 343 | } |
el13gs | 4:f9a899c6ac50 | 344 | } |
el13gs | 4:f9a899c6ac50 | 345 | |
el13gs | 4:f9a899c6ac50 | 346 | void calibrateJoystick() |
el13gs | 4:f9a899c6ac50 | 347 | { |
el13gs | 4:f9a899c6ac50 | 348 | button.mode(PullDown); |
el13gs | 4:f9a899c6ac50 | 349 | // must not move during calibration |
el13gs | 4:f9a899c6ac50 | 350 | joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) |
el13gs | 4:f9a899c6ac50 | 351 | joystick.y0 = yPot; |
el13gs | 4:f9a899c6ac50 | 352 | } |
el13gs | 4:f9a899c6ac50 | 353 | void updateJoystick() |
el13gs | 4:f9a899c6ac50 | 354 | { |
el13gs | 4:f9a899c6ac50 | 355 | joystick.x = xPot - joystick.x0; |
el13gs | 4:f9a899c6ac50 | 356 | joystick.y = yPot - joystick.y0; |
el13gs | 4:f9a899c6ac50 | 357 | // read button state |
el13gs | 4:f9a899c6ac50 | 358 | joystick.button = button; |
el13gs | 4:f9a899c6ac50 | 359 | |
el13gs | 4:f9a899c6ac50 | 360 | // calculate direction depending on x,y values |
el13gs | 4:f9a899c6ac50 | 361 | // tolerance allows a little lee-way in case joystick not exactly in the stated direction |
el13gs | 4:f9a899c6ac50 | 362 | if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
el13gs | 4:f9a899c6ac50 | 363 | joystick.direction = CENTRE; |
el13gs | 4:f9a899c6ac50 | 364 | } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
el13gs | 4:f9a899c6ac50 | 365 | joystick.direction = UP; |
el13gs | 4:f9a899c6ac50 | 366 | } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
el13gs | 4:f9a899c6ac50 | 367 | joystick.direction = DOWN; |
el13gs | 4:f9a899c6ac50 | 368 | } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
el13gs | 4:f9a899c6ac50 | 369 | joystick.direction = LEFT; |
el13gs | 4:f9a899c6ac50 | 370 | } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
el13gs | 4:f9a899c6ac50 | 371 | joystick.direction = RIGHT; |
el13gs | 4:f9a899c6ac50 | 372 | } else { |
el13gs | 4:f9a899c6ac50 | 373 | joystick.direction = UNKNOWN; |
el13gs | 4:f9a899c6ac50 | 374 | } |
el13gs | 4:f9a899c6ac50 | 375 | |
el13gs | 4:f9a899c6ac50 | 376 | // set flag for printing |
el13gs | 4:f9a899c6ac50 | 377 | printFlag = 1; |
el13gs | 4:f9a899c6ac50 | 378 | } |
el13gs | 4:f9a899c6ac50 | 379 | |
el13gs | 4:f9a899c6ac50 | 380 | |
el13gs | 5:243718c3cd8b | 381 | void clearRect(int x,int v,int w,int h) |
el13gs | 4:f9a899c6ac50 | 382 | { |
el13gs | 8:699055e89c7d | 383 | // loop through the array cells, clear the Pixels and array cells value(=0) |
el13gs | 4:f9a899c6ac50 | 384 | for(int i=x; i<=x+w; i++) { |
el13gs | 4:f9a899c6ac50 | 385 | for(int j=v; j<=v+h; j++) { |
el13gs | 4:f9a899c6ac50 | 386 | lcd.clearPixel(i,j); |
el13gs | 4:f9a899c6ac50 | 387 | table[i][j]=0; |
el13gs | 4:f9a899c6ac50 | 388 | } |
el13gs | 4:f9a899c6ac50 | 389 | } |
el13gs | 4:f9a899c6ac50 | 390 | } |
el13gs | 4:f9a899c6ac50 | 391 | |
el13gs | 4:f9a899c6ac50 | 392 | |
el13gs | 4:f9a899c6ac50 | 393 | void enemy1MovesDown() |
el13gs | 4:f9a899c6ac50 | 394 | { |
el13gs | 8:699055e89c7d | 395 | clearRect(enemy1x,(j-a),w,h); // clears the previous Enemy Car |
el13gs | 5:243718c3cd8b | 396 | lcd.refresh(); |
el13gs | 8:699055e89c7d | 397 | lcd.drawRect(enemy1x,j,w,h,1); //draws the next enemy Car |
el13gs | 5:243718c3cd8b | 398 | lcd.refresh(); |
el13gs | 8:699055e89c7d | 399 | setRectCells(enemy1x,j,w,h); //set the current enemy cells to 1 |
el13gs | 5:243718c3cd8b | 400 | lcd.refresh(); |
el13gs | 4:f9a899c6ac50 | 401 | wait(0.05); |
el13gs | 4:f9a899c6ac50 | 402 | |
el13gs | 8:699055e89c7d | 403 | j=j+a; //increase the y-position counter |
el13gs | 4:f9a899c6ac50 | 404 | |
el13gs | 8:699055e89c7d | 405 | if(j>70) { // after reaching certain y position, randomize it again br |
el13gs | 8:699055e89c7d | 406 | j=-(rand()%95+85); |
el13gs | 4:f9a899c6ac50 | 407 | } |
el13gs | 4:f9a899c6ac50 | 408 | |
el13gs | 4:f9a899c6ac50 | 409 | } |
el13gs | 4:f9a899c6ac50 | 410 | |
el13gs | 4:f9a899c6ac50 | 411 | void enemy2MovesDown() |
el13gs | 4:f9a899c6ac50 | 412 | { |
el13gs | 8:699055e89c7d | 413 | clearRect(enemy2x,(p-a),w,h); // clears the previous Enemy Car |
el13gs | 5:243718c3cd8b | 414 | lcd.refresh(); |
el13gs | 8:699055e89c7d | 415 | lcd.drawRect(enemy2x,p,w,h,1); //draws the next enemy Car |
el13gs | 5:243718c3cd8b | 416 | lcd.refresh(); |
el13gs | 8:699055e89c7d | 417 | setRectCells(enemy2x,p,w,h); //set the current enemy cells to 1 |
el13gs | 5:243718c3cd8b | 418 | lcd.refresh(); |
el13gs | 4:f9a899c6ac50 | 419 | wait(0.05); |
el13gs | 8:699055e89c7d | 420 | p=p+a; //increase the y-position counter |
el13gs | 4:f9a899c6ac50 | 421 | |
el13gs | 8:699055e89c7d | 422 | if(p>70) { |
el13gs | 8:699055e89c7d | 423 | p=-(rand()%60+40); |
el13gs | 4:f9a899c6ac50 | 424 | } |
el13gs | 4:f9a899c6ac50 | 425 | |
el13gs | 4:f9a899c6ac50 | 426 | } |
el13gs | 4:f9a899c6ac50 | 427 | |
el13gs | 4:f9a899c6ac50 | 428 | void enemy3MovesDown() |
el13gs | 4:f9a899c6ac50 | 429 | { |
el13gs | 6:289f237b8d90 | 430 | clearRect(enemy3x,(q-a),w,h); |
el13gs | 5:243718c3cd8b | 431 | lcd.refresh(); |
el13gs | 4:f9a899c6ac50 | 432 | lcd.drawRect(enemy3x,q,w,h,1); |
el13gs | 5:243718c3cd8b | 433 | lcd.refresh(); |
el13gs | 5:243718c3cd8b | 434 | setRectCells(enemy3x,q,w,h); |
el13gs | 5:243718c3cd8b | 435 | lcd.refresh(); |
el13gs | 4:f9a899c6ac50 | 436 | wait(0.05); |
el13gs | 4:f9a899c6ac50 | 437 | lcd.refresh(); |
el13gs | 6:289f237b8d90 | 438 | q=q+a; |
el13gs | 4:f9a899c6ac50 | 439 | if (q>70) { |
el13gs | 8:699055e89c7d | 440 | q=-(rand()%34+3); |
el13gs | 4:f9a899c6ac50 | 441 | } |
el13gs | 4:f9a899c6ac50 | 442 | |
el13gs | 4:f9a899c6ac50 | 443 | } |
el13gs | 4:f9a899c6ac50 | 444 | |
el13gs | 5:243718c3cd8b | 445 | void setRectCells(int x,int v,int w,int h) |
el13gs | 4:f9a899c6ac50 | 446 | { |
el13gs | 8:699055e89c7d | 447 | // Sets the Rectangle Array Cells equal to 1, indicating there is an enemy |
el13gs | 4:f9a899c6ac50 | 448 | for(int i=x; i<=x+w; i++) { |
el13gs | 4:f9a899c6ac50 | 449 | for(int j=v; j<=v+h; j++) { |
el13gs | 4:f9a899c6ac50 | 450 | if(j>0&&j<48) { |
el13gs | 4:f9a899c6ac50 | 451 | table[i][j]=1; |
el13gs | 4:f9a899c6ac50 | 452 | } |
el13gs | 4:f9a899c6ac50 | 453 | } |
el13gs | 4:f9a899c6ac50 | 454 | } |
el13gs | 4:f9a899c6ac50 | 455 | } |
el13gs | 4:f9a899c6ac50 | 456 | |
el13gs | 4:f9a899c6ac50 | 457 | void initTable() |
el13gs | 4:f9a899c6ac50 | 458 | { |
el13gs | 8:699055e89c7d | 459 | // initialise the Array's cells, set them equal to 0 |
el13gs | 5:243718c3cd8b | 460 | for (int i=0; i<=83; i++) { |
el13gs | 5:243718c3cd8b | 461 | for(int j=0; j<=47; j++) { |
el13gs | 4:f9a899c6ac50 | 462 | table[i][j]=0; |
el13gs | 4:f9a899c6ac50 | 463 | } |
el13gs | 4:f9a899c6ac50 | 464 | } |
el13gs | 4:f9a899c6ac50 | 465 | } |
el13gs | 4:f9a899c6ac50 | 466 | |
el13gs | 4:f9a899c6ac50 | 467 | void checkPlayerPos() |
el13gs | 4:f9a899c6ac50 | 468 | { |
el13gs | 8:699055e89c7d | 469 | // frequently calling this function to check if player touched an enemy or a coin |
el13gs | 8:699055e89c7d | 470 | /* if player cells equal to 1, means he touched enemy, so loose function is being called */ |
el13gs | 4:f9a899c6ac50 | 471 | if (table[x+w][v+h]==1||table[x][v+h]==1||table[x][v]==1||table[x+w][v]==1) { |
el13gs | 4:f9a899c6ac50 | 472 | loose(); |
el13gs | 4:f9a899c6ac50 | 473 | } |
el13gs | 8:699055e89c7d | 474 | /* if player cells equal to 2, means he touched a coin */ |
el13gs | 4:f9a899c6ac50 | 475 | if (table[x+w][v+h]==2||table[x][v+h]==2||table[x][v]==2||table[x+w][v]==2) { |
el13gs | 8:699055e89c7d | 476 | coins=coins+5; //each coin's value is 5,total coins increase by 5 |
el13gs | 8:699055e89c7d | 477 | clearCoin(c); //clear the collected Coin |
el13gs | 8:699055e89c7d | 478 | lcd.drawRect(x,v,w,h,0);//redraw Player boundaries that were cleared by Coin |
el13gs | 8:699055e89c7d | 479 | if(sounds) { // if sound is enabled in options menu sound produced when got a coin |
el13gs | 8:699055e89c7d | 480 | buzzer.beep(2200,0.4); //produce sound of frequency 2200 Hz and duration 0.4 s |
el13gs | 4:f9a899c6ac50 | 481 | buzzer.beep(1000,0.2); |
el13gs | 8:699055e89c7d | 482 | lcd.setBrightness(brightness); //set brightness again, frequency produced interfere with LCD pin |
el13gs | 4:f9a899c6ac50 | 483 | } |
el13gs | 8:699055e89c7d | 484 | coinAppear=0; //switch coin status to disappear |
el13gs | 8:699055e89c7d | 485 | |
el13gs | 8:699055e89c7d | 486 | /* then Check the total number of coins, to produce to next Round */ |
el13gs | 5:243718c3cd8b | 487 | |
el13gs | 8:699055e89c7d | 488 | if(coins==30) { // if coins are 40 proceed to next Round |
el13gs | 8:699055e89c7d | 489 | round=2; // change Round Value |
el13gs | 8:699055e89c7d | 490 | gamePlays=0; // stop gameplay, preventing user moving on screen |
el13gs | 8:699055e89c7d | 491 | clearCells(84,48); //clear all Cells |
el13gs | 6:289f237b8d90 | 492 | lcd.refresh(); |
el13gs | 8:699055e89c7d | 493 | lcd.printString("ROUND 2",10,2); //print Round message |
el13gs | 6:289f237b8d90 | 494 | wait(1); |
el13gs | 8:699055e89c7d | 495 | clearCells(84,48);// clear all Cells |
el13gs | 6:289f237b8d90 | 496 | lcd.refresh(); |
el13gs | 8:699055e89c7d | 497 | gamePlays=1;//start gameplay |
el13gs | 8:699055e89c7d | 498 | gameReset();//reset player and enemies positions |
el13gs | 8:699055e89c7d | 499 | a=6; //change enemies acceleration (a is added to y positions counter) |
el13gs | 6:289f237b8d90 | 500 | } |
el13gs | 8:699055e89c7d | 501 | if(coins==70) { // if coins are 70 proceed to next Round |
el13gs | 8:699055e89c7d | 502 | round=3; // change Round Value |
el13gs | 8:699055e89c7d | 503 | gamePlays=0;// stop gameplay, preventing user moving on screen |
el13gs | 8:699055e89c7d | 504 | clearCells(84,48); //clear all Cells |
el13gs | 6:289f237b8d90 | 505 | lcd.refresh(); |
el13gs | 8:699055e89c7d | 506 | lcd.printString("ROUND 3",10,2); //print Round message |
el13gs | 6:289f237b8d90 | 507 | wait(1); |
el13gs | 8:699055e89c7d | 508 | clearCells(84,48); // clear all Cells |
el13gs | 6:289f237b8d90 | 509 | lcd.refresh(); |
el13gs | 8:699055e89c7d | 510 | gamePlays=1; //start gameplay |
el13gs | 8:699055e89c7d | 511 | gameReset(); //reset player and enemies positions |
el13gs | 8:699055e89c7d | 512 | a=8; //change enemies acceleration (a is added to y positions counter) |
el13gs | 6:289f237b8d90 | 513 | } |
el13gs | 8:699055e89c7d | 514 | if(coins==90) { // if coins are 90, player WINS the game |
el13gs | 8:699055e89c7d | 515 | timer.detach(); //stop player moving |
el13gs | 8:699055e89c7d | 516 | clearCells(84,48); //clear pixels |
el13gs | 8:699055e89c7d | 517 | lcd.clear(); //clear LCD |
el13gs | 8:699055e89c7d | 518 | lcd.refresh(); |
el13gs | 8:699055e89c7d | 519 | lcd.printString("CONGRATS!",0,2); //print win message |
el13gs | 8:699055e89c7d | 520 | lcd.printString("YOU WON!",8,3); //print win message |
el13gs | 8:699055e89c7d | 521 | wait(4); |
el13gs | 8:699055e89c7d | 522 | gamePlays=0; //game finishes |
el13gs | 6:289f237b8d90 | 523 | } |
el13gs | 4:f9a899c6ac50 | 524 | } |
el13gs | 4:f9a899c6ac50 | 525 | } |
el13gs | 4:f9a899c6ac50 | 526 | |
el13gs | 4:f9a899c6ac50 | 527 | void coinMoves() |
el13gs | 4:f9a899c6ac50 | 528 | { |
el13gs | 8:699055e89c7d | 529 | if(table[xPos][c]==1||table[xPos][c+4]==1||table[xPos+4][c+4]==1) { //prevent coin from appearing above enemies |
el13gs | 8:699055e89c7d | 530 | clearCoin(c); //clear current Coin |
el13gs | 8:699055e89c7d | 531 | coinAppear=0; //make coin disappear (prevent from being redrawn) |
el13gs | 6:289f237b8d90 | 532 | } |
el13gs | 8:699055e89c7d | 533 | clearCoin(c); //clear current Coin |
el13gs | 8:699055e89c7d | 534 | c=c+4; // increase Coin's y coordinate counter |
el13gs | 8:699055e89c7d | 535 | drawCoin(c); //draw next Coin position |
el13gs | 8:699055e89c7d | 536 | if(c>80) { // if counter exceeds 80, randomizes x and y positions and make it reappear |
el13gs | 6:289f237b8d90 | 537 | c=-(rand()%10); |
el13gs | 6:289f237b8d90 | 538 | xPos=rand()%52+4; |
el13gs | 4:f9a899c6ac50 | 539 | coinAppear=1; |
el13gs | 4:f9a899c6ac50 | 540 | } |
el13gs | 4:f9a899c6ac50 | 541 | } |
el13gs | 4:f9a899c6ac50 | 542 | |
el13gs | 4:f9a899c6ac50 | 543 | void drawCoin(int c) |
el13gs | 4:f9a899c6ac50 | 544 | { |
el13gs | 8:699055e89c7d | 545 | if (c>4&&c<48&&coinAppear==1) { //check if coin c point(y pos) is within LCD limits |
el13gs | 6:289f237b8d90 | 546 | lcd.drawCircle(xPos,c,4,0); //Draw Coin Circle Pixels |
el13gs | 6:289f237b8d90 | 547 | setCircleCells(xPos,c,4);// Set Coin array's cells values to 2 |
el13gs | 4:f9a899c6ac50 | 548 | |
el13gs | 6:289f237b8d90 | 549 | // "c" character Print on coin |
el13gs | 4:f9a899c6ac50 | 550 | lcd.setPixel(xPos,c-1); |
el13gs | 4:f9a899c6ac50 | 551 | lcd.setPixel(xPos,c+2); |
el13gs | 4:f9a899c6ac50 | 552 | lcd.setPixel(xPos+1,c-1); |
el13gs | 4:f9a899c6ac50 | 553 | lcd.setPixel(xPos+1,c+2); |
el13gs | 4:f9a899c6ac50 | 554 | lcd.setPixel(xPos-1,c); |
el13gs | 4:f9a899c6ac50 | 555 | lcd.setPixel(xPos-1,c+1); |
el13gs | 5:243718c3cd8b | 556 | |
el13gs | 4:f9a899c6ac50 | 557 | lcd.refresh(); |
el13gs | 4:f9a899c6ac50 | 558 | } |
el13gs | 4:f9a899c6ac50 | 559 | } |
el13gs | 4:f9a899c6ac50 | 560 | |
el13gs | 4:f9a899c6ac50 | 561 | void clearCoin(int c) |
el13gs | 4:f9a899c6ac50 | 562 | { |
el13gs | 8:699055e89c7d | 563 | if(c>4&&c<48&&coinAppear==1) { // if c point of the Coin within the LCD limits |
el13gs | 7:edf0f1fcb16b | 564 | lcd.clearCircle(xPos,c,4,1); //Clear the Coin |
el13gs | 6:289f237b8d90 | 565 | clearCircleCells(xPos,c,4);// Set Coin array's cells values to 0 |
el13gs | 6:289f237b8d90 | 566 | lcd.refresh(); |
el13gs | 6:289f237b8d90 | 567 | } |
el13gs | 4:f9a899c6ac50 | 568 | } |
el13gs | 4:f9a899c6ac50 | 569 | |
el13gs | 8:699055e89c7d | 570 | void loose() |
el13gs | 8:699055e89c7d | 571 | { |
el13gs | 8:699055e89c7d | 572 | |
el13gs | 8:699055e89c7d | 573 | if (sounds) { //check if sound is enabled and produce a "loose" sound of frequency 400 Hz |
el13gs | 8:699055e89c7d | 574 | buzzer.beep(400,0.2); |
el13gs | 8:699055e89c7d | 575 | lcd.setBrightness(brightness); //set brightness |
el13gs | 8:699055e89c7d | 576 | } |
el13gs | 8:699055e89c7d | 577 | timer.detach(); //detach moving Player function timer, so player cannot move |
el13gs | 8:699055e89c7d | 578 | lives=lives-1; //decrease lives value |
el13gs | 8:699055e89c7d | 579 | startButtonFlag=0; //reset START button flag |
el13gs | 8:699055e89c7d | 580 | resetButtonFlag=0;//reset SELECT button flag |
el13gs | 8:699055e89c7d | 581 | if(lives==0) { //if lives are equal to 0, Game Over is displayed |
el13gs | 8:699055e89c7d | 582 | |
el13gs | 8:699055e89c7d | 583 | clearCells(84,48); |
el13gs | 8:699055e89c7d | 584 | lcd.refresh(); |
el13gs | 8:699055e89c7d | 585 | lcd.clear(); |
el13gs | 8:699055e89c7d | 586 | |
el13gs | 8:699055e89c7d | 587 | wait(1); |
el13gs | 8:699055e89c7d | 588 | while(startButtonFlag==0&&resetButtonFlag==0) { //while no button is pressed |
el13gs | 8:699055e89c7d | 589 | lcd.printString("Game Over!!!",0,0); |
el13gs | 8:699055e89c7d | 590 | lcd.printString("Press START",0,2); |
el13gs | 8:699055e89c7d | 591 | lcd.printString("to try again",0,3); |
el13gs | 8:699055e89c7d | 592 | lcd.printString("or RESET",0,4); |
el13gs | 8:699055e89c7d | 593 | lcd.printString("to exit :)",0,5); |
el13gs | 8:699055e89c7d | 594 | } |
el13gs | 8:699055e89c7d | 595 | if(startButtonFlag) { //START button pressed, resets the game |
el13gs | 8:699055e89c7d | 596 | a=4; |
el13gs | 8:699055e89c7d | 597 | gamePlays=1; |
el13gs | 8:699055e89c7d | 598 | lives=5; |
el13gs | 8:699055e89c7d | 599 | coins=0; |
el13gs | 8:699055e89c7d | 600 | round=1; |
el13gs | 8:699055e89c7d | 601 | clearCells(84,48); |
el13gs | 8:699055e89c7d | 602 | gameReset(); |
el13gs | 8:699055e89c7d | 603 | startButtonFlag=0; |
el13gs | 8:699055e89c7d | 604 | wait(1); |
el13gs | 8:699055e89c7d | 605 | timer.attach(&movePlayer,0.1); |
el13gs | 8:699055e89c7d | 606 | } |
el13gs | 8:699055e89c7d | 607 | if (resetButtonFlag) { //RESET button pressed, sets gameplay to 0 ( when gameplay is 0 , returns to MAIN MENU) |
el13gs | 8:699055e89c7d | 608 | gamePlays=0; |
el13gs | 8:699055e89c7d | 609 | resetButtonFlag=0; |
el13gs | 8:699055e89c7d | 610 | } |
el13gs | 8:699055e89c7d | 611 | } |
el13gs | 8:699055e89c7d | 612 | |
el13gs | 8:699055e89c7d | 613 | else { |
el13gs | 8:699055e89c7d | 614 | clearCells(60,48); //else if lives are not 0, clearCells and reset the game |
el13gs | 8:699055e89c7d | 615 | lcd.refresh(); |
el13gs | 8:699055e89c7d | 616 | gameReset(); // reset player and enemies values |
el13gs | 8:699055e89c7d | 617 | wait(2); |
el13gs | 8:699055e89c7d | 618 | gamePlays=1;// game is being played |
el13gs | 8:699055e89c7d | 619 | timer.attach(&movePlayer,0.1); //attach move Player function |
el13gs | 8:699055e89c7d | 620 | } |
el13gs | 8:699055e89c7d | 621 | } |
el13gs | 8:699055e89c7d | 622 | |
el13gs | 8:699055e89c7d | 623 | void gameReset() |
el13gs | 8:699055e89c7d | 624 | { |
el13gs | 8:699055e89c7d | 625 | initTable(); // initialise the array (set cells values to 0) |
el13gs | 8:699055e89c7d | 626 | x=26; //initial player x position |
el13gs | 8:699055e89c7d | 627 | v=30; //initial player y position |
el13gs | 8:699055e89c7d | 628 | lcd.drawRect(x,v,w,h,0); //draw player at initial position |
el13gs | 8:699055e89c7d | 629 | lcd.refresh(); |
el13gs | 8:699055e89c7d | 630 | j=-70; // enemy y position, initialise |
el13gs | 8:699055e89c7d | 631 | q=-10; // enemy y position, initialise |
el13gs | 8:699055e89c7d | 632 | p=-40; // enemy y position, initialise |
el13gs | 8:699055e89c7d | 633 | |
el13gs | 8:699055e89c7d | 634 | } |
el13gs | 8:699055e89c7d | 635 | |
el13gs | 4:f9a899c6ac50 | 636 | |
el13gs | 4:f9a899c6ac50 | 637 | void setCircleCells(int x0,int y0,int radius) |
el13gs | 4:f9a899c6ac50 | 638 | { |
el13gs | 8:699055e89c7d | 639 | //function from N5110 Library, works only for filled circle |
el13gs | 4:f9a899c6ac50 | 640 | int x = radius; |
el13gs | 4:f9a899c6ac50 | 641 | int y = 0; |
el13gs | 4:f9a899c6ac50 | 642 | |
el13gs | 6:289f237b8d90 | 643 | while(x >= y) { |
el13gs | 4:f9a899c6ac50 | 644 | |
el13gs | 6:289f237b8d90 | 645 | setLineCells(x+x0,y+y0,-x+x0,y+y0,0); //set line's Cells values to 2 |
el13gs | 6:289f237b8d90 | 646 | setLineCells(y+x0,x+y0,-y+x0,x+y0,0);//set line's Cells values to 2 |
el13gs | 6:289f237b8d90 | 647 | setLineCells(y+x0,-x+y0,-y+x0,-x+y0,0);//set line's Cells values to 2 |
el13gs | 6:289f237b8d90 | 648 | setLineCells(x+x0,-y+y0,-x+x0,-y+y0,0);//set line's Cells values to 2 |
el13gs | 4:f9a899c6ac50 | 649 | |
el13gs | 6:289f237b8d90 | 650 | y++; |
el13gs | 4:f9a899c6ac50 | 651 | } |
el13gs | 4:f9a899c6ac50 | 652 | } |
el13gs | 4:f9a899c6ac50 | 653 | |
el13gs | 4:f9a899c6ac50 | 654 | void clearCircleCells(int x0,int y0,int radius) |
el13gs | 4:f9a899c6ac50 | 655 | { |
el13gs | 8:699055e89c7d | 656 | //function from N5110 Library, works only for filled circle |
el13gs | 4:f9a899c6ac50 | 657 | int x = radius; |
el13gs | 4:f9a899c6ac50 | 658 | int y = 0; |
el13gs | 4:f9a899c6ac50 | 659 | |
el13gs | 4:f9a899c6ac50 | 660 | while(x >= y) { |
el13gs | 8:699055e89c7d | 661 | clearLineCells(x+x0,y+y0,-x+x0,y+y0,0);//set line's Cells values to 0 |
el13gs | 8:699055e89c7d | 662 | clearLineCells(y+x0,x+y0,-y+x0,x+y0,0);//set line's Cells values to 0 |
el13gs | 8:699055e89c7d | 663 | clearLineCells(y+x0,-x+y0,-y+x0,-x+y0,0);//set line's Cells values to 0 |
el13gs | 8:699055e89c7d | 664 | clearLineCells(x+x0,-y+y0,-x+x0,-y+y0,0);//set line's Cells values to 0 |
el13gs | 4:f9a899c6ac50 | 665 | y++; |
el13gs | 4:f9a899c6ac50 | 666 | } |
el13gs | 4:f9a899c6ac50 | 667 | } |
el13gs | 4:f9a899c6ac50 | 668 | |
el13gs | 4:f9a899c6ac50 | 669 | void clearCells(int x,int y) |
el13gs | 4:f9a899c6ac50 | 670 | { |
el13gs | 8:699055e89c7d | 671 | // loops through cells, clears the pixels and set the cells value equal to 0 |
el13gs | 4:f9a899c6ac50 | 672 | for(int i=0; i<=x; i++) { |
el13gs | 4:f9a899c6ac50 | 673 | for (int j=0; j<=y; j++) { |
el13gs | 4:f9a899c6ac50 | 674 | lcd.clearPixel(i,j); |
el13gs | 4:f9a899c6ac50 | 675 | table[i][j]=0; |
el13gs | 4:f9a899c6ac50 | 676 | } |
el13gs | 4:f9a899c6ac50 | 677 | } |
el13gs | 5:243718c3cd8b | 678 | lcd.refresh(); |
el13gs | 4:f9a899c6ac50 | 679 | } |
el13gs | 4:f9a899c6ac50 | 680 | |
el13gs | 6:289f237b8d90 | 681 | |
el13gs | 6:289f237b8d90 | 682 | void setLineCells(int x0,int y0,int x1,int y1,int type) |
el13gs | 6:289f237b8d90 | 683 | { |
el13gs | 6:289f237b8d90 | 684 | //function from N5110 Library |
el13gs | 6:289f237b8d90 | 685 | int y_range = y1-y0; // calc range of y and x |
el13gs | 6:289f237b8d90 | 686 | int x_range = x1-x0; |
el13gs | 6:289f237b8d90 | 687 | int start,stop,step; |
el13gs | 6:289f237b8d90 | 688 | |
el13gs | 6:289f237b8d90 | 689 | // if dotted line, set step to 2, else step is 1 |
el13gs | 6:289f237b8d90 | 690 | step = (type==2) ? 2:1; |
el13gs | 6:289f237b8d90 | 691 | |
el13gs | 6:289f237b8d90 | 692 | // make sure we loop over the largest range to get the most pixels on the display |
el13gs | 6:289f237b8d90 | 693 | // for instance, if drawing a vertical line (x_range = 0), we need to loop down the y pixels |
el13gs | 6:289f237b8d90 | 694 | // or else we'll only end up with 1 pixel in the x column |
el13gs | 6:289f237b8d90 | 695 | if ( abs(x_range) > abs(y_range) ) { |
el13gs | 6:289f237b8d90 | 696 | |
el13gs | 6:289f237b8d90 | 697 | // ensure we loop from smallest to largest or else for-loop won't run as expected |
el13gs | 6:289f237b8d90 | 698 | start = x1>x0 ? x0:x1; |
el13gs | 6:289f237b8d90 | 699 | stop = x1>x0 ? x1:x0; |
el13gs | 6:289f237b8d90 | 700 | |
el13gs | 6:289f237b8d90 | 701 | // loop between x pixels |
el13gs | 6:289f237b8d90 | 702 | for (int x = start; x<= stop ; x+=step) { |
el13gs | 6:289f237b8d90 | 703 | // do linear interpolation |
el13gs | 6:289f237b8d90 | 704 | int y = y0 + (y1-y0)*(x-x0)/(x1-x0); |
el13gs | 8:699055e89c7d | 705 | table[x][y]=2; // set table values to 2 |
el13gs | 6:289f237b8d90 | 706 | } |
el13gs | 6:289f237b8d90 | 707 | } else { |
el13gs | 6:289f237b8d90 | 708 | |
el13gs | 6:289f237b8d90 | 709 | // ensure we loop from smallest to largest or else for-loop won't run as expected |
el13gs | 6:289f237b8d90 | 710 | start = y1>y0 ? y0:y1; |
el13gs | 6:289f237b8d90 | 711 | stop = y1>y0 ? y1:y0; |
el13gs | 6:289f237b8d90 | 712 | |
el13gs | 6:289f237b8d90 | 713 | for (int y = start; y<= stop ; y+=step) { |
el13gs | 6:289f237b8d90 | 714 | // do linear interpolation |
el13gs | 6:289f237b8d90 | 715 | int x = x0 + (x1-x0)*(y-y0)/(y1-y0); |
el13gs | 8:699055e89c7d | 716 | table[x][y]=2; // set table values to 2 |
el13gs | 6:289f237b8d90 | 717 | } |
el13gs | 6:289f237b8d90 | 718 | } |
el13gs | 6:289f237b8d90 | 719 | |
el13gs | 6:289f237b8d90 | 720 | } |
el13gs | 6:289f237b8d90 | 721 | |
el13gs | 6:289f237b8d90 | 722 | void clearLineCells(int x0,int y0,int x1,int y1,int type) |
el13gs | 6:289f237b8d90 | 723 | { |
el13gs | 6:289f237b8d90 | 724 | //function from N5110 Library |
el13gs | 6:289f237b8d90 | 725 | int y_range = y1-y0; // calc range of y and x |
el13gs | 6:289f237b8d90 | 726 | int x_range = x1-x0; |
el13gs | 6:289f237b8d90 | 727 | int start,stop,step; |
el13gs | 6:289f237b8d90 | 728 | |
el13gs | 6:289f237b8d90 | 729 | // if dotted line, set step to 2, else step is 1 |
el13gs | 6:289f237b8d90 | 730 | step = (type==2) ? 2:1; |
el13gs | 6:289f237b8d90 | 731 | |
el13gs | 6:289f237b8d90 | 732 | // make sure we loop over the largest range to get the most pixels on the display |
el13gs | 6:289f237b8d90 | 733 | // for instance, if drawing a vertical line (x_range = 0), we need to loop down the y pixels |
el13gs | 6:289f237b8d90 | 734 | // or else we'll only end up with 1 pixel in the x column |
el13gs | 6:289f237b8d90 | 735 | if ( abs(x_range) > abs(y_range) ) { |
el13gs | 6:289f237b8d90 | 736 | |
el13gs | 6:289f237b8d90 | 737 | // ensure we loop from smallest to largest or else for-loop won't run as expected |
el13gs | 6:289f237b8d90 | 738 | start = x1>x0 ? x0:x1; |
el13gs | 6:289f237b8d90 | 739 | stop = x1>x0 ? x1:x0; |
el13gs | 6:289f237b8d90 | 740 | // loop between x pixels |
el13gs | 6:289f237b8d90 | 741 | for (int x = start; x<= stop ; x+=step) { |
el13gs | 6:289f237b8d90 | 742 | // do linear interpolation |
el13gs | 6:289f237b8d90 | 743 | int y = y0 + (y1-y0)*(x-x0)/(x1-x0); |
el13gs | 8:699055e89c7d | 744 | table[x][y]=0; // set Table Cell to 0 |
el13gs | 6:289f237b8d90 | 745 | } |
el13gs | 6:289f237b8d90 | 746 | } else { |
el13gs | 6:289f237b8d90 | 747 | // ensure we loop from smallest to largest or else for-loop won't run as expected |
el13gs | 6:289f237b8d90 | 748 | start = y1>y0 ? y0:y1; |
el13gs | 6:289f237b8d90 | 749 | stop = y1>y0 ? y1:y0; |
el13gs | 6:289f237b8d90 | 750 | |
el13gs | 6:289f237b8d90 | 751 | for (int y = start; y<= stop ; y+=step) { |
el13gs | 6:289f237b8d90 | 752 | // do linear interpolation |
el13gs | 6:289f237b8d90 | 753 | int x = x0 + (x1-x0)*(y-y0)/(y1-y0); |
el13gs | 8:699055e89c7d | 754 | table[x][y]=0; // set Table Cell to 0 |
el13gs | 6:289f237b8d90 | 755 | } |
el13gs | 6:289f237b8d90 | 756 | } |
el13gs | 6:289f237b8d90 | 757 | } |
el13gs | 8:699055e89c7d | 758 |