Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Ball/Ball.cpp@0:92b180c8d407, 2021-01-05 (annotated)
- Committer:
- jamesheavey
- Date:
- Tue Jan 05 01:14:11 2021 +0000
- Revision:
- 0:92b180c8d407
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jamesheavey | 0:92b180c8d407 | 1 | #include "Ball.h" |
jamesheavey | 0:92b180c8d407 | 2 | |
jamesheavey | 0:92b180c8d407 | 3 | Ball::Ball() |
jamesheavey | 0:92b180c8d407 | 4 | { |
jamesheavey | 0:92b180c8d407 | 5 | |
jamesheavey | 0:92b180c8d407 | 6 | } |
jamesheavey | 0:92b180c8d407 | 7 | |
jamesheavey | 0:92b180c8d407 | 8 | Ball::~Ball() |
jamesheavey | 0:92b180c8d407 | 9 | { |
jamesheavey | 0:92b180c8d407 | 10 | |
jamesheavey | 0:92b180c8d407 | 11 | } |
jamesheavey | 0:92b180c8d407 | 12 | |
jamesheavey | 0:92b180c8d407 | 13 | void Ball::init(int size,int speed, int x) |
jamesheavey | 0:92b180c8d407 | 14 | { |
jamesheavey | 0:92b180c8d407 | 15 | _size = size; // size of the ball |
jamesheavey | 0:92b180c8d407 | 16 | |
jamesheavey | 0:92b180c8d407 | 17 | _x = x; // init position of the ball will be at the centre of the paddle |
jamesheavey | 0:92b180c8d407 | 18 | _y = HEIGHT - _size/2 - 3; // fixed y init position, just above the paddle |
jamesheavey | 0:92b180c8d407 | 19 | |
jamesheavey | 0:92b180c8d407 | 20 | srand(time(NULL)); |
jamesheavey | 0:92b180c8d407 | 21 | int direction = rand() % 2; // randomise initial direction. |
jamesheavey | 0:92b180c8d407 | 22 | |
jamesheavey | 0:92b180c8d407 | 23 | // 2 directions, both directed upwards (N) from the paddle |
jamesheavey | 0:92b180c8d407 | 24 | if (direction == 0) { |
jamesheavey | 0:92b180c8d407 | 25 | _velocity.x = speed; // direction = E |
jamesheavey | 0:92b180c8d407 | 26 | _velocity.y = -speed; // direction = N |
jamesheavey | 0:92b180c8d407 | 27 | } else { |
jamesheavey | 0:92b180c8d407 | 28 | _velocity.x = -speed; // direction = W |
jamesheavey | 0:92b180c8d407 | 29 | _velocity.y = -speed; // direction = N |
jamesheavey | 0:92b180c8d407 | 30 | } |
jamesheavey | 0:92b180c8d407 | 31 | } |
jamesheavey | 0:92b180c8d407 | 32 | |
jamesheavey | 0:92b180c8d407 | 33 | |
jamesheavey | 0:92b180c8d407 | 34 | void Ball::draw(N5110 &lcd) // draw the ball at the specified location |
jamesheavey | 0:92b180c8d407 | 35 | { |
jamesheavey | 0:92b180c8d407 | 36 | lcd.drawRect(_x,_y,_size,_size,FILL_BLACK); |
jamesheavey | 0:92b180c8d407 | 37 | } |
jamesheavey | 0:92b180c8d407 | 38 | |
jamesheavey | 0:92b180c8d407 | 39 | |
jamesheavey | 0:92b180c8d407 | 40 | void Ball::update() // update the ball's position, based on its current velocity |
jamesheavey | 0:92b180c8d407 | 41 | { |
jamesheavey | 0:92b180c8d407 | 42 | _x += _velocity.x; |
jamesheavey | 0:92b180c8d407 | 43 | _y += _velocity.y; |
jamesheavey | 0:92b180c8d407 | 44 | } |
jamesheavey | 0:92b180c8d407 | 45 | |
jamesheavey | 0:92b180c8d407 | 46 | |
jamesheavey | 0:92b180c8d407 | 47 | void Ball::set_velocity(Vector2D v) // set the velocity in its x/y directions (used for collision velocity correction) |
jamesheavey | 0:92b180c8d407 | 48 | { |
jamesheavey | 0:92b180c8d407 | 49 | _velocity.x = v.x; |
jamesheavey | 0:92b180c8d407 | 50 | _velocity.y = v.y; |
jamesheavey | 0:92b180c8d407 | 51 | } |
jamesheavey | 0:92b180c8d407 | 52 | |
jamesheavey | 0:92b180c8d407 | 53 | |
jamesheavey | 0:92b180c8d407 | 54 | Vector2D Ball::get_velocity() // retrieve the ball's current velocities |
jamesheavey | 0:92b180c8d407 | 55 | { |
jamesheavey | 0:92b180c8d407 | 56 | Vector2D v = {_velocity.x,_velocity.y}; |
jamesheavey | 0:92b180c8d407 | 57 | return v; |
jamesheavey | 0:92b180c8d407 | 58 | } |
jamesheavey | 0:92b180c8d407 | 59 | |
jamesheavey | 0:92b180c8d407 | 60 | |
jamesheavey | 0:92b180c8d407 | 61 | Vector2D Ball::get_pos() // retrieve the ball's current coordinates |
jamesheavey | 0:92b180c8d407 | 62 | { |
jamesheavey | 0:92b180c8d407 | 63 | Vector2D p = {_x,_y}; |
jamesheavey | 0:92b180c8d407 | 64 | return p; |
jamesheavey | 0:92b180c8d407 | 65 | } |
jamesheavey | 0:92b180c8d407 | 66 | |
jamesheavey | 0:92b180c8d407 | 67 | |
jamesheavey | 0:92b180c8d407 | 68 | void Ball::set_pos(Vector2D p) // set the ball's coordinates (used for collision velocity correction) |
jamesheavey | 0:92b180c8d407 | 69 | { |
jamesheavey | 0:92b180c8d407 | 70 | _x = p.x; |
jamesheavey | 0:92b180c8d407 | 71 | _y = p.y; |
jamesheavey | 0:92b180c8d407 | 72 | } |