Zhang Xin yu
/
ZhangXinyu201090208
zhangxinyu01text
Ball/Ball.cpp
- Committer:
- Jenny121
- Date:
- 2019-05-06
- Revision:
- 12:3952ba0683c7
- Parent:
- 5:3c9407e2fe55
File content as of revision 12:3952ba0683c7:
#include "Ball.h" Ball::Ball() { } Ball::~Ball() { } void Ball::init(int size,int speed, int direction) { _size = size; _x = _size/2; _y = _size/2; srand(time(NULL)); 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 { _velocity.x = -speed; _velocity.y = -speed; } } void Ball::draw(N5110 &lcd) { lcd.drawCircle(_x,_y,_size,FILL_BLACK); } void Ball::update() { _x += _velocity.x; _y += _velocity.y; } void Ball::set_velocity(Vector2D v) { _velocity.x = v.x; _velocity.y = v.y; } Vector2D Ball::get_velocity() { Vector2D v = {_velocity.x,_velocity.y}; return v; } Vector2D Ball::get_pos() // get ball pos form lcd { Vector2D p = {_x,_y}; return p; } void Ball::set_pos(Vector2D p) // change lcd into cooredinat { _x = p.x; _y = p.y; }