We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexDiscussionExhibition › First Project: one/four paddle pong
Page Index Toggle Pages: 1
First Project: one/four paddle pong (Read 4210 times)
First Project: one/four paddle pong
Apr 8th, 2009, 5:09pm
 
Ive recently started using processing. After a few hours of learning basic code I put together a one paddle pong knock-off.

Code:
float ballx = width - 100;
float bally = height / 2;
float yspeed = 6;
float xspeed = 6;
int leftscore = 0;
PFont font;
int x = 0;
int pause;
int tmpscore = leftscore;
int menu = 1;

void setup() {
 size(600, 500, P2D);
 font = loadFont("font.vlw");
 background(0);
 fill(255);
 noStroke();
 

}

void draw() {

 background(0);
 ball();
 paddle();
 action();
 textFont(font, 32);
 textAlign(CENTER);
 text(leftscore, 300, 30);
 if(menu == 1) {
   background(0);
   noLoop();
   textFont(font, 32);
   textAlign(CENTER);
   text("Click to Begin", 300, 250);
   
   
  }
}

void ball() {
 ballx += xspeed;
 bally += yspeed;
 if(ballx <= 0 || ballx >= width - 10) {
   xspeed = -xspeed;
 }
 if(bally <= 0 || bally >= height - 10) {
   yspeed = -yspeed;
 }
 fill(255);
 rect(ballx, bally, 10, 10);
}

void paddle() {
 rectMode(CENTER);
 rect(30, mouseY, 10, 60);
}

void action() {
 if(ballx <= 35) {
   if (bally >= mouseY - 30 && bally <= mouseY) {
     xspeed = -xspeed;
     
     leftscore++;
   }
   if(bally <= mouseY + 30 && bally > mouseY) {
     xspeed = -xspeed;
     leftscore++;
   }
 }
 if(ballx <=20) {
   reset();
 }
}

void reset() {
 ballx = width - 100;
 bally = height /2;
 xspeed = 5;
 yspeed = 5;
 leftscore = 0;
 menu = 1;
}

void keyPressed() {
 if(key == 'p' || key == 'P') {
   if(pause == 0) {
     noLoop();
     pause = 1;
     textFont(font, 32);
     textAlign(CENTER);
     text("PAUSED", 300, 250);
   }
   else {
     loop();
     pause = 0;
   }
 
 }
 if (key == 'r' || key == 'R') {
   reset();
 }
}
void mouseClicked() {
 if(menu==1) {
   loop();
   menu = 0;
 }
}



Then the next day I added three more paddles all controlled by ones cursor. The speed increases at 10, 20, 25, and 30.

Code:
float ballx = 300;
float bally = 250;
float yspeed = 3;
float xspeed = 2.5;
int leftscore = 0;
PFont font;
int x = 0;
int pause;
int tmpscore = leftscore;
int menu = 1;

void setup() {
 size(600, 500, P2D);
 font = loadFont("font.vlw");
 background(0);
 fill(255);
 noStroke();
}


void draw() {
 background(0);
 ball();
 paddles();
 action();
 speedplus();
 numbers();
 if(menu == 1) {
   background(0);
   textFont(font, 32);
   textAlign(CENTER);
   text("Click to Begin", 300, 250);
   noLoop();
   
  }
}


void ball() {
 ballx += xspeed;
 bally += yspeed;
 fill(255);
 rect(ballx, bally, 10, 10);
}


void paddles() {
 rectMode(CENTER);
 rect(30, mouseY, 10, 60);
 rect(mouseX, 30, 60, 10);
 rect(width-30, mouseY, 10, 60);
 rect(mouseX, height-30, 60, 10);
 
}


