The classic game of snake. Control using a joystick and displayed on a VGA screen

Dependencies:   4DGL direction draw game list mbed

Files at this revision

API Documentation at this revision

Comitter:
lucoby
Date:
Thu Oct 11 20:24:26 2012 +0000
Parent:
0:1ca938e58b91
Commit message:
updated drawing and game engine

Changed in this revision

draw.lib Show annotated file Show diff for this revision Revisions of this file
game.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 1ca938e58b91 -r aa27e5ef379b draw.lib
--- a/draw.lib	Thu Oct 11 19:00:03 2012 +0000
+++ b/draw.lib	Thu Oct 11 20:24:26 2012 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/lucoby/code/draw/#06f3e47afa60
+http://mbed.org/users/lucoby/code/draw/#2e528b145987
diff -r 1ca938e58b91 -r aa27e5ef379b game.lib
--- a/game.lib	Thu Oct 11 19:00:03 2012 +0000
+++ b/game.lib	Thu Oct 11 20:24:26 2012 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/lucoby/code/game/#3668e8f103be
+http://mbed.org/users/lucoby/code/game/#fae551b1bd58
diff -r 1ca938e58b91 -r aa27e5ef379b main.cpp
--- a/main.cpp	Thu Oct 11 19:00:03 2012 +0000
+++ b/main.cpp	Thu Oct 11 20:24:26 2012 +0000
@@ -17,26 +17,44 @@
     int points;
     int tempRow, tempCol;
     int draw_yes = 0;
+    int draw_y2 = 0;
+    int spx = 0;
+    int spy = 0;
     int i;
-    State game = start;
+    State game = splash;
     drawSetup();
 
     while(1) {
     wait(0.016666667);
     myled = !myled;
         switch(game) {
-            case start:
+            case splash:
+                draw_y2++;
+                if(draw_y2 == 19) {
+                    drawBlank(spy,spx);
+                    spx = rand() % COL;
+                    spy = rand() % ROW;
+                    drawApple(spy,spx);
+                    draw_y2 = 0;
+                }    
+                if((direction = getDirection(none)) != none) game = start;
+                break;
+            case start:               
                 if((direction = getDirection(none)) != none) {
                     deleteList(&snake);
                     clearscr();
-                    for(i = -1; i < ROW + 2; i++) {                      
-                        drawPixel(i,-1,0xFFFFFF);
+                    for(i = -1; i < COL + 2; i++){
+                        drawPixel(-1,i,0xFFFFFF);
+                    }
+                    for(i = 0; i < ROW + 2; i++) {                      
                         drawPixel(i,COL+1,0xFFFFFF);
                     }
-                    for(i = -1; i < COL + 2; i++){
-                        drawPixel(-1,i,0xFFFFFF);
+                    for(i = COL + 1; i > -2; i--){
                         drawPixel(ROW+1,i,0xFFFFFF);
                     }
+                    for(i = ROW + 1; i > -2; i--) {                      
+                        drawPixel(i,-1,0xFFFFFF);
+                    }
                     addToHead(&snake, 5, 5);
                     addToHead(&snake, 5, 6);
                     addToHead(&snake, 5, 7);
@@ -117,6 +135,7 @@
                 
                if(!checkGameOver(&snake)){
                     game = end;
+                    break;
                 }
                 
                 //view
@@ -128,7 +147,10 @@
                 drawScore(points);
                 break;
             case end:            
-                if(!pb) game = start;
+                if(!pb) { 
+                game = splash;
+                clearscr();
+                }
                 break;
         }                
     }