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