Ping Pong Game Using Processing
Introduction to processing
Processing is an open-source development environment and programming language, designed for learning how to code. It’s very flexible and powerful and is often used in the realm of visual arts. Processing was created by Ben Fry and Casey Reas in 2001. With the help of Dan Shiffman, they continue to lead a team of volunteer developers to enhance and maintain the project. As a participant in Google’s Summer of Code, Gottfried Haider worked on Raspberry Pi support and the GPIO libraries
Ping pong game using processing code
In this game, the task of the player is to prevent the ball from falling down by obstructing the path of the ball using the paddle. This is similar to the physical game of Table Tennis. Currently this is basic Pong having paddle on one side. Soon we will try to update this game by providing players on both sides which is called “Advanced Ping Pong”
Coding
int base=40;
int x,y,gameScore=0;
int changeX=-5;
int changeY=-5;
int gameOver=0;
void setup()
{
size(760, 640);
x=(int)random(width);
y=height-base;
}
void draw()
{
if(gameOver==0)
{
background(0);
text("SCORE:"+gameScore+"00",width/2,height/2);
rect(mouseX,height-base,200,base);
ellipse(x,y,10,10);
x=x+changeX;
y=y+changeY;
if(x<0 | x>width)
{
changeX=-changeX;
}
if(y<0)
{
changeY=-changeY;
}
if(y>height-base)
{
//check whether it is falling inside the rectangle or not
if(x>mouseX && x<mouseX+200)
{
changeY=-changeY; //bounce back
gameScore++;
}
else
{
gameOverSplash();
}
}
}
else
{
background(100,100,200);
text("Game Over!",width/2,height/2);
text("CLICK TO RESTART",width/2,height/2+20);
}
}
void gameOverSplash()
{
gameOver=1;
}
void mouseClicked()
{
changeY=-changeY;
gameScore=0;
gameOver=0;
}