ELEC2645 (2018/19) / Mbed 2 deprecated ll16o2l_ELEC2645

Dependencies:   mbed Gamepad

Committer:
ll16o2l
Date:
Tue May 07 18:01:54 2019 +0000
Revision:
15:807eba7c7811
Parent:
3:aa82968b7a8e
Re-organised the documentation and finished the project.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ll16o2l 2:888634fff8ff 1 #include "Objects.h"
ll16o2l 1:2d3139578aca 2
ll16o2l 1:2d3139578aca 3 // nothing doing in the constructor and destructor
ll16o2l 2:888634fff8ff 4 Objects::Objects()
ll16o2l 1:2d3139578aca 5 {
ll16o2l 1:2d3139578aca 6
ll16o2l 1:2d3139578aca 7 }
ll16o2l 1:2d3139578aca 8
ll16o2l 2:888634fff8ff 9 Objects::~Objects()
ll16o2l 1:2d3139578aca 10 {
ll16o2l 1:2d3139578aca 11
ll16o2l 1:2d3139578aca 12 }
ll16o2l 1:2d3139578aca 13
ll16o2l 15:807eba7c7811 14
ll16o2l 2:888634fff8ff 15 void Objects::init(int size,int speed)
ll16o2l 1:2d3139578aca 16 {
ll16o2l 1:2d3139578aca 17 _size = size;
ll16o2l 1:2d3139578aca 18
ll16o2l 2:888634fff8ff 19 _x_edge = WIDTH - _size/2; // Edge of horizontal
ll16o2l 2:888634fff8ff 20 _y_edge = HEIGHT - _size/2; // Edge of vertical
ll16o2l 2:888634fff8ff 21
ll16o2l 2:888634fff8ff 22
ll16o2l 2:888634fff8ff 23 _x = rand() % _x_edge; // Generate random position on the screen - 0 to the edge
ll16o2l 2:888634fff8ff 24 _y = rand() % _y_edge; // Generate random position on the screen - 0 to the edge
ll16o2l 1:2d3139578aca 25
ll16o2l 1:2d3139578aca 26 srand(time(NULL));
ll16o2l 1:2d3139578aca 27 int direction = rand() % 4; // randomise initial direction.
ll16o2l 1:2d3139578aca 28
ll16o2l 1:2d3139578aca 29 // 4 possibilities. Get random modulo and set velocities accordingly
ll16o2l 1:2d3139578aca 30 if (direction == 0) {
ll16o2l 1:2d3139578aca 31 _velocity.x = speed;
ll16o2l 1:2d3139578aca 32 _velocity.y = speed;
ll16o2l 1:2d3139578aca 33 } else if (direction == 1) {
ll16o2l 1:2d3139578aca 34 _velocity.x = speed;
ll16o2l 1:2d3139578aca 35 _velocity.y = -speed;
ll16o2l 1:2d3139578aca 36 } else if (direction == 2) {
ll16o2l 1:2d3139578aca 37 _velocity.x = speed;
ll16o2l 1:2d3139578aca 38 _velocity.y = speed;
ll16o2l 1:2d3139578aca 39 } else {
ll16o2l 1:2d3139578aca 40 _velocity.x = -speed;
ll16o2l 1:2d3139578aca 41 _velocity.y = -speed;
ll16o2l 1:2d3139578aca 42 }
ll16o2l 1:2d3139578aca 43 }
ll16o2l 1:2d3139578aca 44
ll16o2l 2:888634fff8ff 45 void Objects::draw(N5110 &lcd)
ll16o2l 1:2d3139578aca 46 {
ll16o2l 2:888634fff8ff 47 lcd.drawCircle(_x,_y,_size,FILL_BLACK); // x,y,radius,black fill
ll16o2l 1:2d3139578aca 48 }
ll16o2l 1:2d3139578aca 49
ll16o2l 2:888634fff8ff 50 void Objects::update()
ll16o2l 1:2d3139578aca 51 {
ll16o2l 1:2d3139578aca 52 _x += _velocity.x;
ll16o2l 1:2d3139578aca 53 _y += _velocity.y;
ll16o2l 1:2d3139578aca 54 }
ll16o2l 1:2d3139578aca 55
ll16o2l 2:888634fff8ff 56 void Objects::set_velocity(Vector2D v)
ll16o2l 1:2d3139578aca 57 {
ll16o2l 1:2d3139578aca 58 _velocity.x = v.x;
ll16o2l 1:2d3139578aca 59 _velocity.y = v.y;
ll16o2l 1:2d3139578aca 60 }
ll16o2l 1:2d3139578aca 61
ll16o2l 2:888634fff8ff 62 Vector2D Objects::get_velocity()
ll16o2l 1:2d3139578aca 63 {
ll16o2l 1:2d3139578aca 64 Vector2D v = {_velocity.x,_velocity.y};
ll16o2l 1:2d3139578aca 65 return v;
ll16o2l 1:2d3139578aca 66 }
ll16o2l 1:2d3139578aca 67
ll16o2l 3:aa82968b7a8e 68 /**
ll16o2l 3:aa82968b7a8e 69 * This method will be used to set the postion of the objects.
ll16o2l 3:aa82968b7a8e 70 * @author Oliver Luong
ll16o2l 3:aa82968b7a8e 71 * @date 22/04/2019
ll16o2l 3:aa82968b7a8e 72 */
ll16o2l 3:aa82968b7a8e 73 void Objects::set_pos(Vector2D p)
ll16o2l 3:aa82968b7a8e 74 {
ll16o2l 3:aa82968b7a8e 75 _x = p.x;
ll16o2l 3:aa82968b7a8e 76 _y = p.y;
ll16o2l 3:aa82968b7a8e 77 }
ll16o2l 3:aa82968b7a8e 78
ll16o2l 3:aa82968b7a8e 79
ll16o2l 3:aa82968b7a8e 80 /**
ll16o2l 3:aa82968b7a8e 81 * This method will be used to return the position when called.
ll16o2l 3:aa82968b7a8e 82 * @author Oliver Luong
ll16o2l 3:aa82968b7a8e 83 * @date 22/04/2019
ll16o2l 3:aa82968b7a8e 84 */
ll16o2l 2:888634fff8ff 85 Vector2D Objects::get_pos()
ll16o2l 1:2d3139578aca 86 {
ll16o2l 1:2d3139578aca 87 Vector2D p = {_x,_y};
ll16o2l 1:2d3139578aca 88 return p;
ll16o2l 1:2d3139578aca 89 }