ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el17sdl_v2

Dependencies:   mbed

Revision:
11:c4b740a970f8
Parent:
10:3958fb08696d
Child:
12:cb3a81adf48b
diff -r 3958fb08696d -r c4b740a970f8 GameEngine/GameEngine.cpp
--- a/GameEngine/GameEngine.cpp	Fri May 22 12:29:53 2020 +0000
+++ b/GameEngine/GameEngine.cpp	Sat May 23 10:50:43 2020 +0000
@@ -13,11 +13,12 @@
 void GameEngine::init(int head_size, int head_speed) 
 {
     srand(time(NULL));
-     _mouse_x = rand() %WIDTH;
-     _mouse_y = rand() %HEIGHT;
+     _mouse_x = rand() %WIDTH-4;
+     _mouse_y = rand() %HEIGHT-4;
     _game_over = false;
     _score = 0;
     _mouse = 0;
+    _length = 0;
     _head_size = head_size;
     _head_speed = head_speed;
     
@@ -33,6 +34,8 @@
 void GameEngine::update(Gamepad &pad, N5110 &lcd) 
 {
     head.change_direction(_d);
+    Vector2D prevHead = head.get_pos();
+    tail.update(prevHead, _length);
     head.update();
     check_mouse_eaten(pad);
     check_wall_collision(pad, lcd);
@@ -42,14 +45,16 @@
 {
     lcd.drawRect(0, 0, WIDTH, HEIGHT, FILL_TRANSPARENT);
     head.draw(lcd);
+    tail.draw(lcd, _length);
+   
     spawn_mouse(lcd);
 }
 
 void GameEngine::spawn_mouse(N5110 &lcd) 
 {
     //create random x and y coordinates for mouse
-    printf("mouse x = %i\n", _mouse_x);
-    printf("mouse y = %i\n", _mouse_y);
+    //printf("mouse x = %i\n", _mouse_x);
+    //printf("mouse y = %i\n", _mouse_y);
     lcd.drawRect(_mouse_x, _mouse_y, 2, 2, FILL_BLACK);
     
         
@@ -60,16 +65,17 @@
     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);
+    //printf("head x = %i\n", headX);
+    //printf("head y = %i\n", headY);
     
     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;
+     _mouse_x = rand() %WIDTH-4;
+     _mouse_y = rand() %HEIGHT-4;
+     _length++;
     }
 }