ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el17sdl_v2

Dependencies:   mbed

Revision:
10:3958fb08696d
Parent:
9:f0213e632379
Child:
11:c4b740a970f8
--- a/GameEngine/GameEngine.cpp	Fri May 22 11:08:40 2020 +0000
+++ b/GameEngine/GameEngine.cpp	Fri May 22 12:29:53 2020 +0000
@@ -12,6 +12,9 @@
 
 void GameEngine::init(int head_size, int head_speed) 
 {
+    srand(time(NULL));
+     _mouse_x = rand() %WIDTH;
+     _mouse_y = rand() %HEIGHT;
     _game_over = false;
     _score = 0;
     _mouse = 0;
@@ -39,20 +42,16 @@
 {
     lcd.drawRect(0, 0, WIDTH, HEIGHT, FILL_TRANSPARENT);
     head.draw(lcd);
-    if (_mouse < 1) {
-        spawn_mouse(lcd);
-    };
+    spawn_mouse(lcd);
 }
 
 void GameEngine::spawn_mouse(N5110 &lcd) 
 {
     //create random x and y coordinates for mouse
-    srand(time(NULL));
-    int _mouse_x = rand() %WIDTH;
-    int _mouse_y = rand() %HEIGHT;
-    //check if mouse count is less than one, we only want one mouse spawned at a time 
+    printf("mouse x = %i\n", _mouse_x);
+    printf("mouse y = %i\n", _mouse_y);
     lcd.drawRect(_mouse_x, _mouse_y, 2, 2, FILL_BLACK);
-    _mouse = 1;
+    
         
 }
 
@@ -61,11 +60,16 @@
     Vector2D headPos = head.get_pos();
     int headX = headPos.x;
     int headY = headPos.y;
+    printf("head x = %i\n", headX);
+    printf("head y = %i\n", headY);
     
-    if(headX == _mouse_x && headY == _mouse_y) {
+    if((headX == _mouse_x || headX == _mouse_x+1 || headX == _mouse_x-1) && (headY == _mouse_y || (headY == _mouse_y+1 || headY == _mouse_y-1))) {
         _score = _score + 10;
         _mouse = 0;
         pad.tone(750.0, 0.1);
+        srand(time(NULL));
+     _mouse_x = rand() %WIDTH;
+     _mouse_y = rand() %HEIGHT;
     }
 }