James Cummins / Mbed 2 deprecated el17jnc

Dependencies:   mbed

Committer:
JamesCummins
Date:
Thu May 09 01:09:18 2019 +0000
Revision:
38:a85bc227b907
Parent:
29:42651f87522b
Child:
40:a1cdb6ab08af
Doxygen documentation written out and in line commenting completed. Game working and finished. Tests.h still to write out

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JamesCummins 11:2cf0d4ce8677 1 #include "Ball.h"
JamesCummins 9:ce0a12fb205b 2
JamesCummins 9:ce0a12fb205b 3 Ball::Ball()
JamesCummins 9:ce0a12fb205b 4 {}
JamesCummins 9:ce0a12fb205b 5
JamesCummins 9:ce0a12fb205b 6 Ball::~Ball()
JamesCummins 9:ce0a12fb205b 7 {}
JamesCummins 9:ce0a12fb205b 8
JamesCummins 9:ce0a12fb205b 9 void Ball::init(int radius){
JamesCummins 9:ce0a12fb205b 10 _radius = radius;
JamesCummins 38:a85bc227b907 11 _x = 42; //start ball in the centre of the screen
JamesCummins 9:ce0a12fb205b 12 _y = 24;
JamesCummins 38:a85bc227b907 13 _velocity.x = 0; //initially have the velocity as 0 until the
JamesCummins 38:a85bc227b907 14 _velocity.y = 0; //accelerometer senses otherwise
JamesCummins 38:a85bc227b907 15 _ball_speed = 5; //medium sensitivity to tilt input
JamesCummins 9:ce0a12fb205b 16 }
JamesCummins 9:ce0a12fb205b 17
JamesCummins 13:e5a36fbd48ae 18 void Ball::draw(N5110 &lcd){
JamesCummins 13:e5a36fbd48ae 19 int radius = get_radius();
JamesCummins 38:a85bc227b907 20 lcd.drawCircle(_x, _y, radius, FILL_BLACK); //draw black circle at coord (x, y) with preset radius
JamesCummins 9:ce0a12fb205b 21 }
JamesCummins 9:ce0a12fb205b 22
JamesCummins 17:5104ecef5bd0 23 void Ball::read_input(FXOS8700CQ &accelerometer){
JamesCummins 13:e5a36fbd48ae 24 Data values = accelerometer.get_values();
JamesCummins 23:61fa82f76808 25 _velocity.x = -(5+_ball_speed)*values.ay; //axes of the accelerometer are different to orientation of the screen
JamesCummins 23:61fa82f76808 26 _velocity.y = -(5+_ball_speed)*values.ax; //negative to account for reversed direction
JamesCummins 23:61fa82f76808 27 /*printf("ax = %f | ay = %f | ", values.ax, values.ay);
JamesCummins 23:61fa82f76808 28 printf("vel.x = %f | vel.y = %f\n", _velocity.x, _velocity.y);*/
JamesCummins 17:5104ecef5bd0 29 }
JamesCummins 17:5104ecef5bd0 30
JamesCummins 17:5104ecef5bd0 31 void Ball::update(){
JamesCummins 17:5104ecef5bd0 32 int RADIUS = get_radius();
JamesCummins 38:a85bc227b907 33 _x += _velocity.x; //update position based on how much tilt inputted
JamesCummins 9:ce0a12fb205b 34 _y += _velocity.y;
JamesCummins 38:a85bc227b907 35 if (_x < RADIUS){ _x = RADIUS;} //check wall collisions
JamesCummins 38:a85bc227b907 36 if (_x > 84 - RADIUS){ _x = 83 - RADIUS;} //if ball position is going to
JamesCummins 38:a85bc227b907 37 if (_y < RADIUS){ _y = RADIUS;} //exceed boundaries of the screen
JamesCummins 38:a85bc227b907 38 if (_y > 48 - RADIUS){ _y = 47 - RADIUS;} //then fix it to the edge of the screen
JamesCummins 23:61fa82f76808 39 printf("ball speed = %d\n", _ball_speed);
JamesCummins 9:ce0a12fb205b 40 }
JamesCummins 9:ce0a12fb205b 41
JamesCummins 9:ce0a12fb205b 42 Vector2D Ball::get_position(){
JamesCummins 38:a85bc227b907 43 Vector2D pos = {_x, _y}; //combine x and y into Vector2D
JamesCummins 9:ce0a12fb205b 44 return pos;
JamesCummins 9:ce0a12fb205b 45 }
JamesCummins 9:ce0a12fb205b 46
JamesCummins 9:ce0a12fb205b 47 Vector2D Ball::get_velocity(){
JamesCummins 38:a85bc227b907 48 Vector2D vel = {_velocity.x, _velocity.y}; //combine velocity.x and .y into a Vector2D
JamesCummins 9:ce0a12fb205b 49 return vel;
JamesCummins 9:ce0a12fb205b 50 }
JamesCummins 9:ce0a12fb205b 51
JamesCummins 10:40c77d69e83c 52 int Ball::get_radius(){
JamesCummins 38:a85bc227b907 53 int radius = _radius; //find from private member variable
JamesCummins 10:40c77d69e83c 54 return radius;
JamesCummins 10:40c77d69e83c 55 }
JamesCummins 10:40c77d69e83c 56
JamesCummins 29:42651f87522b 57 int Ball::get_ball_speed(){
JamesCummins 38:a85bc227b907 58 int ball_speed = _ball_speed; //find from private member variable
JamesCummins 29:42651f87522b 59 return ball_speed;
JamesCummins 29:42651f87522b 60 }
JamesCummins 29:42651f87522b 61
JamesCummins 23:61fa82f76808 62 void Ball::set_ball_speed(int ball_speed){
JamesCummins 38:a85bc227b907 63 _ball_speed = ball_speed; //update private member variable with user input
JamesCummins 23:61fa82f76808 64 }
JamesCummins 23:61fa82f76808 65
JamesCummins 9:ce0a12fb205b 66 void Ball::set_velocity(Vector2D vel){
JamesCummins 38:a85bc227b907 67 _velocity.x = vel.x; //must pass each part of the struct individually,
JamesCummins 38:a85bc227b907 68 _velocity.y = vel.y; //can't pass both at once
JamesCummins 9:ce0a12fb205b 69 }
JamesCummins 9:ce0a12fb205b 70
JamesCummins 9:ce0a12fb205b 71 void Ball::set_position(Vector2D pos){
JamesCummins 38:a85bc227b907 72 _x = pos.x; //as with set_velocity
JamesCummins 9:ce0a12fb205b 73 _y = pos.y;
JamesCummins 10:40c77d69e83c 74 }
JamesCummins 10:40c77d69e83c 75
JamesCummins 10:40c77d69e83c 76 void Ball::set_radius(int radius){
JamesCummins 38:a85bc227b907 77 _radius = radius; //don't use in code, other than initially where radius is defined using #define in main.cpp
JamesCummins 9:ce0a12fb205b 78 }