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.
Page Index Toggle Pages: 1
Pong! (Read 1066 times)
Pong!
Oct 23rd, 2009, 8:53am
 
Hi there ! Smiley

I have created a very well known program : the game Pong !

But I only started to use Processing since 1.5 months so I need help.

In fact my rectangles move with keys 's' , 'z' , DOWN , BOTTOM. But when the player on the left use 's' or 'z' , the player on the right can't use DOWN or BOTTOM. Sad So I would like to know a solution for this problem.

I thought that the player on the right can use the mouse, and I think it works, but I prefer to use only the keys.

You can see in red the part of my program where we can use the rectangles (all the text in french isn't important ^^) :

_________________________________________________________


void setup() {
 size(800,400);
 println("Bienvenue dans Pong ! La partie se joue en 5 points !");
 println("A la fin d'une manche il suffit d'appuyer sur M pour");
 println("continuer avec la manche suivante ! Bonne chance !");
}

// Le super random servira à faire varier le y de l'ellipse:
int superRandom() {
 return (int) random(8) - 4;
}

int y1 = 160, y2 = 160, x = 28, y = 200, igor = 0, n = 0, j1 = 0, j2 = 0;

void draw() {
 background(255);
 smooth();
 line(400,0,400,800);
 fill(0);
 rect(5,y1,15,80);
 rect(780,y2,15,80);
 // Gestion des rectangles noirs:
 if (keyPressed) {
   if (keyCode == DOWN && y2 < 320) y2 = y2 + 4;
   if (keyCode == UP && y2 > 0) y2 = y2 - 4;
   if (key == 'z' && y1 > 0) y1 = y1 - 4;
   if (key == 's' && y1 < 320) y1 = y1 + 4;
 }

 // On desine la jolie ellipse:
 ellipse(x,y,20,20);
 // Gestion de fin de manche ou de la partie:
 if (x <= 5) {
   x = -50;
   if (igor == 2) {
     j2++;
     println("Joueur de gauche tu as perdu !");
     println("Points : "+j1+" (J. de gauche) à "+j2+" (J. de droite) !");
     if (j2 == 5) {
       println("J. de droite a gagné la partie ! Bravo !");
       stop();
     }
   }
   igor = 0;
   if (keyPressed) {
     if (key == 'm') {
       igor = 1;
       x = 50;
     }
   }
 }
 if (x >= 795) {
   x = 850;
   if (igor == 1) {
     j1++;
     println("Joueur de droite tu as perdu !");
     println("Points : "+j1+" (J. de gauche) à "+j2+" (J. de droite) !");
     if (j1 == 5) {
       println("J. de gauche a gagné la partie ! Bravo !");
       stop();
     }
   }
   igor = 0;
   if (keyPressed) {
     if (key == 'm') {
       igor = 2;
       x = 750;
     }
   }
 }
 // Cas où l'ellipse touche le rectangle noir de gauche:
 if (x <= 28 && y >= y1 && y <= y1 + 80 && x != 850) {
  igor = 1;
  n = superRandom();
 }
 // Ensuite x doit aller vers la droite:
 if (igor == 1) {
   if (y <= 10) n = superRandom() + 4;
   if (y >= 390) n = superRandom() - 4;
   if (abs(y) <= 2) x = x + 8;
   else x = x + 5;
   y = y + n;
 }
 // Cas où l'ellipse touche le rectangle noir de droite:
 if (x >= 772 && y >= y2 && y <= y2 + 80 && x != 850) {
  igor = 2;
  n = superRandom();
 }
 // Ensuite x doit aller vers la gauche:
 if (igor == 2) {
   if (y <= 10) n = superRandom() + 4;
   if (y >= 390) n = superRandom() - 4;
   if (abs(y) <= 2) x = x - 8;
   else x = x - 5;
   y = y + n;
 }
}

_________________________________________________________

That's all !  Cheesy

Thank you !
Re: Pong!
Reply #1 - Oct 23rd, 2009, 10:09am
 
I remember somebody wroting a little piece of code for that problem. You can do this by using booleans. I believe it was BenHem, but i cant find his post anymore.
Re: Pong!
Reply #2 - Oct 23rd, 2009, 11:55am
 
Looks like the OP in that thread deleted their post, so it has its own thread now!

http://processing.org/discourse/yabb2/num_1255215840.html

Go Ye Forth and Keymash
Re: Pong!
Reply #3 - Oct 23rd, 2009, 11:55am
 
This demonstrate the use of booleans with keyPressed and keyReleased functions. (On ne peut être plus clair!)

Code:
boolean leftPlayerUp, leftPlayerDown, rightPlayerUp, rightPlayerDown;

void setup() {
size(800,400);
println("Bienvenue dans Pong ! La partie se joue en 5 points !");
println("A la fin d'une manche il suffit d'appuyer sur M pour");
println("continuer avec la manche suivante ! Bonne chance !");
}

// Le super random servira à faire varier le y de l'ellipse:
int superRandom() {
return (int) random(8) - 4;
}

int y1 = 160, y2 = 160, x = 28, y = 200, igor = 0, n = 0, j1 = 0, j2 = 0;

void draw() {
background(255);
smooth();
line(400,0,400,800);
fill(0);
rect(5,y1,15,80);
rect(780,y2,15,80);
// Gestion des rectangles noirs:
if (rightPlayerDown && y2 < 320) y2 = y2 + 4;
if (rightPlayerUp && y2 > 0) y2 = y2 - 4;
if (leftPlayerUp && y1 > 0) y1 = y1 - 4;
if (leftPlayerDown && y1 < 320) y1 = y1 + 4;
// On desine la jolie ellipse:
ellipse(x,y,20,20);
// Gestion de fin de manche ou de la partie:
if (x <= 5) {
  x = -50;
  if (igor == 2) {
    j2++;
    println("Joueur de gauche tu as perdu !");
    println("Points : "+j1+" (J. de gauche) à "+j2+" (J. de droite) !");
    if (j2 == 5) {
println("J. de droite a gagné la partie ! Bravo !");
stop();
    }
  }
  igor = 0;
  if (keyPressed) {
    if (key == 'm') {
igor = 1;
x = 50;
    }
  }
}
if (x >= 795) {
  x = 850;
  if (igor == 1) {
    j1++;
    println("Joueur de droite tu as perdu !");
    println("Points : "+j1+" (J. de gauche) à "+j2+" (J. de droite) !");
    if (j1 == 5) {
println("J. de gauche a gagné la partie ! Bravo !");
stop();
    }
  }
  igor = 0;
  if (keyPressed) {
    if (key == 'm') {
igor = 2;
x = 750;
    }
  }
}
// Cas où l'ellipse touche le rectangle noir de gauche:
if (x <= 28 && y >= y1 && y <= y1 + 80 && x != 850) {
 igor = 1;
 n = superRandom();
}
// Ensuite x doit aller vers la droite:
if (igor == 1) {
  if (y <= 10) n = superRandom() + 4;
  if (y >= 390) n = superRandom() - 4;
  if (abs(y) <= 2) x = x + 8;
  else x = x + 5;
  y = y + n;
}
// Cas où l'ellipse touche le rectangle noir de droite:
if (x >= 772 && y >= y2 && y <= y2 + 80 && x != 850) {
 igor = 2;
 n = superRandom();
}
// Ensuite x doit aller vers la gauche:
if (igor == 2) {
  if (y <= 10) n = superRandom() + 4;
  if (y >= 390) n = superRandom() - 4;
  if (abs(y) <= 2) x = x - 8;
  else x = x - 5;
  y = y + n;
}
}

void keyPressed() {
 if(key == 'z') leftPlayerUp = true;
 if(key == 's') leftPlayerDown = true;
 if(keyCode == UP) rightPlayerUp = true;
 if(keyCode == DOWN) rightPlayerDown = true;  
}

void keyReleased() {
 if(key == 'z') leftPlayerUp = false;
 if(key == 's') leftPlayerDown = false;
 if(keyCode == UP) rightPlayerUp = false;
 if(keyCode == DOWN) rightPlayerDown = false;  
}


PS: Si tu veux améliorer ton Pong, n'hésite pas à demander.
Re: Pong!
Reply #4 - Oct 24th, 2009, 5:34am
 
Thank you very much for the help !

To Nicolas ATEK:

En effet je pensais faire un Pong contre l'ordinateur aussi, puis avec plus d'effets et en plus joli ! Je demanderai si j'ai un autre problème alors. Merci !
Page Index Toggle Pages: 1