ELEC2645 (2018/19) / Mbed 2 deprecated ll16o2l_ELEC2645

Dependencies:   mbed Gamepad

Revision:
1:2d3139578aca
diff -r 03363a508918 -r 2d3139578aca Asteroids/Asteroid.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Asteroids/Asteroid.cpp	Mon Mar 25 11:54:51 2019 +0000
@@ -0,0 +1,78 @@
+#ifndef BITMAP_H
+#define BITMAP_H
+ 
+#include <vector>
+
+#include "Asteroid.h"
+
+// nothing doing in the constructor and destructor
+Asteroid::Asteroid()
+{
+
+}
+
+Asteroid::~Asteroid()
+{
+
+}
+
+void Ball::init(int size,int speed)
+{
+    _size = size;
+
+    _x = WIDTH/2 -  _size/2;
+    _y = HEIGHT/2 - _size/2;
+
+    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 Ball::draw(N5110 &lcd)
+{
+    lcd.drawRect(_x,_y,_size,_size,FILL_BLACK);
+}
+
+void Ball::update()
+{
+    _x += _velocity.x;
+    _y += _velocity.y;
+}
+
+void Ball::set_velocity(Vector2D v)
+{
+    _velocity.x = v.x;
+    _velocity.y = v.y;
+}
+
+Vector2D Ball::get_velocity()
+{
+    Vector2D v = {_velocity.x,_velocity.y};
+    return v;
+}
+
+Vector2D Ball::get_pos()
+{
+    Vector2D p = {_x,_y};
+    return p;
+}
+
+void Ball::set_pos(Vector2D p)
+{
+    _x = p.x;
+    _y = p.y;
+}
\ No newline at end of file