FINAL VERSION

Dependencies:   mbed

Committer:
jamesheavey
Date:
Thu May 09 14:36:51 2019 +0000
Revision:
140:d8634e76ecce
Parent:
136:04a2724f90cf
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jamesheavey 0:7d4d2023ed9c 1 #include "Ball.h"
jamesheavey 0:7d4d2023ed9c 2
jamesheavey 0:7d4d2023ed9c 3 Ball::Ball()
jamesheavey 0:7d4d2023ed9c 4 {
jamesheavey 0:7d4d2023ed9c 5
jamesheavey 0:7d4d2023ed9c 6 }
jamesheavey 0:7d4d2023ed9c 7
jamesheavey 0:7d4d2023ed9c 8 Ball::~Ball()
jamesheavey 0:7d4d2023ed9c 9 {
jamesheavey 0:7d4d2023ed9c 10
jamesheavey 0:7d4d2023ed9c 11 }
jamesheavey 0:7d4d2023ed9c 12
jamesheavey 76:52410264a72d 13 void Ball::init(int size,int speed, int x)
jamesheavey 0:7d4d2023ed9c 14 {
jamesheavey 85:6b21dc88f2fc 15 _size = size; // size of the ball
jamesheavey 0:7d4d2023ed9c 16
jamesheavey 85:6b21dc88f2fc 17 _x = x; // init position of the ball will be at the centre of the paddle
jamesheavey 93:18f81996ea89 18 _y = HEIGHT - _size/2 - 3; // fixed y init position, just above the paddle
jamesheavey 0:7d4d2023ed9c 19
jamesheavey 0:7d4d2023ed9c 20 srand(time(NULL));
jamesheavey 91:c01a736fb0d9 21 int direction = rand() % 2; // randomise initial direction.
jamesheavey 0:7d4d2023ed9c 22
jamesheavey 85:6b21dc88f2fc 23 // 2 directions, both directed upwards (N) from the paddle
jamesheavey 0:7d4d2023ed9c 24 if (direction == 0) {
jamesheavey 85:6b21dc88f2fc 25 _velocity.x = speed; // direction = E
jamesheavey 85:6b21dc88f2fc 26 _velocity.y = -speed; // direction = N
jamesheavey 0:7d4d2023ed9c 27 } else {
jamesheavey 85:6b21dc88f2fc 28 _velocity.x = -speed; // direction = W
jamesheavey 85:6b21dc88f2fc 29 _velocity.y = -speed; // direction = N
jamesheavey 0:7d4d2023ed9c 30 }
jamesheavey 0:7d4d2023ed9c 31 }
jamesheavey 0:7d4d2023ed9c 32
jamesheavey 136:04a2724f90cf 33
jamesheavey 85:6b21dc88f2fc 34 void Ball::draw(N5110 &lcd) // draw the ball at the specified location
jamesheavey 0:7d4d2023ed9c 35 {
jamesheavey 0:7d4d2023ed9c 36 lcd.drawRect(_x,_y,_size,_size,FILL_BLACK);
jamesheavey 0:7d4d2023ed9c 37 }
jamesheavey 0:7d4d2023ed9c 38
jamesheavey 136:04a2724f90cf 39
jamesheavey 85:6b21dc88f2fc 40 void Ball::update() // update the ball's position, based on its current velocity
jamesheavey 0:7d4d2023ed9c 41 {
jamesheavey 0:7d4d2023ed9c 42 _x += _velocity.x;
jamesheavey 0:7d4d2023ed9c 43 _y += _velocity.y;
jamesheavey 0:7d4d2023ed9c 44 }
jamesheavey 0:7d4d2023ed9c 45
jamesheavey 136:04a2724f90cf 46
jamesheavey 85:6b21dc88f2fc 47 void Ball::set_velocity(Vector2D v) // set the velocity in its x/y directions (used for collision velocity correction)
jamesheavey 0:7d4d2023ed9c 48 {
jamesheavey 0:7d4d2023ed9c 49 _velocity.x = v.x;
jamesheavey 0:7d4d2023ed9c 50 _velocity.y = v.y;
jamesheavey 0:7d4d2023ed9c 51 }
jamesheavey 0:7d4d2023ed9c 52
jamesheavey 136:04a2724f90cf 53
jamesheavey 85:6b21dc88f2fc 54 Vector2D Ball::get_velocity() // retrieve the ball's current velocities
jamesheavey 0:7d4d2023ed9c 55 {
jamesheavey 0:7d4d2023ed9c 56 Vector2D v = {_velocity.x,_velocity.y};
jamesheavey 0:7d4d2023ed9c 57 return v;
jamesheavey 0:7d4d2023ed9c 58 }
jamesheavey 0:7d4d2023ed9c 59
jamesheavey 136:04a2724f90cf 60
jamesheavey 85:6b21dc88f2fc 61 Vector2D Ball::get_pos() // retrieve the ball's current coordinates
jamesheavey 0:7d4d2023ed9c 62 {
jamesheavey 0:7d4d2023ed9c 63 Vector2D p = {_x,_y};
jamesheavey 0:7d4d2023ed9c 64 return p;
jamesheavey 0:7d4d2023ed9c 65 }
jamesheavey 0:7d4d2023ed9c 66
jamesheavey 136:04a2724f90cf 67
jamesheavey 85:6b21dc88f2fc 68 void Ball::set_pos(Vector2D p) // set the ball's coordinates (used for collision velocity correction)
jamesheavey 0:7d4d2023ed9c 69 {
jamesheavey 0:7d4d2023ed9c 70 _x = p.x;
jamesheavey 0:7d4d2023ed9c 71 _y = p.y;
jamesheavey 0:7d4d2023ed9c 72 }