James Heavey
/
EL17JH
FINAL VERSION
Ball/Ball.cpp@8:1ab6d90c4d60, 2019-04-21 (annotated)
- Committer:
- jamesheavey
- Date:
- Sun Apr 21 19:40:05 2019 +0000
- Revision:
- 8:1ab6d90c4d60
- Parent:
- 0:7d4d2023ed9c
- Child:
- 40:ac53905346fb
edited the check paddle collision to account for direction and angle of reflection based on where the paddle is hit. Buggy, needs refinement
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jamesheavey | 0:7d4d2023ed9c | 1 | #include "Ball.h" |
jamesheavey | 0:7d4d2023ed9c | 2 | |
jamesheavey | 0:7d4d2023ed9c | 3 | Ball::Ball() |
jamesheavey | 0:7d4d2023ed9c | 4 | { |
jamesheavey | 0:7d4d2023ed9c | 5 | |
jamesheavey | 0:7d4d2023ed9c | 6 | } |
jamesheavey | 0:7d4d2023ed9c | 7 | |
jamesheavey | 0:7d4d2023ed9c | 8 | Ball::~Ball() |
jamesheavey | 0:7d4d2023ed9c | 9 | { |
jamesheavey | 0:7d4d2023ed9c | 10 | |
jamesheavey | 0:7d4d2023ed9c | 11 | } |
jamesheavey | 0:7d4d2023ed9c | 12 | |
jamesheavey | 0:7d4d2023ed9c | 13 | void Ball::init(int size,int speed) |
jamesheavey | 0:7d4d2023ed9c | 14 | { |
jamesheavey | 0:7d4d2023ed9c | 15 | _size = size; |
jamesheavey | 0:7d4d2023ed9c | 16 | |
jamesheavey | 0:7d4d2023ed9c | 17 | _x = WIDTH/2 - _size/2; |
jamesheavey | 0:7d4d2023ed9c | 18 | _y = HEIGHT/2 - _size/2; |
jamesheavey | 0:7d4d2023ed9c | 19 | |
jamesheavey | 0:7d4d2023ed9c | 20 | srand(time(NULL)); |
jamesheavey | 0:7d4d2023ed9c | 21 | int direction = rand() % 4; // randomise initial direction. |
jamesheavey | 0:7d4d2023ed9c | 22 | |
jamesheavey | 0:7d4d2023ed9c | 23 | // 4 possibilities. Get random modulo and set velocities accordingly |
jamesheavey | 0:7d4d2023ed9c | 24 | if (direction == 0) { |
jamesheavey | 0:7d4d2023ed9c | 25 | _velocity.x = speed; |
jamesheavey | 0:7d4d2023ed9c | 26 | _velocity.y = speed; |
jamesheavey | 0:7d4d2023ed9c | 27 | } else if (direction == 1) { |
jamesheavey | 8:1ab6d90c4d60 | 28 | _velocity.x = -speed; |
jamesheavey | 8:1ab6d90c4d60 | 29 | _velocity.y = speed; |
jamesheavey | 0:7d4d2023ed9c | 30 | } else if (direction == 2) { |
jamesheavey | 0:7d4d2023ed9c | 31 | _velocity.x = speed; |
jamesheavey | 0:7d4d2023ed9c | 32 | _velocity.y = speed; |
jamesheavey | 0:7d4d2023ed9c | 33 | } else { |
jamesheavey | 0:7d4d2023ed9c | 34 | _velocity.x = -speed; |
jamesheavey | 8:1ab6d90c4d60 | 35 | _velocity.y = speed; |
jamesheavey | 0:7d4d2023ed9c | 36 | } |
jamesheavey | 0:7d4d2023ed9c | 37 | } |
jamesheavey | 0:7d4d2023ed9c | 38 | |
jamesheavey | 0:7d4d2023ed9c | 39 | void Ball::draw(N5110 &lcd) |
jamesheavey | 0:7d4d2023ed9c | 40 | { |
jamesheavey | 0:7d4d2023ed9c | 41 | lcd.drawRect(_x,_y,_size,_size,FILL_BLACK); |
jamesheavey | 0:7d4d2023ed9c | 42 | } |
jamesheavey | 0:7d4d2023ed9c | 43 | |
jamesheavey | 0:7d4d2023ed9c | 44 | void Ball::update() |
jamesheavey | 0:7d4d2023ed9c | 45 | { |
jamesheavey | 0:7d4d2023ed9c | 46 | _x += _velocity.x; |
jamesheavey | 0:7d4d2023ed9c | 47 | _y += _velocity.y; |
jamesheavey | 0:7d4d2023ed9c | 48 | } |
jamesheavey | 0:7d4d2023ed9c | 49 | |
jamesheavey | 0:7d4d2023ed9c | 50 | void Ball::set_velocity(Vector2D v) |
jamesheavey | 0:7d4d2023ed9c | 51 | { |
jamesheavey | 0:7d4d2023ed9c | 52 | _velocity.x = v.x; |
jamesheavey | 0:7d4d2023ed9c | 53 | _velocity.y = v.y; |
jamesheavey | 0:7d4d2023ed9c | 54 | } |
jamesheavey | 0:7d4d2023ed9c | 55 | |
jamesheavey | 0:7d4d2023ed9c | 56 | Vector2D Ball::get_velocity() |
jamesheavey | 0:7d4d2023ed9c | 57 | { |
jamesheavey | 0:7d4d2023ed9c | 58 | Vector2D v = {_velocity.x,_velocity.y}; |
jamesheavey | 0:7d4d2023ed9c | 59 | return v; |
jamesheavey | 0:7d4d2023ed9c | 60 | } |
jamesheavey | 0:7d4d2023ed9c | 61 | |
jamesheavey | 0:7d4d2023ed9c | 62 | Vector2D Ball::get_pos() |
jamesheavey | 0:7d4d2023ed9c | 63 | { |
jamesheavey | 0:7d4d2023ed9c | 64 | Vector2D p = {_x,_y}; |
jamesheavey | 0:7d4d2023ed9c | 65 | return p; |
jamesheavey | 0:7d4d2023ed9c | 66 | } |
jamesheavey | 0:7d4d2023ed9c | 67 | |
jamesheavey | 0:7d4d2023ed9c | 68 | void Ball::set_pos(Vector2D p) |
jamesheavey | 0:7d4d2023ed9c | 69 | { |
jamesheavey | 0:7d4d2023ed9c | 70 | _x = p.x; |
jamesheavey | 0:7d4d2023ed9c | 71 | _y = p.y; |
jamesheavey | 0:7d4d2023ed9c | 72 | } |