ELEC2645 (2015/16)
/
Dodgemania
Wall dodging game utilising a joystick and Nokia 5110 LCD display
Diff: main.cpp
- Revision:
- 9:ca800196baeb
- Parent:
- 8:b49e0cc362f2
- Child:
- 10:dd2886f3ac0a
--- a/main.cpp Sun Apr 24 00:42:45 2016 +0000 +++ b/main.cpp Sun Apr 24 16:17:55 2016 +0000 @@ -203,6 +203,7 @@ blink(); while(1) { + g_button_flag = 0; joyspeed = 10; game_timer.detach(); @@ -217,6 +218,7 @@ while(game_start_flag == 0) // ticker interrupt if (g_timer_flag) { g_timer_flag = 0; // clear flag + if (joystick.direction == UP) { direction = 2; } else if (joystick.direction == DOWN) { @@ -224,6 +226,7 @@ } else { direction = 0; } + output = fsm[state].output; state = fsm[state].next_state[direction]; @@ -244,21 +247,32 @@ lcd.drawCircle(6,35,2,1); } lcd.refresh(); + + if (((direction == 1)&&(output != 8))||((direction == 2)&&(output != 1))) { + playNote(NOTE_G2,0.05); + } if (g_button_flag) { - g_button_flag = 0; if (output == 1) { + g_button_flag = 0; + playNote(NOTE_B2,0.1); initGame(); game_start_flag = 1; } else if (output == 2) { + g_button_flag = 0; + playNote(NOTE_B2,0.1); initGame(); counter = 1501; game_start_flag = 1; } else if (output == 4) { + g_button_flag = 0; + playNote(NOTE_B2,0.1); blink(); lcd.clear(); printHelp(); } else if (output == 8) { + g_button_flag = 0; + playNote(NOTE_B2,0.1); blink(); lcd.clear(); printScores(); @@ -267,11 +281,8 @@ } blink(); lcd.clear(); - - // Draw game border draw_border(); - // Countdown wait(0.5); lcd.printString("3",40,2); @@ -313,9 +324,7 @@ moveBall(); invincible(); checkBorderCollision(); - if (mortal) { - checkWallCollision(); - } + checkWallCollision(); updateScreen(); warning(); debug(); @@ -388,8 +397,7 @@ wait(0.1); lcd.printString("CONGRATS!!",13,5); playNote(NOTE_E2,0.2); - wait(1.5); - + } else { playNote(NOTE_A2,0.05); wait(0.05); @@ -398,10 +406,17 @@ playNote(NOTE_A2,0.05); wait(0.05); playNote(NOTE_A2,0.3); - wait(5.0); } - blink(); - lcd.clear(); + lcd.refresh(); + while(1) { + if (g_button_flag) { + g_button_flag = 0; + playNote(NOTE_E2,0.1); + blink(); + lcd.clear(); + break; + } + } game_start_flag = 0; //return 0; @@ -598,25 +613,27 @@ void checkWallCollision() // checks for any collisions (i.e. player has hit a wall or side of the screen) { + if (mortal) { - // LEFT WALL - if ((((i - PLAYERRADIUS) <= leftwall.x) && (leftwall.x <= (i + PLAYERRADIUS))) && (j > (leftwall.y+3) || j < (leftwall.y-3))) { - game_over_flag = 1; - } + // LEFT WALL + if ((((i - PLAYERRADIUS) <= leftwall.x) && (leftwall.x <= (i + PLAYERRADIUS))) && (j > (leftwall.y+3) || j < (leftwall.y-3))) { + game_over_flag = 1; + } - // RIGHT WALL - if ((((i - PLAYERRADIUS) <= rightwall.x) && (rightwall.x <= (i + PLAYERRADIUS))) && (j > (rightwall.y+3) || j < (rightwall.y-3))) { - game_over_flag = 1; - } + // RIGHT WALL + if ((((i - PLAYERRADIUS) <= rightwall.x) && (rightwall.x <= (i + PLAYERRADIUS))) && (j > (rightwall.y+3) || j < (rightwall.y-3))) { + game_over_flag = 1; + } - // DOWN WALL - if ((((j - PLAYERRADIUS) <= downwall.y) && (downwall.y <= (j - PLAYERRADIUS))) && (i > (downwall.x+9) || i < (downwall.x-9))) { // if player x is 4 or more than wall x, it has missed the gap - game_over_flag = 1; - } + // DOWN WALL + if ((((j - PLAYERRADIUS) <= downwall.y) && (downwall.y <= (j - PLAYERRADIUS))) && (i > (downwall.x+9) || i < (downwall.x-9))) { // if player x is 4 or more than wall x, it has missed the gap + game_over_flag = 1; + } - // UP WALL - if (((j + PLAYERRADIUS) == upwall.y || (j - PLAYERRADIUS) == upwall.y) && (i > (upwall.x+9) || i < (upwall.x-9))) { // if player x is 4 or more than wall x, it has missed the gap - game_over_flag = 1; + // UP WALL + if (((j + PLAYERRADIUS) == upwall.y || (j - PLAYERRADIUS) == upwall.y) && (i > (upwall.x+9) || i < (upwall.x-9))) { // if player x is 4 or more than wall x, it has missed the gap + game_over_flag = 1; + } } } @@ -765,38 +782,12 @@ { if (printFlag) { // if flag set, clear flag and print joystick values to serial port printFlag = 0; - /* - if (joystick.direction == UP) - serial.printf(" UP\n"); - if (joystick.direction == DOWN) - serial.printf(" DOWN\n"); - if (joystick.direction == LEFT) - serial.printf(" LEFT\n"); - if (joystick.direction == RIGHT) - serial.printf(" RIGHT\n"); - if (joystick.direction == UPRIGHT) - serial.printf(" UPRIGHT\n"); // Diagonally up + right - if (joystick.direction == UPLEFT) - serial.printf(" UPLEFT\n"); // Diagonally up + left - if (joystick.direction == DOWNRIGHT) - serial.printf(" DOWNRIGHT\n"); // Diagonally down + right - if (joystick.direction == DOWNLEFT) - serial.printf(" DOWNLEFT\n"); // Diagonally down + left - if (joystick.direction == CENTRE) - serial.printf(" CENTRE\n"); - if (joystick.direction == UNKNOWN) - serial.printf(" Unsupported direction\n"); - */ serial.printf("saves = %d \n",saves); - //serial.printf("Right-wall Cooldown = %d \n",rightwall.cooldown); - //serial.printf("counter = %d \n",counter); - } } void initSerial() // sets baud rate for serial { - // set to highest baud - ensure terminal software matches serial.baud(115200); } void initGame() @@ -805,11 +796,11 @@ g_button_flag = 0; leftwall.y = rand() % 27+8; leftwall.x = 0; - rightwall.y = rand() % 58+13; + rightwall.y = rand() % 27+8; rightwall.x = 6; - downwall.x = rand() % 27+8; + downwall.x = rand() % 58+13; downwall.y = 0; - upwall.x = rand() % 5814; + upwall.x = rand() % 58+13; upwall.y = 0; rightwall.cooldown = 61; counter = 0; @@ -831,87 +822,125 @@ void printHelp() { - lcd.printString("Try and",0,1); - lcd.printString("survive for",0,2); - lcd.printString("as long as ",0,3); - lcd.printString("possible!",0,4); - wait(4); - - blink(); - lcd.clear(); - - lcd.printString("Get through",0,1); - lcd.printString("the holes in",0,2); - lcd.printString("the walls with",0,3); - lcd.printString("the joystick",0,4); - wait(4); - - blink(); - lcd.clear(); - - lcd.printString("Press the",0,1); - lcd.printString("stick to",0,2); - lcd.printString("be briefly",0,3); - lcd.printString("untouchable",0,4); - wait(4); - - blink(); - lcd.clear(); - - lcd.printString("But don't use",0,1); - lcd.printString("it too much!",0,2); - lcd.printString("you can only",0,3); - lcd.printString("do it 5 times",0,4); - wait(4); - - blink(); - lcd.clear(); - - lcd.printString("Lastly, keep",0,1); - lcd.printString("an eye on the",0,2); - lcd.printString("borders of",0,3); - lcd.printString("the screen...",0,4); - wait(4); - - blink(); - lcd.clear(); - - lcd.printString("See where the",0,1); - lcd.printString("holes are",0,2); - lcd.printString("before the",0,3); - lcd.printString("walls move!",0,4); - wait(4); - - blink(); - lcd.clear(); + while (1) { + lcd.printString("Try and",0,1); + lcd.printString("survive for",0,2); + lcd.printString("as long as ",0,3); + lcd.printString("possible!",0,4); + lcd.refresh(); + if (g_button_flag) { + //g_button_flag = 0; + playNote(NOTE_E2,0.1); + blink(); + lcd.clear(); + break; + } + } + g_button_flag = 0; + while(1) { + lcd.printString("Get through",0,1); + lcd.printString("the holes in",0,2); + lcd.printString("the walls with",0,3); + lcd.printString("the joystick",0,4); + lcd.refresh(); + if (g_button_flag) { + //g_button_flag = 0; + playNote(NOTE_E2,0.1); + blink(); + lcd.clear(); + break; + } + } + g_button_flag = 0; + while(1) { + lcd.printString("Press the",0,1); + lcd.printString("stick to",0,2); + lcd.printString("be briefly",0,3); + lcd.printString("untouchable",0,4); + lcd.refresh(); + if (g_button_flag) { + // g_button_flag = 0; + playNote(NOTE_E2,0.1); + blink(); + lcd.clear(); + break; + } + } + g_button_flag = 0; + while(1) { + lcd.printString("But don't use",0,1); + lcd.printString("it too much!",0,2); + lcd.printString("you can only",0,3); + lcd.printString("do it 5 times",0,4); + lcd.refresh(); + if (g_button_flag) { + // g_button_flag = 0; + playNote(NOTE_E2,0.1); + blink(); + lcd.clear(); + break; + } + } + g_button_flag = 0; + while(1) { + lcd.printString("Lastly, keep",0,1); + lcd.printString("an eye on the",0,2); + lcd.printString("borders of",0,3); + lcd.printString("the screen...",0,4); + lcd.refresh(); + if (g_button_flag) { + // g_button_flag = 0; + playNote(NOTE_E2,0.1); + blink(); + lcd.clear(); + break; + } + } + g_button_flag = 0; + while(1) { + lcd.printString("See where the",0,1); + lcd.printString("holes are",0,2); + lcd.printString("before the",0,3); + lcd.printString("walls move!",0,4); + lcd.refresh(); + if (g_button_flag) { + g_button_flag = 0; + playNote(NOTE_E2,0.1); + blink(); + lcd.clear(); + break; + } + } } void printScores() { - lcd.printString("Highscores",13,0); - lcd.printString("1st:",6,2); - lcd.printString("2nd:",6,3); - lcd.printString("3rd:",6,4); - char first[14]; - char second[14]; - char third[14]; - int one = sprintf(first,"%d",hiscore.one); - int two = sprintf(second,"%d",hiscore.two); - int three = sprintf(third,"%d",hiscore.three); + while(1) { + lcd.printString("Highscores",13,0); + lcd.printString("1st:",6,2); + lcd.printString("2nd:",6,3); + lcd.printString("3rd:",6,4); + char first[14]; + char second[14]; + char third[14]; + int one = sprintf(first,"%d",hiscore.one); + int two = sprintf(second,"%d",hiscore.two); + int three = sprintf(third,"%d",hiscore.three); - lcd.printString(first,30,2); - + lcd.printString(first,30,2); + lcd.printString(second,30,3); + lcd.printString(third,30,4); - lcd.printString(second,30,3); - + lcd.refresh(); - lcd.printString(third,30,4); - - lcd.refresh(); - wait(4); - blink(); - lcd.clear(); - + if (g_button_flag) { + g_button_flag = 0; + playNote(NOTE_E2,0.1); + blink(); + lcd.clear(); + break; + } + } } void draw_border() // draws game border