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
Diff: Body/Body.cpp
- Revision:
- 9:f3259d652208
- Parent:
- 8:59b01b57f0da
--- a/Body/Body.cpp Sun May 31 16:06:31 2020 +0000
+++ b/Body/Body.cpp Thu Jun 04 18:47:58 2020 +0000
@@ -1,40 +1,49 @@
#include "Body.h"
-Body::Body() {
- }
+//Constructor
+Body::Body()
+{
-Body::~Body() {
- }
+}
+//Destructor
+Body::~Body()
+{
+}
+// Passing the position of current body position to the next body position
void Body::update(Vector2D hP, int nbody) {
-_nbody = nbody;
-_x_init = hP.x;
-_y_init = hP.y;
- prevx = bodyx[0];
- prevy = bodyy[0];
-bodyx[0] = _x_init;
-bodyy[0] = _y_init;
+ _nbody = nbody;
+ _x = hP.x;
+ _y = hP.y;
+ _prevx = bodyx[0];
+ _prevy = bodyy[0];
+ bodyx[0] = _x;
+ bodyy[0] = _y;
+
+
for (int i = 1; i < nbody; i++) {
- prev2x = bodyx[i];
- prev2y = bodyy[i];
- bodyx[i] = prevx;
- bodyy[i] = prevy;
- prevx = prev2x;
- prevy = prev2y;
+ _prev2x = bodyx[i];
+ _prev2y = bodyy[i];
+ bodyx[i] = _prevx;
+ bodyy[i] = _prevy;
+ _prevx = _prev2x;
+ _prevy = _prev2y;
}
- }
- void Body::draw(N5110 &lcd, int nbody) {
+}
+
+void Body::draw(N5110 &lcd, int nbody) {
int _nbody = nbody;
-for (int k = 0; k < _nbody; k++){ //Snake tail
- lcd.drawRect(bodyx[k], bodyy[k], 2, 2, FILL_BLACK);
- }
- }
+
+for (int k = 0; k < _nbody; k++){
+ lcd.drawRect(bodyx[k], bodyy[k], 2, 2, FILL_BLACK); // Snake body drawn as rectangles
+ }
+}
- Vector2D Body::get_pos() {
+ Vector2D Body::get_pos() {
- for (int j = 0; j < _nbody; j++) {
- Vector2D b = {bodyx[j], bodyy[j]};
+for (int j = 0; j < _nbody; j++) {
+ Vector2D b = {bodyx[j], bodyy[j]}; // Getting the positions of the whole body
return b;
-}
+ }
}
\ No newline at end of file