Various applications using the LCD. Cycle through the applications by pressing down on the joystick

Dependencies:   C12832 MMA7660 mbed

Files at this revision

API Documentation at this revision

Comitter:
jad19925
Date:
Fri Feb 21 22:33:25 2014 +0000
Parent:
0:b8fc40cb537b
Commit message:
Bug fixes in Snake

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Fri Feb 21 22:12:31 2014 +0000
+++ b/main.cpp	Fri Feb 21 22:33:25 2014 +0000
@@ -41,6 +41,7 @@
     const int SNAKE_MAX = 256;
     Point snake[SNAKE_MAX];
     int snakeHead = 4;//the front filled square in the snake
+    int newHead = 0;
     int snakeTail = 0;
     SnakeDirection snakeGo = RIGHT;
     bool gameOver = false;
@@ -165,16 +166,18 @@
                         lcd.fillrect(snake[snakeTail].x, snake[snakeTail].y, snake[snakeTail].x+1, snake[snakeTail].y+1, 0);
                         snakeTail = (snakeTail+1)%SNAKE_MAX;
                         //draw new front
-                        snake[snakeHead+1].x = snake[snakeHead].x + 2;
-                        snake[snakeHead+1].y = snake[snakeHead].y;
-                        snakeHead = (snakeHead+1)%SNAKE_MAX;
+                        newHead = (snakeHead+1)%SNAKE_MAX;
+                        snake[newHead].x = snake[snakeHead].x + 2;
+                        snake[newHead].y = snake[snakeHead].y;
+                        snakeHead = newHead;
                         lcd.fillrect(snake[snakeHead].x, snake[snakeHead].y, snake[snakeHead].x+1, snake[snakeHead].y+1, 1);
                         //copy to lcd
                         lcd.copy_to_lcd();
                         //check if we went off the screen
                         if(snake[snakeHead].x >= SCREEN_WIDTH){
                             gameOver = true;
-                            pc.printf("Off screen to right\n");
+                            pc.printf("Off screen to right\r\n");
+                            pc.printf("snakeHead:%d x=%d, y=%d",snakeHead,snake[snakeHead].x,snake[snakeHead].y);
                         }
                         break;
                     case LEFT:
@@ -182,16 +185,18 @@
                         lcd.fillrect(snake[snakeTail].x, snake[snakeTail].y, snake[snakeTail].x+1, snake[snakeTail].y+1, 0);
                         snakeTail = (snakeTail+1)%SNAKE_MAX;
                         //draw new front
-                        snake[snakeHead+1].x = snake[snakeHead].x - 2;
-                        snake[snakeHead+1].y = snake[snakeHead].y;
-                        snakeHead = (snakeHead+1)%SNAKE_MAX;
+                        newHead = (snakeHead+1)%SNAKE_MAX;
+                        snake[newHead].x = snake[snakeHead].x - 2;
+                        snake[newHead].y = snake[snakeHead].y;
+                        snakeHead = newHead;
                         lcd.fillrect(snake[snakeHead].x, snake[snakeHead].y, snake[snakeHead].x+1, snake[snakeHead].y+1, 1);
                         //copy to lcd
                         lcd.copy_to_lcd();
                         //check if we went off the screen
                         if(snake[snakeHead].x < 0){
                             gameOver = true;
-                            pc.printf("Off screen to left\n");
+                            pc.printf("Off screen to left\r\n");
+                            pc.printf("snakeHead:%d x=%d, y=%d",snakeHead,snake[snakeHead].x,snake[snakeHead].y);
                         }
                         break;
                     case UP:
@@ -199,16 +204,18 @@
                         lcd.fillrect(snake[snakeTail].x, snake[snakeTail].y, snake[snakeTail].x+1, snake[snakeTail].y+1, 0);
                         snakeTail = (snakeTail+1)%SNAKE_MAX;
                         //draw new front
-                        snake[snakeHead+1].x = snake[snakeHead].x;
-                        snake[snakeHead+1].y = snake[snakeHead].y - 2;
-                        snakeHead = (snakeHead+1)%SNAKE_MAX;
+                        newHead = (snakeHead+1)%SNAKE_MAX;
+                        snake[newHead].x = snake[snakeHead].x;
+                        snake[newHead].y = snake[snakeHead].y - 2;
+                        snakeHead = newHead;
                         lcd.fillrect(snake[snakeHead].x, snake[snakeHead].y, snake[snakeHead].x+1, snake[snakeHead].y+1, 1);
                         //copy to lcd
                         lcd.copy_to_lcd();
                         //check if we went off the screen
                         if(snake[snakeHead].y < 0){
                             gameOver = true;
-                            pc.printf("Off screen top\n");
+                            pc.printf("Off screen top\r\n");
+                            pc.printf("snakeHead:%d x=%d, y=%d",snakeHead,snake[snakeHead].x,snake[snakeHead].y);
                         }
                         //wait?
                         break;
@@ -217,16 +224,18 @@
                         lcd.fillrect(snake[snakeTail].x, snake[snakeTail].y, snake[snakeTail].x+1, snake[snakeTail].y+1, 0);
                         snakeTail = (snakeTail+1)%SNAKE_MAX;
                         //draw new front
-                        snake[snakeHead+1].x = snake[snakeHead].x;
-                        snake[snakeHead+1].y = snake[snakeHead].y + 2;
-                        snakeHead = (snakeHead+1)%SNAKE_MAX;
+                        newHead = (snakeHead+1)%SNAKE_MAX;
+                        snake[newHead].x = snake[snakeHead].x;
+                        snake[newHead].y = snake[snakeHead].y + 2;
+                        snakeHead = newHead;
                         lcd.fillrect(snake[snakeHead].x, snake[snakeHead].y, snake[snakeHead].x+1, snake[snakeHead].y+1, 1);
                         //copy to lcd
                         lcd.copy_to_lcd();
                         //check if we went off the screen
                         if(snake[snakeHead].y >= SCREEN_HEIGHT){
                             gameOver = true;
-                            pc.printf("Off screen bottom\n");
+                            pc.printf("Off screen bottom\r\n");
+                            pc.printf("snakeHead:%d x=%d, y=%d",snakeHead,snake[snakeHead].x,snake[snakeHead].y);
                         }
                         //wait?
                         break;
@@ -251,7 +260,7 @@
                     if(points > SNAKE_MAX-5)
                     {
                         gameOver = true;
-                        pc.printf("Game Winner!\n");
+                        pc.printf("Game Winner!\r\n");
                     }
                 }
                 //check if we hit ourself
@@ -263,7 +272,8 @@
                     int iMod = i%SNAKE_MAX;
                     if(snake[snakeHead].x == snake[iMod].x && snake[snakeHead].y == snake[iMod].y){
                         gameOver = true;
-                        pc.printf("Hit Self\n");
+                        pc.printf("Hit Self\r\n");
+                        pc.printf("snakeHead:%d x=%d, y=%d",snakeHead,snake[snakeHead].x,snake[snakeHead].y);
                     }
                 }
                 
@@ -271,7 +281,7 @@
                     //lcd.cls();
                     lcd.locate(0,0);
                     lcd.printf("Game Over, Press Joystick to start over\nPoints Scored: %d",points);
-                    pc.printf("Game Over\n");
+                    pc.printf("Game Over\r\n");
                     while(!fire){
                         //infinite loop to wait for user to press joystick to restart
                     }