ELEC2645 (2017/18) / Mbed 2 deprecated el15ww

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
weiway
Date:
Fri May 04 13:25:18 2018 +0000
Parent:
11:f026cd7d66ee
Child:
13:4026781772cb
Commit message:
add a boolean function. bool getfruit . This is for making the position of snake == fruit position so that snake achieve to eat fruit successfully. Also add a function to print the point of the snake ate at the top-left corner on LCD screen.

Changed in this revision

Snake_engine/Snake_engine.cpp Show annotated file Show diff for this revision Revisions of this file
Snake_engine/Snake_engine.h Show annotated file Show diff for this revision Revisions of this file
--- a/Snake_engine/Snake_engine.cpp	Thu May 03 23:53:58 2018 +0000
+++ b/Snake_engine/Snake_engine.cpp	Fri May 04 13:25:18 2018 +0000
@@ -0,0 +1,69 @@
+#include "Snake_engine.h"
+
+snake_engine::snake_engine()
+{
+
+}
+
+snake_engine::~snake_engine()
+{
+    
+        
+    
+}
+
+void snakengine::init()
+{
+    s.init();
+
+}
+
+void snakengine::draw(N5110 &lcd)
+{
+        lcd.refresh();
+        lcd.clear();
+        lcd.drawRect(0,8,WIDTH ,HEIGHT -8,FILL_TRANSPARENT);    
+
+    s.draw(lcd);
+    f.draw(lcd);
+    printscore(lcd);
+}
+
+void snakengine::update(Gamepad &pad, N5110 &lcd)
+{
+    s.update(_d,_mag); 
+   
+    if(getfruit(pad)) { 
+        f.update();
+        s.add_score();
+
+    }
+}
+
+
+bool snakengine::getftuit(Gamepad &pad)
+{
+    Vector2D _f_pos = f.get_pos();
+    Vector2D _s_pos = s.get_pos();
+    if ((_f_pos.y >= _s_pos.y || _f_pos.y == _s_pos.y-1) &&
+            (_f_pos.y <= _s_pos.y || _f_pos.y+1 == _s_pos.y-1) &&
+            (_f_pos.x >= _s_pos.x || _f_pos.x+1 >= _s_pos.x || _f_pos.x+1 >= _s_pos.x ) &&
+            (_f_pos.x <= _s_pos.x || _f_pos.x+1 <= _s_pos.x)) { 
+        return true;
+    } else {
+        return false;
+    }
+}
+
+
+void snakengine::print_point(N5110 &lcd)
+{
+    int print_point = s.get_point();
+    char buffer[14];
+    sprintf(buffer,"%2d",snakepoint);
+    lcd.printString(buffer,WIDTH/2 - 40,0); 
+}
+
+
+
+
--- a/Snake_engine/Snake_engine.h	Thu May 03 23:53:58 2018 +0000
+++ b/Snake_engine/Snake_engine.h	Fri May 04 13:25:18 2018 +0000
@@ -16,6 +16,7 @@
         void draw(N5110 &lcd);
         void update(Gamepad &pad);
         void read_input(Gamepad &pad);
+        bool getfruit(Gamepad &pad);