Yang Hongxiao 201199678
Dependencies: mbed
Bullet.cpp@1:a6ead8050c23, 2020-05-14 (annotated)
- Committer:
- YHX
- Date:
- Thu May 14 17:00:34 2020 +0000
- Revision:
- 1:a6ead8050c23
Yang Hongxiao; el17hy; 201199678
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
YHX | 1:a6ead8050c23 | 1 | #include "Bullet.h" |
YHX | 1:a6ead8050c23 | 2 | |
YHX | 1:a6ead8050c23 | 3 | Bullet::Bullet() |
YHX | 1:a6ead8050c23 | 4 | { |
YHX | 1:a6ead8050c23 | 5 | |
YHX | 1:a6ead8050c23 | 6 | } |
YHX | 1:a6ead8050c23 | 7 | |
YHX | 1:a6ead8050c23 | 8 | Bullet::~Bullet() |
YHX | 1:a6ead8050c23 | 9 | { |
YHX | 1:a6ead8050c23 | 10 | |
YHX | 1:a6ead8050c23 | 11 | } |
YHX | 1:a6ead8050c23 | 12 | |
YHX | 1:a6ead8050c23 | 13 | void Bullet::init(int r,int vv) |
YHX | 1:a6ead8050c23 | 14 | { |
YHX | 1:a6ead8050c23 | 15 | _r = r; |
YHX | 1:a6ead8050c23 | 16 | srand(time(NULL)); |
YHX | 1:a6ead8050c23 | 17 | _x = rand()%(84-_r); //the inintial ball fall from the top |
YHX | 1:a6ead8050c23 | 18 | _y = 0; |
YHX | 1:a6ead8050c23 | 19 | |
YHX | 1:a6ead8050c23 | 20 | srand(time(NULL)); |
YHX | 1:a6ead8050c23 | 21 | int direction = rand()%4; //the direction of the ball has four possibility |
YHX | 1:a6ead8050c23 | 22 | |
YHX | 1:a6ead8050c23 | 23 | // 4 possibilities. Get random modulo and set velocities accordingly |
YHX | 1:a6ead8050c23 | 24 | if (direction == 0) { |
YHX | 1:a6ead8050c23 | 25 | _velocity.x = vv; |
YHX | 1:a6ead8050c23 | 26 | _velocity.y = vv; |
YHX | 1:a6ead8050c23 | 27 | } else if (direction == 1) { |
YHX | 1:a6ead8050c23 | 28 | _velocity.x = vv; |
YHX | 1:a6ead8050c23 | 29 | _velocity.y = -vv; |
YHX | 1:a6ead8050c23 | 30 | } else if (direction == 2) { |
YHX | 1:a6ead8050c23 | 31 | _velocity.x = vv; |
YHX | 1:a6ead8050c23 | 32 | _velocity.y = vv; |
YHX | 1:a6ead8050c23 | 33 | } else { |
YHX | 1:a6ead8050c23 | 34 | _velocity.x = -vv; |
YHX | 1:a6ead8050c23 | 35 | _velocity.y = -vv; |
YHX | 1:a6ead8050c23 | 36 | } |
YHX | 1:a6ead8050c23 | 37 | } |
YHX | 1:a6ead8050c23 | 38 | |
YHX | 1:a6ead8050c23 | 39 | void Bullet::draw(N5110 &lcd) |
YHX | 1:a6ead8050c23 | 40 | { |
YHX | 1:a6ead8050c23 | 41 | lcd.drawRect(_x,_y,_r,_r,FILL_BLACK); //draw the ball |
YHX | 1:a6ead8050c23 | 42 | } |
YHX | 1:a6ead8050c23 | 43 | |
YHX | 1:a6ead8050c23 | 44 | void Bullet::update() |
YHX | 1:a6ead8050c23 | 45 | { |
YHX | 1:a6ead8050c23 | 46 | _x += _velocity.x; |
YHX | 1:a6ead8050c23 | 47 | _y += _velocity.y; |
YHX | 1:a6ead8050c23 | 48 | } |
YHX | 1:a6ead8050c23 | 49 | |
YHX | 1:a6ead8050c23 | 50 | void Bullet::set_velocity(Vector2D v) |
YHX | 1:a6ead8050c23 | 51 | { |
YHX | 1:a6ead8050c23 | 52 | _velocity.x = v.x; |
YHX | 1:a6ead8050c23 | 53 | _velocity.y = v.y; |
YHX | 1:a6ead8050c23 | 54 | } |
YHX | 1:a6ead8050c23 | 55 | |
YHX | 1:a6ead8050c23 | 56 | Vector2D Bullet::get_velocity() |
YHX | 1:a6ead8050c23 | 57 | { |
YHX | 1:a6ead8050c23 | 58 | Vector2D v = {_velocity.x,_velocity.y}; |
YHX | 1:a6ead8050c23 | 59 | return v; |
YHX | 1:a6ead8050c23 | 60 | } |
YHX | 1:a6ead8050c23 | 61 | |
YHX | 1:a6ead8050c23 | 62 | Vector2D Bullet::get_pos() |
YHX | 1:a6ead8050c23 | 63 | { |
YHX | 1:a6ead8050c23 | 64 | Vector2D p = {_x,_y}; |
YHX | 1:a6ead8050c23 | 65 | return p; |
YHX | 1:a6ead8050c23 | 66 | } |
YHX | 1:a6ead8050c23 | 67 | |
YHX | 1:a6ead8050c23 | 68 | void Bullet::set_pos(Vector2D p) |
YHX | 1:a6ead8050c23 | 69 | { |
YHX | 1:a6ead8050c23 | 70 | _x = p.x; |
YHX | 1:a6ead8050c23 | 71 | _y = p.y; |
YHX | 1:a6ead8050c23 | 72 | } |