# how to increase the speed of the ball when it hits the edge of a paddle and then return back to original speed?

 Refresh February 2019 Views 4 time
1

how to increase the speed of the ball when it hits the edge of a paddle and then return back to original speed? In this pong game, I want my ball to increase its speed when it hits the edge of a paddle and then return back to its normal speed when it hits the middle of the paddle. i commented a few "if statements" because i tried with them and they did not work. The language I am using is "processing"

//size of the ball
int ballSize;

//position of the ball
float ballPositionX;
float ballPositionY;

//speed of the ball
float ballSpeedX, ballSpeedY;

//score of keyboard
int keyScore = 0;

//score of mouse
int mouseScore = 0;

//movement of the ball
boolean moveDown = true;
boolean moveRight = true;

//gameover
boolean gameOver, moveNewSpeed;

final int MIN_SPEED = 3;
final int MAX_SPEED = 6;

void setup () {

size (500, 500);
background (#FF7C00);
frameRate (60);
ballPositionX = width/2;
ballPositionY = height/2;

ballSpeedX = 3;
ballSpeedY = 3;

ballSize = 20;

keyScore = 0;
mouseScore = 0;

}

void draw () {
background (#FF7C00);
drawGame ();
bounce ();
scoreCount ();
gameover ();
canvasBounce ();

if (keyPressed) {

if (keyCode == UP) {

}

}*/

if (keyCode == DOWN) {

}
}

}*/

if (mousePressed) {

if (mouseButton == LEFT ) {

}

if (mouseButton == RIGHT ) {

}
}
}//VOID BRAC
void drawGame () {

drawScore ();
defaultBall ();

//the ball
fill ( 255);
strokeWeight (2);
ellipse (ballPositionX, ballPositionY, ballSize, ballSize);

fill (255);
strokeWeight (0.8);
//the left paddle, which is controlled by the keyboard

//the right paddle, which is controlled by the mouse
}

void drawScore() {

textSize(20);

String toPrint = "Keyboard: " + keyScore;

text(toPrint, width/4-textWidth(toPrint)/2, 50);

toPrint = "Mouse: "+ mouseScore;

text(toPrint, width*3/4-textWidth(toPrint)/2, 50);
}

/*this funtion puts the ball back into the centre of the screen when it fails to hit either
the paddle or the top or the bottom of the screen
*/
void defaultBall () {

if ((ballPositionX < -ballSize/2) || (ballPositionX > width + ballSize)) {

ballPositionX = width/2;
ballPositionY = height/2;
}
}

void scoreCount () {

if (ballPositionX  < -ballSize/2) {

mouseScore = mouseScore + 1;
}

if (ballPositionX > width + ballSize) {

keyScore =  keyScore + 1;
}
}

void bounce () {
//bouncing off the top and bottom of the screen
if (moveDown) {

ballPositionY += MIN_SPEED +(sin(QUARTER_PI));
} else {

ballPositionY -= MIN_SPEED + (sin(QUARTER_PI));
}

if (moveRight) {

ballPositionX += MIN_SPEED + (sin(HALF_PI + QUARTER_PI));

} else {

ballPositionX -= MIN_SPEED + (sin(-PI));
}

/*if (moveNewSpeed) {

ballPositionX = ballPositionX + MAX_SPEED + (sin(QUARTER_PI));
} else {

ballPositionX = ballPositionX - MAX_SPEED + (cos(QUARTER_PI));
}
*/
}

moveRight = true;

}

moveRight = false;

}

moveNewSpeed  = false;

}

moveNewSpeed = true;

}

*/

}

void canvasBounce () {

if (ballPositionY < ballSize/2) {

moveDown = true;
}

if (ballPositionY > height - ballSize/2) {

moveDown = false;
}

}

void gameover () {

if (gameOver) {

ballSpeedX = 0;
ballSpeedY = 0;
ballPositionX = width/2;
ballPositionY = height/2;

textSize(50);

fill (random(255), random(255), random (255));

String toPrint = "GAME OVER!!";

text(toPrint, width/2-textWidth(toPrint)/2, height/2);
}

if (keyScore == 11 || mouseScore == 11) {

gameOver = true;
} else {

gameOver = false;
}
}

----------
##

----------

##