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.
Dependencies: mbed
Ball/Ball.cpp@0:19e5a063d7ea, 2019-05-09 (annotated)
- Committer:
- fusihui
- Date:
- Thu May 09 04:28:26 2019 +0000
- Revision:
- 0:19e5a063d7ea
pin game
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fusihui | 0:19e5a063d7ea | 1 | #include "Ball.h" |
fusihui | 0:19e5a063d7ea | 2 | |
fusihui | 0:19e5a063d7ea | 3 | Ball::Ball() |
fusihui | 0:19e5a063d7ea | 4 | { |
fusihui | 0:19e5a063d7ea | 5 | |
fusihui | 0:19e5a063d7ea | 6 | } |
fusihui | 0:19e5a063d7ea | 7 | |
fusihui | 0:19e5a063d7ea | 8 | Ball::~Ball() |
fusihui | 0:19e5a063d7ea | 9 | { |
fusihui | 0:19e5a063d7ea | 10 | |
fusihui | 0:19e5a063d7ea | 11 | } |
fusihui | 0:19e5a063d7ea | 12 | |
fusihui | 0:19e5a063d7ea | 13 | void Ball::init(int size, int speed) |
fusihui | 0:19e5a063d7ea | 14 | { |
fusihui | 0:19e5a063d7ea | 15 | _size = size; |
fusihui | 0:19e5a063d7ea | 16 | |
fusihui | 0:19e5a063d7ea | 17 | _x = WIDTH/2 - _size/2; |
fusihui | 0:19e5a063d7ea | 18 | _y = HEIGHT/2 - _size/2; |
fusihui | 0:19e5a063d7ea | 19 | |
fusihui | 0:19e5a063d7ea | 20 | _radius = _size/2; |
fusihui | 0:19e5a063d7ea | 21 | |
fusihui | 0:19e5a063d7ea | 22 | // let the ball be fired randomly in one direction |
fusihui | 0:19e5a063d7ea | 23 | srand(time(NULL)); |
fusihui | 0:19e5a063d7ea | 24 | int direction = rand() % 4; |
fusihui | 0:19e5a063d7ea | 25 | // 4 possiblities |
fusihui | 0:19e5a063d7ea | 26 | if (direction == 0) { |
fusihui | 0:19e5a063d7ea | 27 | _speed.x = speed; |
fusihui | 0:19e5a063d7ea | 28 | _speed.y = speed; |
fusihui | 0:19e5a063d7ea | 29 | } else if (direction == 1) { |
fusihui | 0:19e5a063d7ea | 30 | _speed.x = speed; |
fusihui | 0:19e5a063d7ea | 31 | _speed.y = -speed; |
fusihui | 0:19e5a063d7ea | 32 | } else if (direction == 2) { |
fusihui | 0:19e5a063d7ea | 33 | _speed.x = speed; |
fusihui | 0:19e5a063d7ea | 34 | _speed.y = speed; |
fusihui | 0:19e5a063d7ea | 35 | } else { |
fusihui | 0:19e5a063d7ea | 36 | _speed.x = -speed; |
fusihui | 0:19e5a063d7ea | 37 | _speed.y = -speed; |
fusihui | 0:19e5a063d7ea | 38 | } |
fusihui | 0:19e5a063d7ea | 39 | |
fusihui | 0:19e5a063d7ea | 40 | |
fusihui | 0:19e5a063d7ea | 41 | } |
fusihui | 0:19e5a063d7ea | 42 | |
fusihui | 0:19e5a063d7ea | 43 | // draw pinball |
fusihui | 0:19e5a063d7ea | 44 | void Ball::draw(N5110 &lcd) |
fusihui | 0:19e5a063d7ea | 45 | { |
fusihui | 0:19e5a063d7ea | 46 | lcd.drawCircle(_x,_y,_radius,FILL_TRANSPARENT); |
fusihui | 0:19e5a063d7ea | 47 | } |
fusihui | 0:19e5a063d7ea | 48 | |
fusihui | 0:19e5a063d7ea | 49 | void Ball::update() |
fusihui | 0:19e5a063d7ea | 50 | { |
fusihui | 0:19e5a063d7ea | 51 | _x += _speed.x; |
fusihui | 0:19e5a063d7ea | 52 | _y += _speed.y; |
fusihui | 0:19e5a063d7ea | 53 | } |
fusihui | 0:19e5a063d7ea | 54 | |
fusihui | 0:19e5a063d7ea | 55 | void Ball::set_speed(Vector2D speed) |
fusihui | 0:19e5a063d7ea | 56 | { |
fusihui | 0:19e5a063d7ea | 57 | _speed.x = speed.x; |
fusihui | 0:19e5a063d7ea | 58 | _speed.y = speed.y; |
fusihui | 0:19e5a063d7ea | 59 | } |
fusihui | 0:19e5a063d7ea | 60 | |
fusihui | 0:19e5a063d7ea | 61 | Vector2D Ball::get_speed() |
fusihui | 0:19e5a063d7ea | 62 | { |
fusihui | 0:19e5a063d7ea | 63 | Vector2D speed = {_speed.x,_speed.y}; |
fusihui | 0:19e5a063d7ea | 64 | return speed; |
fusihui | 0:19e5a063d7ea | 65 | } |
fusihui | 0:19e5a063d7ea | 66 | |
fusihui | 0:19e5a063d7ea | 67 | void Ball::set_position(Vector2D position) |
fusihui | 0:19e5a063d7ea | 68 | { |
fusihui | 0:19e5a063d7ea | 69 | _x = position.x; |
fusihui | 0:19e5a063d7ea | 70 | _y = position.y; |
fusihui | 0:19e5a063d7ea | 71 | } |
fusihui | 0:19e5a063d7ea | 72 | |
fusihui | 0:19e5a063d7ea | 73 | Vector2D Ball::get_position() |
fusihui | 0:19e5a063d7ea | 74 | { |
fusihui | 0:19e5a063d7ea | 75 | Vector2D position = {_x,_y}; |
fusihui | 0:19e5a063d7ea | 76 | return position; |
fusihui | 0:19e5a063d7ea | 77 | } |
fusihui | 0:19e5a063d7ea | 78 | |
fusihui | 0:19e5a063d7ea | 79 | |
fusihui | 0:19e5a063d7ea | 80 | |
fusihui | 0:19e5a063d7ea | 81 |