4

Files at this revision

API Documentation at this revision

Comitter:
fy14ta
Date:
Thu May 04 06:38:59 2017 +0000
Parent:
4:d3f87c8e4027
Commit message:
ball1 is big size of the ball

Changed in this revision

Ball.cpp Show diff for this revision Revisions of this file
Ball.h Show diff for this revision Revisions of this file
Ball1.cpp Show annotated file Show diff for this revision Revisions of this file
Ball1.h Show annotated file Show diff for this revision Revisions of this file
diff -r d3f87c8e4027 -r 381cd0ea08aa Ball.cpp
--- a/Ball.cpp	Sun Mar 05 23:10:45 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,72 +0,0 @@
-#include "Ball.h"
-
-Ball::Ball()
-{
-
-}
-
-Ball::~Ball()
-{
-
-}
-
-void Ball::init(int size,int speed)
-{
-    _size = size;
-
-    _x = WIDTH/2 -  _size/2;
-    _y = HEIGHT/2 - _size/2;
-
-    srand(time(NULL));
-    int direction = rand() % 4; // randomise initial direction. 
-
-    // 4 possibilities. Get random modulo and set velocities accordingly
-    if (direction == 0) {
-        _velocity.x = speed;
-        _velocity.y = speed;
-    } else if (direction == 1) {
-        _velocity.x = speed;
-        _velocity.y = -speed;
-    } else if (direction == 2) {
-        _velocity.x = speed;
-        _velocity.y = speed;
-    } else {
-        _velocity.x = -speed;
-        _velocity.y = -speed;
-    }
-}
-
-void Ball::draw(N5110 &lcd)
-{
-    lcd.drawRect(_x,_y,_size,_size,FILL_BLACK);
-}
-
-void Ball::update()
-{
-    _x += _velocity.x;
-    _y += _velocity.y;
-}
-
-void Ball::set_velocity(Vector2D v)
-{
-    _velocity.x = v.x;
-    _velocity.y = v.y;
-}
-
-Vector2D Ball::get_velocity()
-{
-    Vector2D v = {_velocity.x,_velocity.y};
-    return v;
-}
-
-Vector2D Ball::get_pos()
-{
-    Vector2D p = {_x,_y};
-    return p;
-}
-
-void Ball::set_pos(Vector2D p)
-{
-    _x = p.x;
-    _y = p.y;
-}
\ No newline at end of file
diff -r d3f87c8e4027 -r 381cd0ea08aa Ball.h
--- a/Ball.h	Sun Mar 05 23:10:45 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,31 +0,0 @@
-#ifndef BALL_H
-#define BALL_H
-
-#include "mbed.h"
-#include "N5110.h"
-#include "Gamepad.h"
-#include "Paddle.h"
-
-class Ball
-{
-
-public:
-    Ball();
-    ~Ball();
-    void init(int size,int speed);
-    void draw(N5110 &lcd);
-    void update();
-    /// accessors and mutators
-    void set_velocity(Vector2D v);
-    Vector2D get_velocity();
-    Vector2D get_pos();
-    void set_pos(Vector2D p);
-    
-private:
-
-    Vector2D _velocity;
-    int _size;
-    int _x;
-    int _y;
-};
-#endif
\ No newline at end of file
diff -r d3f87c8e4027 -r 381cd0ea08aa Ball1.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Ball1.cpp	Thu May 04 06:38:59 2017 +0000
@@ -0,0 +1,93 @@
+#include "Ball1.h"
+
+Ball1::Ball1()
+{
+
+}
+
+Ball1::~Ball1()
+{
+
+}
+
+void Ball1::init(int size,int speed)
+{
+
+    _size = size;
+
+    _x = WIDTH/2 -  _size/2;
+    _y = HEIGHT/2 - _size/2;
+    
+
+
+        _velocity.x = speed;
+        _velocity.y = speed; }
+
+
+void Ball1::draw(N5110 &lcd)
+{
+    lcd.drawRect(_x,_y,_size,_size,FILL_BLACK);
+}
+
+
+
+void Ball1::update(Direction d,float mag)
+{
+
+    
+    
+    
+        _velocity.x = int(mag*1.0f); 
+                _velocity.y = int(mag*1.0f);  // scale is arbitrary, could be changed in future
+
+    // update y value depending on direction of movement
+    // North is decrement as origin is at the top-left so decreasing moves up
+    if (d == N) {
+        _y+=_velocity.y;
+    } else if (d == S) {
+        _y+=_velocity.y;
+        }
+
+    if (d == W) {
+        _x+=_velocity.x;
+    } else if (d == E) {
+        _x+=_velocity.x;
+        }
+        
+        
+    
+
+}
+
+
+
+void Ball1::set_velocity(Vector2D v)
+{
+    _velocity.x = v.x;
+    _velocity.y = v.y;
+}
+
+Vector2D Ball1::get_velocity()
+{
+    Vector2D v = {_velocity.x,_velocity.y};
+    return v;
+}
+
+Vector2D Ball1::get_pos()
+{
+    Vector2D p = {_x,_y};
+    return p;
+}
+
+void Ball1::set_pos(Vector2D p)
+{
+    _x = p.x;
+    _y = p.y;
+}
+
+
+void Ball1::set_size(int size)
+{
+    _size = size;
+}
+
diff -r d3f87c8e4027 -r 381cd0ea08aa Ball1.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Ball1.h	Thu May 04 06:38:59 2017 +0000
@@ -0,0 +1,33 @@
+#ifndef BALL1_H
+#define BALL1_H
+
+#include "mbed.h"
+#include "N5110.h"
+#include "Gamepad.h"
+#include "Paddle.h"
+
+
+class Ball1
+{
+
+public:
+    Ball1();
+    ~Ball1();
+    void init(int size,int speed);
+    void draw(N5110 &lcd);
+    void update(Direction d,float mag);
+    /// accessors and mutators
+    void set_velocity(Vector2D v);
+    Vector2D get_velocity();
+    Vector2D get_pos();
+    void set_pos(Vector2D p);
+    void set_size(int size);
+    
+private:
+
+    Vector2D _velocity;
+    int _size;
+    int _x;
+    int _y;
+};
+#endif
\ No newline at end of file