![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Asteroids
cometCrusher.cpp@3:520df1778717, 2016-05-05 (annotated)
- Committer:
- el15s3p
- Date:
- Thu May 05 14:59:03 2016 +0000
- Revision:
- 3:520df1778717
Asteroids like game
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el15s3p | 3:520df1778717 | 1 | #include "mbed.h" |
el15s3p | 3:520df1778717 | 2 | #include "N5110.h" |
el15s3p | 3:520df1778717 | 3 | |
el15s3p | 3:520df1778717 | 4 | #define DIRECTION_TOLERANCE 0.05L |
el15s3p | 3:520df1778717 | 5 | |
el15s3p | 3:520df1778717 | 6 | // VCC, SCE, RST, D/C, MOSI, SCLK, LED |
el15s3p | 3:520df1778717 | 7 | N5110 lcd (PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3); |
el15s3p | 3:520df1778717 | 8 | |
el15s3p | 3:520df1778717 | 9 | // connections for joystick |
el15s3p | 3:520df1778717 | 10 | DigitalIn button(PTB11); |
el15s3p | 3:520df1778717 | 11 | AnalogIn xPot(PTB2); |
el15s3p | 3:520df1778717 | 12 | AnalogIn yPot(PTB3); |
el15s3p | 3:520df1778717 | 13 | |
el15s3p | 3:520df1778717 | 14 | AnalogIn Pot1(PTB10); //Declaring the variable resistor as an AnalogIn |
el15s3p | 3:520df1778717 | 15 | |
el15s3p | 3:520df1778717 | 16 | PwmOut led1(PTC2);//Declaring that led 1 is used as a PwmOut |
el15s3p | 3:520df1778717 | 17 | |
el15s3p | 3:520df1778717 | 18 | //InterruptIn sw2 (SW2); |
el15s3p | 3:520df1778717 | 19 | |
el15s3p | 3:520df1778717 | 20 | DigitalIn buttonA(PTB18); //Declaring that button A is used as a DigitalIn |
el15s3p | 3:520df1778717 | 21 | DigitalIn buttonB(PTB19); //Declaring that button B is used as a DigitalIn |
el15s3p | 3:520df1778717 | 22 | |
el15s3p | 3:520df1778717 | 23 | PwmOut buzzer(PTA2);//Declaring that buzzer is used as a PwmOut |
el15s3p | 3:520df1778717 | 24 | |
el15s3p | 3:520df1778717 | 25 | //volatile int g_sw2_flag = 0; |
el15s3p | 3:520df1778717 | 26 | |
el15s3p | 3:520df1778717 | 27 | // timer to regularly read the joystick |
el15s3p | 3:520df1778717 | 28 | Ticker pollJoystick; |
el15s3p | 3:520df1778717 | 29 | |
el15s3p | 3:520df1778717 | 30 | // Giving the names of the direction of the joystick |
el15s3p | 3:520df1778717 | 31 | enum DirectionName { |
el15s3p | 3:520df1778717 | 32 | UP, |
el15s3p | 3:520df1778717 | 33 | DOWN, |
el15s3p | 3:520df1778717 | 34 | LEFT, |
el15s3p | 3:520df1778717 | 35 | RIGHT, |
el15s3p | 3:520df1778717 | 36 | CENTRE, |
el15s3p | 3:520df1778717 | 37 | UNKNOWN |
el15s3p | 3:520df1778717 | 38 | }; |
el15s3p | 3:520df1778717 | 39 | |
el15s3p | 3:520df1778717 | 40 | // struct for Joystick |
el15s3p | 3:520df1778717 | 41 | typedef struct JoyStick Joystick; |
el15s3p | 3:520df1778717 | 42 | struct JoyStick { |
el15s3p | 3:520df1778717 | 43 | float x; // current x value |
el15s3p | 3:520df1778717 | 44 | float x0; // 'centred' x value |
el15s3p | 3:520df1778717 | 45 | float y; // current y value |
el15s3p | 3:520df1778717 | 46 | float y0; // 'centred' y value |
el15s3p | 3:520df1778717 | 47 | int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed) |
el15s3p | 3:520df1778717 | 48 | int buttonA; // buttonA state (assume pull-down used, so 1 = pressed, 0 = unpressed) |
el15s3p | 3:520df1778717 | 49 | int buttonB; // buttonB state (assume pull-down used, so 1 = pressed, 0 = unpressed) |
el15s3p | 3:520df1778717 | 50 | DirectionName direction; // current direction |
el15s3p | 3:520df1778717 | 51 | }; |
el15s3p | 3:520df1778717 | 52 | // create struct variable |
el15s3p | 3:520df1778717 | 53 | Joystick joystick; |
el15s3p | 3:520df1778717 | 54 | |
el15s3p | 3:520df1778717 | 55 | int printFlag = 0; |
el15s3p | 3:520df1778717 | 56 | |
el15s3p | 3:520df1778717 | 57 | // function prototypes |
el15s3p | 3:520df1778717 | 58 | void calibrateJoystick(); //Declaring the calibration of joystick void |
el15s3p | 3:520df1778717 | 59 | void updateJoystick(); //Declaring the updating of the joystick displacement void |
el15s3p | 3:520df1778717 | 60 | |
el15s3p | 3:520df1778717 | 61 | //void sw2_isr(); |
el15s3p | 3:520df1778717 | 62 | |
el15s3p | 3:520df1778717 | 63 | void menu(); //Declaring the menu void |
el15s3p | 3:520df1778717 | 64 | void game();//Declaring the game void |
el15s3p | 3:520df1778717 | 65 | void difficultySet();//Declaring the difficulty setting void |
el15s3p | 3:520df1778717 | 66 | void highScore();//Declaring the highscore displaying void |
el15s3p | 3:520df1778717 | 67 | void instructions();//Declaring the instructions display void |
el15s3p | 3:520df1778717 | 68 | void ship();//Declaring the ship displaying void |
el15s3p | 3:520df1778717 | 69 | void bullets();//Declaring the bullets displaying void |
el15s3p | 3:520df1778717 | 70 | void comets();//Declaring the comet displaying void |
el15s3p | 3:520df1778717 | 71 | |
el15s3p | 3:520df1778717 | 72 | |
el15s3p | 3:520df1778717 | 73 | int menuSelect = 2; //Default value of menuSelect so cursor is already on "Start" option |
el15s3p | 3:520df1778717 | 74 | int menuUp = 1;//Default value for indicator to show menu is up |
el15s3p | 3:520df1778717 | 75 | int gameUp = 0; //Default value to show if game is playing |
el15s3p | 3:520df1778717 | 76 | int instructionsPage = 0; //Default value to show what page of instructions is up |
el15s3p | 3:520df1778717 | 77 | int shipDirection = 0; //Default value to decide with direction the space ship is facing |
el15s3p | 3:520df1778717 | 78 | int gameOver = 0; //Default value to say when the game is over |
el15s3p | 3:520df1778717 | 79 | int cometMoveX = 0; //Default value to the X axis movement of the comets |
el15s3p | 3:520df1778717 | 80 | int cometMoveY = 0; //Default value to the Y axis movement of the comets |
el15s3p | 3:520df1778717 | 81 | int bulletMoveX = 0; //Default value to the X axis movement of the bullets |
el15s3p | 3:520df1778717 | 82 | int bulletMoveY = 0; //Default value to the Y axis movement of the bullets |
el15s3p | 3:520df1778717 | 83 | |
el15s3p | 3:520df1778717 | 84 | int main() |
el15s3p | 3:520df1778717 | 85 | { |
el15s3p | 3:520df1778717 | 86 | while(1) { |
el15s3p | 3:520df1778717 | 87 | calibrateJoystick(); // get centred values of joystick |
el15s3p | 3:520df1778717 | 88 | pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second |
el15s3p | 3:520df1778717 | 89 | while(menuUp == 1) { |
el15s3p | 3:520df1778717 | 90 | menu(); |
el15s3p | 3:520df1778717 | 91 | } |
el15s3p | 3:520df1778717 | 92 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 93 | } |
el15s3p | 3:520df1778717 | 94 | } |
el15s3p | 3:520df1778717 | 95 | |
el15s3p | 3:520df1778717 | 96 | void menu() |
el15s3p | 3:520df1778717 | 97 | { |
el15s3p | 3:520df1778717 | 98 | calibrateJoystick(); // get centred values of joystick |
el15s3p | 3:520df1778717 | 99 | pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second |
el15s3p | 3:520df1778717 | 100 | |
el15s3p | 3:520df1778717 | 101 | lcd.init(); |
el15s3p | 3:520df1778717 | 102 | lcd.clear(); |
el15s3p | 3:520df1778717 | 103 | lcd.normalMode(); |
el15s3p | 3:520df1778717 | 104 | //Displaying the main menu and showing the name of the game |
el15s3p | 3:520df1778717 | 105 | lcd.printString("Comet Crusher!",0,0); |
el15s3p | 3:520df1778717 | 106 | lcd.printString("Start",10,2); |
el15s3p | 3:520df1778717 | 107 | lcd.printString("Instructions",10,3); |
el15s3p | 3:520df1778717 | 108 | lcd.printString("Difficulty",10,4); |
el15s3p | 3:520df1778717 | 109 | lcd.printString("Scores",10,5); |
el15s3p | 3:520df1778717 | 110 | lcd.printString(">",5,menuSelect); //this is the cursor to show what is being selected |
el15s3p | 3:520df1778717 | 111 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 112 | sleep(); |
el15s3p | 3:520df1778717 | 113 | |
el15s3p | 3:520df1778717 | 114 | //the switch case allows you to move the cursor depending on the displacement of the joystick |
el15s3p | 3:520df1778717 | 115 | switch (menuSelect) { |
el15s3p | 3:520df1778717 | 116 | case 2: |
el15s3p | 3:520df1778717 | 117 | if (joystick.direction == DOWN) { |
el15s3p | 3:520df1778717 | 118 | menuSelect = 3; |
el15s3p | 3:520df1778717 | 119 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 120 | wait (0.2); |
el15s3p | 3:520df1778717 | 121 | sleep(); |
el15s3p | 3:520df1778717 | 122 | } else if (joystick.direction == UP) { |
el15s3p | 3:520df1778717 | 123 | menuSelect = 5; |
el15s3p | 3:520df1778717 | 124 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 125 | wait (0.2); |
el15s3p | 3:520df1778717 | 126 | sleep(); |
el15s3p | 3:520df1778717 | 127 | } |
el15s3p | 3:520df1778717 | 128 | if (buttonA == 1) { |
el15s3p | 3:520df1778717 | 129 | menuUp = 0; |
el15s3p | 3:520df1778717 | 130 | gameUp = 1; |
el15s3p | 3:520df1778717 | 131 | game(); |
el15s3p | 3:520df1778717 | 132 | } |
el15s3p | 3:520df1778717 | 133 | break; |
el15s3p | 3:520df1778717 | 134 | case 3: |
el15s3p | 3:520df1778717 | 135 | if (joystick.direction == DOWN) { |
el15s3p | 3:520df1778717 | 136 | menuSelect = 4; |
el15s3p | 3:520df1778717 | 137 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 138 | wait (0.2); |
el15s3p | 3:520df1778717 | 139 | sleep(); |
el15s3p | 3:520df1778717 | 140 | } else if (joystick.direction == UP) { |
el15s3p | 3:520df1778717 | 141 | menuSelect = 2; |
el15s3p | 3:520df1778717 | 142 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 143 | wait (0.2); |
el15s3p | 3:520df1778717 | 144 | sleep(); |
el15s3p | 3:520df1778717 | 145 | } |
el15s3p | 3:520df1778717 | 146 | if (buttonA == 1) { |
el15s3p | 3:520df1778717 | 147 | menuUp = 0; |
el15s3p | 3:520df1778717 | 148 | instructions(); |
el15s3p | 3:520df1778717 | 149 | } |
el15s3p | 3:520df1778717 | 150 | break; |
el15s3p | 3:520df1778717 | 151 | case 4: |
el15s3p | 3:520df1778717 | 152 | if (joystick.direction == DOWN) { |
el15s3p | 3:520df1778717 | 153 | menuSelect = 5; |
el15s3p | 3:520df1778717 | 154 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 155 | wait (0.2); |
el15s3p | 3:520df1778717 | 156 | sleep(); |
el15s3p | 3:520df1778717 | 157 | } else if (joystick.direction == UP) { |
el15s3p | 3:520df1778717 | 158 | menuSelect = 3; |
el15s3p | 3:520df1778717 | 159 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 160 | wait (0.2); |
el15s3p | 3:520df1778717 | 161 | sleep(); |
el15s3p | 3:520df1778717 | 162 | } |
el15s3p | 3:520df1778717 | 163 | if (buttonA == 1) { |
el15s3p | 3:520df1778717 | 164 | menuUp = 0; |
el15s3p | 3:520df1778717 | 165 | difficultySet(); |
el15s3p | 3:520df1778717 | 166 | } |
el15s3p | 3:520df1778717 | 167 | break; |
el15s3p | 3:520df1778717 | 168 | case 5: |
el15s3p | 3:520df1778717 | 169 | if (joystick.direction == DOWN) { |
el15s3p | 3:520df1778717 | 170 | menuSelect = 2; |
el15s3p | 3:520df1778717 | 171 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 172 | wait (0.2); |
el15s3p | 3:520df1778717 | 173 | sleep(); |
el15s3p | 3:520df1778717 | 174 | } else if (joystick.direction == UP) { |
el15s3p | 3:520df1778717 | 175 | menuSelect = 4; |
el15s3p | 3:520df1778717 | 176 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 177 | wait (0.2); |
el15s3p | 3:520df1778717 | 178 | sleep(); |
el15s3p | 3:520df1778717 | 179 | } |
el15s3p | 3:520df1778717 | 180 | if (buttonA == 1) { |
el15s3p | 3:520df1778717 | 181 | menuUp = 0; |
el15s3p | 3:520df1778717 | 182 | highScore(); |
el15s3p | 3:520df1778717 | 183 | } |
el15s3p | 3:520df1778717 | 184 | break; |
el15s3p | 3:520df1778717 | 185 | } |
el15s3p | 3:520df1778717 | 186 | } |
el15s3p | 3:520df1778717 | 187 | void game() |
el15s3p | 3:520df1778717 | 188 | { |
el15s3p | 3:520df1778717 | 189 | // SW2 has a pull-up resistor, so the pin will be at 3.3 V by default |
el15s3p | 3:520df1778717 | 190 | // and fall to 0 V when pressed. We therefore need to look for a falling edge |
el15s3p | 3:520df1778717 | 191 | // on the pin to fire the interrupt |
el15s3p | 3:520df1778717 | 192 | //sw2.fall(&sw2_isr); |
el15s3p | 3:520df1778717 | 193 | //since SW2 has an external pull-up, we should disable to internal pull-down |
el15s3p | 3:520df1778717 | 194 | // resistor that is enabled by default using InterruptIn |
el15s3p | 3:520df1778717 | 195 | //sw2.mode(PullNone); |
el15s3p | 3:520df1778717 | 196 | |
el15s3p | 3:520df1778717 | 197 | while(gameUp) { |
el15s3p | 3:520df1778717 | 198 | lcd.clear(); //Clear menu and starts game |
el15s3p | 3:520df1778717 | 199 | lcd.inverseMode(); |
el15s3p | 3:520df1778717 | 200 | ship(); |
el15s3p | 3:520df1778717 | 201 | comets(); |
el15s3p | 3:520df1778717 | 202 | /*if (g_sw2_flag == 1) { |
el15s3p | 3:520df1778717 | 203 | gameUp = 0; |
el15s3p | 3:520df1778717 | 204 | lcd.printString("pause",20,3); |
el15s3p | 3:520df1778717 | 205 | if(g_sw2_flag == 0) { |
el15s3p | 3:520df1778717 | 206 | gameUp = 1; |
el15s3p | 3:520df1778717 | 207 | } |
el15s3p | 3:520df1778717 | 208 | }*/ |
el15s3p | 3:520df1778717 | 209 | } |
el15s3p | 3:520df1778717 | 210 | |
el15s3p | 3:520df1778717 | 211 | } |
el15s3p | 3:520df1778717 | 212 | |
el15s3p | 3:520df1778717 | 213 | //This is for the difficulty setting page to decide how hard the game play will be |
el15s3p | 3:520df1778717 | 214 | void difficultySet() |
el15s3p | 3:520df1778717 | 215 | { |
el15s3p | 3:520df1778717 | 216 | lcd.clear(); |
el15s3p | 3:520df1778717 | 217 | lcd.printString("Difficulty:",0,0); |
el15s3p | 3:520df1778717 | 218 | while(!menuUp) { |
el15s3p | 3:520df1778717 | 219 | if (buttonB == 1) { |
el15s3p | 3:520df1778717 | 220 | menuUp = 1; |
el15s3p | 3:520df1778717 | 221 | menu(); |
el15s3p | 3:520df1778717 | 222 | } |
el15s3p | 3:520df1778717 | 223 | } |
el15s3p | 3:520df1778717 | 224 | } |
el15s3p | 3:520df1778717 | 225 | |
el15s3p | 3:520df1778717 | 226 | //This is for the highscores that people have gotten on the game |
el15s3p | 3:520df1778717 | 227 | void highScore() |
el15s3p | 3:520df1778717 | 228 | { |
el15s3p | 3:520df1778717 | 229 | lcd.clear(); |
el15s3p | 3:520df1778717 | 230 | lcd.printString("HighScores:",0,0); |
el15s3p | 3:520df1778717 | 231 | lcd.printString("1)",0,1); |
el15s3p | 3:520df1778717 | 232 | lcd.printString("2)",0,2); |
el15s3p | 3:520df1778717 | 233 | lcd.printString("3)",0,3); |
el15s3p | 3:520df1778717 | 234 | while(!menuUp) { |
el15s3p | 3:520df1778717 | 235 | if (buttonB == 1) { |
el15s3p | 3:520df1778717 | 236 | menuUp = 1; |
el15s3p | 3:520df1778717 | 237 | menu(); |
el15s3p | 3:520df1778717 | 238 | } |
el15s3p | 3:520df1778717 | 239 | } |
el15s3p | 3:520df1778717 | 240 | } |
el15s3p | 3:520df1778717 | 241 | |
el15s3p | 3:520df1778717 | 242 | //this displays the instructions on how to play the game |
el15s3p | 3:520df1778717 | 243 | void instructions() |
el15s3p | 3:520df1778717 | 244 | { |
el15s3p | 3:520df1778717 | 245 | switch(instructionsPage) { |
el15s3p | 3:520df1778717 | 246 | case 0: |
el15s3p | 3:520df1778717 | 247 | lcd.clear(); |
el15s3p | 3:520df1778717 | 248 | lcd.printString(" Game controls:",0,0); |
el15s3p | 3:520df1778717 | 249 | lcd.printString("Joystick L/R =",5,1); |
el15s3p | 3:520df1778717 | 250 | lcd.printString("rotate ship",10,2); |
el15s3p | 3:520df1778717 | 251 | lcd.printString("Button A =",5,3); |
el15s3p | 3:520df1778717 | 252 | lcd.printString("shoot",10,4); |
el15s3p | 3:520df1778717 | 253 | lcd.printString("next page->",0,5); |
el15s3p | 3:520df1778717 | 254 | break; |
el15s3p | 3:520df1778717 | 255 | case 1: |
el15s3p | 3:520df1778717 | 256 | lcd.clear(); |
el15s3p | 3:520df1778717 | 257 | lcd.printString("Game controls:",0,0); |
el15s3p | 3:520df1778717 | 258 | lcd.printString("Button B =",5,1); |
el15s3p | 3:520df1778717 | 259 | lcd.printString("block",10,2); |
el15s3p | 3:520df1778717 | 260 | lcd.printString("next page->",0,5); |
el15s3p | 3:520df1778717 | 261 | break; |
el15s3p | 3:520df1778717 | 262 | case 2: |
el15s3p | 3:520df1778717 | 263 | lcd.printString("Diff control:",0,0); |
el15s3p | 3:520df1778717 | 264 | lcd.printString("Variable res =",5,1); |
el15s3p | 3:520df1778717 | 265 | lcd.printString("change diff",10,2); |
el15s3p | 3:520df1778717 | 266 | lcd.printString("First page->",0,5); |
el15s3p | 3:520df1778717 | 267 | |
el15s3p | 3:520df1778717 | 268 | while(!menuUp) { |
el15s3p | 3:520df1778717 | 269 | if(buttonA == 1) { |
el15s3p | 3:520df1778717 | 270 | for(instructionsPage = 0; instructionsPage < 3; instructionsPage ++) { |
el15s3p | 3:520df1778717 | 271 | instructionsPage =instructionsPage + 1; |
el15s3p | 3:520df1778717 | 272 | } |
el15s3p | 3:520df1778717 | 273 | } |
el15s3p | 3:520df1778717 | 274 | if (buttonB == 1) { |
el15s3p | 3:520df1778717 | 275 | menuUp = 1; |
el15s3p | 3:520df1778717 | 276 | menu(); |
el15s3p | 3:520df1778717 | 277 | } |
el15s3p | 3:520df1778717 | 278 | } |
el15s3p | 3:520df1778717 | 279 | } |
el15s3p | 3:520df1778717 | 280 | } |
el15s3p | 3:520df1778717 | 281 | //this will display the ship when game is playing and will let you be able to change the direction of the ship |
el15s3p | 3:520df1778717 | 282 | void ship() |
el15s3p | 3:520df1778717 | 283 | { |
el15s3p | 3:520df1778717 | 284 | calibrateJoystick(); // get centred values of joystick |
el15s3p | 3:520df1778717 | 285 | pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second |
el15s3p | 3:520df1778717 | 286 | |
el15s3p | 3:520df1778717 | 287 | int shipShape1 [7][11] = { |
el15s3p | 3:520df1778717 | 288 | {0,0,0,0,0,0,0,0,1,1,1}, |
el15s3p | 3:520df1778717 | 289 | {0,0,0,0,0,1,1,1,1,1,0}, |
el15s3p | 3:520df1778717 | 290 | {0,0,1,1,1,1,1,1,1,0,0}, |
el15s3p | 3:520df1778717 | 291 | {1,1,1,1,1,1,1,1,0,0,0}, |
el15s3p | 3:520df1778717 | 292 | {0,0,1,1,1,1,1,1,1,0,0}, |
el15s3p | 3:520df1778717 | 293 | {0,0,0,0,0,1,1,1,1,1,0}, |
el15s3p | 3:520df1778717 | 294 | {0,0,0,0,0,0,0,0,1,1,1} |
el15s3p | 3:520df1778717 | 295 | }; |
el15s3p | 3:520df1778717 | 296 | int shipShape2 [10][11] = { |
el15s3p | 3:520df1778717 | 297 | {0,0,0,0,0,0,1,0,0,0,0}, |
el15s3p | 3:520df1778717 | 298 | {0,0,0,0,0,1,1,0,0,0,0}, |
el15s3p | 3:520df1778717 | 299 | {0,0,0,0,1,1,1,0,0,0,0}, |
el15s3p | 3:520df1778717 | 300 | {0,0,0,0,1,1,1,0,0,0,0}, |
el15s3p | 3:520df1778717 | 301 | {0,0,0,1,1,1,1,1,1,1,1}, |
el15s3p | 3:520df1778717 | 302 | {0,0,0,1,1,1,1,1,1,1,0}, |
el15s3p | 3:520df1778717 | 303 | {0,0,1,1,1,1,1,1,1,0,0}, |
el15s3p | 3:520df1778717 | 304 | {0,0,1,1,1,1,1,0,0,0,0}, |
el15s3p | 3:520df1778717 | 305 | {0,1,1,1,1,0,0,0,0,0,0}, |
el15s3p | 3:520df1778717 | 306 | {0,1,1,0,0,0,0,0,0,0,0} |
el15s3p | 3:520df1778717 | 307 | |
el15s3p | 3:520df1778717 | 308 | }; |
el15s3p | 3:520df1778717 | 309 | int shipShape3 [11][7] = { |
el15s3p | 3:520df1778717 | 310 | {1,0,0,0,0,0,1}, |
el15s3p | 3:520df1778717 | 311 | {1,1,0,0,0,1,1}, |
el15s3p | 3:520df1778717 | 312 | {1,1,1,0,1,1,1}, |
el15s3p | 3:520df1778717 | 313 | {0,1,1,1,1,1,0}, |
el15s3p | 3:520df1778717 | 314 | {0,1,1,1,1,1,0}, |
el15s3p | 3:520df1778717 | 315 | {0,1,1,1,1,1,0}, |
el15s3p | 3:520df1778717 | 316 | {0,0,1,1,1,0,0}, |
el15s3p | 3:520df1778717 | 317 | {0,0,1,1,1,0,0}, |
el15s3p | 3:520df1778717 | 318 | {0,0,1,1,1,0,0}, |
el15s3p | 3:520df1778717 | 319 | {0,0,0,1,0,0,0}, |
el15s3p | 3:520df1778717 | 320 | {0,0,0,1,0,0,0} |
el15s3p | 3:520df1778717 | 321 | }; |
el15s3p | 3:520df1778717 | 322 | int shipShape4 [10][11] = { |
el15s3p | 3:520df1778717 | 323 | {0,0,0,0,1,0,0,0,0,0,0}, |
el15s3p | 3:520df1778717 | 324 | {0,0,0,0,1,1,0,0,0,0,0}, |
el15s3p | 3:520df1778717 | 325 | {0,0,0,0,1,1,1,0,0,0,0}, |
el15s3p | 3:520df1778717 | 326 | {0,0,0,0,1,1,1,0,0,0,0}, |
el15s3p | 3:520df1778717 | 327 | {1,1,1,1,1,1,1,1,0,0,0}, |
el15s3p | 3:520df1778717 | 328 | {0,1,1,1,1,1,1,1,0,0,0}, |
el15s3p | 3:520df1778717 | 329 | {0,0,1,1,1,1,1,1,1,0,0}, |
el15s3p | 3:520df1778717 | 330 | {0,0,0,0,1,1,1,1,1,0,0}, |
el15s3p | 3:520df1778717 | 331 | {0,0,0,0,0,0,1,1,1,1,0}, |
el15s3p | 3:520df1778717 | 332 | {0,0,0,0,0,0,0,0,1,1,0} |
el15s3p | 3:520df1778717 | 333 | }; |
el15s3p | 3:520df1778717 | 334 | int shipShape5 [7][11] = { |
el15s3p | 3:520df1778717 | 335 | {1,1,1,0,0,0,0,0,0,0,0}, |
el15s3p | 3:520df1778717 | 336 | {0,1,1,1,1,1,0,0,0,0,0}, |
el15s3p | 3:520df1778717 | 337 | {0,0,1,1,1,1,1,1,1,0,0}, |
el15s3p | 3:520df1778717 | 338 | {0,0,0,1,1,1,1,1,1,1,1}, |
el15s3p | 3:520df1778717 | 339 | {0,0,1,1,1,1,1,1,1,0,0}, |
el15s3p | 3:520df1778717 | 340 | {0,1,1,1,1,1,0,0,0,0,0}, |
el15s3p | 3:520df1778717 | 341 | {1,1,1,0,0,0,0,0,0,0,0} |
el15s3p | 3:520df1778717 | 342 | }; |
el15s3p | 3:520df1778717 | 343 | int shipShape6 [10][11] = { |
el15s3p | 3:520df1778717 | 344 | {0,0,0,0,0,0,0,0,1,1,0}, |
el15s3p | 3:520df1778717 | 345 | {0,0,0,0,0,0,1,1,1,1,0}, |
el15s3p | 3:520df1778717 | 346 | {0,0,0,0,1,1,1,1,1,0,0}, |
el15s3p | 3:520df1778717 | 347 | {0,0,1,1,1,1,1,1,1,0,0}, |
el15s3p | 3:520df1778717 | 348 | {0,1,1,1,1,1,1,1,0,0,0}, |
el15s3p | 3:520df1778717 | 349 | {1,1,1,1,1,1,1,1,0,0,0}, |
el15s3p | 3:520df1778717 | 350 | {0,0,0,0,1,1,1,0,0,0,0}, |
el15s3p | 3:520df1778717 | 351 | {0,0,0,0,1,1,1,0,0,0,0}, |
el15s3p | 3:520df1778717 | 352 | {0,0,0,0,1,1,0,0,0,0,0}, |
el15s3p | 3:520df1778717 | 353 | {0,0,0,0,1,0,0,0,0,0,0} |
el15s3p | 3:520df1778717 | 354 | }; |
el15s3p | 3:520df1778717 | 355 | int shipShape7 [11][7] = { |
el15s3p | 3:520df1778717 | 356 | {0,0,0,1,0,0,0}, |
el15s3p | 3:520df1778717 | 357 | {0,0,0,1,0,0,0}, |
el15s3p | 3:520df1778717 | 358 | {0,0,1,1,1,0,0}, |
el15s3p | 3:520df1778717 | 359 | {0,0,1,1,1,0,0}, |
el15s3p | 3:520df1778717 | 360 | {0,0,1,1,1,0,0}, |
el15s3p | 3:520df1778717 | 361 | {0,1,1,1,1,1,0}, |
el15s3p | 3:520df1778717 | 362 | {0,1,1,1,1,1,0}, |
el15s3p | 3:520df1778717 | 363 | {0,1,1,1,1,1,0}, |
el15s3p | 3:520df1778717 | 364 | {1,1,1,0,1,1,1}, |
el15s3p | 3:520df1778717 | 365 | {1,1,0,0,0,1,1}, |
el15s3p | 3:520df1778717 | 366 | {1,0,0,0,0,0,1} |
el15s3p | 3:520df1778717 | 367 | }; |
el15s3p | 3:520df1778717 | 368 | int shipShape8 [10][11] = { |
el15s3p | 3:520df1778717 | 369 | {0,1,1,0,0,0,0,0,0,0,0}, |
el15s3p | 3:520df1778717 | 370 | {0,1,1,1,1,0,0,0,0,0,0}, |
el15s3p | 3:520df1778717 | 371 | {0,0,1,1,1,1,1,0,0,0,0}, |
el15s3p | 3:520df1778717 | 372 | {0,0,1,1,1,1,1,1,0,0,0}, |
el15s3p | 3:520df1778717 | 373 | {0,0,0,1,1,1,1,1,1,1,0}, |
el15s3p | 3:520df1778717 | 374 | {0,0,0,1,1,1,1,1,1,1,1}, |
el15s3p | 3:520df1778717 | 375 | {0,0,0,0,1,1,1,0,0,0,0}, |
el15s3p | 3:520df1778717 | 376 | {0,0,0,0,0,1,1,0,0,0,0}, |
el15s3p | 3:520df1778717 | 377 | {0,0,0,0,0,1,1,0,0,0,0}, |
el15s3p | 3:520df1778717 | 378 | {0,0,0,0,0,0,1,0,0,0,0} |
el15s3p | 3:520df1778717 | 379 | }; |
el15s3p | 3:520df1778717 | 380 | |
el15s3p | 3:520df1778717 | 381 | |
el15s3p | 3:520df1778717 | 382 | while(gameUp) { |
el15s3p | 3:520df1778717 | 383 | lcd.printString("Score:",0,0); |
el15s3p | 3:520df1778717 | 384 | |
el15s3p | 3:520df1778717 | 385 | if (joystick.direction == RIGHT) { |
el15s3p | 3:520df1778717 | 386 | if (shipDirection < 7) { |
el15s3p | 3:520df1778717 | 387 | shipDirection = shipDirection + 1; |
el15s3p | 3:520df1778717 | 388 | } else if (shipDirection == 7) { |
el15s3p | 3:520df1778717 | 389 | shipDirection = 0; |
el15s3p | 3:520df1778717 | 390 | } |
el15s3p | 3:520df1778717 | 391 | } else if (joystick.direction == LEFT) { |
el15s3p | 3:520df1778717 | 392 | if (shipDirection > 0) { |
el15s3p | 3:520df1778717 | 393 | shipDirection = shipDirection - 1; |
el15s3p | 3:520df1778717 | 394 | } else if(shipDirection == 0) { |
el15s3p | 3:520df1778717 | 395 | shipDirection = 7; |
el15s3p | 3:520df1778717 | 396 | } |
el15s3p | 3:520df1778717 | 397 | } |
el15s3p | 3:520df1778717 | 398 | |
el15s3p | 3:520df1778717 | 399 | switch(shipDirection) { |
el15s3p | 3:520df1778717 | 400 | case 0: |
el15s3p | 3:520df1778717 | 401 | lcd.clear(); |
el15s3p | 3:520df1778717 | 402 | lcd.printString("Score:",0,0); |
el15s3p | 3:520df1778717 | 403 | for(int shipR = 0; shipR <= 6; shipR ++) { |
el15s3p | 3:520df1778717 | 404 | for(int shipC = 0; shipC <= 10; shipC ++) { |
el15s3p | 3:520df1778717 | 405 | if(shipShape1[shipR][shipC] == 0) { |
el15s3p | 3:520df1778717 | 406 | lcd.clearPixel(shipR + 38,shipC + 18); |
el15s3p | 3:520df1778717 | 407 | } else if(shipShape1[shipR][shipC] == 1) { |
el15s3p | 3:520df1778717 | 408 | lcd.setPixel(shipR + 38,shipC + 18); |
el15s3p | 3:520df1778717 | 409 | } |
el15s3p | 3:520df1778717 | 410 | } |
el15s3p | 3:520df1778717 | 411 | } |
el15s3p | 3:520df1778717 | 412 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 413 | break; |
el15s3p | 3:520df1778717 | 414 | case 1: |
el15s3p | 3:520df1778717 | 415 | lcd.clear(); |
el15s3p | 3:520df1778717 | 416 | lcd.printString("Score:",0,0); |
el15s3p | 3:520df1778717 | 417 | for(int shipR = 0; shipR <= 9; shipR ++) { |
el15s3p | 3:520df1778717 | 418 | for(int shipC = 0; shipC <= 10; shipC ++) { |
el15s3p | 3:520df1778717 | 419 | if(shipShape2[shipR][shipC] == 0) { |
el15s3p | 3:520df1778717 | 420 | lcd.clearPixel(shipR + 38,shipC + 18); |
el15s3p | 3:520df1778717 | 421 | } else if(shipShape2[shipR][shipC] == 1) { |
el15s3p | 3:520df1778717 | 422 | lcd.setPixel(shipR + 38,shipC + 18); |
el15s3p | 3:520df1778717 | 423 | } |
el15s3p | 3:520df1778717 | 424 | } |
el15s3p | 3:520df1778717 | 425 | } |
el15s3p | 3:520df1778717 | 426 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 427 | break; |
el15s3p | 3:520df1778717 | 428 | case 2: |
el15s3p | 3:520df1778717 | 429 | lcd.clear(); |
el15s3p | 3:520df1778717 | 430 | lcd.printString("Score:",0,0); |
el15s3p | 3:520df1778717 | 431 | for(int shipR = 0; shipR <= 10; shipR ++) { |
el15s3p | 3:520df1778717 | 432 | for(int shipC = 0; shipC <= 6; shipC ++) { |
el15s3p | 3:520df1778717 | 433 | if(shipShape3[shipR][shipC] == 0) { |
el15s3p | 3:520df1778717 | 434 | lcd.clearPixel(shipR + 36,shipC + 20); |
el15s3p | 3:520df1778717 | 435 | } else if(shipShape3[shipR][shipC] == 1) { |
el15s3p | 3:520df1778717 | 436 | lcd.setPixel(shipR + 36,shipC + 20); |
el15s3p | 3:520df1778717 | 437 | } |
el15s3p | 3:520df1778717 | 438 | } |
el15s3p | 3:520df1778717 | 439 | } |
el15s3p | 3:520df1778717 | 440 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 441 | break; |
el15s3p | 3:520df1778717 | 442 | case 3: |
el15s3p | 3:520df1778717 | 443 | lcd.clear(); |
el15s3p | 3:520df1778717 | 444 | lcd.printString("Score:",0,0); |
el15s3p | 3:520df1778717 | 445 | for(int shipR = 0; shipR <= 9; shipR ++) { |
el15s3p | 3:520df1778717 | 446 | for(int shipC = 0; shipC <= 10; shipC ++) { |
el15s3p | 3:520df1778717 | 447 | if(shipShape4[shipR][shipC] == 0) { |
el15s3p | 3:520df1778717 | 448 | lcd.clearPixel(shipR + 38,shipC + 18); |
el15s3p | 3:520df1778717 | 449 | } else if(shipShape4[shipR][shipC] == 1) { |
el15s3p | 3:520df1778717 | 450 | lcd.setPixel(shipR + 38,shipC + 18); |
el15s3p | 3:520df1778717 | 451 | } |
el15s3p | 3:520df1778717 | 452 | } |
el15s3p | 3:520df1778717 | 453 | } |
el15s3p | 3:520df1778717 | 454 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 455 | break; |
el15s3p | 3:520df1778717 | 456 | case 4: |
el15s3p | 3:520df1778717 | 457 | lcd.clear(); |
el15s3p | 3:520df1778717 | 458 | lcd.printString("Score:",0,0); |
el15s3p | 3:520df1778717 | 459 | for(int shipR = 0; shipR <= 6; shipR ++) { |
el15s3p | 3:520df1778717 | 460 | for(int shipC = 0; shipC <= 10; shipC ++) { |
el15s3p | 3:520df1778717 | 461 | if(shipShape5[shipR][shipC] == 0) { |
el15s3p | 3:520df1778717 | 462 | lcd.clearPixel(shipR + 38,shipC + 18); |
el15s3p | 3:520df1778717 | 463 | } else if(shipShape5[shipR][shipC] == 1) { |
el15s3p | 3:520df1778717 | 464 | lcd.setPixel(shipR + 38,shipC + 18); |
el15s3p | 3:520df1778717 | 465 | } |
el15s3p | 3:520df1778717 | 466 | } |
el15s3p | 3:520df1778717 | 467 | } |
el15s3p | 3:520df1778717 | 468 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 469 | break; |
el15s3p | 3:520df1778717 | 470 | case 5: |
el15s3p | 3:520df1778717 | 471 | lcd.clear(); |
el15s3p | 3:520df1778717 | 472 | lcd.printString("Score:",0,0); |
el15s3p | 3:520df1778717 | 473 | for(int shipR = 0; shipR <= 9; shipR ++) { |
el15s3p | 3:520df1778717 | 474 | for(int shipC = 0; shipC <= 10; shipC ++) { |
el15s3p | 3:520df1778717 | 475 | if(shipShape6[shipR][shipC] == 0) { |
el15s3p | 3:520df1778717 | 476 | lcd.clearPixel(shipR + 38,shipC + 18); |
el15s3p | 3:520df1778717 | 477 | } else if(shipShape6[shipR][shipC] == 1) { |
el15s3p | 3:520df1778717 | 478 | lcd.setPixel(shipR + 38,shipC + 18); |
el15s3p | 3:520df1778717 | 479 | } |
el15s3p | 3:520df1778717 | 480 | } |
el15s3p | 3:520df1778717 | 481 | } |
el15s3p | 3:520df1778717 | 482 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 483 | break; |
el15s3p | 3:520df1778717 | 484 | case 6: |
el15s3p | 3:520df1778717 | 485 | lcd.clear(); |
el15s3p | 3:520df1778717 | 486 | lcd.printString("Score:",0,0); |
el15s3p | 3:520df1778717 | 487 | for(int shipR = 0; shipR <= 10; shipR ++) { |
el15s3p | 3:520df1778717 | 488 | for(int shipC = 0; shipC <= 6; shipC ++) { |
el15s3p | 3:520df1778717 | 489 | if(shipShape7[shipR][shipC] == 0) { |
el15s3p | 3:520df1778717 | 490 | lcd.clearPixel(shipR + 36,shipC + 20); |
el15s3p | 3:520df1778717 | 491 | } else if(shipShape7[shipR][shipC] == 1) { |
el15s3p | 3:520df1778717 | 492 | lcd.setPixel(shipR + 36,shipC + 20); |
el15s3p | 3:520df1778717 | 493 | } |
el15s3p | 3:520df1778717 | 494 | } |
el15s3p | 3:520df1778717 | 495 | } |
el15s3p | 3:520df1778717 | 496 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 497 | break; |
el15s3p | 3:520df1778717 | 498 | case 7: |
el15s3p | 3:520df1778717 | 499 | lcd.clear(); |
el15s3p | 3:520df1778717 | 500 | lcd.printString("Score:",0,0); |
el15s3p | 3:520df1778717 | 501 | for(int shipR = 0; shipR <= 9; shipR ++) { |
el15s3p | 3:520df1778717 | 502 | for(int shipC = 0; shipC <= 10; shipC ++) { |
el15s3p | 3:520df1778717 | 503 | if(shipShape8[shipR][shipC] == 0) { |
el15s3p | 3:520df1778717 | 504 | lcd.clearPixel(shipR + 38,shipC + 18); |
el15s3p | 3:520df1778717 | 505 | } else if(shipShape8[shipR][shipC] == 1) { |
el15s3p | 3:520df1778717 | 506 | lcd.setPixel(shipR + 38,shipC + 18); |
el15s3p | 3:520df1778717 | 507 | } |
el15s3p | 3:520df1778717 | 508 | } |
el15s3p | 3:520df1778717 | 509 | } |
el15s3p | 3:520df1778717 | 510 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 511 | break; |
el15s3p | 3:520df1778717 | 512 | } |
el15s3p | 3:520df1778717 | 513 | sleep(); |
el15s3p | 3:520df1778717 | 514 | |
el15s3p | 3:520df1778717 | 515 | } |
el15s3p | 3:520df1778717 | 516 | } |
el15s3p | 3:520df1778717 | 517 | |
el15s3p | 3:520df1778717 | 518 | //this void is to display the bullets on when game is playing if button A is pressed |
el15s3p | 3:520df1778717 | 519 | void bullets() |
el15s3p | 3:520df1778717 | 520 | { |
el15s3p | 3:520df1778717 | 521 | switch(shipDirection) { |
el15s3p | 3:520df1778717 | 522 | case 0: |
el15s3p | 3:520df1778717 | 523 | for(bulletMoveY = 0; bulletMoveY >= 0; bulletMoveY++) { |
el15s3p | 3:520df1778717 | 524 | lcd.setPixel(41,18 + bulletMoveY); |
el15s3p | 3:520df1778717 | 525 | sleep(); |
el15s3p | 3:520df1778717 | 526 | } |
el15s3p | 3:520df1778717 | 527 | break; |
el15s3p | 3:520df1778717 | 528 | case 1: |
el15s3p | 3:520df1778717 | 529 | for(bulletMoveY = 0; bulletMoveY >= 0; bulletMoveY++) { |
el15s3p | 3:520df1778717 | 530 | lcd.setPixel(41,18 + bulletMoveY); |
el15s3p | 3:520df1778717 | 531 | sleep(); |
el15s3p | 3:520df1778717 | 532 | |
el15s3p | 3:520df1778717 | 533 | } |
el15s3p | 3:520df1778717 | 534 | break; |
el15s3p | 3:520df1778717 | 535 | case 2: |
el15s3p | 3:520df1778717 | 536 | for(bulletMoveY = 0; bulletMoveY >= 0; bulletMoveY++) { |
el15s3p | 3:520df1778717 | 537 | lcd.setPixel(44,23 + bulletMoveY); |
el15s3p | 3:520df1778717 | 538 | sleep(); |
el15s3p | 3:520df1778717 | 539 | } |
el15s3p | 3:520df1778717 | 540 | break; |
el15s3p | 3:520df1778717 | 541 | case 3: |
el15s3p | 3:520df1778717 | 542 | for(bulletMoveY = 0; bulletMoveY >= 0; bulletMoveY++) { |
el15s3p | 3:520df1778717 | 543 | lcd.setPixel(41,18 + bulletMoveY); |
el15s3p | 3:520df1778717 | 544 | sleep(); |
el15s3p | 3:520df1778717 | 545 | } |
el15s3p | 3:520df1778717 | 546 | break; |
el15s3p | 3:520df1778717 | 547 | case 4: |
el15s3p | 3:520df1778717 | 548 | for(bulletMoveY = 0; bulletMoveY >= 0; bulletMoveY++) { |
el15s3p | 3:520df1778717 | 549 | lcd.setPixel(41,28 + bulletMoveY); |
el15s3p | 3:520df1778717 | 550 | sleep(); |
el15s3p | 3:520df1778717 | 551 | } |
el15s3p | 3:520df1778717 | 552 | break; |
el15s3p | 3:520df1778717 | 553 | case 5: |
el15s3p | 3:520df1778717 | 554 | for(bulletMoveY = 0; bulletMoveY >= 0; bulletMoveY++) { |
el15s3p | 3:520df1778717 | 555 | lcd.setPixel(41,18 + bulletMoveY); |
el15s3p | 3:520df1778717 | 556 | sleep(); |
el15s3p | 3:520df1778717 | 557 | } |
el15s3p | 3:520df1778717 | 558 | break; |
el15s3p | 3:520df1778717 | 559 | case 6: |
el15s3p | 3:520df1778717 | 560 | for(bulletMoveY = 0; bulletMoveY >= 0; bulletMoveY++) { |
el15s3p | 3:520df1778717 | 561 | lcd.setPixel(38,23 + bulletMoveY); |
el15s3p | 3:520df1778717 | 562 | sleep(); |
el15s3p | 3:520df1778717 | 563 | } |
el15s3p | 3:520df1778717 | 564 | break; |
el15s3p | 3:520df1778717 | 565 | case 7: |
el15s3p | 3:520df1778717 | 566 | for(bulletMoveY = 0; bulletMoveY >= 0; bulletMoveY++) { |
el15s3p | 3:520df1778717 | 567 | lcd.setPixel(41,18 + bulletMoveY); |
el15s3p | 3:520df1778717 | 568 | sleep(); |
el15s3p | 3:520df1778717 | 569 | } |
el15s3p | 3:520df1778717 | 570 | break; |
el15s3p | 3:520df1778717 | 571 | } |
el15s3p | 3:520df1778717 | 572 | } |
el15s3p | 3:520df1778717 | 573 | |
el15s3p | 3:520df1778717 | 574 | //this void will create comets to display on the screen for the ship to destroy or for it to destroy the ship |
el15s3p | 3:520df1778717 | 575 | void comets() |
el15s3p | 3:520df1778717 | 576 | { |
el15s3p | 3:520df1778717 | 577 | while(gameUp) { |
el15s3p | 3:520df1778717 | 578 | int cometDirection = rand() % 8 + 1; |
el15s3p | 3:520df1778717 | 579 | |
el15s3p | 3:520df1778717 | 580 | int comet [11][11] = { |
el15s3p | 3:520df1778717 | 581 | {0,0,0,0,1,1,0,0,0,0,0}, |
el15s3p | 3:520df1778717 | 582 | {0,0,0,1,0,0,1,0,0,0,0}, |
el15s3p | 3:520df1778717 | 583 | {0,0,1,0,0,0,0,1,1,0,0}, |
el15s3p | 3:520df1778717 | 584 | {0,1,0,0,0,0,0,0,0,1,0}, |
el15s3p | 3:520df1778717 | 585 | {0,1,0,0,0,0,0,0,0,0,1}, |
el15s3p | 3:520df1778717 | 586 | {1,0,0,0,0,0,0,0,0,0,1}, |
el15s3p | 3:520df1778717 | 587 | {0,1,1,0,0,0,0,0,0,0,1}, |
el15s3p | 3:520df1778717 | 588 | {0,0,0,1,1,0,0,0,1,1,0}, |
el15s3p | 3:520df1778717 | 589 | {0,0,0,0,0,1,0,0,1,0,0}, |
el15s3p | 3:520df1778717 | 590 | {0,0,0,0,0,1,0,1,0,0,0}, |
el15s3p | 3:520df1778717 | 591 | {0,0,0,0,0,1,1,0,0,0,0} |
el15s3p | 3:520df1778717 | 592 | }; |
el15s3p | 3:520df1778717 | 593 | |
el15s3p | 3:520df1778717 | 594 | switch(cometDirection) { |
el15s3p | 3:520df1778717 | 595 | case 1: |
el15s3p | 3:520df1778717 | 596 | for(int cometR = 0; cometR <= 10; cometR ++) { |
el15s3p | 3:520df1778717 | 597 | for(int cometC = 0; cometC <= 10; cometC ++) { |
el15s3p | 3:520df1778717 | 598 | if(comet[cometR][cometC] == 0) { |
el15s3p | 3:520df1778717 | 599 | lcd.clearPixel(cometR + cometMoveX,cometC + cometMoveY); |
el15s3p | 3:520df1778717 | 600 | } else if(comet[cometR][cometC] == 1) { |
el15s3p | 3:520df1778717 | 601 | lcd.setPixel(cometR + cometMoveX,cometC + cometMoveY); |
el15s3p | 3:520df1778717 | 602 | } |
el15s3p | 3:520df1778717 | 603 | } |
el15s3p | 3:520df1778717 | 604 | } |
el15s3p | 3:520df1778717 | 605 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 606 | break; |
el15s3p | 3:520df1778717 | 607 | case 2: |
el15s3p | 3:520df1778717 | 608 | for(int cometR = 0; cometR <= 10; cometR ++) { |
el15s3p | 3:520df1778717 | 609 | for(int cometC = 0; cometC <= 10; cometC ++) { |
el15s3p | 3:520df1778717 | 610 | if(comet[cometR][cometC] == 0) { |
el15s3p | 3:520df1778717 | 611 | lcd.clearPixel(cometR + cometMoveX,cometC + cometMoveY); |
el15s3p | 3:520df1778717 | 612 | } else if(comet[cometR][cometC] == 1) { |
el15s3p | 3:520df1778717 | 613 | lcd.setPixel(cometR + cometMoveX,cometC + cometMoveY); |
el15s3p | 3:520df1778717 | 614 | } |
el15s3p | 3:520df1778717 | 615 | } |
el15s3p | 3:520df1778717 | 616 | } |
el15s3p | 3:520df1778717 | 617 | |
el15s3p | 3:520df1778717 | 618 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 619 | break; |
el15s3p | 3:520df1778717 | 620 | case 3: |
el15s3p | 3:520df1778717 | 621 | for(int cometR = 0; cometR <= 10; cometR ++) { |
el15s3p | 3:520df1778717 | 622 | for(int cometC = 0; cometC <= 10; cometC ++) { |
el15s3p | 3:520df1778717 | 623 | if(comet[cometR][cometC] == 0) { |
el15s3p | 3:520df1778717 | 624 | lcd.clearPixel(cometR + cometMoveX,cometC + cometMoveY); |
el15s3p | 3:520df1778717 | 625 | } else if(comet[cometR][cometC] == 1) { |
el15s3p | 3:520df1778717 | 626 | lcd.setPixel(cometR + cometMoveX,cometC + cometMoveY); |
el15s3p | 3:520df1778717 | 627 | } |
el15s3p | 3:520df1778717 | 628 | } |
el15s3p | 3:520df1778717 | 629 | } |
el15s3p | 3:520df1778717 | 630 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 631 | break; |
el15s3p | 3:520df1778717 | 632 | case 4: |
el15s3p | 3:520df1778717 | 633 | for(int cometR = 0; cometR <= 10; cometR ++) { |
el15s3p | 3:520df1778717 | 634 | for(int cometC = 0; cometC <= 10; cometC ++) { |
el15s3p | 3:520df1778717 | 635 | if(comet[cometR][cometC] == 0) { |
el15s3p | 3:520df1778717 | 636 | lcd.clearPixel(cometR + cometMoveX,cometC + cometMoveY); |
el15s3p | 3:520df1778717 | 637 | } else if(comet[cometR][cometC] == 1) { |
el15s3p | 3:520df1778717 | 638 | lcd.setPixel(cometR + cometMoveX,cometC + cometMoveY); |
el15s3p | 3:520df1778717 | 639 | } |
el15s3p | 3:520df1778717 | 640 | } |
el15s3p | 3:520df1778717 | 641 | } |
el15s3p | 3:520df1778717 | 642 | |
el15s3p | 3:520df1778717 | 643 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 644 | break; |
el15s3p | 3:520df1778717 | 645 | case 5: |
el15s3p | 3:520df1778717 | 646 | for(int cometR = 0; cometR <= 10; cometR ++) { |
el15s3p | 3:520df1778717 | 647 | for(int cometC = 0; cometC <= 10; cometC ++) { |
el15s3p | 3:520df1778717 | 648 | if(comet[cometR][cometC] == 0) { |
el15s3p | 3:520df1778717 | 649 | lcd.clearPixel(cometR + cometMoveX,cometC + cometMoveY); |
el15s3p | 3:520df1778717 | 650 | } else if(comet[cometR][cometC] == 1) { |
el15s3p | 3:520df1778717 | 651 | lcd.setPixel(cometR + cometMoveX,cometC + cometMoveY); |
el15s3p | 3:520df1778717 | 652 | } |
el15s3p | 3:520df1778717 | 653 | } |
el15s3p | 3:520df1778717 | 654 | } |
el15s3p | 3:520df1778717 | 655 | |
el15s3p | 3:520df1778717 | 656 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 657 | break; |
el15s3p | 3:520df1778717 | 658 | case 6: |
el15s3p | 3:520df1778717 | 659 | for(int cometR = 0; cometR <= 10; cometR ++) { |
el15s3p | 3:520df1778717 | 660 | for(int cometC = 0; cometC <= 10; cometC ++) { |
el15s3p | 3:520df1778717 | 661 | if(comet[cometR][cometC] == 0) { |
el15s3p | 3:520df1778717 | 662 | lcd.clearPixel(cometR + cometMoveX,cometC + cometMoveY); |
el15s3p | 3:520df1778717 | 663 | } else if(comet[cometR][cometC] == 1) { |
el15s3p | 3:520df1778717 | 664 | lcd.setPixel(cometR + cometMoveX,cometC + cometMoveY); |
el15s3p | 3:520df1778717 | 665 | } |
el15s3p | 3:520df1778717 | 666 | } |
el15s3p | 3:520df1778717 | 667 | } |
el15s3p | 3:520df1778717 | 668 | |
el15s3p | 3:520df1778717 | 669 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 670 | break; |
el15s3p | 3:520df1778717 | 671 | case 7: |
el15s3p | 3:520df1778717 | 672 | for(int cometR = 0; cometR <= 10; cometR ++) { |
el15s3p | 3:520df1778717 | 673 | for(int cometC = 0; cometC <= 10; cometC ++) { |
el15s3p | 3:520df1778717 | 674 | if(comet[cometR][cometC] == 0) { |
el15s3p | 3:520df1778717 | 675 | lcd.clearPixel(cometR + cometMoveX,cometC + cometMoveY); |
el15s3p | 3:520df1778717 | 676 | } else if(comet[cometR][cometC] == 1) { |
el15s3p | 3:520df1778717 | 677 | lcd.setPixel(cometR + cometMoveX,cometC + cometMoveY); |
el15s3p | 3:520df1778717 | 678 | } |
el15s3p | 3:520df1778717 | 679 | } |
el15s3p | 3:520df1778717 | 680 | } |
el15s3p | 3:520df1778717 | 681 | |
el15s3p | 3:520df1778717 | 682 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 683 | break; |
el15s3p | 3:520df1778717 | 684 | case 8: |
el15s3p | 3:520df1778717 | 685 | for(int cometR = 0; cometR <= 10; cometR ++) { |
el15s3p | 3:520df1778717 | 686 | for(int cometC = 0; cometC <= 10; cometC ++) { |
el15s3p | 3:520df1778717 | 687 | if(comet[cometR][cometC] == 0) { |
el15s3p | 3:520df1778717 | 688 | lcd.clearPixel(cometR + cometMoveX,cometC + cometMoveY); |
el15s3p | 3:520df1778717 | 689 | } else if(comet[cometR][cometC] == 1) { |
el15s3p | 3:520df1778717 | 690 | lcd.setPixel(cometR + cometMoveX,cometC + cometMoveY); |
el15s3p | 3:520df1778717 | 691 | } |
el15s3p | 3:520df1778717 | 692 | } |
el15s3p | 3:520df1778717 | 693 | } |
el15s3p | 3:520df1778717 | 694 | |
el15s3p | 3:520df1778717 | 695 | lcd.refresh(); |
el15s3p | 3:520df1778717 | 696 | break; |
el15s3p | 3:520df1778717 | 697 | |
el15s3p | 3:520df1778717 | 698 | }; |
el15s3p | 3:520df1778717 | 699 | } |
el15s3p | 3:520df1778717 | 700 | } |
el15s3p | 3:520df1778717 | 701 | |
el15s3p | 3:520df1778717 | 702 | void GameOver() |
el15s3p | 3:520df1778717 | 703 | { |
el15s3p | 3:520df1778717 | 704 | lcd.clear(); |
el15s3p | 3:520df1778717 | 705 | lcd.printString("GAME OVER",20,3); |
el15s3p | 3:520df1778717 | 706 | wait(2.0); |
el15s3p | 3:520df1778717 | 707 | menuUp = 1; |
el15s3p | 3:520df1778717 | 708 | menu(); |
el15s3p | 3:520df1778717 | 709 | } |
el15s3p | 3:520df1778717 | 710 | |
el15s3p | 3:520df1778717 | 711 | //this will calibrate the joystick |
el15s3p | 3:520df1778717 | 712 | void calibrateJoystick() |
el15s3p | 3:520df1778717 | 713 | { |
el15s3p | 3:520df1778717 | 714 | button.mode(PullDown); |
el15s3p | 3:520df1778717 | 715 | buttonA.mode(PullDown); |
el15s3p | 3:520df1778717 | 716 | buttonB.mode(PullDown); |
el15s3p | 3:520df1778717 | 717 | |
el15s3p | 3:520df1778717 | 718 | // must not move during calibration |
el15s3p | 3:520df1778717 | 719 | joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) |
el15s3p | 3:520df1778717 | 720 | joystick.y0 = yPot; |
el15s3p | 3:520df1778717 | 721 | } |
el15s3p | 3:520df1778717 | 722 | |
el15s3p | 3:520df1778717 | 723 | //this will update the joystick to show it's current displacement |
el15s3p | 3:520df1778717 | 724 | void updateJoystick() |
el15s3p | 3:520df1778717 | 725 | { |
el15s3p | 3:520df1778717 | 726 | // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) |
el15s3p | 3:520df1778717 | 727 | joystick.x = xPot - joystick.x0; |
el15s3p | 3:520df1778717 | 728 | joystick.y = yPot - joystick.y0; |
el15s3p | 3:520df1778717 | 729 | // read button state |
el15s3p | 3:520df1778717 | 730 | joystick.button = button; |
el15s3p | 3:520df1778717 | 731 | joystick.buttonA = buttonA; |
el15s3p | 3:520df1778717 | 732 | joystick.buttonB = buttonB; |
el15s3p | 3:520df1778717 | 733 | |
el15s3p | 3:520df1778717 | 734 | // calculate direction depending on x,y values |
el15s3p | 3:520df1778717 | 735 | // tolerance allows a little lee-way in case joystick not exactly in the stated direction |
el15s3p | 3:520df1778717 | 736 | if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
el15s3p | 3:520df1778717 | 737 | joystick.direction = CENTRE; |
el15s3p | 3:520df1778717 | 738 | } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
el15s3p | 3:520df1778717 | 739 | joystick.direction = DOWN; |
el15s3p | 3:520df1778717 | 740 | } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
el15s3p | 3:520df1778717 | 741 | joystick.direction = UP; |
el15s3p | 3:520df1778717 | 742 | } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
el15s3p | 3:520df1778717 | 743 | joystick.direction = RIGHT; |
el15s3p | 3:520df1778717 | 744 | } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
el15s3p | 3:520df1778717 | 745 | joystick.direction = LEFT; |
el15s3p | 3:520df1778717 | 746 | } else { |
el15s3p | 3:520df1778717 | 747 | joystick.direction = UNKNOWN; |
el15s3p | 3:520df1778717 | 748 | } |
el15s3p | 3:520df1778717 | 749 | |
el15s3p | 3:520df1778717 | 750 | // set flag for printing |
el15s3p | 3:520df1778717 | 751 | printFlag = 1; |
el15s3p | 3:520df1778717 | 752 | } |