Zhang Xin yu
/
ZhangXinyu201090208
zhangxinyu01text
Diff: Ball/Ball.cpp
- Revision:
- 12:3952ba0683c7
- Parent:
- 5:3c9407e2fe55
--- a/Ball/Ball.cpp Tue Apr 17 08:00:08 2018 +0000 +++ b/Ball/Ball.cpp Mon May 06 06:09:02 2019 +0000 @@ -10,35 +10,37 @@ } -void Ball::init(int size,int speed) +void Ball::init(int size,int speed, int direction) { _size = size; - _x = WIDTH/2 - _size/2; - _y = HEIGHT/2 - _size/2; + _x = _size/2; + _y = _size/2; srand(time(NULL)); - int direction = rand() % 4; // randomise initial direction. - + direction = rand() % 4; // randomise initial direction. + _direction = direction; // 4 possibilities. Get random modulo and set velocities accordingly if (direction == 0) { _velocity.x = speed; _velocity.y = speed; } else if (direction == 1) { + _velocity.x = -speed; + _velocity.y = speed; + } else if (direction == 2) { _velocity.x = speed; _velocity.y = -speed; - } else if (direction == 2) { - _velocity.x = speed; - _velocity.y = speed; } else { _velocity.x = -speed; _velocity.y = -speed; } + + } void Ball::draw(N5110 &lcd) { - lcd.drawRect(_x,_y,_size,_size,FILL_BLACK); + lcd.drawCircle(_x,_y,_size,FILL_BLACK); } void Ball::update() @@ -59,13 +61,13 @@ return v; } -Vector2D Ball::get_pos() +Vector2D Ball::get_pos() // get ball pos form lcd { Vector2D p = {_x,_y}; return p; } -void Ball::set_pos(Vector2D p) +void Ball::set_pos(Vector2D p) // change lcd into cooredinat { _x = p.x; _y = p.y;