diff options
| author | Waleed El-Geresy <waleed.el-geresy15@imperial.ac.uk> | 2017-05-08 11:16:51 +0100 | 
|---|---|---|
| committer | Vasil Zlatanov <v@skozl.com> | 2017-05-08 11:48:21 +0100 | 
| commit | a2b00a23a892a465a5fcab5ecbdd9ecc76ecf406 (patch) | |
| tree | 914a16c8cc6defd329b15101fe8dae3536a614f7 | |
| parent | 673e4900e93767b8bb3241b89c13424ff6fbf547 (diff) | |
| download | e2-switch-a2b00a23a892a465a5fcab5ecbdd9ecc76ecf406.tar.gz e2-switch-a2b00a23a892a465a5fcab5ecbdd9ecc76ecf406.tar.bz2 e2-switch-a2b00a23a892a465a5fcab5ecbdd9ecc76ecf406.zip | |
2048 finished
| m--------- | e2-switch | 0 | ||||
| -rw-r--r-- | src/main.cpp | 63 | 
2 files changed, 49 insertions, 14 deletions
| diff --git a/e2-switch b/e2-switch new file mode 160000 +Subproject 28b37c923bb84c753b22c083bc3e9a8ef4d6e11 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");  	}  } + | 
