2222222

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Ball.cpp Source File

Ball.cpp

00001 #include "Ball.h"
00002 
00003 Ball::Ball()
00004 {
00005 
00006 }
00007 
00008 Ball::~Ball()
00009 {
00010 
00011 }
00012 
00013 void Ball::init(int size,int speed, int direction)
00014 {
00015     _size = size;
00016 
00017     _x =  _size/2;
00018     _y =  _size/2;
00019 
00020     srand(time(NULL));
00021      direction = rand() % 4; // randomise initial direction. 
00022     _direction = direction;
00023     // 4 possibilities. Get random modulo and set velocities accordingly
00024     if (direction == 0) {
00025         _velocity.x = speed;
00026         _velocity.y = speed;
00027     } else if (direction == 1) {
00028         _velocity.x = -speed;
00029         _velocity.y = speed;
00030     } else if (direction == 2) {
00031         _velocity.x = speed;
00032         _velocity.y = -speed;
00033     } else {
00034         _velocity.x = -speed;
00035         _velocity.y = -speed;
00036     }
00037     
00038     
00039 }
00040 
00041 void Ball::draw(N5110 &lcd)
00042 {
00043     lcd.drawCircle(_x,_y,_size,FILL_BLACK);
00044 }
00045 
00046 void Ball::update()
00047 {
00048     _x += _velocity.x;
00049     _y += _velocity.y;
00050 }
00051 
00052 void Ball::set_velocity(Vector2D v)
00053 {
00054     _velocity.x = v.x;
00055     _velocity.y = v.y;
00056 }
00057 
00058 Vector2D Ball::get_velocity()
00059 {
00060     Vector2D v = {_velocity.x,_velocity.y};
00061     return v;
00062 }
00063 
00064 Vector2D Ball::get_pos() // get ball pos form lcd
00065 {
00066     Vector2D p = {_x,_y};
00067     return p;
00068 }
00069 
00070 void Ball::set_pos(Vector2D p) // change lcd into cooredinat
00071 {
00072     _x = p.x;
00073     _y = p.y;
00074 }