zhenye yang
/
el17zy
Yang Zhenye 201199680
Ball/Ball.cpp@2:baca89f466bc, 2020-04-30 (annotated)
- Committer:
- yangzhenye
- Date:
- Thu Apr 30 07:08:09 2020 +0000
- Revision:
- 2:baca89f466bc
- Parent:
- 1:74f525027af6
- Child:
- 5:fcad75e9b9e1
complete menu and improve roony
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 | 2:baca89f466bc | 9 | { for(i = 20; i >0; i--) { |
yangzhenye | 2:baca89f466bc | 10 | _x = 40 - _speed |
yangzhenye | 2:baca89f466bc | 11 | _y = 40 - 0.5*_speed |
yangzhenye | 2:baca89f466bc | 12 | if (_x < 0); |
yangzhenye | 2:baca89f466bc | 13 | _x =0; |
yangzhenye | 2:baca89f466bc | 14 | for (i = 20; i >0; i--) { |
yangzhenye | 2:baca89f466bc | 15 | _x = 0 + _speed |
yangzhenye | 2:baca89f466bc | 16 | |
yangzhenye | 1:74f525027af6 | 17 | |
yangzhenye | 1:74f525027af6 | 18 | } |
yangzhenye | 1:74f525027af6 | 19 | |
yangzhenye | 2:baca89f466bc | 20 | void shoot() { |
yangzhenye | 2:baca89f466bc | 21 | |
yangzhenye | 1:74f525027af6 | 22 | void Ball::init(int size,int speed) |
yangzhenye | 1:74f525027af6 | 23 | { |
yangzhenye | 1:74f525027af6 | 24 | _size = size; |
yangzhenye | 1:74f525027af6 | 25 | |
yangzhenye | 1:74f525027af6 | 26 | _x = WIDTH/2 - _size/2; |
yangzhenye | 1:74f525027af6 | 27 | _y = HEIGHT/2 - _size/2; |
yangzhenye | 1:74f525027af6 | 28 | |
yangzhenye | 1:74f525027af6 | 29 | srand(time(NULL)); |
yangzhenye | 1:74f525027af6 | 30 | int direction = rand() % 4; // randomise initial direction. |
yangzhenye | 1:74f525027af6 | 31 | |
yangzhenye | 1:74f525027af6 | 32 | // 4 possibilities. Get random modulo and set velocities accordingly |
yangzhenye | 1:74f525027af6 | 33 | if (direction == 0) { |
yangzhenye | 1:74f525027af6 | 34 | _velocity.x = speed; |
yangzhenye | 1:74f525027af6 | 35 | _velocity.y = speed; |
yangzhenye | 1:74f525027af6 | 36 | } else if (direction == 1) { |
yangzhenye | 1:74f525027af6 | 37 | _velocity.x = speed; |
yangzhenye | 1:74f525027af6 | 38 | _velocity.y = -speed; |
yangzhenye | 1:74f525027af6 | 39 | } else if (direction == 2) { |
yangzhenye | 1:74f525027af6 | 40 | _velocity.x = speed; |
yangzhenye | 1:74f525027af6 | 41 | _velocity.y = speed; |
yangzhenye | 1:74f525027af6 | 42 | } else { |
yangzhenye | 1:74f525027af6 | 43 | _velocity.x = -speed; |
yangzhenye | 1:74f525027af6 | 44 | _velocity.y = -speed; |
yangzhenye | 1:74f525027af6 | 45 | } |
yangzhenye | 1:74f525027af6 | 46 | } |
yangzhenye | 1:74f525027af6 | 47 | |
yangzhenye | 1:74f525027af6 | 48 | void Ball::draw(N5110 &lcd) |
yangzhenye | 1:74f525027af6 | 49 | { |
yangzhenye | 1:74f525027af6 | 50 | lcd.drawRect(_x,_y,_size,_size,FILL_BLACK); |
yangzhenye | 1:74f525027af6 | 51 | } |
yangzhenye | 1:74f525027af6 | 52 | |
yangzhenye | 1:74f525027af6 | 53 | void Ball::update() |
yangzhenye | 1:74f525027af6 | 54 | { |
yangzhenye | 1:74f525027af6 | 55 | _x += _velocity.x; |
yangzhenye | 1:74f525027af6 | 56 | _y += _velocity.y; |
yangzhenye | 1:74f525027af6 | 57 | } |
yangzhenye | 1:74f525027af6 | 58 | |
yangzhenye | 1:74f525027af6 | 59 | void Ball::set_velocity(Vector2D v) |
yangzhenye | 1:74f525027af6 | 60 | { |
yangzhenye | 1:74f525027af6 | 61 | _velocity.x = v.x; |
yangzhenye | 1:74f525027af6 | 62 | _velocity.y = v.y; |
yangzhenye | 1:74f525027af6 | 63 | } |
yangzhenye | 1:74f525027af6 | 64 | |
yangzhenye | 1:74f525027af6 | 65 | Vector2D Ball::get_velocity() |
yangzhenye | 1:74f525027af6 | 66 | { |
yangzhenye | 1:74f525027af6 | 67 | Vector2D v = {_velocity.x,_velocity.y}; |
yangzhenye | 1:74f525027af6 | 68 | return v; |
yangzhenye | 1:74f525027af6 | 69 | } |
yangzhenye | 1:74f525027af6 | 70 | |
yangzhenye | 1:74f525027af6 | 71 | Vector2D Ball::get_pos() |
yangzhenye | 1:74f525027af6 | 72 | { |
yangzhenye | 1:74f525027af6 | 73 | Vector2D p = {_x,_y}; |
yangzhenye | 1:74f525027af6 | 74 | return p; |
yangzhenye | 1:74f525027af6 | 75 | } |
yangzhenye | 1:74f525027af6 | 76 | |
yangzhenye | 1:74f525027af6 | 77 | void Ball::set_pos(Vector2D p) |
yangzhenye | 1:74f525027af6 | 78 | { |
yangzhenye | 1:74f525027af6 | 79 | _x = p.x; |
yangzhenye | 1:74f525027af6 | 80 | _y = p.y; |
yangzhenye | 1:74f525027af6 | 81 | } |