Hi there !
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.
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 !
Thank you !