My ELEC2645 project. Nikollao Sulollari. 200804685

Dependencies:   N5110 SoftPWM mbed

Fork of Treasure_Hunt by ELEC2645 (2015/16)

Committer:
Nikollao
Date:
Wed May 04 09:19:50 2016 +0000
Revision:
21:6b02ef341358
Parent:
20:59aa6189a47d
Final Version for Submission! Well done!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Nikollao 5:ffa498d7071f 1 /**
Nikollao 5:ffa498d7071f 2 @file main.cpp
Nikollao 5:ffa498d7071f 3 @brief Game implementation
Nikollao 4:f31bdc212241 4
Nikollao 5:ffa498d7071f 5 */
Nikollao 5:ffa498d7071f 6 #include "main.h"
Nikollao 0:2d0f77943105 7 #include "stdlib.h"
Nikollao 2:af5b5d42e835 8
Nikollao 20:59aa6189a47d 9 int main(void)
Nikollao 0:2d0f77943105 10 {
Nikollao 20:59aa6189a47d 11 lcd.init(); ///initialise LED
Nikollao 21:6b02ef341358 12 init_K64F(); ///init K64F
Nikollao 21:6b02ef341358 13 init_serial(); ///initiate connection with PC port
Nikollao 16:a6ca6858af24 14 init_game(); ///initialize game
Nikollao 21:6b02ef341358 15 led_output = fsm[fsm_state]; ///set FSM to turn on LEDs
Nikollao 5:ffa498d7071f 16 calibrateJoystick(); ///calibrate joystick
Nikollao 18:5405ee7e243b 17
Nikollao 20:59aa6189a47d 18 game_ticker.attach(&game_timer_isr,0.2); ///attach ticker for the menu different from the main ticker
Nikollao 15:c7af2ea5f164 19 menu();
Nikollao 16:a6ca6858af24 20 game_ticker.detach();
Nikollao 20:59aa6189a47d 21 //timer.start();
Nikollao 18:5405ee7e243b 22
Nikollao 20:59aa6189a47d 23 ticker.attach(&timer_isr,game_speed); ///attach ticker with ISR. Speed is selected by the user
Nikollao 5:ffa498d7071f 24 reset = level; ///set reset = level to check later if level has increased
Nikollao 21:6b02ef341358 25 g_button1_flag = 0;
Nikollao 21:6b02ef341358 26 timer.start();
Nikollao 0:2d0f77943105 27 while (1) {
Nikollao 21:6b02ef341358 28
Nikollao 20:59aa6189a47d 29 if (g_timer_flag) {///if timer flag is UP
Nikollao 0:2d0f77943105 30
Nikollao 6:c11bb31a06b3 31 g_timer_flag = 0; ///reset flag
Nikollao 0:2d0f77943105 32 lcd.clear();
Nikollao 21:6b02ef341358 33 if (reset < level) { ///if level has increased
Nikollao 21:6b02ef341358 34 g_button1_flag = 0; ///reset weapon button
Nikollao 21:6b02ef341358 35 reset = level; ///update reset
Nikollao 21:6b02ef341358 36 rectY = 0; ///set enemies to default position
Nikollao 21:6b02ef341358 37 circleX = 0;
Nikollao 21:6b02ef341358 38 }
Nikollao 20:59aa6189a47d 39 guidance(); ///get guidance
Nikollao 20:59aa6189a47d 40 hero(); ///call hero
Nikollao 20:59aa6189a47d 41 enemies(); ///generate enemies
Nikollao 20:59aa6189a47d 42 obstacles(); ///generate obstacles
Nikollao 21:6b02ef341358 43 //float count_time = timer.read();
Nikollao 21:6b02ef341358 44
Nikollao 21:6b02ef341358 45 if (g_button1_flag) { ///the blue LED indicates if the weapon is ready to use or not
Nikollao 21:6b02ef341358 46 blue_led = 0;
Nikollao 21:6b02ef341358 47 } else {
Nikollao 21:6b02ef341358 48 blue_led = 1;
Nikollao 20:59aa6189a47d 49 }
Nikollao 21:6b02ef341358 50
Nikollao 20:59aa6189a47d 51 checkOverlap(); ///check is hero has overlapped with enemies or obstacles
Nikollao 21:6b02ef341358 52 updateJoystick(); ///update joystick
Nikollao 16:a6ca6858af24 53 lcd.refresh();
Nikollao 0:2d0f77943105 54 }
Nikollao 20:59aa6189a47d 55 sleep();///use sleep and timer triggered interrupt to save power
Nikollao 0:2d0f77943105 56 }
Nikollao 0:2d0f77943105 57 }
Nikollao 0:2d0f77943105 58
Nikollao 20:59aa6189a47d 59
Nikollao 21:6b02ef341358 60 //mbed functions
Nikollao 20:59aa6189a47d 61
Nikollao 20:59aa6189a47d 62
Nikollao 0:2d0f77943105 63 void init_serial()
Nikollao 0:2d0f77943105 64 {
Nikollao 0:2d0f77943105 65 // set to highest baud - ensure terminal software matches
Nikollao 0:2d0f77943105 66 pc.baud(115200);
Nikollao 0:2d0f77943105 67 }
Nikollao 0:2d0f77943105 68
Nikollao 0:2d0f77943105 69 void init_K64F()
Nikollao 0:2d0f77943105 70 {
Nikollao 0:2d0f77943105 71 // on-board LEDs are active-low, so set pin high to turn them off.
Nikollao 0:2d0f77943105 72 r_led = 1;
Nikollao 0:2d0f77943105 73 g_led = 1;
Nikollao 0:2d0f77943105 74 b_led = 1;
Nikollao 18:5405ee7e243b 75
Nikollao 15:c7af2ea5f164 76 blue_led = 1;
Nikollao 0:2d0f77943105 77
Nikollao 0:2d0f77943105 78 // since the on-board switches have external pull-ups, we should disable the internal pull-down
Nikollao 0:2d0f77943105 79 // resistors that are enabled by default using InterruptIn
Nikollao 0:2d0f77943105 80 sw2.mode(PullNone);
Nikollao 0:2d0f77943105 81 sw3.mode(PullNone);
Nikollao 21:6b02ef341358 82
Nikollao 21:6b02ef341358 83 button.rise(&button_isr); ///assign rise with ISR
Nikollao 21:6b02ef341358 84 button1.rise(&button1_isr);
Nikollao 21:6b02ef341358 85 button.mode(PullDown); ///use PullDown mode
Nikollao 11:53caf3d944a9 86 button1.mode(PullDown);
Nikollao 0:2d0f77943105 87 }
Nikollao 0:2d0f77943105 88
Nikollao 20:59aa6189a47d 89
Nikollao 20:59aa6189a47d 90 ///ISR functions
Nikollao 0:2d0f77943105 91 void timer_isr ()
Nikollao 0:2d0f77943105 92 {
Nikollao 0:2d0f77943105 93 g_timer_flag = 1;
Nikollao 0:2d0f77943105 94 }
Nikollao 0:2d0f77943105 95
Nikollao 0:2d0f77943105 96 void game_timer_isr()
Nikollao 0:2d0f77943105 97 {
Nikollao 0:2d0f77943105 98 g_game_timer_flag = 1;
Nikollao 0:2d0f77943105 99 }
Nikollao 0:2d0f77943105 100
Nikollao 0:2d0f77943105 101 void sw2_isr()
Nikollao 0:2d0f77943105 102 {
Nikollao 0:2d0f77943105 103 g_sw2_flag = 1;
Nikollao 0:2d0f77943105 104 }
Nikollao 0:2d0f77943105 105
Nikollao 4:f31bdc212241 106 void sw3_isr()
Nikollao 4:f31bdc212241 107 {
Nikollao 1:12803d645d32 108 g_sw3_flag = 1;
Nikollao 1:12803d645d32 109 }
Nikollao 1:12803d645d32 110
Nikollao 1:12803d645d32 111 void button_isr()
Nikollao 1:12803d645d32 112 {
Nikollao 1:12803d645d32 113 g_button_flag =1;
Nikollao 1:12803d645d32 114 }
Nikollao 1:12803d645d32 115
Nikollao 18:5405ee7e243b 116 void button1_isr()
Nikollao 18:5405ee7e243b 117 {
Nikollao 18:5405ee7e243b 118
Nikollao 11:53caf3d944a9 119 g_button1_flag =1;
Nikollao 11:53caf3d944a9 120 }
Nikollao 11:53caf3d944a9 121
Nikollao 1:12803d645d32 122 void timeout_isr()
Nikollao 1:12803d645d32 123 {
Nikollao 1:12803d645d32 124 if (button) {
Nikollao 16:a6ca6858af24 125 pc.printf("timeout \n");
Nikollao 1:12803d645d32 126 }
Nikollao 16:a6ca6858af24 127 }
Nikollao 18:5405ee7e243b 128
Nikollao 20:59aa6189a47d 129 ///game functions
Nikollao 20:59aa6189a47d 130
Nikollao 18:5405ee7e243b 131 void enemies()
Nikollao 18:5405ee7e243b 132 {
Nikollao 20:59aa6189a47d 133 /// generate enemies in the screen depending on the level of difficulty
Nikollao 21:6b02ef341358 134 if (level == 0) { ///generate enemies at level 0
Nikollao 21:6b02ef341358 135
Nikollao 21:6b02ef341358 136 enemyRect();
Nikollao 21:6b02ef341358 137 if (g_button1_flag == 0) {
Nikollao 21:6b02ef341358 138 enemyCircle();
Nikollao 21:6b02ef341358 139 }
Nikollao 21:6b02ef341358 140 } else if (level == 1) {///generate enemies at level 1
Nikollao 18:5405ee7e243b 141
Nikollao 18:5405ee7e243b 142 enemyRect();
Nikollao 21:6b02ef341358 143 if (g_button1_flag == 0) {
Nikollao 21:6b02ef341358 144 enemyCircle();
Nikollao 21:6b02ef341358 145 }
Nikollao 21:6b02ef341358 146 } else if (level == 2) {///generate enemies at level 2
Nikollao 21:6b02ef341358 147
Nikollao 21:6b02ef341358 148 enemyRocket();
Nikollao 21:6b02ef341358 149 if (g_button1_flag == 0) {
Nikollao 21:6b02ef341358 150 enemyCircle();
Nikollao 21:6b02ef341358 151 }
Nikollao 21:6b02ef341358 152 } else if (level == 3) {///generate enemies at level 3
Nikollao 21:6b02ef341358 153
Nikollao 21:6b02ef341358 154 enemyRect();
Nikollao 21:6b02ef341358 155 if (g_button1_flag == 0) {
Nikollao 21:6b02ef341358 156 enemyCircle();
Nikollao 21:6b02ef341358 157 }
Nikollao 21:6b02ef341358 158 } else if (level == 4) {///generate enemies at level 4
Nikollao 18:5405ee7e243b 159
Nikollao 18:5405ee7e243b 160 enemyRect();
Nikollao 21:6b02ef341358 161 if (g_button1_flag == 0) {
Nikollao 21:6b02ef341358 162 enemyCircle();
Nikollao 21:6b02ef341358 163 }
Nikollao 21:6b02ef341358 164 } else if (level == 5) {///generate enemies at level 5
Nikollao 19:70d40dac5ae0 165 enemyRocket();
Nikollao 21:6b02ef341358 166 if (g_button1_flag == 0) {
Nikollao 21:6b02ef341358 167 enemyCircle();
Nikollao 21:6b02ef341358 168 }
Nikollao 21:6b02ef341358 169 } else if (level == 6) {///generate enemies at level 6
Nikollao 21:6b02ef341358 170 enemyRocket();
Nikollao 21:6b02ef341358 171 if (g_button1_flag == 0) {
Nikollao 21:6b02ef341358 172 enemyCircle();
Nikollao 21:6b02ef341358 173 }
Nikollao 21:6b02ef341358 174 } else if (level == 7) {///generate enemies at level 7
Nikollao 21:6b02ef341358 175 enemyRocket();
Nikollao 19:70d40dac5ae0 176 enemyCircle();
Nikollao 21:6b02ef341358 177 if (g_button1_flag == 1) {///if button1 is pressed don't allow to use at level 7
Nikollao 21:6b02ef341358 178 g_button1_flag = 0;
Nikollao 21:6b02ef341358 179 lcd.clear();
Nikollao 21:6b02ef341358 180 lcd.printString("Can't use now!",0,0);
Nikollao 21:6b02ef341358 181 lcd.refresh();
Nikollao 21:6b02ef341358 182 wait(1);
Nikollao 21:6b02ef341358 183 lcd.clear();
Nikollao 21:6b02ef341358 184 }
Nikollao 18:5405ee7e243b 185 }
Nikollao 21:6b02ef341358 186
Nikollao 18:5405ee7e243b 187 }
Nikollao 18:5405ee7e243b 188
Nikollao 18:5405ee7e243b 189 void enemyRect()
Nikollao 18:5405ee7e243b 190 {
Nikollao 21:6b02ef341358 191 ///generate rectangular shape enemy
Nikollao 18:5405ee7e243b 192 lcd.drawRect(rectX,rectY,5,4,1);
Nikollao 21:6b02ef341358 193
Nikollao 21:6b02ef341358 194 rectX = rectX + rand()%4 - 2;
Nikollao 21:6b02ef341358 195 rectY++; ///enemy moving towards hero
Nikollao 20:59aa6189a47d 196
Nikollao 21:6b02ef341358 197 if (rectY > 50) {///set boundaries for enemy position
Nikollao 21:6b02ef341358 198 rectY = 0;
Nikollao 21:6b02ef341358 199 }
Nikollao 21:6b02ef341358 200 if (rectX > 75 || rectX < 25) {
Nikollao 21:6b02ef341358 201 rectX = 40+rand()%6 - 3;
Nikollao 18:5405ee7e243b 202 }
Nikollao 18:5405ee7e243b 203 }
Nikollao 18:5405ee7e243b 204
Nikollao 21:6b02ef341358 205 void enemyRocket() { ///generate a "rocket-like" enemy
Nikollao 19:70d40dac5ae0 206
Nikollao 19:70d40dac5ae0 207 lcd.drawRect(rectX,rectY,2,7,1);
Nikollao 19:70d40dac5ae0 208 lcd.drawCircle(rectX+1,rectY+1,2,1);
Nikollao 19:70d40dac5ae0 209 lcd.drawLine(rectX-2,rectY+2,rectX,rectY-1,1);
Nikollao 19:70d40dac5ae0 210 lcd.drawLine(rectX+2,rectY+2,rectX,rectY-1,1);
Nikollao 21:6b02ef341358 211
Nikollao 21:6b02ef341358 212 rectX = rectX + rand()%4 - 2;
Nikollao 21:6b02ef341358 213 rectY++; ///enemy moving towards hero
Nikollao 21:6b02ef341358 214
Nikollao 21:6b02ef341358 215 if (rectY > 50) {///set boundaries for position of enemy
Nikollao 21:6b02ef341358 216 rectY = 0;
Nikollao 21:6b02ef341358 217 }
Nikollao 21:6b02ef341358 218 if (rectX > 75 || rectX < 10) {
Nikollao 21:6b02ef341358 219 rectX = 40+rand()%6 - 3;
Nikollao 21:6b02ef341358 220 }
Nikollao 21:6b02ef341358 221 }
Nikollao 20:59aa6189a47d 222
Nikollao 21:6b02ef341358 223 void enemyCircle()
Nikollao 21:6b02ef341358 224 {
Nikollao 21:6b02ef341358 225 ///generate circle shape enemy
Nikollao 21:6b02ef341358 226 lcd.drawCircle(circleX,circleY,4,1);
Nikollao 21:6b02ef341358 227 circleY = circleY + rand() %4 - 2;
Nikollao 21:6b02ef341358 228
Nikollao 21:6b02ef341358 229 if (circleY <= 10) { ///set the boundaries of the enemy
Nikollao 21:6b02ef341358 230 circleY = 20;
Nikollao 19:70d40dac5ae0 231 }
Nikollao 21:6b02ef341358 232 if (circleY >= 35) {
Nikollao 21:6b02ef341358 233 circleY = 35;
Nikollao 21:6b02ef341358 234 }
Nikollao 21:6b02ef341358 235 if (circleX > 84) {
Nikollao 21:6b02ef341358 236 circleX = 0;
Nikollao 21:6b02ef341358 237 }
Nikollao 21:6b02ef341358 238 circleX++; ///enemy moving towards hero
Nikollao 19:70d40dac5ae0 239 }
Nikollao 18:5405ee7e243b 240 void hero()
Nikollao 18:5405ee7e243b 241 {
Nikollao 18:5405ee7e243b 242 ///cotrol hero
Nikollao 21:6b02ef341358 243 if (joystick.direction == RIGHT) { ///joystick direction points to the right
Nikollao 18:5405ee7e243b 244 heroX--;
Nikollao 21:6b02ef341358 245 } else if (joystick.direction == LEFT) {///joystick direction points to the left
Nikollao 18:5405ee7e243b 246 heroX++;
Nikollao 18:5405ee7e243b 247 } else {
Nikollao 18:5405ee7e243b 248 heroX = heroX;
Nikollao 18:5405ee7e243b 249 }
Nikollao 18:5405ee7e243b 250
Nikollao 21:6b02ef341358 251 if (joystick.direction == UP) {///joystick direction points to the top
Nikollao 18:5405ee7e243b 252 heroY--;
Nikollao 21:6b02ef341358 253 } else if (joystick.direction == DOWN) {///joystick direction points down
Nikollao 18:5405ee7e243b 254 heroY++;
Nikollao 18:5405ee7e243b 255 } else {
Nikollao 21:6b02ef341358 256 heroY = heroY; ///joystick is centred
Nikollao 18:5405ee7e243b 257 }
Nikollao 18:5405ee7e243b 258
Nikollao 21:6b02ef341358 259 if (joystick.direction == UP_LEFT) { ///joystick direction points up and left
Nikollao 18:5405ee7e243b 260 heroY--;
Nikollao 18:5405ee7e243b 261 heroX++;
Nikollao 21:6b02ef341358 262 } else if (joystick.direction == UP_RIGHT) { ///joystick direction points up and right
Nikollao 18:5405ee7e243b 263 heroY--;
Nikollao 18:5405ee7e243b 264 heroX--;
Nikollao 21:6b02ef341358 265 } else if (joystick.direction == DOWN_RIGHT) {///joystick direction points down and right
Nikollao 18:5405ee7e243b 266 heroY++;
Nikollao 18:5405ee7e243b 267 heroX--;
Nikollao 21:6b02ef341358 268 } else if (joystick.direction == DOWN_LEFT) {///joystick direction points down and left
Nikollao 18:5405ee7e243b 269 heroY++;
Nikollao 18:5405ee7e243b 270 heroX++;
Nikollao 18:5405ee7e243b 271 }
Nikollao 18:5405ee7e243b 272
Nikollao 18:5405ee7e243b 273 ///set x-axis boundaries so hero does not go out of screen
Nikollao 18:5405ee7e243b 274 if (heroX > 40) {
Nikollao 18:5405ee7e243b 275 heroX = -40;
Nikollao 18:5405ee7e243b 276 }
Nikollao 18:5405ee7e243b 277 if (heroX < -45) {
Nikollao 18:5405ee7e243b 278 heroX = 35;
Nikollao 18:5405ee7e243b 279 }
Nikollao 18:5405ee7e243b 280 if (heroY < -45) { ///if hero has reached the top of the screen
Nikollao 18:5405ee7e243b 281 heroY = 0; ///hero goes back to the bottom of the screen
Nikollao 21:6b02ef341358 282 g_button1_flag = 0;
Nikollao 21:6b02ef341358 283 level++; ///increase level of the game
Nikollao 18:5405ee7e243b 284 }
Nikollao 18:5405ee7e243b 285
Nikollao 18:5405ee7e243b 286 if (heroY >= 0) {/// hero cannot go to previous level
Nikollao 18:5405ee7e243b 287 heroY = 0;
Nikollao 18:5405ee7e243b 288 }
Nikollao 18:5405ee7e243b 289 ///draw hero
Nikollao 21:6b02ef341358 290 lcd.drawLine(40+heroX, 47+heroY, 48+heroX, 43+heroY,1); ///variables heroX and heroY are used to control the direction of the hero
Nikollao 18:5405ee7e243b 291 lcd.drawLine(40+heroX, 43+heroY,48+heroX, 47+heroY,1);
Nikollao 18:5405ee7e243b 292 lcd.drawLine(44+heroX, 45+heroY,44+heroX, 41+heroY,1);
Nikollao 18:5405ee7e243b 293 lcd.drawCircle(44+heroX, 39+heroY,2,0);
Nikollao 18:5405ee7e243b 294 }
Nikollao 18:5405ee7e243b 295
Nikollao 18:5405ee7e243b 296 void init_game()
Nikollao 18:5405ee7e243b 297 {
Nikollao 18:5405ee7e243b 298 ///initialise game
Nikollao 18:5405ee7e243b 299 srand(time(NULL)); /// generate random numbers
Nikollao 18:5405ee7e243b 300 rectY = 0; /// init rectX, rectY
Nikollao 18:5405ee7e243b 301 rectX = rand() %40 + 20;
Nikollao 18:5405ee7e243b 302 circleY = rand() %20 + 10; /// init circleX, circleY
Nikollao 18:5405ee7e243b 303 circleX = 0;
Nikollao 18:5405ee7e243b 304
Nikollao 18:5405ee7e243b 305 lcd.setBrightness(0.5); // put LED backlight on 50%
Nikollao 18:5405ee7e243b 306 lcd.printString("Welcome to",11,1);
Nikollao 18:5405ee7e243b 307 lcd.printString("Treasure Hunt!",1,3);
Nikollao 18:5405ee7e243b 308 lcd.refresh();
Nikollao 18:5405ee7e243b 309 //timeout.attach(&timeout_isr,3);
Nikollao 21:6b02ef341358 310 // sleep();
Nikollao 21:6b02ef341358 311 wait(3); ///small delay prints welcome message
Nikollao 18:5405ee7e243b 312 lcd.clear();
Nikollao 18:5405ee7e243b 313
Nikollao 18:5405ee7e243b 314 }
Nikollao 18:5405ee7e243b 315 void guidance()
Nikollao 18:5405ee7e243b 316 {
Nikollao 21:6b02ef341358 317 if (level < 7) {
Nikollao 18:5405ee7e243b 318 lcd.drawLine(42,0,40,2,1);
Nikollao 18:5405ee7e243b 319 lcd.drawLine(42,0,44,2,1);
Nikollao 21:6b02ef341358 320 lcd.drawLine(42,0,42,3,1);
Nikollao 21:6b02ef341358 321 }
Nikollao 21:6b02ef341358 322 /// function acts as guidance towards the treasure
Nikollao 21:6b02ef341358 323 else if (level == 7) {
Nikollao 18:5405ee7e243b 324
Nikollao 18:5405ee7e243b 325 lcd.printString("F",42,0); /// print the treasure icon
Nikollao 18:5405ee7e243b 326 } else if (level == 8) {
Nikollao 21:6b02ef341358 327
Nikollao 21:6b02ef341358 328 timer.stop(); ///stop timer
Nikollao 21:6b02ef341358 329 timer.reset();
Nikollao 21:6b02ef341358 330 float win_time = timer.read();///value reads the time taken to win the game
Nikollao 21:6b02ef341358 331 char win[80]; ///create an array of chars
Nikollao 21:6b02ef341358 332 sprintf(win,"Time:%.2f sec",win_time); ///create string
Nikollao 18:5405ee7e243b 333 ticker.detach();
Nikollao 21:6b02ef341358 334 game_ticker.attach(&game_timer_isr,0.2);
Nikollao 18:5405ee7e243b 335 lcd.clear();
Nikollao 18:5405ee7e243b 336 lcd.printString("Well done!",0,0);
Nikollao 18:5405ee7e243b 337 lcd.refresh();
Nikollao 18:5405ee7e243b 338 wait(2);
Nikollao 18:5405ee7e243b 339 lcd.clear();
Nikollao 21:6b02ef341358 340 lcd.printString(win,0,0); ///print the time taken to win the game on screen
Nikollao 21:6b02ef341358 341 wait(3);
Nikollao 20:59aa6189a47d 342 lcd.clear();
Nikollao 21:6b02ef341358 343 lcd.printString("Play again!",0,0); ///provide play again option
Nikollao 18:5405ee7e243b 344 lcd.drawCircle(70,4,2,1);
Nikollao 18:5405ee7e243b 345 lcd.refresh();
Nikollao 18:5405ee7e243b 346 g_button_flag = 0;
Nikollao 18:5405ee7e243b 347
Nikollao 18:5405ee7e243b 348 while(1) {
Nikollao 21:6b02ef341358 349 if (g_button1_flag) { ///if button1 is pressed
Nikollao 21:6b02ef341358 350 ///reset
Nikollao 19:70d40dac5ae0 351 g_button1_flag = 0;
Nikollao 18:5405ee7e243b 352 level = 0;
Nikollao 18:5405ee7e243b 353 lcd.clear();
Nikollao 18:5405ee7e243b 354 main();
Nikollao 18:5405ee7e243b 355 r_led = 0;
Nikollao 18:5405ee7e243b 356 lcd.refresh();
Nikollao 18:5405ee7e243b 357 }
Nikollao 18:5405ee7e243b 358 sleep();
Nikollao 18:5405ee7e243b 359 }
Nikollao 18:5405ee7e243b 360 }
Nikollao 18:5405ee7e243b 361 }
Nikollao 18:5405ee7e243b 362
Nikollao 18:5405ee7e243b 363 void obstacles()
Nikollao 18:5405ee7e243b 364 {
Nikollao 18:5405ee7e243b 365 /// place obstacles in the screen
Nikollao 18:5405ee7e243b 366 /// as level difficulty increases, more obstacles are added
Nikollao 21:6b02ef341358 367
Nikollao 21:6b02ef341358 368 if (level == 0) { ///obstacles are located in the ground and they change from one level to another
Nikollao 18:5405ee7e243b 369
Nikollao 18:5405ee7e243b 370 lcd.drawRect(10,15,2,2,1);
Nikollao 18:5405ee7e243b 371 lcd.drawRect(74,15,2,2,1);
Nikollao 19:70d40dac5ae0 372 } else if (level == 1) {
Nikollao 18:5405ee7e243b 373
Nikollao 18:5405ee7e243b 374 lcd.drawRect(10,15,2,2,1);
Nikollao 18:5405ee7e243b 375 lcd.drawRect(74,15,2,2,1);
Nikollao 18:5405ee7e243b 376 lcd.drawRect(10,28,2,2,1);
Nikollao 18:5405ee7e243b 377 lcd.drawRect(74,28,2,2,1);
Nikollao 19:70d40dac5ae0 378 } else if (level == 2) {
Nikollao 19:70d40dac5ae0 379
Nikollao 21:6b02ef341358 380 lcd.drawCircle(25,25,5,0);
Nikollao 21:6b02ef341358 381 lcd.drawCircle(65,25,5,0);
Nikollao 21:6b02ef341358 382 lcd.drawCircle(45,11,3,1);
Nikollao 21:6b02ef341358 383 lcd.drawCircle(25,25,3,1);
Nikollao 21:6b02ef341358 384 lcd.drawCircle(65,25,3,1);
Nikollao 19:70d40dac5ae0 385 lcd.drawRect(80,2,2,42,1);
Nikollao 19:70d40dac5ae0 386 lcd.drawRect(2,2,2,42,1);
Nikollao 19:70d40dac5ae0 387
Nikollao 18:5405ee7e243b 388 } else if (level == 3) {
Nikollao 18:5405ee7e243b 389
Nikollao 18:5405ee7e243b 390 lcd.drawRect(5,12,2,2,1);
Nikollao 18:5405ee7e243b 391 lcd.drawRect(79,12,2,2,1);
Nikollao 18:5405ee7e243b 392 lcd.drawRect(5,30,2,2,1);
Nikollao 18:5405ee7e243b 393 lcd.drawRect(79,30,2,2,1);
Nikollao 18:5405ee7e243b 394 lcd.drawRect(28,12,2,2,1);
Nikollao 18:5405ee7e243b 395 lcd.drawRect(52,12,2,2,1);
Nikollao 18:5405ee7e243b 396 lcd.drawRect(28,30,2,2,1);
Nikollao 18:5405ee7e243b 397 lcd.drawRect(52,30,2,2,1);
Nikollao 18:5405ee7e243b 398 } else if (level == 4) {
Nikollao 18:5405ee7e243b 399
Nikollao 21:6b02ef341358 400 lcd.drawRect(5+rand() %4 - 2 ,12+rand() %4 - 2,2,2,1); ///make obstacles moveable by adding a small random variance in position
Nikollao 18:5405ee7e243b 401 lcd.drawRect(79 + rand() %4 - 2,12 + rand() %4 - 2,2,2,1);
Nikollao 19:70d40dac5ae0 402 lcd.drawRect(5+ rand() %4 - 2,30+rand() %4 - 2 ,2,2,1);
Nikollao 19:70d40dac5ae0 403 lcd.drawRect(79 + rand() %4 - 2,30+rand() %4 - 2,2,2,1);
Nikollao 18:5405ee7e243b 404 lcd.drawRect(28 + rand() %4 - 2,12+ rand() %4,2,2,1);
Nikollao 19:70d40dac5ae0 405 lcd.drawRect(52+rand() %4 - 2,12 + rand() %4 - 2,2,2,1);
Nikollao 19:70d40dac5ae0 406 lcd.drawRect(28+rand() %4 - 2,30 + rand() %4 - 2,2,2,1);
Nikollao 19:70d40dac5ae0 407 lcd.drawRect(52 + rand() %4 - 2,30+ rand() %4,2,2,1);
Nikollao 18:5405ee7e243b 408 }
Nikollao 18:5405ee7e243b 409 if (objectX == 0) { ///check position of obstacle
Nikollao 18:5405ee7e243b 410 state = 0; ///assign states to the position of the obstacle
Nikollao 18:5405ee7e243b 411 } else if (objectX == 60) {
Nikollao 18:5405ee7e243b 412 state = 1;
Nikollao 18:5405ee7e243b 413 } else {
Nikollao 18:5405ee7e243b 414 state = state;
Nikollao 18:5405ee7e243b 415 }
Nikollao 18:5405ee7e243b 416 if (state == 0) { ///if state is 0 increase position on x-axis
Nikollao 18:5405ee7e243b 417 objectX++;
Nikollao 18:5405ee7e243b 418 } else {
Nikollao 18:5405ee7e243b 419 objectX--; ///else if state is 1 decrease position on x-axis
Nikollao 18:5405ee7e243b 420 }
Nikollao 18:5405ee7e243b 421
Nikollao 18:5405ee7e243b 422 if (objectX1 == 68) {
Nikollao 18:5405ee7e243b 423
Nikollao 18:5405ee7e243b 424 state1 = 1;
Nikollao 18:5405ee7e243b 425 } else if (objectX1 == 10) {
Nikollao 18:5405ee7e243b 426 state1 = 0;
Nikollao 18:5405ee7e243b 427 }
Nikollao 18:5405ee7e243b 428 if (state1 == 1) {
Nikollao 18:5405ee7e243b 429 objectX1--;
Nikollao 18:5405ee7e243b 430 } else if (state1 == 0) {
Nikollao 18:5405ee7e243b 431 objectX1++;
Nikollao 18:5405ee7e243b 432 }
Nikollao 18:5405ee7e243b 433 objectY = objectY + rand() %4 - 2; ///set poistion of obstacle on y-axis to be valuable
Nikollao 18:5405ee7e243b 434 ///keep moving object within boundaries
Nikollao 18:5405ee7e243b 435 if (objectY <= 10) {
Nikollao 18:5405ee7e243b 436 objectY = 10;
Nikollao 18:5405ee7e243b 437 }
Nikollao 18:5405ee7e243b 438 if (objectY >= 37) {
Nikollao 18:5405ee7e243b 439 objectY = 37;
Nikollao 18:5405ee7e243b 440 }
Nikollao 18:5405ee7e243b 441 if ( level == 5) {
Nikollao 18:5405ee7e243b 442
Nikollao 18:5405ee7e243b 443 lcd.drawLine(15,10,15,37,1);
Nikollao 18:5405ee7e243b 444 lcd.drawLine(71,10,71,37,1);
Nikollao 18:5405ee7e243b 445
Nikollao 18:5405ee7e243b 446 lcd.drawRect(11+objectX,objectY,2,2,1); ///draw obstacle
Nikollao 18:5405ee7e243b 447
Nikollao 18:5405ee7e243b 448 lcd.drawRect(80,10,2,2,1);
Nikollao 18:5405ee7e243b 449 lcd.drawRect(5,10,2,2,1);
Nikollao 18:5405ee7e243b 450 lcd.drawRect(80,20,2,2,1);
Nikollao 18:5405ee7e243b 451 lcd.drawRect(5,20,2,2,1);
Nikollao 18:5405ee7e243b 452 lcd.drawRect(80,30,2,2,1);
Nikollao 18:5405ee7e243b 453 lcd.drawRect(5,30,2,2,1);
Nikollao 18:5405ee7e243b 454
Nikollao 18:5405ee7e243b 455 } else if (level == 6) {
Nikollao 18:5405ee7e243b 456
Nikollao 18:5405ee7e243b 457 lcd.drawLine(15,10,15,37,1); ///draw boundaries
Nikollao 18:5405ee7e243b 458 lcd.drawLine(71,10,71,37,1);
Nikollao 18:5405ee7e243b 459
Nikollao 18:5405ee7e243b 460 lcd.drawRect(11+objectX,objectY,2,2,1); /// draw moving obstacles on screen
Nikollao 18:5405ee7e243b 461 lcd.drawRect(objectX1,10+objectY,2,2,1);
Nikollao 18:5405ee7e243b 462 lcd.drawRect(80,10,2,2,1); ///draw stable obstacles
Nikollao 18:5405ee7e243b 463 lcd.drawRect(5,10,2,2,1);
Nikollao 18:5405ee7e243b 464 lcd.drawRect(80,20,2,2,1);
Nikollao 18:5405ee7e243b 465 lcd.drawRect(5,20,2,2,1);
Nikollao 18:5405ee7e243b 466 lcd.drawRect(80,30,2,2,1);
Nikollao 18:5405ee7e243b 467 lcd.drawRect(5,30,2,2,1);
Nikollao 18:5405ee7e243b 468 } else if (level == 7) {
Nikollao 20:59aa6189a47d 469 lcd.printString("??",65,0);
Nikollao 18:5405ee7e243b 470
Nikollao 20:59aa6189a47d 471 lcd.drawRect(1,23,82,2,1);
Nikollao 20:59aa6189a47d 472 if (button && heroX <-30) {
Nikollao 20:59aa6189a47d 473 g_button_flag = 0;
Nikollao 20:59aa6189a47d 474 heroX = -30;
Nikollao 20:59aa6189a47d 475 heroY = -30;
Nikollao 20:59aa6189a47d 476 }
Nikollao 20:59aa6189a47d 477
Nikollao 20:59aa6189a47d 478 pc.printf("x = %d , y = %d \n",heroX,heroY);
Nikollao 18:5405ee7e243b 479 }
Nikollao 18:5405ee7e243b 480 }
Nikollao 18:5405ee7e243b 481
Nikollao 18:5405ee7e243b 482
Nikollao 18:5405ee7e243b 483 void calibrateJoystick()
Nikollao 18:5405ee7e243b 484 {
Nikollao 18:5405ee7e243b 485 // must not move during calibration
Nikollao 18:5405ee7e243b 486 joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
Nikollao 18:5405ee7e243b 487 joystick.y0 = yPot;
Nikollao 18:5405ee7e243b 488 }
Nikollao 18:5405ee7e243b 489 void updateJoystick()
Nikollao 18:5405ee7e243b 490 {
Nikollao 18:5405ee7e243b 491 // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
Nikollao 18:5405ee7e243b 492 joystick.x = xPot - joystick.x0;
Nikollao 18:5405ee7e243b 493 joystick.y = yPot - joystick.y0;
Nikollao 18:5405ee7e243b 494 // read button state
Nikollao 18:5405ee7e243b 495 joystick.button = button;
Nikollao 18:5405ee7e243b 496
Nikollao 18:5405ee7e243b 497 // calculate direction depending on x,y values
Nikollao 18:5405ee7e243b 498 // tolerance allows a little lee-way in case joystick not exactly in the stated direction
Nikollao 18:5405ee7e243b 499 if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
Nikollao 18:5405ee7e243b 500 joystick.direction = CENTRE;
Nikollao 18:5405ee7e243b 501 } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
Nikollao 18:5405ee7e243b 502 joystick.direction = UP;
Nikollao 18:5405ee7e243b 503 } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
Nikollao 18:5405ee7e243b 504 joystick.direction = DOWN;
Nikollao 18:5405ee7e243b 505 } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
Nikollao 18:5405ee7e243b 506 joystick.direction = RIGHT;
Nikollao 18:5405ee7e243b 507 } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
Nikollao 18:5405ee7e243b 508 joystick.direction = LEFT;
Nikollao 18:5405ee7e243b 509 } else if (joystick.y > DIRECTION_TOLERANCE && joystick.x < DIRECTION_TOLERANCE) {
Nikollao 18:5405ee7e243b 510 joystick.direction = UP_LEFT;
Nikollao 18:5405ee7e243b 511 } else if (joystick.y > DIRECTION_TOLERANCE && joystick.x > DIRECTION_TOLERANCE) {
Nikollao 18:5405ee7e243b 512 joystick.direction = UP_RIGHT;
Nikollao 18:5405ee7e243b 513 } else if (joystick.y < DIRECTION_TOLERANCE && joystick.x < DIRECTION_TOLERANCE) {
Nikollao 18:5405ee7e243b 514 joystick.direction = DOWN_LEFT;
Nikollao 18:5405ee7e243b 515 } else if (joystick.y < DIRECTION_TOLERANCE && joystick.x > DIRECTION_TOLERANCE) {
Nikollao 18:5405ee7e243b 516 joystick.direction = DOWN_RIGHT;
Nikollao 18:5405ee7e243b 517 }
Nikollao 18:5405ee7e243b 518 }
Nikollao 18:5405ee7e243b 519
Nikollao 20:59aa6189a47d 520 void menu()
Nikollao 18:5405ee7e243b 521 {
Nikollao 18:5405ee7e243b 522 while(1) {
Nikollao 18:5405ee7e243b 523 if (g_game_timer_flag) {
Nikollao 18:5405ee7e243b 524 g_game_timer_flag = 0;
Nikollao 18:5405ee7e243b 525
Nikollao 18:5405ee7e243b 526 updateJoystick();
Nikollao 18:5405ee7e243b 527 lcd.clear();
Nikollao 18:5405ee7e243b 528
Nikollao 18:5405ee7e243b 529 lcd.printString("Start Game",0,0); ///print the main
Nikollao 18:5405ee7e243b 530 lcd.printString("Settings",0,2);
Nikollao 18:5405ee7e243b 531 lcd.printString("Exit",0,4);
Nikollao 18:5405ee7e243b 532 //lcd.drawCircle(70,4,2,1);
Nikollao 19:70d40dac5ae0 533 lcd.refresh();
Nikollao 18:5405ee7e243b 534 switch (joystick.direction) { ///check the direction of joystick
Nikollao 18:5405ee7e243b 535 case UP:
Nikollao 18:5405ee7e243b 536 option--;
Nikollao 18:5405ee7e243b 537 break;
Nikollao 18:5405ee7e243b 538 case DOWN:
Nikollao 18:5405ee7e243b 539 option++;
Nikollao 18:5405ee7e243b 540 break;
Nikollao 18:5405ee7e243b 541 }
Nikollao 21:6b02ef341358 542 if (option < 0) { /// if the last (down) option is set for selection and user presses joystick down, selector moves at the top
Nikollao 18:5405ee7e243b 543 option = 2;
Nikollao 18:5405ee7e243b 544 }
Nikollao 21:6b02ef341358 545 if (option > 2) { /// if the first (up) option is set for selection and user presses joystick up, selector moves at the bottom
Nikollao 18:5405ee7e243b 546 option = 0;
Nikollao 18:5405ee7e243b 547 }
Nikollao 18:5405ee7e243b 548
Nikollao 18:5405ee7e243b 549 if (option == 0) { ///selection in menu depends on the value of int option
Nikollao 18:5405ee7e243b 550 lcd.drawCircle(70,4,2,1);
Nikollao 18:5405ee7e243b 551 } else if (option == 1) {
Nikollao 18:5405ee7e243b 552 lcd.drawCircle(55,20,2,1);
Nikollao 18:5405ee7e243b 553 } else if (option == 2) {
Nikollao 18:5405ee7e243b 554 lcd.drawCircle(35,35,2,1);
Nikollao 18:5405ee7e243b 555 }
Nikollao 18:5405ee7e243b 556
Nikollao 18:5405ee7e243b 557 if(g_button_flag) { /// if button is pressed
Nikollao 18:5405ee7e243b 558
Nikollao 18:5405ee7e243b 559 g_button_flag = 0; ///reset flag
Nikollao 18:5405ee7e243b 560
Nikollao 18:5405ee7e243b 561 if (option == 0) { ///break the while loop, start game
Nikollao 18:5405ee7e243b 562 break;
Nikollao 18:5405ee7e243b 563 }
Nikollao 18:5405ee7e243b 564
Nikollao 18:5405ee7e243b 565 else if (option == 1) { ///enter settings menu
Nikollao 18:5405ee7e243b 566
Nikollao 18:5405ee7e243b 567 option = 0;
Nikollao 18:5405ee7e243b 568
Nikollao 18:5405ee7e243b 569 while(1) {
Nikollao 18:5405ee7e243b 570 if (g_game_timer_flag) {
Nikollao 18:5405ee7e243b 571 g_game_timer_flag = 0;
Nikollao 18:5405ee7e243b 572
Nikollao 18:5405ee7e243b 573 updateJoystick(); ///update joystick position
Nikollao 18:5405ee7e243b 574 lcd.clear();
Nikollao 18:5405ee7e243b 575 if (normal) {
Nikollao 18:5405ee7e243b 576 if (sound) {
Nikollao 18:5405ee7e243b 577 lcd.printString("Game speed",0,0); ///print settings menu
Nikollao 18:5405ee7e243b 578 lcd.printString("Lcd:Inverse",0,1);
Nikollao 18:5405ee7e243b 579 lcd.printString("Sound:ON",0,2);
Nikollao 21:6b02ef341358 580 lcd.printString("Back",0,4);
Nikollao 18:5405ee7e243b 581 } else {
Nikollao 18:5405ee7e243b 582 lcd.printString("Game speed",0,0); ///print settings menu
Nikollao 18:5405ee7e243b 583 lcd.printString("Lcd:Inverse",0,1);
Nikollao 18:5405ee7e243b 584 lcd.printString("Sound:OFF",0,2);
Nikollao 21:6b02ef341358 585 lcd.printString("Back",0,4);
Nikollao 18:5405ee7e243b 586 }
Nikollao 18:5405ee7e243b 587 } else {
Nikollao 18:5405ee7e243b 588 if (sound) {
Nikollao 18:5405ee7e243b 589 lcd.printString("Game speed",0,0); ///print settings menu
Nikollao 20:59aa6189a47d 590 lcd.printString("Lcd:Normal",0,1);
Nikollao 20:59aa6189a47d 591 lcd.printString("Sound:ON",0,2);
Nikollao 21:6b02ef341358 592 lcd.printString("Back",0,4);
Nikollao 18:5405ee7e243b 593 } else {
Nikollao 18:5405ee7e243b 594 lcd.printString("Game speed",0,0); ///print settings menu
Nikollao 20:59aa6189a47d 595 lcd.printString("Lcd:Normal",0,1);
Nikollao 20:59aa6189a47d 596 lcd.printString("Sound:OFF",0,2);
Nikollao 21:6b02ef341358 597 lcd.printString("Back",0,4);
Nikollao 18:5405ee7e243b 598 }
Nikollao 18:5405ee7e243b 599 }
Nikollao 18:5405ee7e243b 600 switch (joystick.direction) {
Nikollao 18:5405ee7e243b 601 case UP:
Nikollao 18:5405ee7e243b 602 option--;
Nikollao 18:5405ee7e243b 603 break;
Nikollao 18:5405ee7e243b 604 case DOWN:
Nikollao 18:5405ee7e243b 605 option++;
Nikollao 18:5405ee7e243b 606 break;
Nikollao 18:5405ee7e243b 607 case CENTRE:
Nikollao 18:5405ee7e243b 608 option = option;
Nikollao 18:5405ee7e243b 609 break;
Nikollao 18:5405ee7e243b 610 }
Nikollao 18:5405ee7e243b 611 if (option < 0) { ///if selector is on the top of the screen and UP is pressed, selector goes to the bottom
Nikollao 18:5405ee7e243b 612 option = 3;
Nikollao 18:5405ee7e243b 613 }
Nikollao 18:5405ee7e243b 614 if (option > 3) { ///if selector is on the bottom of the screen and DOWN is pressed, selector goes to the top
Nikollao 18:5405ee7e243b 615 option = 0;
Nikollao 18:5405ee7e243b 616 }
Nikollao 18:5405ee7e243b 617
Nikollao 18:5405ee7e243b 618 /// menu selection depends on the position of the Joystick
Nikollao 18:5405ee7e243b 619 if (option == 0) {
Nikollao 18:5405ee7e243b 620 lcd.drawCircle(70,4,2,1);
Nikollao 18:5405ee7e243b 621 } else if (option == 1) {
Nikollao 18:5405ee7e243b 622 lcd.drawCircle(70,12,2,1);
Nikollao 18:5405ee7e243b 623 } else if (option == 2) {
Nikollao 18:5405ee7e243b 624 lcd.drawCircle(70,20,2,1);
Nikollao 18:5405ee7e243b 625 } else {
Nikollao 21:6b02ef341358 626 lcd.drawCircle(35,36,2,1);
Nikollao 18:5405ee7e243b 627 }
Nikollao 18:5405ee7e243b 628 if (g_button_flag) { ///if button is pressed
Nikollao 18:5405ee7e243b 629 g_button_flag = 0; ///reset button
Nikollao 18:5405ee7e243b 630
Nikollao 18:5405ee7e243b 631 if (option == 0) { /// if user selects to modify speed of the game
Nikollao 18:5405ee7e243b 632
Nikollao 18:5405ee7e243b 633 while(1) { ///set speed of the game
Nikollao 18:5405ee7e243b 634
Nikollao 18:5405ee7e243b 635 if (g_game_timer_flag) {
Nikollao 18:5405ee7e243b 636 g_game_timer_flag = 0;
Nikollao 18:5405ee7e243b 637 updateJoystick();
Nikollao 18:5405ee7e243b 638 lcd.clear();
Nikollao 18:5405ee7e243b 639 if (speed == 0) {
Nikollao 21:6b02ef341358 640 lcd.printString("Slow<",0,0); ///print game speed menu
Nikollao 18:5405ee7e243b 641 lcd.printString("Normal",0,1);
Nikollao 18:5405ee7e243b 642 lcd.printString("Fast",0,2);
Nikollao 21:6b02ef341358 643 lcd.printString("Back",0,4);
Nikollao 18:5405ee7e243b 644 } else if (speed == 1) {
Nikollao 21:6b02ef341358 645 lcd.printString("Slow",0,0); //print game speed menu
Nikollao 21:6b02ef341358 646 lcd.printString("Normal<",0,1);
Nikollao 18:5405ee7e243b 647 lcd.printString("Fast",0,2);
Nikollao 21:6b02ef341358 648 lcd.printString("Back",0,4);
Nikollao 18:5405ee7e243b 649 } else if (speed == 2) {
Nikollao 21:6b02ef341358 650 lcd.printString("Slow",0,0); //print game speed menu
Nikollao 18:5405ee7e243b 651 lcd.printString("Normal",0,1);
Nikollao 21:6b02ef341358 652 lcd.printString("Fast<",0,2);
Nikollao 21:6b02ef341358 653 lcd.printString("Back",0,4);
Nikollao 18:5405ee7e243b 654 }
Nikollao 18:5405ee7e243b 655
Nikollao 18:5405ee7e243b 656 switch (joystick.direction) {
Nikollao 18:5405ee7e243b 657 case UP:
Nikollao 18:5405ee7e243b 658 option--;
Nikollao 18:5405ee7e243b 659 break;
Nikollao 18:5405ee7e243b 660 case DOWN:
Nikollao 18:5405ee7e243b 661 option++;
Nikollao 18:5405ee7e243b 662 break;
Nikollao 18:5405ee7e243b 663 case CENTRE:
Nikollao 18:5405ee7e243b 664 option = option;
Nikollao 18:5405ee7e243b 665 break;
Nikollao 18:5405ee7e243b 666 }
Nikollao 18:5405ee7e243b 667 if (option < 0) {
Nikollao 18:5405ee7e243b 668 option = 3;
Nikollao 18:5405ee7e243b 669 }
Nikollao 18:5405ee7e243b 670 if (option > 3) {
Nikollao 18:5405ee7e243b 671 option = 0;
Nikollao 18:5405ee7e243b 672 }
Nikollao 18:5405ee7e243b 673
Nikollao 18:5405ee7e243b 674 if (option == 0) {
Nikollao 18:5405ee7e243b 675 lcd.drawCircle(70,4,2,1);
Nikollao 18:5405ee7e243b 676 } else if (option == 1) {
Nikollao 18:5405ee7e243b 677 lcd.drawCircle(55,12,2,1);
Nikollao 18:5405ee7e243b 678 } else if (option == 2) {
Nikollao 18:5405ee7e243b 679 lcd.drawCircle(35,20,2,1);
Nikollao 18:5405ee7e243b 680 } else {
Nikollao 21:6b02ef341358 681 lcd.drawCircle(35,36,2,1);
Nikollao 18:5405ee7e243b 682 }
Nikollao 18:5405ee7e243b 683
Nikollao 18:5405ee7e243b 684 if (g_button_flag) {
Nikollao 18:5405ee7e243b 685 g_button_flag = 0;
Nikollao 18:5405ee7e243b 686
Nikollao 18:5405ee7e243b 687 if (option == 0) {
Nikollao 21:6b02ef341358 688 game_speed = 0.1; ///slow
Nikollao 18:5405ee7e243b 689 speed = 0;
Nikollao 18:5405ee7e243b 690 } else if (option == 1) {
Nikollao 21:6b02ef341358 691 game_speed = 0.05; ///normal
Nikollao 18:5405ee7e243b 692 speed = 1;
Nikollao 18:5405ee7e243b 693 } else if (option == 2) {
Nikollao 21:6b02ef341358 694 game_speed = 0.04; ///fast
Nikollao 18:5405ee7e243b 695 speed = 2;
Nikollao 18:5405ee7e243b 696 } else if (option == 3) {
Nikollao 18:5405ee7e243b 697 break;
Nikollao 18:5405ee7e243b 698 }
Nikollao 18:5405ee7e243b 699 }
Nikollao 18:5405ee7e243b 700 lcd.refresh();
Nikollao 18:5405ee7e243b 701 }
Nikollao 18:5405ee7e243b 702 sleep();
Nikollao 18:5405ee7e243b 703 }
Nikollao 18:5405ee7e243b 704 } else if (option == 1) { /// Lcd inverse mode
Nikollao 18:5405ee7e243b 705 normal =! normal;
Nikollao 18:5405ee7e243b 706 if (normal) {
Nikollao 18:5405ee7e243b 707 lcd.inverseMode();
Nikollao 18:5405ee7e243b 708 } else {
Nikollao 18:5405ee7e243b 709 lcd.normalMode();
Nikollao 18:5405ee7e243b 710 }
Nikollao 18:5405ee7e243b 711 } else if (option == 2) { ///select sound or not
Nikollao 18:5405ee7e243b 712
Nikollao 18:5405ee7e243b 713 sound =! sound;
Nikollao 18:5405ee7e243b 714
Nikollao 21:6b02ef341358 715 if (sound) {
Nikollao 21:6b02ef341358 716 //buzzer.write(0.5);
Nikollao 21:6b02ef341358 717 // buzzer.period(1/4000);
Nikollao 21:6b02ef341358 718 }
Nikollao 21:6b02ef341358 719 else {
Nikollao 18:5405ee7e243b 720 buzzer.write(0.0);
Nikollao 21:6b02ef341358 721 }
Nikollao 18:5405ee7e243b 722 // buzzer.period(1/400);
Nikollao 18:5405ee7e243b 723 } else if(option == 3) { ///go back to main menu
Nikollao 18:5405ee7e243b 724 break;
Nikollao 18:5405ee7e243b 725 }
Nikollao 18:5405ee7e243b 726 }
Nikollao 18:5405ee7e243b 727 lcd.refresh();
Nikollao 18:5405ee7e243b 728 }
Nikollao 18:5405ee7e243b 729 sleep();
Nikollao 18:5405ee7e243b 730 }
Nikollao 18:5405ee7e243b 731 } else if (option == 2) { ///turn off LED
Nikollao 18:5405ee7e243b 732 lcd.clear();
Nikollao 18:5405ee7e243b 733 lcd.turnOff();
Nikollao 18:5405ee7e243b 734 while(1) {
Nikollao 18:5405ee7e243b 735 deepsleep();
Nikollao 18:5405ee7e243b 736 }
Nikollao 18:5405ee7e243b 737 }
Nikollao 18:5405ee7e243b 738 }
Nikollao 18:5405ee7e243b 739 lcd.refresh();
Nikollao 18:5405ee7e243b 740 }
Nikollao 18:5405ee7e243b 741 sleep();
Nikollao 18:5405ee7e243b 742 }
Nikollao 18:5405ee7e243b 743 }
Nikollao 18:5405ee7e243b 744
Nikollao 18:5405ee7e243b 745 int intersection(int i, int j)
Nikollao 18:5405ee7e243b 746 {
Nikollao 18:5405ee7e243b 747 /// check for overlap between enemies and hero
Nikollao 18:5405ee7e243b 748 n=0;
Nikollao 18:5405ee7e243b 749
Nikollao 18:5405ee7e243b 750 if (lcd.getPixel(i-1,j-1)!=0) //pixel to the top-left
Nikollao 18:5405ee7e243b 751 n++; // increase n by 1
Nikollao 18:5405ee7e243b 752 if (lcd.getPixel(i-1,j)!=0) //pixel to the left
Nikollao 18:5405ee7e243b 753 n++; // increase n by 1
Nikollao 18:5405ee7e243b 754 if (lcd.getPixel(i-1,j+1)!=0) //pixel to the bottom-left
Nikollao 18:5405ee7e243b 755 n++; // increase n by 1
Nikollao 18:5405ee7e243b 756 if (lcd.getPixel(i,j-1)!=0) // pixel to the top
Nikollao 18:5405ee7e243b 757 n++; // increase n by 1
Nikollao 18:5405ee7e243b 758 if (lcd.getPixel(i,j+1)!=0) //pixel to the bottom
Nikollao 18:5405ee7e243b 759 n++; // increase n by 1
Nikollao 18:5405ee7e243b 760 if (lcd.getPixel(i+1,j-1)!=0) //pixel to the top-right
Nikollao 18:5405ee7e243b 761 n++; // increase n by 1
Nikollao 18:5405ee7e243b 762 if (lcd.getPixel(i+1,j)!=0) // pixel to the right
Nikollao 18:5405ee7e243b 763 n++; // increase n by 1
Nikollao 18:5405ee7e243b 764 if (lcd.getPixel(i+1,j+1)!=0) //pixel to the bottom right
Nikollao 18:5405ee7e243b 765 n++; // increase n by 1
Nikollao 18:5405ee7e243b 766 return n;
Nikollao 18:5405ee7e243b 767 }
Nikollao 18:5405ee7e243b 768
Nikollao 18:5405ee7e243b 769 void checkOverlap()
Nikollao 18:5405ee7e243b 770 {
Nikollao 18:5405ee7e243b 771
Nikollao 21:6b02ef341358 772 for (int i=40+heroX; i<50+heroX; i++) { ///loop follows the hero and checks for overlap around him
Nikollao 18:5405ee7e243b 773 for (int j=35+heroY; j<48+heroY; j++) {
Nikollao 18:5405ee7e243b 774
Nikollao 21:6b02ef341358 775 int check = intersection(i,j); ///int gets number of pixels around the hero
Nikollao 18:5405ee7e243b 776 //lcd.setPixel(i,j);
Nikollao 18:5405ee7e243b 777 char bit[50];
Nikollao 18:5405ee7e243b 778 sprintf(bit,"Pixels: %d",check);
Nikollao 18:5405ee7e243b 779
Nikollao 18:5405ee7e243b 780
Nikollao 21:6b02ef341358 781 for(int i = 40; i < 50; i++) { ///provides safety if hero is in default position he doesn't get killed by enemies
Nikollao 18:5405ee7e243b 782 for (int j = 35; j < 47; j++) {
Nikollao 18:5405ee7e243b 783
Nikollao 21:6b02ef341358 784 if (lcd.getPixel(i,j) && check > 6) { ///6 is the offset of the neigbours, as hero has his own neighbours pixels
Nikollao 18:5405ee7e243b 785 check = 6;
Nikollao 18:5405ee7e243b 786 }
Nikollao 18:5405ee7e243b 787 }
Nikollao 18:5405ee7e243b 788 }
Nikollao 18:5405ee7e243b 789 if (check > 7) {
Nikollao 18:5405ee7e243b 790 tries--;
Nikollao 18:5405ee7e243b 791 //if tries
Nikollao 21:6b02ef341358 792 lcd.printString("BANG!",0,0); ///Bang message is displayed when overlap has occured
Nikollao 21:6b02ef341358 793 if (sound) {
Nikollao 21:6b02ef341358 794 buzzer.write(0.5);
Nikollao 21:6b02ef341358 795 //buzzer.pulsewidth(0.9);
Nikollao 21:6b02ef341358 796 } else {
Nikollao 21:6b02ef341358 797 buzzer.write(0.0);
Nikollao 21:6b02ef341358 798 }
Nikollao 18:5405ee7e243b 799 lcd.refresh();
Nikollao 18:5405ee7e243b 800 wait(2);
Nikollao 18:5405ee7e243b 801 lcd.clear();
Nikollao 18:5405ee7e243b 802
Nikollao 18:5405ee7e243b 803 if (tries == 2) {
Nikollao 21:6b02ef341358 804
Nikollao 21:6b02ef341358 805 g_button1_flag = 0;
Nikollao 18:5405ee7e243b 806 lcd.printString("Ready.",20,1);
Nikollao 18:5405ee7e243b 807 lcd.refresh();
Nikollao 18:5405ee7e243b 808 wait(0.5);
Nikollao 18:5405ee7e243b 809 lcd.printString("Ready..",20,1);
Nikollao 18:5405ee7e243b 810 lcd.refresh();
Nikollao 18:5405ee7e243b 811 wait(0.5);
Nikollao 18:5405ee7e243b 812 lcd.printString("Ready...",20,1);
Nikollao 18:5405ee7e243b 813 lcd.refresh();
Nikollao 18:5405ee7e243b 814 wait(1);
Nikollao 21:6b02ef341358 815 lcd.printString("GO!",31,3);
Nikollao 18:5405ee7e243b 816 lcd.refresh();
Nikollao 18:5405ee7e243b 817 wait(1);
Nikollao 18:5405ee7e243b 818 lcd.clear();
Nikollao 18:5405ee7e243b 819 fsm_state = 1; ///2 lives left
Nikollao 18:5405ee7e243b 820 } else if (tries == 1) {
Nikollao 21:6b02ef341358 821
Nikollao 21:6b02ef341358 822 g_button1_flag = 0;
Nikollao 18:5405ee7e243b 823 lcd.clear();
Nikollao 18:5405ee7e243b 824 lcd.printString("Last Chance!",10,2);
Nikollao 18:5405ee7e243b 825 lcd.refresh();
Nikollao 18:5405ee7e243b 826 wait(1);
Nikollao 18:5405ee7e243b 827 fsm_state = 2; //1 life left
Nikollao 18:5405ee7e243b 828 } else if (tries == 0) {
Nikollao 21:6b02ef341358 829
Nikollao 21:6b02ef341358 830 g_button1_flag = 0;
Nikollao 18:5405ee7e243b 831 fsm_state = 3;
Nikollao 18:5405ee7e243b 832 led_output=fsm[fsm_state];
Nikollao 18:5405ee7e243b 833 lcd.clear();
Nikollao 18:5405ee7e243b 834 lcd.printString("Game Over!",10,2);
Nikollao 21:6b02ef341358 835 timer.stop();
Nikollao 21:6b02ef341358 836 timer.reset();
Nikollao 18:5405ee7e243b 837 lcd.refresh();
Nikollao 18:5405ee7e243b 838 wait(2);
Nikollao 18:5405ee7e243b 839
Nikollao 19:70d40dac5ae0 840 ticker.detach();
Nikollao 21:6b02ef341358 841 game_ticker.attach(&game_timer_isr,0.2); ///attach a slower ticker to save power while not playing
Nikollao 18:5405ee7e243b 842 lcd.clear();
Nikollao 19:70d40dac5ae0 843 lcd.printString("Play again!",0,0);
Nikollao 18:5405ee7e243b 844 lcd.drawCircle(70,4,2,1);
Nikollao 18:5405ee7e243b 845 lcd.refresh();
Nikollao 18:5405ee7e243b 846 g_button_flag = 0;
Nikollao 18:5405ee7e243b 847
Nikollao 18:5405ee7e243b 848 while(1) {
Nikollao 21:6b02ef341358 849 if (g_button1_flag) { ///if button1 is pressed
Nikollao 18:5405ee7e243b 850
Nikollao 21:6b02ef341358 851 g_button1_flag = 0; ///reset button1 flag
Nikollao 21:6b02ef341358 852 level = 0; ///reset parameters
Nikollao 18:5405ee7e243b 853 tries = 3;
Nikollao 18:5405ee7e243b 854 fsm_state = 0;
Nikollao 18:5405ee7e243b 855 led_output=fsm[fsm_state];
Nikollao 21:6b02ef341358 856 buzzer.write(0.0);
Nikollao 18:5405ee7e243b 857 lcd.clear();
Nikollao 21:6b02ef341358 858 ///reset values
Nikollao 19:70d40dac5ae0 859 heroX = 0;
Nikollao 19:70d40dac5ae0 860 heroY = 0;
Nikollao 19:70d40dac5ae0 861 rectY = 0;
Nikollao 19:70d40dac5ae0 862 circleX = 0;
Nikollao 21:6b02ef341358 863 main(); ///call main
Nikollao 18:5405ee7e243b 864 }
Nikollao 18:5405ee7e243b 865 sleep();
Nikollao 18:5405ee7e243b 866 }
Nikollao 18:5405ee7e243b 867
Nikollao 18:5405ee7e243b 868 }
Nikollao 18:5405ee7e243b 869 heroX = 0;
Nikollao 18:5405ee7e243b 870 heroY = 0;
Nikollao 18:5405ee7e243b 871 rectY = 0;
Nikollao 18:5405ee7e243b 872 circleX = 0;
Nikollao 18:5405ee7e243b 873 led_output=fsm[fsm_state];
Nikollao 21:6b02ef341358 874 buzzer.write(0.0);
Nikollao 18:5405ee7e243b 875 }
Nikollao 18:5405ee7e243b 876
Nikollao 18:5405ee7e243b 877 }
Nikollao 18:5405ee7e243b 878 }
Nikollao 18:5405ee7e243b 879 }
Nikollao 18:5405ee7e243b 880
Nikollao 18:5405ee7e243b 881
Nikollao 18:5405ee7e243b 882
Nikollao 18:5405ee7e243b 883
Nikollao 18:5405ee7e243b 884
Nikollao 21:6b02ef341358 885