
//The ball class
class Ball {
float r; // radius
float x,y;
float xspeed,yspeed;
// Constructor that makes it easy to increase or decrease ball speed to alter difficulty
Ball(float ballhori, float ballvert) {
x = 200;
y = 350;
xspeed = ballhori;
yspeed = ballvert;
}
//Controls ball speed
void move() {
x += xspeed;
y += yspeed;
// Check horizontal edges
if (x > width + 5 || x < 0 - 5) {
xspeed *= - 1;
}
//Resets the ball if it glitches out or scores a point
if (x > width + 30 || x < 0 - 30 || y > height || y < 0) {
x = random(300,500);
y = random(350);
}
}
void score() {
fill(0, 102, 153);
text("Pongout!", 145, 150);
text("Top: " + int(scoretop) + " / Bottom:" + int(scorebot), 75, 500);
if (y > height-3) {
scoretop = scoretop + 1; }
if (y < 3) {
scorebot = scorebot + 1; }
}
//Bounces the ball when it hits a normal bumper
void bounce() {
xspeed *= -1.004;
yspeed *= -1;
}
//Bounces the ball when it hits the middle bumper.
void rbounce() {
xspeed *= random(-1.0010, -0.96);
}
//Displays the ball
void display() {
r = 15;
stroke(0);
fill(0);
ellipse(x,y,r*2,r*2);
}
}
//Bumper class
class Obstacle {
float yposob, xspeeob, xposob, rob;
/*Constructor that lets you position bumpers where you want
them and change their speed*/
Obstacle(float yposob2, float xspeeob2) {
xspeeob = xspeeob2;
yposob = yposob2;
}
//Moves the bumpers
void move() {
xposob = xposob + xspeeob;
// Check horizontal edges
if (xposob > width || xposob < 0) {
xspeeob *= - 1;
}
}
//Shows the bumpers
void display() {
rob = 20;
rectMode(CENTER);
fill(23,109,131);
rect(xposob,yposob,rob*2,rob);
}
//Checks to see if a bumper is touching the ball
boolean intersect(Ball a) {
float distance = dist(xposob,yposob,a.x,a.y);
if (distance < rob + a.r -10 ) {
return true;
} else {
return false;
}
}
// What the ball does when it hits a bumper
void destroyblock() {
xposob = -900;
xspeeob *= 0;
}
}
//The bottom Paddle
class Paddle {
float r,xpos,ypos,xspee,s;
/*Constructor that allows you to change the paddle size
and speed for difficulty*/
Paddle(float psize, float xspee) {
s = xspee;
r = psize;
}
//Moves the Paddle
void move() {
if (mousePressed == true) {
xpos += s;
if (xpos > width || xpos < 0) {
xpos = 0;
}
}
}
//Shows the Paddle
void display() {
ypos = 650;
rectMode(CENTER);
fill(0);
rect(xpos,ypos,r*2,r);
}
//Checks to see if the paddle is touching the ball.
boolean intersect(Ball a) {
float distance = dist(xpos,ypos,a.x,a.y);
if (distance < r + a.r -10 ) {
return true;
} else {
return false;
}
}
}
//Player Two Paddle
class Paddletw {
float rtw,xpostw,ypostw,xspeetw,stw;
/*Constructor that allows you to change the paddle size
and speed for difficulty*/
Paddletw(float psizetw, float xspeetw) {
stw = xspeetw;
rtw = psizetw;
}
//Shows the paddle
void display() {
ypostw = 50;
rectMode(CENTER);
fill(0);
rect(xpostw,ypostw,rtw*2,rtw);
if (xpostw > width + 20 || xpostw < 0 - 20){
xpostw = 200;}
}
//Moves the Paddle with the left and right arrow keys
void Left(){
xpostw = xpostw - 4; }
void Right(){
xpostw = xpostw + 4; }
void keyPressed() {
if(key == CODED) {
if(keyCode==LEFT){
gameptw.Left(); }
else if(keyCode==RIGHT){
gameptw.Right();}
}
}
//Checks if paddle is touching ball.
boolean intersect(Ball a) {
float distance = dist(xpostw,ypostw,a.x,a.y);
if (distance < rtw + a.r -10 ) {
return true;
} else {
return false;
}
}
}
//Reginald M. Watkins
//NMD 102
//Breakout game
/* This is my version of the game Breakout. The normal version has been done to death; so I tried to spice it up a bit by making it more intense.
The blocks move, the ball moves at varying speeds depending on the type of block it hits, and the game is now two player. Controls are simple
Clicking the mouse controls the bottom bumper. Hold it to make the bumper move to the right, and when it reaches the end of the screen it will reset.
It's recommended for the player to reset each time they hit the ball for a better position. This is the harder paddle and is recommended for the more
experienced gamer.
The arrow keys control the top bumper. Left and right move it in the respective directions, and up and down stop it. Going off screen resets the bumper
to the middle, which can be a handy trick. Most players find this paddle easier to control and is recommended for the less experienced gamer.*/
//Declaring
float scorebot;
float scoretop;
float y;
Paddle gamep;
Ball ball;
Paddletw gameptw;
//Moving half blocks
Obstacle obs;
Obstacle obs2;
Obstacle obs3;
Obstacle obs4;
//Moving top blocks
Obstacle obs5;
Obstacle obs6;
Obstacle obs7;
Obstacle obs8;
//Moving bottom blocks
Obstacle obs9;
Obstacle obs10;
Obstacle obs11;
Obstacle obs12;
ObstacleW obsw;
PFont font;
//Initializing
void setup () {
size(400,700);
gamep = new Paddle(30,7);
gameptw = new Paddletw(30,5);
ball = new Ball(4,4);
//Moving Half Blocks
obs = new Obstacle(175,3);
obs2 = new Obstacle(525,3);
obs3 = new Obstacle(300,6);
obs4 = new Obstacle(400,6);
//Moving top blocks
obs5 = new Obstacle(325,8);
obs6 = new Obstacle(325,4.5);
obs7 = new Obstacle(325,5.5);
obs8 = new Obstacle(325,9);
//Moving bottom blocks
obs9 = new Obstacle(375,8);
obs10 = new Obstacle(375,4.5);
obs11 = new Obstacle(375,5.5);
obs12 = new Obstacle(375,9);
obsw = new ObstacleW(350,7);
//Font loading
PFont font;
font = loadFont("AngsanaNew-Italic-48.vlw");
textFont(font);
}
void draw() {
//Draws the scenic background, looks more like a tennis field
background(5,10,54);
rectMode(CORNER);
fill(44,10,72);
rect(0,0,400,65);
rect(0,635,400,65);
rect(0,320,400,60);
fill(0);
rect(0,350,400,2);
//Player One Paddle
gamep.display();
gamep.move();
//Player Two Paddle
gameptw.display();
gameptw.keyPressed();
//The Ball
ball.score();
ball.display();
ball.move();
/*The bumpers that send the ball back where it came with a
very small boost in speed. */
//Moving middle blocks
obs.display();
obs.move();
obs2.display();
obs2.move();
obs3.display();
obs3.move();
obs4.display();
obs4.move();
//Fast moving north blocks
obs5.display();
obs5.move();
obs6.display();
obs6.move();
obs7.display();
obs7.move();
obs8.display();
obs8.move();
//Fast moving south blocks
obs9.display();
obs9.move();
obs10.display();
obs10.move();
obs11.display();
obs11.move();
obs12.display();
obs12.move();
/*The middle bumper that can either slow down or speed up the ball*/
obsw.display();
obsw.move();
//Code to bounce the ball back when it hits a bumper.
if (gamep.intersect(ball)) {
ball.bounce(); }
if (gameptw.intersect(ball)) {
ball.bounce(); }
if (obs.intersect(ball)) {
ball.bounce(); }
if (obs2.intersect(ball)) {
ball.bounce(); }
if (obs3.intersect(ball)) {
ball.bounce(); }
if (obs4.intersect(ball)) {
ball.bounce(); }
if (obs5.intersect(ball)) {
ball.bounce(); }
if (obs6.intersect(ball)) {
ball.bounce(); }
if (obs7.intersect(ball)) {
ball.bounce(); }
if (obs8.intersect(ball)) {
ball.bounce(); }
if (obs9.intersect(ball)) {
ball.bounce(); }
if (obs10.intersect(ball)) {
ball.bounce(); }
if (obs11.intersect(ball)) {
ball.bounce(); }
if (obs12.intersect(ball)) {
ball.bounce(); }
//Block Destroying
if (obs.intersect(ball)) {
obs.destroyblock(); }
if (obs2.intersect(ball)) {
obs2.destroyblock(); }
if (obs3.intersect(ball)) {
obs3.destroyblock(); }
if (obs4.intersect(ball)) {
obs4.destroyblock(); }
if (obs5.intersect(ball)) {
obs5.destroyblock(); }
if (obs6.intersect(ball)) {
obs6.destroyblock(); }
if (obs7.intersect(ball)) {
obs7.destroyblock(); }
if (obs8.intersect(ball)) {
obs8.destroyblock(); }
if (obs9.intersect(ball)) {
obs9.destroyblock(); }
if (obs10.intersect(ball)) {
obs10.destroyblock(); }
if (obs11.intersect(ball)) {
obs11.destroyblock(); }
if (obs12.intersect(ball)) {
obs12.destroyblock(); }
if (obsw.intersect(ball)) {
ball.rbounce();}
}
//Middle Obstacle
class ObstacleW {
float yposw, xspeew, xposw, rw;
/*Constructor that allows you to change the obstacle size
and speed for difficulty*/
ObstacleW(float yposw2, float xspeew2) {
xspeew = xspeew2;
yposw = yposw2;
}
//Moves the center obstacle
void move() {
xposw = xposw + xspeew;
// Check horizontal edges
if (xposw > width || xposw < 0) {
xspeew *= - 1;
}
}
//Shows the middle paddle
void display() {
rw = 20;
rectMode(CENTER);
fill(125);
rect(xposw,yposw,rw*2,rw);
}
//Checks to see if the middle paddle is touching the ball.
boolean intersect(Ball a) {
float distance = dist(xposw,yposw,a.x,a.y);
if (distance < rw + a.r -10 ) {
return true;
} else {
return false;
}
}
}
OpenProcessing is an online community platform devoted to sharing and discussing Processing sketches in a collaborative, open-source environment.
Download Processing
Terms of Service
To contact, send an email to:

See the feedback forum and vote!
Follow OpenProcessing on Twitter.
All sketches are licensed under Creative Commons Attribution-Share Alike 3.0.
Syntax highlighting and Processing brush under LGPL 3.
All the source code is licensed under Creative Commons GNU GPL.
Comments engine by Scriptsmill Comments Script.
