I simplified the library "ILI9225_TFT" provided by Arman Safikhani to better suit my needs in implementing a simple sliding puzzle game.

Committer:
blac3777
Date:
Fri Apr 27 07:33:56 2018 +0000
Revision:
3:251e4d020501
I simplified the library "ILI9225_TFT" provided by Arman Safikhani to suit my needs in implementing a simple sliding puzzle game.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
blac3777 3:251e4d020501 1 #include "Fonts.h"
blac3777 3:251e4d020501 2
blac3777 3:251e4d020501 3 //STATE 0
blac3777 3:251e4d020501 4 //GLCD Displays the start screen - encouraging the player to start the game
blac3777 3:251e4d020501 5 //by displaying the message "PRESS SELECT TO START".
blac3777 3:251e4d020501 6 //
blac3777 3:251e4d020501 7 //Player must press the SELECT button to trigger the next STATE.
blac3777 3:251e4d020501 8 void greetingScreen() {
blac3777 3:251e4d020501 9 tft.clear();
blac3777 3:251e4d020501 10 tft.setFont(Terminal12x16);
blac3777 3:251e4d020501 11 tft.drawText(60, 40, "PRESS", COLOR_RED);
blac3777 3:251e4d020501 12 tft.drawText(54, 65, "SELECT", COLOR_RED);
blac3777 3:251e4d020501 13 tft.drawText(77, 90, "TO", COLOR_RED);
blac3777 3:251e4d020501 14 tft.drawText(60, 115, "START", COLOR_RED);
blac3777 3:251e4d020501 15
blac3777 3:251e4d020501 16 while(1) {
blac3777 3:251e4d020501 17 if(getPlayerInput(wirelessInput_Freq) == 6) //SELECT -> 6
blac3777 3:251e4d020501 18 return;
blac3777 3:251e4d020501 19 }
blac3777 3:251e4d020501 20 }
blac3777 3:251e4d020501 21
blac3777 3:251e4d020501 22 //STATE 1
blac3777 3:251e4d020501 23 //GLCD encourages player to take a picture of something by displaying the
blac3777 3:251e4d020501 24 //message "SELECT TO TAKE PICTURE".
blac3777 3:251e4d020501 25 //
blac3777 3:251e4d020501 26 //Player must press the SELECT button to take a picture and trigger the next
blac3777 3:251e4d020501 27 //STATE.
blac3777 3:251e4d020501 28 void takePicture() {
blac3777 3:251e4d020501 29 tft.clear();
blac3777 3:251e4d020501 30 tft.setFont(Terminal12x16);
blac3777 3:251e4d020501 31 tft.drawText(54, 40, "SELECT", COLOR_RED);
blac3777 3:251e4d020501 32 tft.drawText(77, 65, "TO", COLOR_RED);
blac3777 3:251e4d020501 33 tft.drawText(65, 90, "TAKE", COLOR_RED);
blac3777 3:251e4d020501 34 tft.drawText(50, 115, "PICTURE", COLOR_RED);
blac3777 3:251e4d020501 35
blac3777 3:251e4d020501 36 while(1) {
blac3777 3:251e4d020501 37 if(getPlayerInput(wirelessInput_Freq) == 6) { //SELECT -> 6
blac3777 3:251e4d020501 38 for(int index = 0; index < 19200; index++)
blac3777 3:251e4d020501 39 handlePixel(index, getNextPixel());
blac3777 3:251e4d020501 40 return;
blac3777 3:251e4d020501 41 }
blac3777 3:251e4d020501 42 }
blac3777 3:251e4d020501 43 }
blac3777 3:251e4d020501 44
blac3777 3:251e4d020501 45 //STATE 2
blac3777 3:251e4d020501 46 //GLCD will display the picture taken in STATE 1 @ the bottom of the screen.
blac3777 3:251e4d020501 47 //
blac3777 3:251e4d020501 48 //Player input is ignored during this STATE.
blac3777 3:251e4d020501 49 void previewPicture() {
blac3777 3:251e4d020501 50 tft.clear();
blac3777 3:251e4d020501 51 updatePuzzle();
blac3777 3:251e4d020501 52 }
blac3777 3:251e4d020501 53
blac3777 3:251e4d020501 54 //STATE 3
blac3777 3:251e4d020501 55 //GLCD will display the message "SELECT > ACCEPT" atop "OR" atop
blac3777 3:251e4d020501 56 //"CANCEL > RETAKE" at the top of the screen.
blac3777 3:251e4d020501 57 //
blac3777 3:251e4d020501 58 //Player may press SELECT to continue (STATE 4) w| picture or CANCEL to retake
blac3777 3:251e4d020501 59 //the picture (STATE 1).
blac3777 3:251e4d020501 60 bool confirmPicture() {
blac3777 3:251e4d020501 61 tft.fillRectangle(0, 0, 175, 91, COLOR_BLACK);
blac3777 3:251e4d020501 62 tft.setFont(Terminal12x16);
blac3777 3:251e4d020501 63 tft.drawText(4, 8, "SELECT > ACCEPT", COLOR_RED);
blac3777 3:251e4d020501 64 tft.drawText(77, 33, "OR", COLOR_RED);
blac3777 3:251e4d020501 65 tft.drawText(4, 58, "CANCEL > RETAKE", COLOR_RED);
blac3777 3:251e4d020501 66 //printElementsInPicture();
blac3777 3:251e4d020501 67
blac3777 3:251e4d020501 68 while(1) {
blac3777 3:251e4d020501 69 if(getPlayerInput(wirelessInput_Freq) == 6) //SELECT -> 6
blac3777 3:251e4d020501 70 return true; //Confirm the picture by returning true
blac3777 3:251e4d020501 71 else if(getPlayerInput(wirelessInput_Freq) == 7) //CANCEL -> 7
blac3777 3:251e4d020501 72 return false; //Retake the picture by returning false
blac3777 3:251e4d020501 73 }
blac3777 3:251e4d020501 74 }
blac3777 3:251e4d020501 75
blac3777 3:251e4d020501 76 //STATE 4
blac3777 3:251e4d020501 77 //GLCD will display the message "LOADING" and keeps it there until the
blac3777 3:251e4d020501 78 //LPC1769 has finished shuffling the pieces in the background. When it is done,
blac3777 3:251e4d020501 79 //it will black out the screen, populate the 4x4 grid with the puzzle pieces,
blac3777 3:251e4d020501 80 //and start the player off at index 0 of the puzzle grid.
blac3777 3:251e4d020501 81 //Remark: 2 out of 16 of the puzzle pieces will be missing. There were 3 reasons
blac3777 3:251e4d020501 82 //that led us to this design choice:
blac3777 3:251e4d020501 83 //1) We had to remove, at the very least, 1 puzzle piece to make a classic
blac3777 3:251e4d020501 84 //sliding puzzle game.
blac3777 3:251e4d020501 85 //2) We desired that the sizeof(picture) << 32kB but we still wanted a colored
blac3777 3:251e4d020501 86 //image (i.e., 16 bit aray element) so we needed to get rid of 2 pieces to reach
blac3777 3:251e4d020501 87 //our goal.
blac3777 3:251e4d020501 88 //3) If puzzle piece positions for a classic sliding puzzle game are generated
blac3777 3:251e4d020501 89 //completely randomly then they are only solvable 50% of the time based off
blac3777 3:251e4d020501 90 //whether there is an even amount of permutations. There are applicable methods
blac3777 3:251e4d020501 91 //for only generating solvable puzzles but another (and easier) solution is to
blac3777 3:251e4d020501 92 //just remove a second puzzle piece.
blac3777 3:251e4d020501 93 //
blac3777 3:251e4d020501 94 //The player's button presses are ignored during this time.
blac3777 3:251e4d020501 95 void shufflePuzzle() {
blac3777 3:251e4d020501 96 tft.clear();
blac3777 3:251e4d020501 97 tft.setFont(Terminal12x16);
blac3777 3:251e4d020501 98 tft.drawText(47, 40, "LOADING", COLOR_RED);
blac3777 3:251e4d020501 99
blac3777 3:251e4d020501 100 shuffleGridIndices();
blac3777 3:251e4d020501 101
blac3777 3:251e4d020501 102 tft.clear();
blac3777 3:251e4d020501 103 updatePuzzle();
blac3777 3:251e4d020501 104 cursorIndex = 0;
blac3777 3:251e4d020501 105 highlightGridIndex(cursorIndex, false);
blac3777 3:251e4d020501 106 }
blac3777 3:251e4d020501 107
blac3777 3:251e4d020501 108 //STATE 5
blac3777 3:251e4d020501 109 //We wait for the player to press SELECT. If the player presses SELECT we must
blac3777 3:251e4d020501 110 //verify 2 characteristics about the target piece before we may trigger STATE 6:
blac3777 3:251e4d020501 111 //1) Target puzzle piece is NOT one of the 2 missing puzzle pieces.
blac3777 3:251e4d020501 112 //2) Target puzzle piece is NOT locked on all 4 sides.
blac3777 3:251e4d020501 113 //
blac3777 3:251e4d020501 114 //At this point, the player is able to use the UP, DOWN, LEFT, and RIGHT buttons
blac3777 3:251e4d020501 115 //to move around their cursor until they find a piece that they want to swap
blac3777 3:251e4d020501 116 //for another piece. While their cursor is hovering over the target piece, they
blac3777 3:251e4d020501 117 //would press SELECT to "grab it". The cursor is green in this state.
blac3777 3:251e4d020501 118 uint8_t selectionMode() {
blac3777 3:251e4d020501 119 highlightGridIndex(cursorIndex, false);
blac3777 3:251e4d020501 120 //printElementsInPicture();
blac3777 3:251e4d020501 121 while(1) {
blac3777 3:251e4d020501 122 if(getPlayerInput(wirelessInput_Freq) == 6) { //SELECT -> 6
blac3777 3:251e4d020501 123 if(selectionMode_indexVerified(cursorIndex)) //& index verified
blac3777 3:251e4d020501 124 return cursorIndex;
blac3777 3:251e4d020501 125 }
blac3777 3:251e4d020501 126 else if(getPlayerInput(wirelessInput_Freq) == 2 //UP -> 2
blac3777 3:251e4d020501 127 && cursorIndex - 4 > -1)
blac3777 3:251e4d020501 128 highlightGridIndex(cursorIndex - 4, false);
blac3777 3:251e4d020501 129 else if(getPlayerInput(wirelessInput_Freq) == 3 //DOWN -> 3
blac3777 3:251e4d020501 130 && cursorIndex + 4 < 16)
blac3777 3:251e4d020501 131 highlightGridIndex(cursorIndex + 4, false);
blac3777 3:251e4d020501 132 else if(getPlayerInput(wirelessInput_Freq) == 4 //LEFT -> 4
blac3777 3:251e4d020501 133 && ((cursorIndex%4) - 1) > -1)
blac3777 3:251e4d020501 134 highlightGridIndex(cursorIndex - 1, false);
blac3777 3:251e4d020501 135 else if(getPlayerInput(wirelessInput_Freq) == 5 //RIGHT -> 5
blac3777 3:251e4d020501 136 && ((cursorIndex%4) + 1) < 4)
blac3777 3:251e4d020501 137 highlightGridIndex(cursorIndex + 1, false);
blac3777 3:251e4d020501 138 }
blac3777 3:251e4d020501 139 }
blac3777 3:251e4d020501 140
blac3777 3:251e4d020501 141 //STATE 6
blac3777 3:251e4d020501 142 //If player presses UP, DOWN, LEFT, or RIGHT, we verify that target index is an
blac3777 3:251e4d020501 143 //empty space before making the move and triggering STATE 5. A piece may only be
blac3777 3:251e4d020501 144 //moved 1 index position and it may only be moved to 1 of the 2 missing puzzle
blac3777 3:251e4d020501 145 //piece indices.
blac3777 3:251e4d020501 146 //
blac3777 3:251e4d020501 147 //Only UP, DOWN, LEFT, RIGHT, and CANCEL are monitored during this state.
blac3777 3:251e4d020501 148 //Pressing CANCEL, at any time, would restore us back to STATE 5 and no swapping
blac3777 3:251e4d020501 149 //would occur. The cursor is red in this state.
blac3777 3:251e4d020501 150 void selectedMode(uint8_t selectedIndex) {
blac3777 3:251e4d020501 151 highlightGridIndex(selectedIndex, true);
blac3777 3:251e4d020501 152
blac3777 3:251e4d020501 153 while(1) {
blac3777 3:251e4d020501 154 if(getPlayerInput(wirelessInput_Freq) == 7) //CANCEL -> 7
blac3777 3:251e4d020501 155 return;
blac3777 3:251e4d020501 156 if(getPlayerInput(wirelessInput_Freq) == 2 //UP -> 2
blac3777 3:251e4d020501 157 && selectedIndex - 4 > -1) {
blac3777 3:251e4d020501 158 if(puzzState[selectedIndex - 4] > 13) { //Empty space above?
blac3777 3:251e4d020501 159 swap(&puzzState[selectedIndex], &puzzState[selectedIndex - 4]);
blac3777 3:251e4d020501 160 updatePuzzle();
blac3777 3:251e4d020501 161 cursorIndex = selectedIndex - 4;
blac3777 3:251e4d020501 162 return;
blac3777 3:251e4d020501 163 }
blac3777 3:251e4d020501 164 }
blac3777 3:251e4d020501 165 if(getPlayerInput(wirelessInput_Freq) == 3 //DOWN -> 3
blac3777 3:251e4d020501 166 && selectedIndex + 4 < 16) {
blac3777 3:251e4d020501 167 if(puzzState[selectedIndex + 4] > 13) { //Empty space below?
blac3777 3:251e4d020501 168 swap(&puzzState[selectedIndex], &puzzState[selectedIndex + 4]);
blac3777 3:251e4d020501 169 updatePuzzle();
blac3777 3:251e4d020501 170 cursorIndex = selectedIndex + 4;
blac3777 3:251e4d020501 171 return;
blac3777 3:251e4d020501 172 }
blac3777 3:251e4d020501 173 }
blac3777 3:251e4d020501 174 if(getPlayerInput(wirelessInput_Freq) == 4 //LEFT -> 4
blac3777 3:251e4d020501 175 && ((selectedIndex%4) - 1) > -1) {
blac3777 3:251e4d020501 176 if(puzzState[selectedIndex - 1] > 13) { //Empty space to the left?
blac3777 3:251e4d020501 177 swap(&puzzState[selectedIndex], &puzzState[selectedIndex - 1]);
blac3777 3:251e4d020501 178 updatePuzzle();
blac3777 3:251e4d020501 179 cursorIndex = selectedIndex - 1;
blac3777 3:251e4d020501 180 return;
blac3777 3:251e4d020501 181 }
blac3777 3:251e4d020501 182 }
blac3777 3:251e4d020501 183 if(getPlayerInput(wirelessInput_Freq) == 5 //RIGHT -> 5
blac3777 3:251e4d020501 184 && ((selectedIndex%4) + 1) < 4) {
blac3777 3:251e4d020501 185 if(puzzState[selectedIndex + 1] > 13) { //Empty space to the right?
blac3777 3:251e4d020501 186 swap(&puzzState[selectedIndex], &puzzState[selectedIndex + 1]);
blac3777 3:251e4d020501 187 updatePuzzle();
blac3777 3:251e4d020501 188 cursorIndex = selectedIndex + 1;
blac3777 3:251e4d020501 189 return;
blac3777 3:251e4d020501 190 }
blac3777 3:251e4d020501 191 }
blac3777 3:251e4d020501 192 }
blac3777 3:251e4d020501 193 }