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