FINAL VERSION

Dependencies:   mbed

Committer:
jamesheavey
Date:
Mon May 06 18:41:24 2019 +0000
Revision:
85:6b21dc88f2fc
Parent:
76:52410264a72d
Child:
91:c01a736fb0d9
Ball class comments done

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 85:6b21dc88f2fc 18 _y = HEIGHT - _size/2 - 4; // fixed y init position
jamesheavey 0:7d4d2023ed9c 19
jamesheavey 0:7d4d2023ed9c 20 srand(time(NULL));
jamesheavey 85:6b21dc88f2fc 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 85:6b21dc88f2fc 33 void Ball::draw(N5110 &lcd) // draw the ball at the specified location
jamesheavey 0:7d4d2023ed9c 34 {
jamesheavey 0:7d4d2023ed9c 35 lcd.drawRect(_x,_y,_size,_size,FILL_BLACK);
jamesheavey 0:7d4d2023ed9c 36 }
jamesheavey 0:7d4d2023ed9c 37
jamesheavey 85:6b21dc88f2fc 38 void Ball::update() // update the ball's position, based on its current velocity
jamesheavey 0:7d4d2023ed9c 39 {
jamesheavey 0:7d4d2023ed9c 40 _x += _velocity.x;
jamesheavey 0:7d4d2023ed9c 41 _y += _velocity.y;
jamesheavey 0:7d4d2023ed9c 42 }
jamesheavey 0:7d4d2023ed9c 43
jamesheavey 85:6b21dc88f2fc 44 void Ball::set_velocity(Vector2D v) // set the velocity in its x/y directions (used for collision velocity correction)
jamesheavey 0:7d4d2023ed9c 45 {
jamesheavey 0:7d4d2023ed9c 46 _velocity.x = v.x;
jamesheavey 0:7d4d2023ed9c 47 _velocity.y = v.y;
jamesheavey 0:7d4d2023ed9c 48 }
jamesheavey 0:7d4d2023ed9c 49
jamesheavey 85:6b21dc88f2fc 50 Vector2D Ball::get_velocity() // retrieve the ball's current velocities
jamesheavey 0:7d4d2023ed9c 51 {
jamesheavey 0:7d4d2023ed9c 52 Vector2D v = {_velocity.x,_velocity.y};
jamesheavey 0:7d4d2023ed9c 53 return v;
jamesheavey 0:7d4d2023ed9c 54 }
jamesheavey 0:7d4d2023ed9c 55
jamesheavey 85:6b21dc88f2fc 56 Vector2D Ball::get_pos() // retrieve the ball's current coordinates
jamesheavey 0:7d4d2023ed9c 57 {
jamesheavey 0:7d4d2023ed9c 58 Vector2D p = {_x,_y};
jamesheavey 0:7d4d2023ed9c 59 return p;
jamesheavey 0:7d4d2023ed9c 60 }
jamesheavey 0:7d4d2023ed9c 61
jamesheavey 85:6b21dc88f2fc 62 void Ball::set_pos(Vector2D p) // set the ball's coordinates (used for collision velocity correction)
jamesheavey 0:7d4d2023ed9c 63 {
jamesheavey 0:7d4d2023ed9c 64 _x = p.x;
jamesheavey 0:7d4d2023ed9c 65 _y = p.y;
jamesheavey 0:7d4d2023ed9c 66 }