void action() {
 if(ballx >= 25 && ballx <=35 ) {
   if (bally >= mouseY - 30 && bally <= mouseY + 30) {
     xspeed = -xspeed;
     leftscore++;
   }
 }
 if(ballx <= width-25 && ballx >= width-35) {
   if(bally >= mouseY - 30 && bally <= mouseY + 30){
     xspeed = -xspeed;
     leftscore++;
   }
 }
 if(bally >= 25 && bally <= 35){
   if(ballx >= mouseX - 30 && ballx <= mouseX + 30){
     yspeed = -yspeed;
     leftscore++;
   }
 }
 if(bally >= height-35 && bally < height-25) {
   if(ballx >= mouseX - 30 && ballx <= mouseX + 30){
     yspeed = -yspeed;
     leftscore++;
   }
 }
 if(ballx < 0 || ballx > width || bally < 0 || bally > height) {
   reset();  
 }
}
   
         
void speedplus() {  
 if(leftscore==10){
   if(yspeed==3 && xspeed==2.5){
     yspeed += 0.5;
     xspeed += 0.5;
   }
   if(yspeed == -3 && xspeed==2.5) {
     yspeed += -0.5;
     xspeed += 0.5;
   }
   if(yspeed == 3 && xspeed== -2.5) {
     yspeed += 0.5;
     xspeed += -0.5;
   }
   if(yspeed == -3 && xspeed==-2.5) {
     yspeed += -0.5;
     xspeed += -0.5;
   }
 }
 
 if(leftscore==20){
   if(yspeed==3.5 && xspeed==3){
     yspeed += 1;
     xspeed += 1;
   }
   if(yspeed == -3.5 && xspeed==3) {
     yspeed += -1;
     xspeed += 1;
   }
   if(yspeed == 3.5 && xspeed== -3) {
     yspeed += 1;
     xspeed += -1;
   }
   if(yspeed == -3.5 && xspeed==-3) {
     yspeed += -1;
     xspeed += -1;
   }
 }
 
 if(leftscore==25){
   if(yspeed==4.5 && xspeed==4){
     yspeed += 1.5;
     xspeed += 1.5;
   }
   if(yspeed == -4.5 && xspeed==4) {
     yspeed += -1.5;
     xspeed += 1.5;
   }
   if(yspeed == 4.5 && xspeed== -4) {
     yspeed += 1.5;
     xspeed += -1.5;
   }
   if(yspeed == -4.5 && xspeed==-4) {
     yspeed += -1.5;
     xspeed += -1.5;
   }
 }
 
 if(leftscore==30){
   if(yspeed==6 && xspeed==5.5){
     yspeed += 2.5;
     xspeed += 2.5;
   }
   if(yspeed == -6 && xspeed==5.5) {
     yspeed += -2.5;
     xspeed += 2.5;
   }
   if(yspeed == 6 && xspeed== -5.5) {
     yspeed += 2.5;
     xspeed += -2.5;
   }
   if(yspeed == -6 && xspeed==-5.5) {
     yspeed += -2.5;
     xspeed += -2.5;
   }
 }  
}    


void reset() {
 ballx = 300;
 bally = 250;
 xspeed = 2.5;
 yspeed = 3;
 leftscore=0;
 menu=1;
 
}


void keyPressed() {
 if(key == 'p' || key == 'P') {
   if(pause == 0) {
     noLoop();
     pause = 1;
     textFont(font, 32);
     textAlign(CENTER);
     text("PAUSED", 300, 250);
   }
   else {
     loop();
     pause = 0;
   }
 
 }
 if (key == 'r' || key == 'R') {
   reset();
 }
}


void mouseClicked() {
 if(menu==1) {
   loop();
   menu = 0;
 }
}

void numbers() {
 textFont(font, 48);
 textAlign(CENTER);
 text(leftscore, 300, 250);
}
 





tell me what you think
Re: First Project: one/four paddle pong
Reply #1 - Apr 8th, 2009, 6:59pm
 
its always great if you have the chance to upload it somewhere... much easier to test
Re: First Project: one/four paddle pong
Reply #2 - Apr 9th, 2009, 2:56pm
 
as in the applet? i dont know where or how to go about that but ill look into it
Re: First Project: one/four paddle pong
Reply #3 - Apr 9th, 2009, 3:19pm
 
alright its up on openprocessing
Re: First Project: one/four paddle pong
Reply #4 - Apr 9th, 2009, 3:19pm
 
just making a fifth message so i can add the link
Re: First Project: one/four paddle pong
Reply #5 - Apr 9th, 2009, 3:20pm
 
Re: First Project: one/four paddle pong
Reply #6 - Apr 9th, 2009, 3:58pm
 
thats cool, i like it.... at first i couldnt get much more than 5-6 points. But you just have to follow the ball with your mouse. it works pretty good.
Re: First Project: one/four paddle pong
Reply #7 - Apr 9th, 2009, 5:54pm
 
Woo, I got to 11... had me sucked in for a few minutes Smiley  Sometimes if you catch the ball an the line edge, it increments the number more than 1.  I had it jump from 1 to 21 one time.
Re: First Project: one/four paddle pong
Reply #8 - Apr 10th, 2009, 2:43pm
 
yea its a glitch. i might get around to fixing it soon Tongue
Re: First Project: one/four paddle pong
Reply #9 - Apr 10th, 2009, 2:51pm
 
That was a good game. It's very hard to play when the ball is headed towards a corner, making me think if it is possible to put a few paddles around a circle?? Roll Eyes
Re: First Project: one/four paddle pong
Reply #10 - Apr 10th, 2009, 3:31pm
 
i did start to ponder how one would go about doing so.
Re: First Project: one/four paddle pong
Reply #11 - Jun 13th, 2009, 12:02pm
 
Very nice! Smiley
Re: First Project: one/four paddle pong
Reply #12 - Jun 14th, 2009, 6:08am
 
My palms started getting hairy. :/  I did not play further to see if I would become blind.

But, nice.  Have you seen "PongOut?"
http://www.allgamesallfree.com/games1079-pongout.html
Page Index Toggle Pages: 1