diff options
Diffstat (limited to 'src/main.cpp')
-rw-r--r-- | src/main.cpp | 63 |
1 files changed, 49 insertions, 14 deletions
diff --git a/src/main.cpp b/src/main.cpp index 229ef25..ec5117b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,7 +8,7 @@ #define SW_PERIOD 20000 // 20ms void tout(void); -void draw(int board[4][4]); +void draw(int board[4][4], int score); // Onboard LED PinName switch_pin[] = { SW_PINS }; @@ -33,7 +33,7 @@ int main(void) { int done = 0; int board[4][4]; - init(board); + init(board); //Function from 2048.h to initialise values oled.setRotation(2); wait(0.5); @@ -46,10 +46,23 @@ int main(void) //Attach switch sampling timer ISR to the timer instance with the required period timer.attach_us(&tout, SW_PERIOD); + //Print splash screen for 1 second oled.clearDisplay(); - oled.printf("%ux%u Group Ay08-04\n", oled.width(), oled.height()); - - draw(board); + oled.setTextCursor(oled.width()/2-10,oled.height()/2); + oled.printf("2048"); + oled.setTextCursor(oled.width()/2-30,oled.height()/2+10); + oled.printf("\n%ux%u Group Ay08-04\n", oled.width(), oled.height()); + oled.display(); + + wait(1); + + int score = 0; + bool gameOver = false; + + //Print initial board + oled.clearDisplay(); + draw(board,score); + oled.display(); for (;;) { if (update) { @@ -58,22 +71,40 @@ int main(void) oled.setTextCursor(0, 0); - //Write the latest switch osciallor count + //Write the latest switch osciallor count. done is true if move valid + if(!gameOver) + { for (int i = 3; i >= 0; --i) { if (switch_pressed[i] && !last_pressed[i]) - done = move(i, board); + done = move(i, board); //Apply move to board } if(done){ - add2(board); - draw(board); + add2(board); //Add a new 2 if there are 0's + score++; + oled.clearDisplay(); + draw(board,score); + oled.display(); + } } if(gameover(board)) - oled.printf("\nGame Over!"); + { + oled.clearDisplay(); + oled.setTextCursor(oled.width()/2-30,oled.height()/2); + oled.printf("Game Over!"); + oled.display(); + gameOver = true; + wait(3); + oled.clearDisplay(); + oled.setTextCursor(oled.width()/2-30,oled.height()/2); + oled.printf("Again?"); + oled.display(); + //TODO: Put in code to set game over to false if up is pressed + } //Copy the display buffer to the display - oled.display(); + } } @@ -99,8 +130,11 @@ void tout(void) update = 1; } -void draw(int board[SIZE][SIZE]) +//Board stores powers of 2, so must convert and then print +void draw(int board[SIZE][SIZE], int score) { + oled.setTextCursor(0,0); + oled.printf("Score: %d", score); int x, y; int value; for (y = 0; y < SIZE; y++) { @@ -113,8 +147,9 @@ void draw(int board[SIZE][SIZE]) } else { value = 0; } - oled.printf("%d\t", value); + oled.setTextCursor(oled.width()/4*(x),oled.height()*(y+1)/5); + oled.printf("%d", value); } - oled.printf("\n"); } } + |