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
Head/Head.cpp
- Committer:
- el18lg
- Date:
- 2020-05-27
- Revision:
- 4:748b3e0062f6
- Parent:
- 3:beb0cc405b1e
- Child:
- 5:e0f08e8022de
File content as of revision 4:748b3e0062f6:
#include "Head.h" // nothing doing in the constructor and destructor Head::Head() { } Head::~Head() { } void Head::init(int x, int y, int length, int speed) { _x = x; _y = y; _length = length; _speed = speed; srand(time(NULL)); _x = WIDTH/2 - _length/2; // snake spawns in random position _y = HEIGHT/2 - _length/2; // set the direction of snake to north } void Head::draw(N5110 &lcd) { lcd.drawCircle(_x,_y,_length,FILL_BLACK); } void Head::update(Direction d, float mag) { // update y value depending on direction of movement // North is decrement as origin is at the top-left so decreasing moves up _speed = int(mag*10.0f); if (d == N && d != S){ _y -= _speed; } if (d == S && d != N){ _y += _speed; } if (d == W && d != E){ _x -= _speed; } if (d == E && d != W){ _x += _speed; } switch(d){ case S: _y; break; case N: _y = 1; break; case W: _x = 1; break; case E: _x = 1; break; } } // The head changes to the new position Vector2D Head::get_pos() { Vector2D pos = {_x,_y}; return pos; }