Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: N5110 PinDetect PowerControl mbed
SubmitHighscore.cpp
00001 /// @file SubmitHighscore.cpp 00002 00003 #include "SubmitHighscore.h " 00004 00005 int SubmitHighscore::letters[3] = {0, 0, 0}; 00006 int SubmitHighscore::currentState = LETTER1; 00007 00008 void SubmitHighscore::init() 00009 { 00010 input->addBtnPressInterrupt(Input::ButtonA, &btnAPress); 00011 input->addBtnPressInterrupt(Input::ButtonB, &btnBPress); 00012 input->addBtnPressInterrupt(Input::ButtonC, &btnCPress); 00013 00014 SubmitHighscore::currentState = LETTER1; 00015 } 00016 00017 void SubmitHighscore::changeLetter(int index, bool next) 00018 { 00019 int letter = (SubmitHighscore::letters[index] + ((next) ? 1 : - 1) + 26) % 26; // + 26 in case it is negative, % => wrap around from Z->A 00020 SubmitHighscore::letters[index] = letter; 00021 } 00022 00023 void SubmitHighscore::btnAPress() 00024 { 00025 switch (currentState) 00026 { 00027 case LETTER1: 00028 changeLetter(0, true); 00029 break; 00030 00031 case LETTER2: 00032 changeLetter(1, true); 00033 break; 00034 00035 case LETTER3: 00036 changeLetter(2, true); 00037 break; 00038 00039 case SEL_SUBMIT: 00040 currentState = WRITE_TO_FILE; 00041 break; 00042 } 00043 } 00044 00045 void SubmitHighscore::btnBPress() 00046 { 00047 // Change to previous letter 00048 switch (currentState) 00049 { 00050 case LETTER1: 00051 changeLetter(0, false); 00052 break; 00053 00054 case LETTER2: 00055 changeLetter(1, false); 00056 break; 00057 00058 case LETTER3: 00059 changeLetter(2, false); 00060 break; 00061 } 00062 } 00063 00064 void SubmitHighscore::btnCPress() 00065 { 00066 switch (currentState) 00067 { 00068 case LETTER1: 00069 currentState = LETTER2; 00070 break; 00071 00072 case LETTER2: 00073 currentState = LETTER3; 00074 break; 00075 00076 case LETTER3: 00077 currentState = SEL_SUBMIT; 00078 break; 00079 00080 case SEL_SUBMIT: 00081 currentState = LETTER1; 00082 break; 00083 } 00084 } 00085 00086 00087 void SubmitHighscore::update(float dt) 00088 { 00089 if (currentState == WRITE_TO_FILE) 00090 { 00091 char s0 = 'A' + static_cast<char>(letters[0]); 00092 char s1 = 'A' + static_cast<char>(letters[1]); 00093 char s2 = 'A' + static_cast<char>(letters[2]); 00094 00095 std::stringstream ss; 00096 ss << s0 << s1 << s2; 00097 00098 std::string initials = ss.str(); 00099 00100 // update high score list 00101 FILE *fp = fopen("/local/highscores.txt", "w"); 00102 00103 if (Global::score > Global::highscores[0].score) 00104 { 00105 Global::highscores[2] = Global::highscores[1]; 00106 Global::highscores[1] = Global::highscores[0]; 00107 Global::highscores[0].initials = initials; 00108 Global::highscores[0].score = Global::score; 00109 } 00110 else if (Global::score > Global::highscores[1].score) 00111 { 00112 Global::highscores[2] = Global::highscores[1]; 00113 Global::highscores[1].initials = initials; 00114 Global::highscores[1].score = Global::score; 00115 } 00116 else if (Global::score > Global::highscores[2].score) 00117 { 00118 Global::highscores[2].initials = initials; 00119 Global::highscores[2].score = Global::score; 00120 } 00121 00122 for (int i = 0; i < 3; ++i) 00123 fprintf(fp, "%s %d ", Global::highscores[i].initials, Global::highscores[i].score); 00124 00125 fclose(fp); 00126 00127 currentState = LOAD_GAME_OVER; 00128 } 00129 00130 if (currentState == LOAD_GAME_OVER) 00131 requestStateChange(GAME_OVER); 00132 } 00133 00134 void SubmitHighscore::render() 00135 { 00136 if (currentState == WRITE_TO_FILE) 00137 { 00138 lcd->printString("Writing...", 12, 2); 00139 } 00140 else // Entering initials 00141 { 00142 lcd->printString("New high score!", 0, 0); 00143 00144 const char charLetters[6] = {'A' + letters[0], '\0', 'A' + letters[1], '\0', 'A' + letters[2], '\0'}; 00145 00146 lcd->printString(&charLetters[0], 25, 2); 00147 lcd->printString(&charLetters[2], 40, 2); 00148 lcd->printString(&charLetters[4], 55, 2); 00149 lcd->printString("Submit", 25, 4); 00150 00151 switch(currentState) 00152 { 00153 case LETTER1: 00154 lcd->printString("^", 25, 3); 00155 break; 00156 00157 case LETTER2: 00158 lcd->printString("^", 40, 3); 00159 break; 00160 00161 case LETTER3: 00162 lcd->printString("^", 55, 3); 00163 break; 00164 00165 case SEL_SUBMIT: 00166 lcd->printString(">",25-6 , 4); 00167 break; 00168 } 00169 } 00170 }
Generated on Tue Jul 12 2022 21:59:48 by
