You are viewing an older revision! See the latest version
Pong
Introduction¶
The pong is a simple single player game which is controlled by the capacitive touch on the FRDM-KL46Z microcontroller. It has a library to use two main functions which are draw and move. The move function is to calculate the location and the velocity of the ball and the paddle. The draw functions is to draw the paddle and the ball while playing the game. The main pong script is to loop those two functions from the library.
Library¶
<<singlepong.h>>
- include "mbed.h" << / #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 player = location_player ; ; PADDLE_A = map(player , 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
}