Final Trivia Game Code
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed wave_player
Fork of TriviaGame by
main.cpp
- Committer:
- mbalourdas9
- Date:
- 2016-12-11
- Revision:
- 3:9842b3f1d809
- Parent:
- 1:1d5055ca2793
- Child:
- 4:55bb76d28153
File content as of revision 3:9842b3f1d809:
#include "mbed.h" #include "SDFileSystem.h" #include "uLCD_4DGL.h" #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 wave_player waver(&DACout); 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 int Score; int HighScore = 0; int question = 0; bool answer = 1; int index = 0; 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() { srand(time(NULL)); while (true){ answer = 1; for (int i=0; i<10; i++){ v.push_back(i+1); } check = 0; question = 0; uLCD.baudrate(30); uLCD.background_color(BLACK); uLCD.color(BLUE); uLCD.cls(); 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); } 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("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; } index = rand() %v.size(); question = v.at(index); v.erase(v.begin()+index); check = 0; uLCD.cls(); if (mode == 1){ uLCD.printf("Score: %d", nScore); } wait(1); if (question == 1){ value = 0; uLCD.cls(); uLCD.locate(1,2); uLCD.printf("Who won the World Series in 2016?\n\n\n"); uLCD.printf("1) Cleveland\nIndians\n"); uLCD.printf("2) Chicago Cubs\n"); uLCD.printf("3) San Diego\nPadres\n"); while (check == 0){ int value=mpr121.read(0x00); value +=mpr121.read(0x01)<<8; if (value == 2){ //play game over uLCD.cls(); uLCD.media_init(); uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); if (mode == 1){ answer = 0; } counter = counter +1; check = 1; } if (value == 4){ //play correct image uLCD.cls(); uLCD.media_init(); uLCD.set_sector_address(0x0000, 0x005E); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/right_taught.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); 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, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); if (mode == 1){ answer = 0; } counter = counter+1; check = 1; } } } if (question == 2){ value = 0; uLCD.cls(); uLCD.locate(1,2); uLCD.printf("What is the\n capital of South\n Dakota?\n\n"); uLCD.printf("1) Pierre\n"); uLCD.printf("2) Bismarck\n"); uLCD.printf("3) Lakota\n"); while (check == 0){ int value=mpr121.read(0x00); value +=mpr121.read(0x01)<<8; if (value == 2){ //play game over uLCD.cls(); uLCD.media_init(); uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); if (mode == 1){ answer = 0; } counter = counter +1; check = 1; } if (value == 4){ //play correct image uLCD.cls(); uLCD.media_init(); uLCD.set_sector_address(0x0000, 0x005E); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/right_taught.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); 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, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); if (mode == 1){ answer = 0; } counter = counter+1; check = 1; } } } if (question == 3){ value = 0; uLCD.cls(); uLCD.locate(1,2); uLCD.printf("Who wrote The \nGrapes of Wrath?\n\n\n"); uLCD.printf("1)Ernest Hemingway"); uLCD.printf("2)George Orwell\n"); uLCD.printf("3)John Steinbeck\n"); while (check == 0){ int value=mpr121.read(0x00); value +=mpr121.read(0x01)<<8; if (value == 2){ //play game over uLCD.cls(); uLCD.media_init(); uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); if (mode == 1){ answer = 0; } counter = counter +1; check = 1; } if (value == 8){ //play correct image uLCD.cls(); uLCD.media_init(); uLCD.set_sector_address(0x0000, 0x005E); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/right_taught.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); 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, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); if (mode == 1){ answer = 0; } counter = counter+1; check = 1; } } } if (question == 4){ value = 0; uLCD.cls(); uLCD.locate(1,2); uLCD.printf("What year was \nEinstein born?\n\n\n"); uLCD.printf("1) 1900\n"); uLCD.printf("2) 1879\n"); uLCD.printf("3) 1873\n"); while (check == 0){ int value=mpr121.read(0x00); value +=mpr121.read(0x01)<<8; if (value == 2){ //play game over uLCD.cls(); uLCD.media_init(); uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); if (mode == 1){ answer = 0; } counter = counter +1; check = 1; } if (value == 4){ //play correct image uLCD.cls(); uLCD.media_init(); uLCD.set_sector_address(0x0000, 0x005E); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/right_taught.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); 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, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); if (mode == 1){ answer = 0; } counter = counter+1; check = 1; } } } if (question == 5){ value = 0; uLCD.cls(); uLCD.locate(1,2); uLCD.printf("Which song is by the Rolling Stones?\n\n\n"); uLCD.printf("1) Paint It Black\n"); uLCD.printf("2) Feel For \nThe Devil\n"); uLCD.printf("3) Let It Be\n"); while (check == 0){ int value=mpr121.read(0x00); value +=mpr121.read(0x01)<<8; if (value == 4){ //play game over uLCD.cls(); uLCD.media_init(); uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); if (mode == 1){ answer = 0; } counter = counter +1; check = 1; } if (value == 2){ //play correct image uLCD.cls(); uLCD.media_init(); uLCD.set_sector_address(0x0000, 0x005E); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/right_taught.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); 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, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); if (mode == 1){ answer = 0; } counter = counter+1; check = 1; } } } if (question == 6){ value = 0; uLCD.cls(); uLCD.locate(1,2); uLCD.printf("Where was Hitler born?\n\n\n"); uLCD.printf("1) Poland\n"); uLCD.printf("2) Germany\n"); uLCD.printf("3) Austria\n"); while (check == 0){ int value=mpr121.read(0x00); value +=mpr121.read(0x01)<<8; if (value == 2){ //play game over uLCD.cls(); uLCD.media_init(); uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); if (mode == 1){ answer = 0; } counter = counter +1; check = 1; } if (value == 8){ //play correct image uLCD.cls(); uLCD.media_init(); uLCD.set_sector_address(0x0000, 0x005E); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/right_taught.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); 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, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); if (mode == 1){ answer = 0; } counter = counter+1; check = 1; } } } if (question == 7){ value = 0; uLCD.cls(); uLCD.locate(1,2); uLCD.printf("The best selling\nmusic single of all time is?\n\n\n"); uLCD.printf("1) Billie Jean\n"); uLCD.printf("2) White Christmas\n"); uLCD.printf("3) Candle in the \nWind\n"); while (check == 0){ int value=mpr121.read(0x00); value +=mpr121.read(0x01)<<8; if (value == 2){ //play game over uLCD.cls(); uLCD.media_init(); uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); if (mode == 1){ answer = 0; } counter = counter +1; check = 1; } if (value == 8){ //play correct image uLCD.cls(); uLCD.media_init(); uLCD.set_sector_address(0x0000, 0x005E); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/right_taught.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); 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, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); if (mode == 1){ answer = 0; } counter = counter+1; check = 1; } } } if (question == 8){ value = 0; uLCD.cls(); uLCD.locate(1,2); uLCD.printf("What is the \ncurrency of \nJapan?\n\n\n"); uLCD.printf("1) Pounds\n"); uLCD.printf("2) Yen\n"); uLCD.printf("3) Riki\n"); while (check == 0){ int value=mpr121.read(0x00); value +=mpr121.read(0x01)<<8; if (value == 2){ //play game over uLCD.cls(); uLCD.media_init(); uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); if (mode == 1){ answer = 0; } counter = counter +1; check = 1; } if (value == 4){ //play correct image uLCD.cls(); uLCD.media_init(); uLCD.set_sector_address(0x0000, 0x005E); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/right_taught.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); 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, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); if (mode == 1){ answer = 0; } counter = counter+1; check = 1; } } } if (question == 9){ value = 0; uLCD.cls(); uLCD.locate(1,2); uLCD.printf("Who was Harry \nHoudini?\n\n\n"); uLCD.printf("1)An Escape Artist\n"); uLCD.printf("2)Fighter Pilot in WWII\n"); uLCD.printf("3) Harry Potter's Cousin\n"); while (check == 0){ int value=mpr121.read(0x00); value +=mpr121.read(0x01)<<8; if (value == 4){ //play game over uLCD.cls(); uLCD.media_init(); uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); if (mode == 1){ answer = 0; } counter = counter +1; check = 1; } if (value == 2){ //play correct image uLCD.cls(); uLCD.media_init(); uLCD.set_sector_address(0x0000, 0x005E); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/right_taught.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); 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, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); if (mode == 1){ answer = 0; } counter = counter+1; check = 1; } } } if (question == 10){ value = 0; uLCD.cls(); uLCD.locate(1,2); uLCD.printf("What is glass \nmainly made up \nof?\n\n\n"); uLCD.printf("1) Sand\n"); uLCD.printf("2) Crystals\n"); uLCD.printf("3) Highly \nCompressed \nWater\n"); while (check == 0){ int value=mpr121.read(0x00); value +=mpr121.read(0x01)<<8; if (value == 4){ //play game over uLCD.cls(); uLCD.media_init(); uLCD.set_sector_address(0x0000, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); if (mode == 1){ answer = 0; } counter = counter +1; check = 1; } if (value == 2){ //play correct image uLCD.cls(); uLCD.media_init(); uLCD.set_sector_address(0x0000, 0x005E); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/right_taught.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); 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, 0x001D); uLCD.display_image(0,0); FILE *wave_file; wave_file=fopen("/sd/sounds/wrong_last.wav","r"); waver.play(wave_file); fclose(wave_file); wait(1); uLCD.cls(); if (mode == 1){ answer = 0; } counter = counter+1; check = 1; } } } } } else{ uLCD.printf("CONGRATS!"); wait(1); } } }