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
SnakeTail/SnakeTail.cpp@14:4356797fd16e, 2020-05-24 (annotated)
- Committer:
- sdlashmar
- Date:
- Sun May 24 15:27:53 2020 +0000
- Revision:
- 14:4356797fd16e
- Parent:
- 11:c4b740a970f8
FINISHED VERSION BEFORE TRYING TO IMPLEMENT TAIL COLLISION FUNCTION
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| sdlashmar | 5:256e5e0b6cd7 | 1 | #include "SnakeTail.h" |
| sdlashmar | 5:256e5e0b6cd7 | 2 | |
| sdlashmar | 5:256e5e0b6cd7 | 3 | SnakeTail::SnakeTail() |
| sdlashmar | 5:256e5e0b6cd7 | 4 | { |
| sdlashmar | 5:256e5e0b6cd7 | 5 | |
| sdlashmar | 5:256e5e0b6cd7 | 6 | } |
| sdlashmar | 5:256e5e0b6cd7 | 7 | |
| sdlashmar | 5:256e5e0b6cd7 | 8 | SnakeTail::~SnakeTail() |
| sdlashmar | 5:256e5e0b6cd7 | 9 | { |
| sdlashmar | 5:256e5e0b6cd7 | 10 | |
| sdlashmar | 5:256e5e0b6cd7 | 11 | } |
| sdlashmar | 14:4356797fd16e | 12 | /* The snake tail is drawn as individual segments |
| sdlashmar | 14:4356797fd16e | 13 | coordinates for each segment are stored in two arrays |
| sdlashmar | 14:4356797fd16e | 14 | this update function iterates through the segments and changes |
| sdlashmar | 14:4356797fd16e | 15 | the coordinates to the previous segments |
| sdlashmar | 14:4356797fd16e | 16 | */ |
| sdlashmar | 11:c4b740a970f8 | 17 | void SnakeTail::update(Vector2D prevHead, int length) |
| sdlashmar | 5:256e5e0b6cd7 | 18 | { |
| sdlashmar | 11:c4b740a970f8 | 19 | _length = length; |
| sdlashmar | 11:c4b740a970f8 | 20 | _x_init = prevHead.x; |
| sdlashmar | 11:c4b740a970f8 | 21 | _y_init = prevHead.y; |
| sdlashmar | 11:c4b740a970f8 | 22 | //printf("x_init = %i\n", _x_init); |
| sdlashmar | 11:c4b740a970f8 | 23 | //printf("y_init = %i\n", _y_init); |
| sdlashmar | 11:c4b740a970f8 | 24 | prevX = tailX[0]; |
| sdlashmar | 11:c4b740a970f8 | 25 | prevY = tailY[0]; |
| sdlashmar | 11:c4b740a970f8 | 26 | tailX[0] = _x_init; |
| sdlashmar | 11:c4b740a970f8 | 27 | tailY[0] = _y_init; |
| sdlashmar | 14:4356797fd16e | 28 | |
| sdlashmar | 11:c4b740a970f8 | 29 | for(int i = 1; i < _length; i++) { |
| sdlashmar | 14:4356797fd16e | 30 | //stores the current x and y coordinates |
| sdlashmar | 11:c4b740a970f8 | 31 | prev2X = tailX[i]; |
| sdlashmar | 11:c4b740a970f8 | 32 | prev2Y = tailY[i]; |
| sdlashmar | 14:4356797fd16e | 33 | //updtes the current coordinates so the are the same as i-1 |
| sdlashmar | 7:c67a5c6a874f | 34 | tailX[i] = prevX; |
| sdlashmar | 7:c67a5c6a874f | 35 | tailY[i] = prevY; |
| sdlashmar | 14:4356797fd16e | 36 | //sets previous x and y to the original coordinates for this i |
| sdlashmar | 11:c4b740a970f8 | 37 | prevX = prev2X; |
| sdlashmar | 11:c4b740a970f8 | 38 | prevY = prev2Y; |
| sdlashmar | 7:c67a5c6a874f | 39 | } |
| sdlashmar | 14:4356797fd16e | 40 | |
| sdlashmar | 14:4356797fd16e | 41 | /*debugging print statements to make sure the array is being updated correctly |
| sdlashmar | 14:4356797fd16e | 42 | for (int l = 0; l < _length; l++) { |
| sdlashmar | 11:c4b740a970f8 | 43 | printf("i = %i\n", l); |
| sdlashmar | 11:c4b740a970f8 | 44 | printf("tailX[i] = %i\n", tailX[l]); |
| sdlashmar | 11:c4b740a970f8 | 45 | printf("tailY[i] = %i\n", tailY[l]); |
| sdlashmar | 11:c4b740a970f8 | 46 | }*/ |
| sdlashmar | 5:256e5e0b6cd7 | 47 | } |
| sdlashmar | 11:c4b740a970f8 | 48 | |
| sdlashmar | 11:c4b740a970f8 | 49 | |
| sdlashmar | 11:c4b740a970f8 | 50 | |
| sdlashmar | 7:c67a5c6a874f | 51 | void SnakeTail::draw(N5110 &lcd, int length) { |
| sdlashmar | 7:c67a5c6a874f | 52 | int _length = length; |
| sdlashmar | 11:c4b740a970f8 | 53 | for(int j = 0; j < _length; j++) { |
| sdlashmar | 7:c67a5c6a874f | 54 | lcd.drawRect(tailX[j], tailY[j], 2, 2, FILL_BLACK); |
| sdlashmar | 7:c67a5c6a874f | 55 | } |
| sdlashmar | 7:c67a5c6a874f | 56 | } |
| sdlashmar | 7:c67a5c6a874f | 57 | |
| sdlashmar | 7:c67a5c6a874f | 58 | |
| sdlashmar | 5:256e5e0b6cd7 | 59 | |
| sdlashmar | 5:256e5e0b6cd7 | 60 | |
| sdlashmar | 5:256e5e0b6cd7 | 61 | |
| sdlashmar | 5:256e5e0b6cd7 | 62 | |
| sdlashmar | 5:256e5e0b6cd7 | 63 | |
| sdlashmar | 5:256e5e0b6cd7 | 64 |