Pong with the score! You need to create a font and have it in the data folder (in this example I created "Dialog-24.vlw")


float ballx = 150;
float bally = 200;
float speedx = 2.7;
float speedy = 0.3;
float player1x = 570;
float player1y = 200;
float player2x = 30;
float player2y = 200;
boolean p1xbutton = false; // booleans are variables that are either true or false.
boolean p1ybutton = false;
boolean p2xbutton = false;
boolean p2ybutton = false;
PFont font;
int score1 = 0;
int score2 = 0;
void setup(){
size(600, 400);
font = loadFont("Dialog-24.vlw");
}
void draw (){
fill (250);
textFont(font, 24);
background (50);
smooth();
text(score1, 40, 20);
text(score2, 560, 20);
ballx = ballx + speedx; // calculate ball position
bally = bally + speedy;
collision(); // check to see if ball has collided with an edge or paddle
if (keyPressed) {
if (key == 'j'){
p1xbutton = true; // check for button and chandges a boolean array if it has been pressed
}
if (key == 'm'){
p1ybutton = true;
}
if (key == 'a'){
p2xbutton = true;
}
if (key == 'z'){
p2ybutton = true;
}
}
if (p1xbutton == true){
player1y = player1y - 6;
}
if (p1ybutton == true){
player1y = player1y + 6;
}
if (p2xbutton == true){
player2y = player2y - 6;
}
if (p2ybutton == true){
player2y = player2y + 6;
}
rect(player1x, player1y, 10, 50);
rect(player2x, player2y, 10, 50);
ellipse (ballx, bally, 10, 10);
}
void collision(){
if (((ballx > player1x) && (ballx < player1x + 10)) && ((bally > player1y) && (bally < player1y + 50))){ // if it hits player 1 paddle
if (p1xbutton == true){ // if the paddle is going up puts up "spin" on the ball
speedy -= .5 ;
}
if (p1ybutton == true){// down "spin"
speedy += .5;
}
ballx = player1x - 1; // ensures it won't hit twice!
speedx = speedx * -1; // go back
}
if (((ballx < player2x+ 10) && (ballx > player2x)) && ((bally > player2y) && (bally < player2y + 50))){ // if it hits player 2 paddle
if (p2xbutton == true){ // up "spin"
speedy -= .5 ;
}
if (p2ybutton == true){// down "spin"
speedy += .5;
}
ballx = player2x + 11; // ensures it won't hit twice!
speedx = speedx * -1; // go back
}
if (speedy < -4){// keeps the ball from going too far up and down.
speedy = -4;
}
else if (speedy > 4){
speedy = 4;
}
if (ballx > width){
reset(1); // runs the routine reset below and passes the value 1
}
if (bally > height){
speedy = speedy * -1;
}
if (bally < 0){
speedy = speedy * -1;
}
if (ballx < 0){
reset(2); // runs the routine reset below and passes the value 2
}
}
void keyReleased(){
if (key == 'j'){ // check for button released
p1xbutton = false; // changes the boolean to false, letting the computer know that j is no longer being pressed
}
if (key == 'm'){
p1ybutton = false;
}
if (key == 'a'){
p2xbutton = false;
}
if (key == 'z'){
p2ybutton = false;
}
}
void reset(int plyr){
if (plyr == 1){
score1 ++;
}
if (plyr == 2){
score2 ++;
}
ballx = 300;
bally = 200;
if(random(2) == 2) { // randomly changes the direction when it restarts
speedx = 2.7;
}
else{
speedx = - 2.7;
}
speedy = (random (.6) - .3);
delay (1000); // pauses at the end for a second
}