Dependencies:   4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player

Revision:
1:b20b76acf0ed
Parent:
0:c9df12bcc92d
--- a/main.cpp	Mon Oct 31 15:08:43 2016 +0000
+++ b/main.cpp	Mon Oct 31 16:34:00 2016 +0000
@@ -26,7 +26,7 @@
 };
 
 struct Ball {
-    int x, y, xvel, yvel;
+    float x, y, xvel, yvel;
 };
 
 struct Block {
@@ -50,6 +50,9 @@
 bool sound = false;
 int old_bx, old_by;
 int old_x1, old_x2;
+int blocksBroken;
+Ticker speedTimeout;
+int speedIncreases;
 
 Mutex splosions;
 
@@ -80,7 +83,6 @@
         splosions.lock();
         check = sound;
         splosions.unlock();
-        printf("%d\n", check);
         if (check) {
             printf("Explosion1\n");
             wave_file=fopen("/sd/mydir/explosion.wav","r");
@@ -186,12 +188,22 @@
     b1.y = b1.y - b1.yvel;
 }
 
+void decrementIncreases() {
+    if (speedIncreases > 0) {
+        speedIncreases--;
+    }
+}
+
+void incSpeed() {
+    if (speedIncreases < 4) {
+        b1.xvel += b1.xvel * 1.1;
+        b1.yvel += b1.yvel * 1.1;
+    }
+    printf("x:%f y:%f", b1.xvel, b1.yvel);
+}
+
 void checkWin() {
-    bool win = true;
-    for(int i = 0; i < 24; i++) {
-        win = win && !blocks[i].active;
-    }
-    if (win) {
+    if (blocksBroken == 24) {
         gameover = 1;
     }
 }
@@ -205,6 +217,7 @@
                     b1.yvel = -b1.yvel;
                     uLCD.filled_rectangle(blocks[i].x1, blocks[i].y1, blocks[i].x2, blocks[i].y2, BLACK);
                     createExplosion(nx, ny);
+                    blocksBroken++;
                     checkWin();
                 }
             }
@@ -252,6 +265,8 @@
     b1.y = 121;
     b1.xvel = 0;
     b1.yvel = 0;
+    blocksBroken = 0;
+    speedIncreases = 0;
     splosions.lock();
     sound = false;
     splosions.unlock();
@@ -264,7 +279,7 @@
         if(myNav.right()) {
             movePaddle(MOVE_AMT);
             drawPaddle();
-            if (b1.xvel == 0 || b1.yvel == 0) {
+            if (b1.xvel == 0 && b1.yvel == 0) {
                 b1.x = (p1.x1 + p1.x2) / 2;
                 drawBall();
             }
@@ -280,12 +295,15 @@
 //          printf("%d\n", p1.x1);
         }
         if (myNav.fire()) {
-            b1.xvel = 3;
-            b1.yvel = 2;
+            b1.xvel = 1 + rand() % 7 - 4;
+            b1.yvel = 1 + rand() % 3;
         }
         if (myNav.down()) {
             gameover = 1;
         }
+        if (myNav.up()) {
+            incSpeed();
+        }
         moveBall();
         if (b1.xvel != 0 || b1.yvel != 0) {
             drawBall();
@@ -301,6 +319,7 @@
     uLCD.baudrate(3000000);
     Thread t2(playExplosion);
     Thread t1(playPaddle);
+    speedTimeout.attach(&decrementIncreases, 5.0);
     while(1) {
         startGame();
         printf("Over\n");