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

2manpong.h

Committer:
tunagonen
Date:
2017-05-24
Revision:
11:d812de0e5136
Parent:
ponglib.h@ 10:6940a3201757

File content as of revision 11:d812de0e5136:

#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 
   
   }