From a2b00a23a892a465a5fcab5ecbdd9ecc76ecf406 Mon Sep 17 00:00:00 2001 From: Waleed El-Geresy Date: Mon, 8 May 2017 11:16:51 +0100 Subject: 2048 finished --- e2-switch | 1 + src/main.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 50 insertions(+), 14 deletions(-) create mode 160000 e2-switch diff --git a/e2-switch b/e2-switch new file mode 160000 index 0000000..28b37c9 --- /dev/null +++ b/e2-switch @@ -0,0 +1 @@ +Subproject commit 28b37c923bb84c753b22c083bc3e9a8ef4d6e110 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"); } } + -- cgit v1.2.3