New in Processing PONG question (i dug up a lot, could not find an answer, so I'm asking for help!)
in
Programming Questions
•
2 years ago
Hi everyone,
I'm new to Processing, and started programming a little Pong Game. My name is Antonio and I'm portuguese (the code comments are in portuguese, but the code itself is still very simple, I think you guys can understand it easily).
I started a Processing workshop recently, but very few hours, and could use your help.
Here's what I've come up with so far:
//variáveis BOLA
float ballX = 20;
float ballY = 20;
float ballDirX = 10;
float ballDirY = 10;
float direction = 0;
//variáveis PADS
float pad1Y;
float pad2Y;
float pad1;
void setup(){
size(800,400);
smooth();
frameRate(25);
}
void draw(){
background(255);
fill(50,50,255);
//direction = 5;
//PAD 1
rect(0,mouseY,20,80);
//PAD 2
rect(780,pad2Y,20,80);
//Bola toca no Pad1, inverte a direcção)
//Bola toca no Pad2, inverte a direcção)
// desenhar a bola
ellipse(ballX,ballY,25,25);
// se a bola chega ao máximo de X, volta para trás
if(ballX > 800){
ballDirX = - ballDirX;
}
// se a bola chega ao minimo de X, vai para a frente
if (ballX < 0) {
ballDirX = - ballDirX;
}
// o idêntico, agora para o Y, com as suas coordenadas de 0 a 400
if (ballY > 400){
ballDirY = - ballDirY;
}
// segunda parte da posição da bola em Y
if(ballY < 0){
ballDirY = - ballDirY;
}
//movimento/equação base da bola, em X e Y
ballX = ballX + ballDirX;
ballY = ballY + ballDirY;
}
void keyPressed() {
println(keyCode);
if (keyCode == 38){
pad2Y -= 18 ;
}
if (keyCode == 40){
pad2Y += 18;
}
}
Well, player one uses mouse, player 2 uses keyboard, the ball bounces off the four corners and such, but I wanted to know how to make the ball bounce off the paddles, I'm getting a lot of trouble trying to get it to work, and for now, without sucess. Please Help! Thanks.
1