ELEC2645 (2019/20)
/
ELEC2645_Project_el18vgt
Game designed for project
Diff: MyClasses/Ball/Ball.cpp
- Revision:
- 3:5ede4ac61af1
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MyClasses/Ball/Ball.cpp Sun May 24 09:15:25 2020 +0000 @@ -0,0 +1,61 @@ +#include "Ball.h" + +void Ball::init(int radius, int height, int speed) +{ + _radius = radius; + + _x = WIDTH/2 - _radius; + _y = height; + + srand(time(NULL)); + int direction = rand() % 2; // randomise initial direction. + + // 4 possibilities. Get random modulo and set velocities accordingly + if (direction == 0) { + _velocity.x = speed; + _velocity.y = speed; + } else (direction == 1) { + _velocity.x = -speed; + _velocity.y = speed; + } +} + +void Ball::print_lcd(N5110 &lcd) +{ + lcd.drawCircle(_x, _y, _radius, FILL_BLACK); +} + +void Ball::set_size(int radius) +{ + _radius = radius; +} + +void Ball::set_position(int x, int y) +{ + _x = x; + _y = y; +} + +void Ball::set_velocity(Vector2D velocity) +{ + _velocity.x = velocity.x; + _velocity.y = velocity.y; +} + +void Ball::update_position() +{ + _x += _velocity.x; + _y += _velocity.y; +} + +void Ball::get_position() +{ + Vector2D position = {_x, _y}; + return position; +} + +void Ball:get_velocity() +{ + Vector2D velocity = {_velocity.x, _velocity.y}; + return velocity +} \ No newline at end of file