![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
N5110
Ball/Ball.cpp@0:90b7f20b3a8d, 2020-04-28 (annotated)
- Committer:
- aileen
- Date:
- Tue Apr 28 01:39:30 2020 +0000
- Revision:
- 0:90b7f20b3a8d
N5110
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aileen | 0:90b7f20b3a8d | 1 | #include "Ball.h" |
aileen | 0:90b7f20b3a8d | 2 | |
aileen | 0:90b7f20b3a8d | 3 | Ball::Ball() |
aileen | 0:90b7f20b3a8d | 4 | { |
aileen | 0:90b7f20b3a8d | 5 | |
aileen | 0:90b7f20b3a8d | 6 | } |
aileen | 0:90b7f20b3a8d | 7 | |
aileen | 0:90b7f20b3a8d | 8 | Ball::~Ball() |
aileen | 0:90b7f20b3a8d | 9 | { |
aileen | 0:90b7f20b3a8d | 10 | |
aileen | 0:90b7f20b3a8d | 11 | } |
aileen | 0:90b7f20b3a8d | 12 | |
aileen | 0:90b7f20b3a8d | 13 | void Ball::init(int size,int speed) |
aileen | 0:90b7f20b3a8d | 14 | { |
aileen | 0:90b7f20b3a8d | 15 | _size = size; |
aileen | 0:90b7f20b3a8d | 16 | |
aileen | 0:90b7f20b3a8d | 17 | _x = WIDTH/2 - _size/2; |
aileen | 0:90b7f20b3a8d | 18 | _y = HEIGHT/2 - _size/2; |
aileen | 0:90b7f20b3a8d | 19 | |
aileen | 0:90b7f20b3a8d | 20 | srand(time(NULL)); |
aileen | 0:90b7f20b3a8d | 21 | int direction = rand() % 4; // randomise initial direction. |
aileen | 0:90b7f20b3a8d | 22 | |
aileen | 0:90b7f20b3a8d | 23 | // 4 possibilities. Get random modulo and set velocities accordingly |
aileen | 0:90b7f20b3a8d | 24 | if (direction == 0) { |
aileen | 0:90b7f20b3a8d | 25 | _velocity.x = speed; |
aileen | 0:90b7f20b3a8d | 26 | _velocity.y = speed; |
aileen | 0:90b7f20b3a8d | 27 | } else if (direction == 1) { |
aileen | 0:90b7f20b3a8d | 28 | _velocity.x = speed; |
aileen | 0:90b7f20b3a8d | 29 | _velocity.y = -speed; |
aileen | 0:90b7f20b3a8d | 30 | } else if (direction == 2) { |
aileen | 0:90b7f20b3a8d | 31 | _velocity.x = speed; |
aileen | 0:90b7f20b3a8d | 32 | _velocity.y = speed; |
aileen | 0:90b7f20b3a8d | 33 | } else { |
aileen | 0:90b7f20b3a8d | 34 | _velocity.x = -speed; |
aileen | 0:90b7f20b3a8d | 35 | _velocity.y = -speed; |
aileen | 0:90b7f20b3a8d | 36 | } |
aileen | 0:90b7f20b3a8d | 37 | } |
aileen | 0:90b7f20b3a8d | 38 | |
aileen | 0:90b7f20b3a8d | 39 | void Ball::draw(N5110 &lcd) |
aileen | 0:90b7f20b3a8d | 40 | { |
aileen | 0:90b7f20b3a8d | 41 | lcd.drawRect(_x,_y,_size,_size,FILL_BLACK); |
aileen | 0:90b7f20b3a8d | 42 | } |
aileen | 0:90b7f20b3a8d | 43 | |
aileen | 0:90b7f20b3a8d | 44 | void Ball::update() |
aileen | 0:90b7f20b3a8d | 45 | { |
aileen | 0:90b7f20b3a8d | 46 | _x += _velocity.x; |
aileen | 0:90b7f20b3a8d | 47 | _y += _velocity.y; |
aileen | 0:90b7f20b3a8d | 48 | } |
aileen | 0:90b7f20b3a8d | 49 | |
aileen | 0:90b7f20b3a8d | 50 | void Ball::set_velocity(Vector2D v) |
aileen | 0:90b7f20b3a8d | 51 | { |
aileen | 0:90b7f20b3a8d | 52 | _velocity.x = v.x; |
aileen | 0:90b7f20b3a8d | 53 | _velocity.y = v.y; |
aileen | 0:90b7f20b3a8d | 54 | } |
aileen | 0:90b7f20b3a8d | 55 | |
aileen | 0:90b7f20b3a8d | 56 | Vector2D Ball::get_velocity() |
aileen | 0:90b7f20b3a8d | 57 | { |
aileen | 0:90b7f20b3a8d | 58 | Vector2D v = {_velocity.x,_velocity.y}; |
aileen | 0:90b7f20b3a8d | 59 | return v; |
aileen | 0:90b7f20b3a8d | 60 | } |
aileen | 0:90b7f20b3a8d | 61 | |
aileen | 0:90b7f20b3a8d | 62 | Vector2D Ball::get_pos() |
aileen | 0:90b7f20b3a8d | 63 | { |
aileen | 0:90b7f20b3a8d | 64 | Vector2D p = {_x,_y}; |
aileen | 0:90b7f20b3a8d | 65 | return p; |
aileen | 0:90b7f20b3a8d | 66 | } |
aileen | 0:90b7f20b3a8d | 67 | |
aileen | 0:90b7f20b3a8d | 68 | void Ball::set_pos(Vector2D p) |
aileen | 0:90b7f20b3a8d | 69 | { |
aileen | 0:90b7f20b3a8d | 70 | _x = p.x; |
aileen | 0:90b7f20b3a8d | 71 | _y = p.y; |
aileen | 0:90b7f20b3a8d | 72 | } |