FINAL VERSION

Dependencies:   mbed

Committer:
jamesheavey
Date:
Mon May 06 13:07:19 2019 +0000
Revision:
76:52410264a72d
Parent:
71:886cde5e2c63
Child:
85:6b21dc88f2fc
ball inits in the right place now

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 0:7d4d2023ed9c 15 _size = size;
jamesheavey 0:7d4d2023ed9c 16
jamesheavey 76:52410264a72d 17 _x = x;
jamesheavey 71:886cde5e2c63 18 _y = HEIGHT - _size/2 - 4;
jamesheavey 0:7d4d2023ed9c 19
jamesheavey 0:7d4d2023ed9c 20 srand(time(NULL));
jamesheavey 0:7d4d2023ed9c 21 int direction = rand() % 4; // randomise initial direction.
jamesheavey 0:7d4d2023ed9c 22
jamesheavey 0:7d4d2023ed9c 23 // 4 possibilities. Get random modulo and set velocities accordingly
jamesheavey 0:7d4d2023ed9c 24 if (direction == 0) {
jamesheavey 0:7d4d2023ed9c 25 _velocity.x = speed;
jamesheavey 70:812178dc3e32 26 _velocity.y = -speed;
jamesheavey 0:7d4d2023ed9c 27 } else if (direction == 1) {
jamesheavey 8:1ab6d90c4d60 28 _velocity.x = -speed;
jamesheavey 70:812178dc3e32 29 _velocity.y = -speed;
jamesheavey 0:7d4d2023ed9c 30 } else if (direction == 2) {
jamesheavey 0:7d4d2023ed9c 31 _velocity.x = speed;
jamesheavey 70:812178dc3e32 32 _velocity.y = -speed;
jamesheavey 0:7d4d2023ed9c 33 } else {
jamesheavey 0:7d4d2023ed9c 34 _velocity.x = -speed;
jamesheavey 70:812178dc3e32 35 _velocity.y = -speed;
jamesheavey 0:7d4d2023ed9c 36 }
jamesheavey 0:7d4d2023ed9c 37 }
jamesheavey 0:7d4d2023ed9c 38
jamesheavey 0:7d4d2023ed9c 39 void Ball::draw(N5110 &lcd)
jamesheavey 0:7d4d2023ed9c 40 {
jamesheavey 0:7d4d2023ed9c 41 lcd.drawRect(_x,_y,_size,_size,FILL_BLACK);
jamesheavey 0:7d4d2023ed9c 42 }
jamesheavey 0:7d4d2023ed9c 43
jamesheavey 0:7d4d2023ed9c 44 void Ball::update()
jamesheavey 0:7d4d2023ed9c 45 {
jamesheavey 0:7d4d2023ed9c 46 _x += _velocity.x;
jamesheavey 0:7d4d2023ed9c 47 _y += _velocity.y;
jamesheavey 0:7d4d2023ed9c 48 }
jamesheavey 0:7d4d2023ed9c 49
jamesheavey 0:7d4d2023ed9c 50 void Ball::set_velocity(Vector2D v)
jamesheavey 0:7d4d2023ed9c 51 {
jamesheavey 0:7d4d2023ed9c 52 _velocity.x = v.x;
jamesheavey 0:7d4d2023ed9c 53 _velocity.y = v.y;
jamesheavey 0:7d4d2023ed9c 54 }
jamesheavey 0:7d4d2023ed9c 55
jamesheavey 0:7d4d2023ed9c 56 Vector2D Ball::get_velocity()
jamesheavey 0:7d4d2023ed9c 57 {
jamesheavey 0:7d4d2023ed9c 58 Vector2D v = {_velocity.x,_velocity.y};
jamesheavey 0:7d4d2023ed9c 59 return v;
jamesheavey 0:7d4d2023ed9c 60 }
jamesheavey 0:7d4d2023ed9c 61
jamesheavey 0:7d4d2023ed9c 62 Vector2D Ball::get_pos()
jamesheavey 0:7d4d2023ed9c 63 {
jamesheavey 0:7d4d2023ed9c 64 Vector2D p = {_x,_y};
jamesheavey 0:7d4d2023ed9c 65 return p;
jamesheavey 0:7d4d2023ed9c 66 }
jamesheavey 0:7d4d2023ed9c 67
jamesheavey 0:7d4d2023ed9c 68 void Ball::set_pos(Vector2D p)
jamesheavey 0:7d4d2023ed9c 69 {
jamesheavey 0:7d4d2023ed9c 70 _x = p.x;
jamesheavey 0:7d4d2023ed9c 71 _y = p.y;
jamesheavey 0:7d4d2023ed9c 72 }