ref: fc5424cb72e477c5f1bbfaeddb5c50b851a965ae
src/displayapp/screens/Twos.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
#include "displayapp/screens/Twos.h" #include <cstdio> #include <cstdlib> #include <lvgl/lvgl.h> using namespace Pinetime::Applications::Screens; Twos::Twos() { struct colorPair { lv_color_t bg; lv_color_t fg; }; static constexpr colorPair colors[nColors] = { {LV_COLOR_MAKE(0xcd, 0xc0, 0xb4), LV_COLOR_BLACK}, {LV_COLOR_MAKE(0xef, 0xdf, 0xc6), LV_COLOR_BLACK}, {LV_COLOR_MAKE(0xef, 0x92, 0x63), LV_COLOR_WHITE}, {LV_COLOR_MAKE(0xf7, 0x61, 0x42), LV_COLOR_WHITE}, {LV_COLOR_MAKE(0x00, 0x7d, 0xc5), LV_COLOR_WHITE}, }; gridDisplay = lv_table_create(lv_scr_act(), nullptr); for (size_t i = 0; i < nColors; i++) { lv_style_init(&cellStyles[i]); lv_style_set_border_color(&cellStyles[i], LV_STATE_DEFAULT, lv_color_hex(0xbbada0)); lv_style_set_border_width(&cellStyles[i], LV_STATE_DEFAULT, 3); lv_style_set_bg_opa(&cellStyles[i], LV_STATE_DEFAULT, LV_OPA_COVER); lv_style_set_bg_color(&cellStyles[i], LV_STATE_DEFAULT, colors[i].bg); lv_style_set_pad_top(&cellStyles[i], LV_STATE_DEFAULT, 29); lv_style_set_text_color(&cellStyles[i], LV_STATE_DEFAULT, colors[i].fg); lv_obj_add_style(gridDisplay, LV_TABLE_PART_CELL1 + i, &cellStyles[i]); } lv_table_set_col_cnt(gridDisplay, nCols); lv_table_set_row_cnt(gridDisplay, nRows); for (int col = 0; col < nCols; col++) { static constexpr int colWidth = LV_HOR_RES_MAX / nCols; lv_table_set_col_width(gridDisplay, col, colWidth); for (int row = 0; row < nRows; row++) { grid[row][col].value = 0; lv_table_set_cell_type(gridDisplay, row, col, 1); lv_table_set_cell_align(gridDisplay, row, col, LV_LABEL_ALIGN_CENTER); } } // Move one pixel down to remove a gap lv_obj_align(gridDisplay, nullptr, LV_ALIGN_IN_BOTTOM_MID, 0, 1); lv_obj_clean_style_list(gridDisplay, LV_TABLE_PART_BG); placeNewTile(); placeNewTile(); // format score text scoreText = lv_label_create(lv_scr_act(), nullptr); lv_obj_set_width(scoreText, LV_HOR_RES); lv_label_set_align(scoreText, LV_ALIGN_IN_LEFT_MID); lv_obj_align(scoreText, nullptr, LV_ALIGN_IN_TOP_LEFT, 0, 0); lv_label_set_recolor(scoreText, true); lv_label_set_text_fmt(scoreText, "Score #FFFF00 %i#", score); } Twos::~Twos() { for (lv_style_t cellStyle : cellStyles) { lv_style_reset(&cellStyle); } lv_obj_clean(lv_scr_act()); } bool Twos::placeNewTile() { unsigned int emptyCells[nCells]; unsigned int nEmpty = 0; for (unsigned int i = 0; i < nCells; i++) { const unsigned int row = i / nCols; const unsigned int col = i % nCols; if (grid[row][col].value == 0) { emptyCells[nEmpty] = i; nEmpty++; } } if (nEmpty == 0) { return false; // game lost } int random = rand() % nEmpty; if ((rand() % 100) < 90) { grid[emptyCells[random] / nCols][emptyCells[random] % nCols].value = 2; } else { grid[emptyCells[random] / nCols][emptyCells[random] % nCols].value = 4; } updateGridDisplay(); return true; } bool Twos::tryMerge(int newRow, int newCol, int oldRow, int oldCol) { if (grid[newRow][newCol].value == grid[oldRow][oldCol].value) { if ((newCol != oldCol) || (newRow != oldRow)) { if (!grid[newRow][newCol].merged) { grid[newRow][newCol].value *= 2; score += grid[newRow][newCol].value; lv_label_set_text_fmt(scoreText, "Score #FFFF00 %i#", score); grid[oldRow][oldCol].value = 0; grid[newRow][newCol].merged = true; return true; } } } return false; } bool Twos::tryMove(int newRow, int newCol, int oldRow, int oldCol) { if (((newCol >= 0) && (newCol != oldCol)) || ((newRow >= 0) && (newRow != oldRow))) { grid[newRow][newCol].value = grid[oldRow][oldCol].value; grid[oldRow][oldCol].value = 0; return true; } return false; } bool Twos::OnTouchEvent(Pinetime::Applications::TouchEvents event) { bool validMove = false; for (unsigned int i = 0; i < nCells; i++) { const unsigned int row = i / nCols; const unsigned int col = i % nCols; grid[row][col].merged = false; // reinitialize merge state } switch (event) { case TouchEvents::SwipeLeft: for (int col = 1; col < nCols; col++) { // ignore tiles already on far left for (int row = 0; row < nRows; row++) { if (grid[row][col].value > 0) { int newCol = -1; for (int potentialNewCol = col - 1; potentialNewCol >= 0; potentialNewCol--) { if (grid[row][potentialNewCol].value == 0) { newCol = potentialNewCol; } else { // blocked by another tile if (tryMerge(row, potentialNewCol, row, col)) { validMove = true; } break; } } if (tryMove(row, newCol, row, col)) { validMove = true; } } } } if (validMove) { placeNewTile(); } return true; case TouchEvents::SwipeRight: for (int col = nCols - 2; col >= 0; col--) { // ignore tiles already on far right for (int row = 0; row < nRows; row++) { if (grid[row][col].value > 0) { int newCol = -1; for (int potentialNewCol = col + 1; potentialNewCol < nCols; potentialNewCol++) { if (grid[row][potentialNewCol].value == 0) { newCol = potentialNewCol; } else { // blocked by another tile if (tryMerge(row, potentialNewCol, row, col)) { validMove = true; } break; } } if (tryMove(row, newCol, row, col)) { validMove = true; } } } } if (validMove) { placeNewTile(); } return true; case TouchEvents::SwipeUp: for (int row = 1; row < nRows; row++) { // ignore tiles already on top for (int col = 0; col < nCols; col++) { if (grid[row][col].value > 0) { int newRow = -1; for (int potentialNewRow = row - 1; potentialNewRow >= 0; potentialNewRow--) { if (grid[potentialNewRow][col].value == 0) { newRow = potentialNewRow; } else { // blocked by another tile if (tryMerge(potentialNewRow, col, row, col)) { validMove = true; } break; } } if (tryMove(newRow, col, row, col)) { validMove = true; } } } } if (validMove) { placeNewTile(); } return true; case TouchEvents::SwipeDown: for (int row = nRows - 2; row >= 0; row--) { // ignore tiles already on bottom for (int col = 0; col < nCols; col++) { if (grid[row][col].value > 0) { int newRow = -1; for (int potentialNewRow = row + 1; potentialNewRow < nRows; potentialNewRow++) { if (grid[potentialNewRow][col].value == 0) { newRow = potentialNewRow; } else { // blocked by another tile if (tryMerge(potentialNewRow, col, row, col)) { validMove = true; } break; } } if (tryMove(newRow, col, row, col)) { validMove = true; } } } } if (validMove) { placeNewTile(); } return true; default: return false; } return false; } void Twos::updateGridDisplay() { for (unsigned int i = 0; i < nCells; i++) { const unsigned int row = i / nCols; const unsigned int col = i % nCols; if (grid[row][col].value > 0) { char buffer[7]; snprintf(buffer, sizeof(buffer), "%u", grid[row][col].value); lv_table_set_cell_value(gridDisplay, row, col, buffer); } else { lv_table_set_cell_value(gridDisplay, row, col, ""); } switch (grid[row][col].value) { case 0: lv_table_set_cell_type(gridDisplay, row, col, 1); break; case 2: case 4: lv_table_set_cell_type(gridDisplay, row, col, 2); break; case 8: case 16: lv_table_set_cell_type(gridDisplay, row, col, 3); break; case 32: case 64: lv_table_set_cell_type(gridDisplay, row, col, 4); break; default: lv_table_set_cell_type(gridDisplay, row, col, 5); break; } } } |