ELEC2645 (2018/19) / Mbed 2 deprecated ll16o2l_ELEC2645

Dependencies:   mbed Gamepad

Committer:
ll16o2l
Date:
Sat Apr 06 12:08:39 2019 +0000
Revision:
2:888634fff8ff
Parent:
Asteroids/Asteroid.cpp@1:2d3139578aca
Child:
3:aa82968b7a8e
Added stopwatch however still needs adjusting

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ll16o2l 1:2d3139578aca 1
ll16o2l 2:888634fff8ff 2 #include "Objects.h"
ll16o2l 1:2d3139578aca 3
ll16o2l 1:2d3139578aca 4 // nothing doing in the constructor and destructor
ll16o2l 2:888634fff8ff 5 Objects::Objects()
ll16o2l 1:2d3139578aca 6 {
ll16o2l 1:2d3139578aca 7
ll16o2l 1:2d3139578aca 8 }
ll16o2l 1:2d3139578aca 9
ll16o2l 2:888634fff8ff 10 Objects::~Objects()
ll16o2l 1:2d3139578aca 11 {
ll16o2l 1:2d3139578aca 12
ll16o2l 1:2d3139578aca 13 }
ll16o2l 1:2d3139578aca 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 2:888634fff8ff 68 Vector2D Objects::get_pos()
ll16o2l 1:2d3139578aca 69 {
ll16o2l 1:2d3139578aca 70 Vector2D p = {_x,_y};
ll16o2l 1:2d3139578aca 71 return p;
ll16o2l 1:2d3139578aca 72 }
ll16o2l 1:2d3139578aca 73
ll16o2l 2:888634fff8ff 74 void Objects::set_pos(Vector2D p)
ll16o2l 1:2d3139578aca 75 {
ll16o2l 1:2d3139578aca 76 _x = p.x;
ll16o2l 1:2d3139578aca 77 _y = p.y;
ll16o2l 1:2d3139578aca 78 }