Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed Gamepad N5110
Diff: snake/snake.cpp
- Revision:
- 10:68076e3dcc33
- Parent:
- 9:18b059e5abb9
- Child:
- 11:543c62bed764
diff -r 18b059e5abb9 -r 68076e3dcc33 snake/snake.cpp
--- a/snake/snake.cpp	Sat Apr 20 08:15:10 2019 +0000
+++ b/snake/snake.cpp	Sat Apr 20 15:15:44 2019 +0000
@@ -75,14 +75,19 @@
     srand(time(NULL));
     ball_x = 3 + rand() % (WIDTH - 6); // randomise initial position of ball. 
     ball_y = 3 + rand() % (HEIGHT - 6);
-    for (int i = 0; i < length; i++) {
-        if(ball_x + _size >= _x[i] && ball_x - _size <= _x[i] && ball_y + _size >= _y[i] && ball_y - _size <= _y[i]){
-                srand(time(NULL));
-                ball_x = 3 + rand() % (WIDTH - 6); // randomise initial position of ball. 
-                ball_y = 3 + rand() % (HEIGHT - 6);
-            }
-    }
-
+    int check = 0;
+    do{
+      check = 0;
+      for (int i = 0; i < length; i++) {
+          //if(ball_x == _x[i] && ball_y ==_y[i]) {
+          if(ball_x + _size  >= _x[i] && ball_x - _size <= _x[i] && ball_y + _size >= _y[i] && ball_y - _size <= _y[i]){
+           check = 1;
+          }
+       }
+        //srand(time(NULL));
+        ball_x = 3 + rand() % (WIDTH - 6); // randomise initial position of ball. 
+        ball_y = 3 + rand() % (HEIGHT - 6);
+     } while(check == 1);
 }
 
 void snake::check_eat(Gamepad &pad)