Library to calculate movement and to draw the objects in the pong game

Dependencies:   RTC-DS1307 SPI_TFT_ILI9341 TFT_fonts mbed

Fork of MainSketch by IoT Ox

Files at this revision

API Documentation at this revision

Comitter:
tunagonen
Date:
Wed May 24 15:18:13 2017 +0000
Parent:
10:6940a3201757
Commit message:
l

Changed in this revision

2manpong.cpp Show annotated file Show diff for this revision Revisions of this file
2manpong.h Show annotated file Show diff for this revision Revisions of this file
pong.cpp Show diff for this revision Revisions of this file
ponglib.h Show diff for this revision Revisions of this file
singlepong.cpp Show annotated file Show diff for this revision Revisions of this file
singlepong.h Show annotated file Show diff for this revision Revisions of this file
diff -r 6940a3201757 -r d812de0e5136 2manpong.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/2manpong.cpp	Wed May 24 15:18:13 2017 +0000
@@ -0,0 +1,30 @@
+
+#include "libs.h"
+#include "2manpong.h"
+
+int main() {
+
+    AnalogIn Player1(); //reading analog inputs from the potentiometers
+    AnalogIn Player2(); 
+    int SCREENH = 239 ; //screen height
+    int SCREENW = 319 ; //screen width
+    int PADDLEW  = 10 ; // paddle width
+    int PADDLEH  = 40 ; // paddle height
+    int PADDLEG =  10 ; // paddle gap from the touchscreen
+    int ABALL = 10 ;   // ball size
+    int PADDLE_A = 0 ; // location paddle A
+    int PADDLE_B = 0 ; // location paddle B 
+    int PADDLE_AF = 0 ; // final location paddle A 
+    int PADDLE_BF = 0 ; // final location paddle B  
+    float XBALL = 0 ; // ball's x location
+    float YBALL = 0 ; // ball's y location
+    
+    float VXball = 3 ; // velocity in x-direction 
+    float VYball = 2 ; // velocity in y-direction
+    
+    while(1) {
+        draw();
+        move(); 
+    } 
+}   
+         
diff -r 6940a3201757 -r d812de0e5136 2manpong.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/2manpong.h	Wed May 24 15:18:13 2017 +0000
@@ -0,0 +1,89 @@
+#include "libs.h"
+
+float map(float in, float inMin, float inMax, float outMin, float outMax) {
+      // check it's within the range
+        if (inMin<inMax) { 
+            if (in <= inMin) 
+              return outMin;
+            if (in >= inMax)
+              return outMax;
+            } 
+        else {  // cope with input range being backwards.
+            if (in >= inMin) 
+              return outMin;
+            if (in <= inMax)
+              return outMax;
+        }
+      // calculate how far into the range we are
+      float scale = (in-inMin)/(inMax-inMin);
+      // calculate the output.
+      return outMin + scale*(outMax-outMin);
+}
+
+
+void move() {
+    
+    int player1 = Player1 ; 
+    int player2 = Player2 ;
+    PADDLE_A = map(player1 , 0 , 1023 , 0 , SCREENH - PADDLEH) ;
+    PADDLE_B = map(player2 , 0 , 1023 , 0 ,SCREENH - PADDLEH) ;
+    int VPADDLE_A = PADDLE_AF - PADDLE_A ; // speed of paddle A
+    int VPADDLE_B = PADDLE_BF - PADDLE_B ; // speed of paddle B 
+    
+    XBALL += VXBALL ; //x location of the ball 
+    YBALL += VYBALL ; //y location of the ball 
+    
+    //bounce from the top and bottom
+    
+    if (YBALL >= SCREENH - ABALL || YBALL <=0) 
+    {
+    VYBALL *= -1 ; 
+    }
+    
+    //bounce from Paddle A
+    if (XBALL >= PADDLEG && XBALL <= PADDLEG + ABALL && VXBALL < 0) 
+    {
+        if (YBALL > PADDLE_A - ABALL && YBALL < PADDLE_A + PADDLEH)
+        {
+        
+        VXBALL *= -1 ;
+        }
+    }
+    
+    // bounce from Paddle B   
+    if ( XBALL >= SCREENW - PADDLEW - PADDLEG - ABALL && XBALL <= SCREENW - PADDLEG - ABALL && VXBALL >0 )
+    { 
+        if (YBALL > PADDLE_B - ABALL && YBALL < PADDLE_B + PADDLEH) 
+        {
+            VXBALL *= -1 ;
+            }
+            }
+            
+            
+   // if someone scores, reset
+   
+   if (XBALL >= SCREENW - ABALL || XBALL <= 0 ) {
+        
+        // reset function //
+        
+        }
+       
+       }
+       
+ void draw() 
+{  
+   
+
+   fillcircle(XBALL , YBALL , ABALL , 1 ) ;  //draw ball 
+   
+   fillrect( PADDLEG , PADDLE_A , PADDLEW , PADDLEH , 1) ; //draw paddle A
+   
+   fillrect( SCREENW - PADDLEW - PADDLEG , PADDLE_B , PADDLEW , PADDLEH , 1) ; // draw paddle B 
+   
+   }
+
+
+
+        
+         
+
diff -r 6940a3201757 -r d812de0e5136 pong.cpp
--- a/pong.cpp	Wed May 24 14:52:39 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,30 +0,0 @@
-
-#include "libs.h"
-#include "ponglib.h"
-
-int main() {
-
-    AnalogIn Player1(); //reading analog inputs from the potentiometers
-    AnalogIn Player2(); 
-    int SCREENH = 239 ; //screen height
-    int SCREENW = 319 ; //screen width
-    int PADDLEW  = 10 ; // paddle width
-    int PADDLEH  = 40 ; // paddle height
-    int PADDLEG =  10 ; // paddle gap from the touchscreen
-    int ABALL = 10 ;   // ball size
-    int PADDLE_A = 0 ; // location paddle A
-    int PADDLE_B = 0 ; // location paddle B 
-    int PADDLE_AF = 0 ; // final location paddle A 
-    int PADDLE_BF = 0 ; // final location paddle B  
-    float XBALL = 0 ; // ball's x location
-    float YBALL = 0 ; // ball's y location
-    
-    float VXball = 3 ; // velocity in x-direction 
-    float VYball = 2 ; // velocity in y-direction
-    
-    while(1) {
-        draw();
-        move(); 
-    } 
-}   
-         
diff -r 6940a3201757 -r d812de0e5136 ponglib.h
--- a/ponglib.h	Wed May 24 14:52:39 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,88 +0,0 @@
-#include "libs.h"
-
-float map(float in, float inMin, float inMax, float outMin, float outMax) {
-      // check it's within the range
-        if (inMin<inMax) { 
-            if (in <= inMin) 
-              return outMin;
-            if (in >= inMax)
-              return outMax;
-            } 
-        else {  // cope with input range being backwards.
-            if (in >= inMin) 
-              return outMin;
-            if (in <= inMax)
-              return outMax;
-        }
-      // calculate how far into the range we are
-      float scale = (in-inMin)/(inMax-inMin);
-      // calculate the output.
-      return outMin + scale*(outMax-outMin);
-}
-
-
-void move() {
-    
-    int player1 = Player1 ; 
-    int player2 = Player2 ;
-    PADDLE_A = map(player1 , 0 , 1023 , 0 , SCREENH - PADDLEH) ;
-    PADDLE_B = map(player2 , 0 , 1023 , 0 ,SCREENH - PADDLEH) ;
-    int VPADDLE_A = PADDLE_AF - PADDLE_A ; // speed of paddle A
-    int VPADDLE_B = PADDLE_BF - PADDLE_B ; // speed of paddle B 
-    
-    XBALL += VXBALL ; //x location of the ball 
-    YBALL += VYBALL ; //y location of the ball 
-    
-    //bounce from the top and bottom
-    
-    if (YBALL >= SCREENH - ABALL || YBALL <=0) 
-    {
-    VYBALL *= -1 ; 
-    }
-    
-    //bounce from Paddle A
-    if (XBALL >= PADDLEG && XBALL <= PADDLEG + ABALL && VXBALL < 0) 
-    {
-        if (YBALL > PADDLE_A - ABALL && YBALL < PADDLE_A + PADDLEH)
-        {
-        
-        VXBALL *= -1 ;
-        }
-    }
-      
-    if ( XBALL >= SCREENW - PADDLEW - PADDLEG - ABALL && XBALL <= SCREENW - PADDLEG - ABALL && VXBALL >0 )
-    { 
-        if (YBALL > PADDLE_B - ABALL && YBALL < PADDLE_B + PADDLEH) 
-        {
-            VXBALL *= -1 ;
-            }
-            }
-            
-            
-   // if someone scores, reset
-   
-   if (XBALL >= SCREENW - ABALL || XBALL <= 0 ) {
-        
-        // reset function //
-        
-        }
-       
-       }
-       
- void draw() 
-{  
-   
-
-   fillcircle(XBALL , YBALL , ABALL , 1 ) ;  //draw ball 
-   
-   fillrect( PADDLEG , PADDLE_A , PADDLEW , PADDLEH , 1) ; //draw paddle A
-   
-   fillrect( SCREENW - PADDLEW - PADDLEG , PADDLE_B , PADDLEW , PADDLEH , 1) ; // draw paddle B 
-   
-   }
-
-
-
-        
-         
-
diff -r 6940a3201757 -r d812de0e5136 singlepong.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/singlepong.cpp	Wed May 24 15:18:13 2017 +0000
@@ -0,0 +1,27 @@
+
+#include "libs.h"
+#include "singlepong.h"
+
+int main() {
+
+    AnalogIn Player1(); //reading analog inputs from the potentiometers
+    int SCREENH = 239 ; //screen height
+    int SCREENW = 319 ; //screen width
+    int PADDLEW  = 10 ; // paddle width
+    int PADDLEH  = 40 ; // paddle height
+    int PADDLEG =  10 ; // paddle gap from the touchscreen
+    int ABALL = 10 ;   // ball size
+    int PADDLE_A = 0 ; // location paddle A
+    int PADDLE_AF = 0 ; // final location paddle A   
+    float XBALL = 0 ; // ball's x location
+    float YBALL = 0 ; // ball's y location
+    
+    float VXball = 3 ; // velocity in x-direction 
+    float VYball = 2 ; // velocity in y-direction
+    
+    while(1) {
+        draw();
+        move(); 
+    } 
+}   
+         
\ No newline at end of file
diff -r 6940a3201757 -r d812de0e5136 singlepong.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/singlepong.h	Wed May 24 15:18:13 2017 +0000
@@ -0,0 +1,81 @@
+#include "libs.h"
+
+float map(float in, float inMin, float inMax, float outMin, float outMax) {
+      // check it's within the range
+        if (inMin<inMax) { 
+            if (in <= inMin) 
+              return outMin;
+            if (in >= inMax)
+              return outMax;
+            } 
+        else {  // cope with input range being backwards.
+            if (in >= inMin) 
+              return outMin;
+            if (in <= inMax)
+              return outMax;
+        }
+      // calculate how far into the range we are
+      float scale = (in-inMin)/(inMax-inMin);
+      // calculate the output.
+      return outMin + scale*(outMax-outMin);
+}
+
+
+void move() {
+    
+    int player1 = Player1 ; ;
+    PADDLE_A = map(player1 , 0 , 1023 , 0 , SCREENH - PADDLEH) ;
+    int VPADDLE_A = PADDLE_AF - PADDLE_A ; // speed of paddle A
+
+    
+    XBALL += VXBALL ; //x location of the ball 
+    YBALL += VYBALL ; //y location of the ball 
+    
+    //bounce from the top and bottom
+    
+    if (YBALL >= SCREENH - ABALL || YBALL <=0) 
+    {
+    VYBALL *= -1 ; 
+    }
+    
+    //bounce from Paddle A
+    if (XBALL >= PADDLEG && XBALL <= PADDLEG + ABALL && VXBALL < 0) 
+    {
+        if (YBALL > PADDLE_A - ABALL && YBALL < PADDLE_A + PADDLEH)
+        {
+        VXBALL *= -1 ;
+        }
+    }
+    
+    //bounce from the opposite wall   
+    
+    if ( XBALL >= SCREENW )
+    { 
+      VXBALL *= -1 ;
+                }
+            
+            
+   // if the the wall scores
+   
+   if (XBALL <= 0 ) {
+        
+        // reset function //
+        
+        }           
+       
+       }
+       
+ void draw() 
+{  
+   
+
+   fillcircle(XBALL , YBALL , ABALL , 1 ) ;  //draw ball 
+   
+   fillrect( PADDLEG , PADDLE_A , PADDLEW , PADDLEH , 1) ; //draw paddle A
+   
+   
+   }
+
+
+
+        
\ No newline at end of file