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

Committer:
tunagonen
Date:
Wed May 24 15:18:13 2017 +0000
Revision:
11:d812de0e5136
Parent:
ponglib.h@10:6940a3201757
l

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tunagonen 10:6940a3201757 1 #include "libs.h"
tunagonen 10:6940a3201757 2
tunagonen 10:6940a3201757 3 float map(float in, float inMin, float inMax, float outMin, float outMax) {
tunagonen 10:6940a3201757 4 // check it's within the range
tunagonen 10:6940a3201757 5 if (inMin<inMax) {
tunagonen 10:6940a3201757 6 if (in <= inMin)
tunagonen 10:6940a3201757 7 return outMin;
tunagonen 10:6940a3201757 8 if (in >= inMax)
tunagonen 10:6940a3201757 9 return outMax;
tunagonen 10:6940a3201757 10 }
tunagonen 10:6940a3201757 11 else { // cope with input range being backwards.
tunagonen 10:6940a3201757 12 if (in >= inMin)
tunagonen 10:6940a3201757 13 return outMin;
tunagonen 10:6940a3201757 14 if (in <= inMax)
tunagonen 10:6940a3201757 15 return outMax;
tunagonen 10:6940a3201757 16 }
tunagonen 10:6940a3201757 17 // calculate how far into the range we are
tunagonen 10:6940a3201757 18 float scale = (in-inMin)/(inMax-inMin);
tunagonen 10:6940a3201757 19 // calculate the output.
tunagonen 10:6940a3201757 20 return outMin + scale*(outMax-outMin);
tunagonen 10:6940a3201757 21 }
tunagonen 10:6940a3201757 22
tunagonen 10:6940a3201757 23
tunagonen 10:6940a3201757 24 void move() {
tunagonen 10:6940a3201757 25
tunagonen 10:6940a3201757 26 int player1 = Player1 ;
tunagonen 10:6940a3201757 27 int player2 = Player2 ;
tunagonen 10:6940a3201757 28 PADDLE_A = map(player1 , 0 , 1023 , 0 , SCREENH - PADDLEH) ;
tunagonen 10:6940a3201757 29 PADDLE_B = map(player2 , 0 , 1023 , 0 ,SCREENH - PADDLEH) ;
tunagonen 10:6940a3201757 30 int VPADDLE_A = PADDLE_AF - PADDLE_A ; // speed of paddle A
tunagonen 10:6940a3201757 31 int VPADDLE_B = PADDLE_BF - PADDLE_B ; // speed of paddle B
tunagonen 10:6940a3201757 32
tunagonen 10:6940a3201757 33 XBALL += VXBALL ; //x location of the ball
tunagonen 10:6940a3201757 34 YBALL += VYBALL ; //y location of the ball
tunagonen 10:6940a3201757 35
tunagonen 10:6940a3201757 36 //bounce from the top and bottom
tunagonen 10:6940a3201757 37
tunagonen 10:6940a3201757 38 if (YBALL >= SCREENH - ABALL || YBALL <=0)
tunagonen 10:6940a3201757 39 {
tunagonen 10:6940a3201757 40 VYBALL *= -1 ;
tunagonen 10:6940a3201757 41 }
tunagonen 10:6940a3201757 42
tunagonen 10:6940a3201757 43 //bounce from Paddle A
tunagonen 10:6940a3201757 44 if (XBALL >= PADDLEG && XBALL <= PADDLEG + ABALL && VXBALL < 0)
tunagonen 10:6940a3201757 45 {
tunagonen 10:6940a3201757 46 if (YBALL > PADDLE_A - ABALL && YBALL < PADDLE_A + PADDLEH)
tunagonen 10:6940a3201757 47 {
tunagonen 10:6940a3201757 48
tunagonen 10:6940a3201757 49 VXBALL *= -1 ;
tunagonen 10:6940a3201757 50 }
tunagonen 10:6940a3201757 51 }
tunagonen 11:d812de0e5136 52
tunagonen 11:d812de0e5136 53 // bounce from Paddle B
tunagonen 10:6940a3201757 54 if ( XBALL >= SCREENW - PADDLEW - PADDLEG - ABALL && XBALL <= SCREENW - PADDLEG - ABALL && VXBALL >0 )
tunagonen 10:6940a3201757 55 {
tunagonen 10:6940a3201757 56 if (YBALL > PADDLE_B - ABALL && YBALL < PADDLE_B + PADDLEH)
tunagonen 10:6940a3201757 57 {
tunagonen 10:6940a3201757 58 VXBALL *= -1 ;
tunagonen 10:6940a3201757 59 }
tunagonen 10:6940a3201757 60 }
tunagonen 10:6940a3201757 61
tunagonen 10:6940a3201757 62
tunagonen 10:6940a3201757 63 // if someone scores, reset
tunagonen 10:6940a3201757 64
tunagonen 10:6940a3201757 65 if (XBALL >= SCREENW - ABALL || XBALL <= 0 ) {
tunagonen 10:6940a3201757 66
tunagonen 10:6940a3201757 67 // reset function //
tunagonen 10:6940a3201757 68
tunagonen 10:6940a3201757 69 }
tunagonen 10:6940a3201757 70
tunagonen 10:6940a3201757 71 }
tunagonen 10:6940a3201757 72
tunagonen 10:6940a3201757 73 void draw()
tunagonen 10:6940a3201757 74 {
tunagonen 10:6940a3201757 75
tunagonen 10:6940a3201757 76
tunagonen 10:6940a3201757 77 fillcircle(XBALL , YBALL , ABALL , 1 ) ; //draw ball
tunagonen 10:6940a3201757 78
tunagonen 10:6940a3201757 79 fillrect( PADDLEG , PADDLE_A , PADDLEW , PADDLEH , 1) ; //draw paddle A
tunagonen 10:6940a3201757 80
tunagonen 10:6940a3201757 81 fillrect( SCREENW - PADDLEW - PADDLEG , PADDLE_B , PADDLEW , PADDLEH , 1) ; // draw paddle B
tunagonen 10:6940a3201757 82
tunagonen 10:6940a3201757 83 }
tunagonen 10:6940a3201757 84
tunagonen 10:6940a3201757 85
tunagonen 10:6940a3201757 86
tunagonen 10:6940a3201757 87
tunagonen 10:6940a3201757 88
tunagonen 10:6940a3201757 89