Yang Hongxiao 201199678
Dependencies: mbed
Diff: Bullet.cpp
- Revision:
- 1:a6ead8050c23
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Bullet.cpp Thu May 14 17:00:34 2020 +0000 @@ -0,0 +1,72 @@ +#include "Bullet.h" + +Bullet::Bullet() +{ + +} + +Bullet::~Bullet() +{ + +} + +void Bullet::init(int r,int vv) +{ + _r = r; + srand(time(NULL)); + _x = rand()%(84-_r); //the inintial ball fall from the top + _y = 0; + + srand(time(NULL)); + int direction = rand()%4; //the direction of the ball has four possibility + + // 4 possibilities. Get random modulo and set velocities accordingly + if (direction == 0) { + _velocity.x = vv; + _velocity.y = vv; + } else if (direction == 1) { + _velocity.x = vv; + _velocity.y = -vv; + } else if (direction == 2) { + _velocity.x = vv; + _velocity.y = vv; + } else { + _velocity.x = -vv; + _velocity.y = -vv; + } +} + +void Bullet::draw(N5110 &lcd) +{ + lcd.drawRect(_x,_y,_r,_r,FILL_BLACK); //draw the ball +} + +void Bullet::update() +{ + _x += _velocity.x; + _y += _velocity.y; +} + +void Bullet::set_velocity(Vector2D v) +{ + _velocity.x = v.x; + _velocity.y = v.y; +} + +Vector2D Bullet::get_velocity() +{ + Vector2D v = {_velocity.x,_velocity.y}; + return v; +} + +Vector2D Bullet::get_pos() +{ + Vector2D p = {_x,_y}; + return p; +} + +void Bullet::set_pos(Vector2D p) +{ + _x = p.x; + _y = p.y; +} \ No newline at end of file