444444444444

Dependencies:   mbed CXK

Committer:
Jenny121
Date:
Mon May 06 04:11:38 2019 +0000
Revision:
12:f8eb397226bc
Parent:
5:3c9407e2fe55
update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
eencae 5:3c9407e2fe55 1 #include "Ball.h"
eencae 5:3c9407e2fe55 2
eencae 5:3c9407e2fe55 3 Ball::Ball()
eencae 5:3c9407e2fe55 4 {
eencae 5:3c9407e2fe55 5
eencae 5:3c9407e2fe55 6 }
eencae 5:3c9407e2fe55 7
eencae 5:3c9407e2fe55 8 Ball::~Ball()
eencae 5:3c9407e2fe55 9 {
eencae 5:3c9407e2fe55 10
eencae 5:3c9407e2fe55 11 }
eencae 5:3c9407e2fe55 12
Jenny121 12:f8eb397226bc 13 void Ball::init(int size,int speed, int direction)
eencae 5:3c9407e2fe55 14 {
eencae 5:3c9407e2fe55 15 _size = size;
eencae 5:3c9407e2fe55 16
Jenny121 12:f8eb397226bc 17 _x = _size/2;
Jenny121 12:f8eb397226bc 18 _y = _size/2;
eencae 5:3c9407e2fe55 19
eencae 5:3c9407e2fe55 20 srand(time(NULL));
Jenny121 12:f8eb397226bc 21 direction = rand() % 4; // randomise initial direction.
Jenny121 12:f8eb397226bc 22 _direction = direction;
eencae 5:3c9407e2fe55 23 // 4 possibilities. Get random modulo and set velocities accordingly
eencae 5:3c9407e2fe55 24 if (direction == 0) {
eencae 5:3c9407e2fe55 25 _velocity.x = speed;
eencae 5:3c9407e2fe55 26 _velocity.y = speed;
eencae 5:3c9407e2fe55 27 } else if (direction == 1) {
Jenny121 12:f8eb397226bc 28 _velocity.x = -speed;
Jenny121 12:f8eb397226bc 29 _velocity.y = speed;
Jenny121 12:f8eb397226bc 30 } else if (direction == 2) {
eencae 5:3c9407e2fe55 31 _velocity.x = speed;
eencae 5:3c9407e2fe55 32 _velocity.y = -speed;
eencae 5:3c9407e2fe55 33 } else {
eencae 5:3c9407e2fe55 34 _velocity.x = -speed;
eencae 5:3c9407e2fe55 35 _velocity.y = -speed;
eencae 5:3c9407e2fe55 36 }
Jenny121 12:f8eb397226bc 37
Jenny121 12:f8eb397226bc 38
eencae 5:3c9407e2fe55 39 }
eencae 5:3c9407e2fe55 40
eencae 5:3c9407e2fe55 41 void Ball::draw(N5110 &lcd)
eencae 5:3c9407e2fe55 42 {
Jenny121 12:f8eb397226bc 43 lcd.drawCircle(_x,_y,_size,FILL_BLACK);
eencae 5:3c9407e2fe55 44 }
eencae 5:3c9407e2fe55 45
eencae 5:3c9407e2fe55 46 void Ball::update()
eencae 5:3c9407e2fe55 47 {
eencae 5:3c9407e2fe55 48 _x += _velocity.x;
eencae 5:3c9407e2fe55 49 _y += _velocity.y;
eencae 5:3c9407e2fe55 50 }
eencae 5:3c9407e2fe55 51
eencae 5:3c9407e2fe55 52 void Ball::set_velocity(Vector2D v)
eencae 5:3c9407e2fe55 53 {
eencae 5:3c9407e2fe55 54 _velocity.x = v.x;
eencae 5:3c9407e2fe55 55 _velocity.y = v.y;
eencae 5:3c9407e2fe55 56 }
eencae 5:3c9407e2fe55 57
eencae 5:3c9407e2fe55 58 Vector2D Ball::get_velocity()
eencae 5:3c9407e2fe55 59 {
eencae 5:3c9407e2fe55 60 Vector2D v = {_velocity.x,_velocity.y};
eencae 5:3c9407e2fe55 61 return v;
eencae 5:3c9407e2fe55 62 }
eencae 5:3c9407e2fe55 63
Jenny121 12:f8eb397226bc 64 Vector2D Ball::get_pos() // get ball pos form lcd
eencae 5:3c9407e2fe55 65 {
eencae 5:3c9407e2fe55 66 Vector2D p = {_x,_y};
eencae 5:3c9407e2fe55 67 return p;
eencae 5:3c9407e2fe55 68 }
eencae 5:3c9407e2fe55 69
Jenny121 12:f8eb397226bc 70 void Ball::set_pos(Vector2D p) // change lcd into cooredinat
eencae 5:3c9407e2fe55 71 {
eencae 5:3c9407e2fe55 72 _x = p.x;
eencae 5:3c9407e2fe55 73 _y = p.y;
eencae 5:3c9407e2fe55 74 }