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.
Diff: Objects/Objects.cpp
- Revision:
- 2:888634fff8ff
- Parent:
- 1:2d3139578aca
- Child:
- 3:aa82968b7a8e
diff -r 2d3139578aca -r 888634fff8ff Objects/Objects.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Objects/Objects.cpp Sat Apr 06 12:08:39 2019 +0000 @@ -0,0 +1,78 @@ + +#include "Objects.h" + +// nothing doing in the constructor and destructor +Objects::Objects() +{ + +} + +Objects::~Objects() +{ + +} + +void Objects::init(int size,int speed) +{ + _size = size; + + _x_edge = WIDTH - _size/2; // Edge of horizontal + _y_edge = HEIGHT - _size/2; // Edge of vertical + + + _x = rand() % _x_edge; // Generate random position on the screen - 0 to the edge + _y = rand() % _y_edge; // Generate random position on the screen - 0 to the edge + + srand(time(NULL)); + int direction = rand() % 4; // randomise initial direction. + + // 4 possibilities. Get random modulo and set velocities accordingly + if (direction == 0) { + _velocity.x = speed; + _velocity.y = speed; + } else if (direction == 1) { + _velocity.x = speed; + _velocity.y = -speed; + } else if (direction == 2) { + _velocity.x = speed; + _velocity.y = speed; + } else { + _velocity.x = -speed; + _velocity.y = -speed; + } +} + +void Objects::draw(N5110 &lcd) +{ + lcd.drawCircle(_x,_y,_size,FILL_BLACK); // x,y,radius,black fill +} + +void Objects::update() +{ + _x += _velocity.x; + _y += _velocity.y; +} + +void Objects::set_velocity(Vector2D v) +{ + _velocity.x = v.x; + _velocity.y = v.y; +} + +Vector2D Objects::get_velocity() +{ + Vector2D v = {_velocity.x,_velocity.y}; + return v; +} + +Vector2D Objects::get_pos() +{ + Vector2D p = {_x,_y}; + return p; +} + +void Objects::set_pos(Vector2D p) +{ + _x = p.x; + _y = p.y; +} \ No newline at end of file