Final Trivia Game Code
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed wave_player
Fork of TriviaGame by
Diff: main.cpp
- Revision:
- 3:9842b3f1d809
- Parent:
- 1:1d5055ca2793
- Child:
- 4:55bb76d28153
diff -r 1d47dc5191a5 -r 9842b3f1d809 main.cpp --- a/main.cpp Fri Nov 04 15:44:32 2016 +0000 +++ b/main.cpp Sun Dec 11 03:09:22 2016 +0000 @@ -4,6 +4,11 @@ #include "wave_player.h" #include "mpr121.h" #include <vector> +#include <string> +#include "stdlib.h" +#include "stdio.h" + + AnalogOut DACout(p18); // used to play sound on speaker //wave player plays a *.wav file to D/A and a PWM @@ -11,6 +16,7 @@ InterruptIn interrupt(p26); I2C i2c(p9, p10); Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); +Serial pc(USBTX, USBRX); uLCD_4DGL uLCD(p28,p27,p29); // serial tx, serial rx, reset pin; SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card setup @@ -22,7 +28,33 @@ bool check = 0; int value = 0; std::vector<int> v; +Timer t; +float tr = 0; +int mode = 0; +int counter = 0; +int cScore = 0; +int tScore = 0; +int nScore = 0; +int tHighScore = 0; +int nHighScore = 0; +int cHighScore = 0; +string game; +char nHigh[2]; +char tHigh[2]; +char cHigh[2]; + void itoa(int n, char s[]) + { + int i, sign; + + if ((sign = n) < 0) /* record sign */ + n = -n; /* make n positive */ + i = 0; + do { /* generate digits in reverse order */ + s[i++] = n % 10 + '0'; /* get next digit */ + } while ((n /= 10) > 0); /* delete it */ + + } int main() { @@ -34,27 +66,110 @@ } check = 0; question = 0; - uLCD.baudrate(3000000); + uLCD.baudrate(30); uLCD.background_color(BLACK); uLCD.color(BLUE); uLCD.cls(); - if (Score > HighScore){ - HighScore = Score; - uLCD.printf("NEW HIGH SCORE!\n %d", HighScore); + if (mode == 1){ + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x001D); + uLCD.display_image(0,0); + wait(2); + uLCD.cls(); + uLCD.printf("Final Score:%d\n", nScore); + if (nScore > nHighScore){ + nHighScore = nScore; + uLCD.printf("NEW NORMAL\n HIGH SCORE!\n %d", nHighScore); + + wait(2); + } + itoa(nHighScore, nHigh); + pc.puts(nHigh); + uLCD.printf("\nClick New Game"); + } + if (mode == 2){ + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x001D); + uLCD.display_image(0,0); + wait(2); + uLCD.cls(); + uLCD.printf("Final Score:%d\n", tScore); + if (tScore > tHighScore){ + tHighScore = tScore; + uLCD.printf("NEW TIMED\n HIGH SCORE!\n %d", tHighScore); + wait(2); + } + + itoa(tHighScore, tHigh); + pc.puts(tHigh); + uLCD.printf("\nClick New Game"); + } + if (mode == 4){ + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x001D); + uLCD.display_image(0,0); + wait(2); + uLCD.cls(); + uLCD.printf("Final Score: %d\n", cScore); + if (cScore > cHighScore){ + cHighScore = cScore; + uLCD.printf("NEW CHALLENGE\n HIGH SCORE!\n %d", cHighScore); wait(2); } - Score = 0; + itoa(cHighScore, cHigh); + pc.puts(cHigh); + uLCD.printf("\nClick New Game"); + } + wait(2); + uLCD.cls(); + nScore = 0; + tScore=0; + cScore=0; + counter = 0; + uLCD.cls(); - uLCD.printf("\nTrivia Game \n Main Menu\n\n"); - uLCD.printf("High Score: %d \n", HighScore); - uLCD.printf("\nFor New Game \n Press 0"); - while (value != 1){ - value=mpr121.read(0x00); - value +=mpr121.read(0x01)<<8; + uLCD.printf("Choose a Game Mode"); + //uLCD.printf("High Score: %d \n", HighScore); + //uLCD.printf("\n0) Normal Game\n High Score:%d\n1) Timed Game\n High Score:%d\n2)Challenge Game\n High Score:%d", nHighScore, tHighScore, cHighScore); + while (1){ + game = pc.getc(); + //mode=mpr121.read(0x00); +// mode +=mpr121.read(0x01)<<8; + if (game == "1"){ + mode = 1; + break; + } + if (game == "2"){ + mode = 2; + break; } + if (game == "4"){ + mode = 4; + break; + } + } + uLCD.cls(); + t.reset(); + t.stop(); + t.start(); + //end of main menu if (v.size() !=0){ while (answer == 1){ + if (mode == 2){ + tr = t.read(); + if (tr > 60){ + break; + } + } + if (mode == 4){ + if (counter > 3){ + break; + } + } if (v.size() == 0){ break; } @@ -63,6 +178,9 @@ v.erase(v.begin()+index); check = 0; uLCD.cls(); + if (mode == 1){ + uLCD.printf("Score: %d", nScore); + } wait(1); if (question == 1){ value = 0; @@ -79,7 +197,7 @@ //play game over uLCD.cls(); uLCD.media_init(); - uLCD.set_sector_address(0x0000, 0x009F); + uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); @@ -87,7 +205,11 @@ fclose(wave_file); wait(1); uLCD.cls(); + if (mode == 1){ answer = 0; + } + counter = counter +1; + check = 1; } if (value == 4){ @@ -102,17 +224,27 @@ fclose(wave_file); wait(1); uLCD.cls(); - Score = Score+1; - uLCD.printf("Score: %d", Score); + if (mode == 1){ + nScore = nScore+1; + } + if (mode == 2){ + tScore = tScore+1; + } + if (mode == 4){ + cScore = cScore+1; + } wait(1); + if (mode == 1){ answer = 1; + } + counter=counter+1; check = 1; } if (value == 8){ //play game over uLCD.cls(); uLCD.media_init(); - uLCD.set_sector_address(0x0000, 0x009F); + uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); @@ -120,7 +252,10 @@ fclose(wave_file); wait(1); uLCD.cls(); + if (mode == 1){ answer = 0; + } + counter = counter+1; check = 1; } } @@ -136,11 +271,11 @@ while (check == 0){ int value=mpr121.read(0x00); value +=mpr121.read(0x01)<<8; - if (value == 4){ + if (value == 2){ //play game over uLCD.cls(); uLCD.media_init(); - uLCD.set_sector_address(0x0000, 0x009F); + uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); @@ -148,10 +283,14 @@ fclose(wave_file); wait(1); uLCD.cls(); + if (mode == 1){ answer = 0; + } + counter = counter +1; + check = 1; } - if (value == 2){ + if (value == 4){ //play correct image uLCD.cls(); uLCD.media_init(); @@ -163,17 +302,28 @@ fclose(wave_file); wait(1); uLCD.cls(); - Score=Score+1;; - uLCD.printf("Score: %d", Score); + if (mode == 1){ + nScore = nScore+1; + } + if (mode == 2){ + tScore = tScore+1; + } + if (mode == 4){ + cScore = cScore+1; + } + //uLCD.printf("Score: %d", Score); wait(1); + if (mode == 1){ answer = 1; + } + counter=counter+1; check = 1; } if (value == 8){ //play game over uLCD.cls(); uLCD.media_init(); - uLCD.set_sector_address(0x0000, 0x009F); + uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); @@ -181,12 +331,14 @@ fclose(wave_file); wait(1); uLCD.cls(); + if (mode == 1){ answer = 0; + } + counter = counter+1; check = 1; - } - } - - } + } + } + } if (question == 3){ value = 0; uLCD.cls(); @@ -202,7 +354,7 @@ //play game over uLCD.cls(); uLCD.media_init(); - uLCD.set_sector_address(0x0000, 0x009F); + uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); @@ -210,7 +362,11 @@ fclose(wave_file); wait(1); uLCD.cls(); + if (mode == 1){ answer = 0; + } + counter = counter +1; + check = 1; } if (value == 8){ @@ -225,17 +381,28 @@ fclose(wave_file); wait(1); uLCD.cls(); - Score = Score+1; - uLCD.printf("Score: %d", Score); + if (mode == 1){ + nScore = nScore+1; + } + if (mode == 2){ + tScore = tScore+1; + } + if (mode == 4){ + cScore = cScore+1; + } + //uLCD.printf("Score: %d", Score); wait(1); + if (mode == 1){ answer = 1; + } + counter=counter+1; check = 1; } if (value == 4){ //play game over uLCD.cls(); uLCD.media_init(); - uLCD.set_sector_address(0x0000, 0x009F); + uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); @@ -243,7 +410,10 @@ fclose(wave_file); wait(1); uLCD.cls(); + if (mode == 1){ answer = 0; + } + counter = counter+1; check = 1; } } @@ -263,7 +433,7 @@ //play game over uLCD.cls(); uLCD.media_init(); - uLCD.set_sector_address(0x0000, 0x009F); + uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); @@ -271,7 +441,11 @@ fclose(wave_file); wait(1); uLCD.cls(); + if (mode == 1){ answer = 0; + } + counter = counter +1; + check = 1; } if (value == 4){ @@ -286,17 +460,28 @@ fclose(wave_file); wait(1); uLCD.cls(); - Score = Score+1; - uLCD.printf("Score: %d", Score); + if (mode == 1){ + nScore = nScore+1; + } + if (mode == 2){ + tScore = tScore+1; + } + if (mode == 4){ + cScore = cScore+1; + } + //uLCD.printf("Score: %d", Score); wait(1); + if (mode == 1){ answer = 1; + } + counter=counter+1; check = 1; } if (value == 8){ //play game over uLCD.cls(); uLCD.media_init(); - uLCD.set_sector_address(0x0000, 0x009F); + uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); @@ -304,7 +489,10 @@ fclose(wave_file); wait(1); uLCD.cls(); + if (mode == 1){ answer = 0; + } + counter = counter+1; check = 1; } } @@ -324,7 +512,7 @@ //play game over uLCD.cls(); uLCD.media_init(); - uLCD.set_sector_address(0x0000, 0x009F); + uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); @@ -332,7 +520,11 @@ fclose(wave_file); wait(1); uLCD.cls(); + if (mode == 1){ answer = 0; + } + counter = counter +1; + check = 1; } if (value == 2){ @@ -347,17 +539,28 @@ fclose(wave_file); wait(1); uLCD.cls(); - Score = Score+1; - uLCD.printf("Score: %d", Score); + if (mode == 1){ + nScore = nScore+1; + } + if (mode == 2){ + tScore = tScore+1; + } + if (mode == 4){ + cScore = cScore+1; + } + //uLCD.printf("Score: %d", Score); wait(1); + if (mode == 1){ answer = 1; + } + counter=counter+1; check = 1; } if (value == 8){ //play game over uLCD.cls(); uLCD.media_init(); - uLCD.set_sector_address(0x0000, 0x009F); + uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); @@ -365,7 +568,10 @@ fclose(wave_file); wait(1); uLCD.cls(); + if (mode == 1){ answer = 0; + } + counter = counter+1; check = 1; } } @@ -381,11 +587,11 @@ while (check == 0){ int value=mpr121.read(0x00); value +=mpr121.read(0x01)<<8; - if (value == 4){ + if (value == 2){ //play game over uLCD.cls(); uLCD.media_init(); - uLCD.set_sector_address(0x0000, 0x009F); + uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); @@ -393,7 +599,11 @@ fclose(wave_file); wait(1); uLCD.cls(); + if (mode == 1){ answer = 0; + } + counter = counter +1; + check = 1; } if (value == 8){ @@ -408,17 +618,28 @@ fclose(wave_file); wait(1); uLCD.cls(); - Score = Score+1; - uLCD.printf("Score: %d", Score); + if (mode == 1){ + nScore = nScore+1; + } + if (mode == 2){ + tScore = tScore+1; + } + if (mode == 4){ + cScore = cScore+1; + } + //uLCD.printf("Score: %d", Score); wait(1); + if (mode == 1){ answer = 1; + } + counter=counter+1; check = 1; } - if (value == 2){ + if (value == 4){ //play game over uLCD.cls(); uLCD.media_init(); - uLCD.set_sector_address(0x0000, 0x009F); + uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); @@ -426,7 +647,10 @@ fclose(wave_file); wait(1); uLCD.cls(); + if (mode == 1){ answer = 0; + } + counter = counter+1; check = 1; } } @@ -442,11 +666,11 @@ while (check == 0){ int value=mpr121.read(0x00); value +=mpr121.read(0x01)<<8; - if (value == 4){ + if (value == 2){ //play game over uLCD.cls(); uLCD.media_init(); - uLCD.set_sector_address(0x0000, 0x009F); + uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); @@ -454,7 +678,11 @@ fclose(wave_file); wait(1); uLCD.cls(); + if (mode == 1){ answer = 0; + } + counter = counter +1; + check = 1; } if (value == 8){ @@ -469,17 +697,28 @@ fclose(wave_file); wait(1); uLCD.cls(); - Score = Score+1; - uLCD.printf("Score: %d", Score); + if (mode == 1){ + nScore = nScore+1; + } + if (mode == 2){ + tScore = tScore+1; + } + if (mode == 4){ + cScore = cScore+1; + } + //uLCD.printf("Score: %d", Score); wait(1); + if (mode == 1){ answer = 1; + } + counter=counter+1; check = 1; } - if (value == 2){ + if (value == 4){ //play game over uLCD.cls(); uLCD.media_init(); - uLCD.set_sector_address(0x0000, 0x009F); + uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); @@ -487,7 +726,10 @@ fclose(wave_file); wait(1); uLCD.cls(); + if (mode == 1){ answer = 0; + } + counter = counter+1; check = 1; } } @@ -507,7 +749,7 @@ //play game over uLCD.cls(); uLCD.media_init(); - uLCD.set_sector_address(0x0000, 0x009F); + uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); @@ -515,7 +757,11 @@ fclose(wave_file); wait(1); uLCD.cls(); + if (mode == 1){ answer = 0; + } + counter = counter +1; + check = 1; } if (value == 4){ @@ -530,17 +776,28 @@ fclose(wave_file); wait(1); uLCD.cls(); - Score = Score+1; - uLCD.printf("Score: %d", Score); + if (mode == 1){ + nScore = nScore+1; + } + if (mode == 2){ + tScore = tScore+1; + } + if (mode == 4){ + cScore = cScore+1; + } + //uLCD.printf("Score: %d", Score); wait(1); + if (mode == 1){ answer = 1; + } + counter=counter+1; check = 1; } if (value == 8){ //play game over uLCD.cls(); uLCD.media_init(); - uLCD.set_sector_address(0x0000, 0x009F); + uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); @@ -548,7 +805,10 @@ fclose(wave_file); wait(1); uLCD.cls(); + if (mode == 1){ answer = 0; + } + counter = counter+1; check = 1; } } @@ -568,7 +828,7 @@ //play game over uLCD.cls(); uLCD.media_init(); - uLCD.set_sector_address(0x0000, 0x009F); + uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); @@ -576,7 +836,11 @@ fclose(wave_file); wait(1); uLCD.cls(); + if (mode == 1){ answer = 0; + } + counter = counter +1; + check = 1; } if (value == 2){ @@ -591,17 +855,28 @@ fclose(wave_file); wait(1); uLCD.cls(); - Score = Score+1; - uLCD.printf("Score: %d", Score); + if (mode == 1){ + nScore = nScore+1; + } + if (mode == 2){ + tScore = tScore+1; + } + if (mode == 4){ + cScore = cScore+1; + } + //uLCD.printf("Score: %d", Score); wait(1); + if (mode == 1){ answer = 1; + } + counter=counter+1; check = 1; } if (value == 8){ //play game over uLCD.cls(); uLCD.media_init(); - uLCD.set_sector_address(0x0000, 0x009F); + uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); @@ -609,7 +884,10 @@ fclose(wave_file); wait(1); uLCD.cls(); + if (mode == 1){ answer = 0; + } + counter = counter+1; check = 1; } } @@ -629,7 +907,7 @@ //play game over uLCD.cls(); uLCD.media_init(); - uLCD.set_sector_address(0x0000, 0x009F); + uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); @@ -637,7 +915,11 @@ fclose(wave_file); wait(1); uLCD.cls(); + if (mode == 1){ answer = 0; + } + counter = counter +1; + check = 1; } if (value == 2){ @@ -652,17 +934,28 @@ fclose(wave_file); wait(1); uLCD.cls(); - Score = Score+1; - uLCD.printf("Score: %d", Score); + if (mode == 1){ + nScore = nScore+1; + } + if (mode == 2){ + tScore = tScore+1; + } + if (mode == 4){ + cScore = cScore+1; + } + //uLCD.printf("Score: %d", Score); wait(1); + if (mode == 1){ answer = 1; + } + counter=counter+1; check = 1; } if (value == 8){ //play game over uLCD.cls(); uLCD.media_init(); - uLCD.set_sector_address(0x0000, 0x009F); + uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); @@ -670,7 +963,10 @@ fclose(wave_file); wait(1); uLCD.cls(); + if (mode == 1){ answer = 0; + } + counter = counter+1; check = 1; } }