complete

Dependencies:   FatFileSystem mbed

Fork of SnakeSnakeGame by Tho Jian Xiang

Files at this revision

API Documentation at this revision

Comitter:
Rexry7878
Date:
Tue Aug 11 05:25:56 2015 +0000
Parent:
1:4222a8f9ca88
Commit message:
complete

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sat Aug 08 09:32:56 2015 +0000
+++ b/main.cpp	Tue Aug 11 05:25:56 2015 +0000
@@ -32,8 +32,8 @@
 
 
 int rad;       //variable for random number
-int p1=10;     //x-coordinate for food
-int p2=20;     //y-coordinate for food
+int p1=50;     //x-coordinate for food
+int p2=15;     //y-coordinate for food
 int fDimX;
 int fDimY;
     
@@ -70,9 +70,20 @@
     r=1.0; //RGB LED off - PWM 100% duty cycle
     g=1.0;
     b=1.0;
+    int x=0, y=0;
+    int bonus = 0;
 
     while(1) 
     {
+            //CONDITION FOR CHECKING FOR THE FIREBALL
+            if (fire && score>=5) {
+            leds=0xf;
+            flag = 100;
+            if(bonus == 0){
+                score -= 2;
+                bonus = 1;
+                }
+            } 
             // CONDITIONS TO CHECK FOR JOYSTICK INPUT
             leds=joy;
             lcd.locate(i,j);
@@ -83,6 +94,7 @@
                 i=i-cst;
                 back_flag=1;
                 do{
+                    b = 1.0- pot2;
                     i--;      
                     lcd.locate(i,j);
                     lcd.printf("~+");
@@ -95,8 +107,8 @@
                     lcd.printf(".");
                         if(i<0){
                             end_flag=1;
-                            r=1.0-pot2;
                             do{
+                                r=1.0-pot2;
                                 lcd.cls();
                                 lcd.locate(25,10);
                                 lcd.printf("GAME OVER!!");
@@ -119,6 +131,7 @@
                 i=i+cst;
                 forward_flag=1;
                 do{
+                    b = 1.0- pot2;
                     i++;      
                     lcd.locate(i,j);
                     lcd.printf("~+");
@@ -131,8 +144,8 @@
                     lcd.printf(".");
                         if(i>100){
                             end_flag=1;
-                            r=1.0-pot2;
                             do{
+                                r=1.0-pot2;
                                 lcd.cls();
                                 lcd.locate(25,10);
                                 lcd.printf("GAME OVER!!");
@@ -155,6 +168,7 @@
                 j=j-cst;
                 up_flag=1;
                 do{
+                    b = 1.0- pot2;
                     j--;      
                     lcd.locate(i,j);
                     lcd.printf("~+");
@@ -167,8 +181,8 @@
                     lcd.printf(".");
                         if(j<0){
                             end_flag=1;
-                            r=1.0-pot2;
                             do{
+                                r=1.0-pot2;
                                 lcd.cls();
                                 lcd.locate(25,10);
                                 lcd.printf("GAME OVER!!");
@@ -191,6 +205,7 @@
                 j=j+cst;
                 down_flag=1;
                 do{
+                    b = 1.0- pot2;
                     j++;      
                     lcd.locate(i,j);
                     lcd.printf("~+");
@@ -203,8 +218,8 @@
                     lcd.printf(".");
                         if(j>26){
                             end_flag=1;
-                            r=1.0-pot2;
                             do{
+                                r=1.0-pot2;
                                 lcd.cls();
                                 lcd.locate(25,10);
                                 lcd.printf("GAME OVER!!");
@@ -221,9 +236,45 @@
                             break;}
                 }while(down_flag==1);
             }
+            
+        // LOGIC FOR THE FIREBALL         
+        if (flag>=1){
+        x = (x + MMA.x() * 32.0)/1.5;
+        y = (y -(MMA.y() * 16.0))/1.5;
+        lcd.fillcircle(x+63, y+15, 3, 1); //draw bubble
+        //lcd.circle(63, 15, 8, 1);
+        wait(.1); //time delay
+        flag -=1;
+        if (abs(x + 63 - p1) <=cst && abs(y + 15 - p2) <=cst+1){
+            score +=15;
+            flag = -1;
+            pc.printf(" score %d\n\r", score);
+            g = 1.0-pot2;
+            wait(0.3);
+            g = 1.0;
+            b = 1.0 - pot2;
+        }
+    
+        if (flag < 0)
+            flag = 0; 
+            lcd.fillcircle(x+63, y+15, 3, 0); //erase bubble
+            //finding a new random location for food
+            rad=rand();
+            p1= rad%fDimX;
+            p2= rad%fDimY;
+            if (p1 < 30)
+                p1 = 30;
+            if (p2 < 10)
+                p2 = 10;
+            lcd.printf(".");
+        }            
+        
                 
         if (flag == 0)
         {
+            lcd.cls();
+            lcd.locate(i,j);
+            lcd.printf("~+");
             //print score on the LCD
             lcd.locate(110, 22);
             lcd.printf("%d", score);
@@ -232,11 +283,12 @@
             lcd.printf(".");
             // CONDITION FOR CHECKING THE SNAKE FOOD COLLISION
             if (abs(i + 8 - p1) <=cst && abs(j - p2) <=cst+1)
-            { 
+            {
                 score = score + 1;
                 pc.printf("score %d\n\r", score);
                 g = 1.0 - pot2; //RGB LED green
-                wait(0.3);
+                wait(0.5);
+                g = 1.0;
                 b = 1.0 - pot2;
                 //finding a new random location for food
                 rad=rand();