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.
Objects/Objects.cpp
- Committer:
- ll16o2l
- Date:
- 2019-04-06
- Revision:
- 2:888634fff8ff
- Parent:
- Asteroids/Asteroid.cpp@ 1:2d3139578aca
- Child:
- 3:aa82968b7a8e
File content as of revision 2:888634fff8ff:
#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; }