I’m going to show the code of what I’ve done and then I’ll ask questions one at a time.
//pong
int point = 0;
Player player;
PlayerCpu playerCpu;
Ball ball;
void settings(){
size(640,480,P2D);
noSmooth();
}
void setup(){
frameRate(60);
player = new Player();
playerCpu = new PlayerCpu();
ball = new Ball();
}
void draw(){
//fondo-----------------
background(0,0,0);
//texto-----------------
textSize(24);
textAlign(CENTER);
fill(color(255,255,255));
text("Puntos: "+point,320,32);
//objetos-------------------
player.Update();
playerCpu.Update();
ball.Update();
}
//funcion para colisiones--------------------------------
boolean collisionRect(float x1,float y1,float w1,float h1,float x2,float y2,float w2,float h2){
return x1 + w1 > x2 && x1 < x2 + w2 && y1 + h1 > y2 && y1 < y2 + h2;
}
public class Ball{
private float x,y,w,h;
private int speedX,speedY;
private color colors;
private boolean live;
public Ball(){
this.w = 14;
this.h = 14;
this.x = 320 - this.w / 2;
this.y = 240 - this.h / 2;
this.speedX = 2;
this.speedY = 2;
this.colors = color(255,255,255);
this.live = true;
}
public void Update(){
if(live == true){
Move();
Bounce();
Collision();
Draw();
}
}
private void Draw(){
fill(colors);
rect(x,y,w,h);
}
private void Move(){
x += speedX;
y += speedY;
}
private void Bounce(){
if(x <= 0 || x >= 640){
speedX *= -1;
}
if(y <= 0 || y >= 480){
speedY *= -1;
}
}
private void Collision(){
//player
if(collisionRect(x,y,w,h,player.x,player.y,player.w,player.h)){
speedX *= -1;
//speedX = abs(speedX);
}
//playeCpu
if(collisionRect(x,y,w,h,playerCpu.x,playerCpu.y,playerCpu.w,playerCpu.h)){
speedX *= -1;
}
}
}
public class Player{
private float x,y,w,h;
private int speed;
private color colors;
private boolean live;
public Player(){
this.w = 20;
this.h = 100;
this.x = 32 - this.w / 2;
this.y = 240 - this.h / 2;
this.speed = 5;
this.colors = color(255,0,204);
this.live = true;
}
public void Update(){
if(live == true){
Move();
Draw();
}
}
private void Draw(){
fill(colors);
rect(x,y,w,h);
}
private void Move(){
if(keyPressed && key == CODED && keyCode == UP && y > 16){
y -= speed;
}else if(keyPressed && key == CODED && keyCode == DOWN && y < 370){
y += speed;
}
}
}//fin clase player
public class PlayerCpu{
private float x,y,w,h;
private int speed;
private color colors;
private boolean live;
public PlayerCpu(){
this.w = 20;
this.h = 100;
this.x = 608 - this.w / 2;
this.y = 240 - this.h / 2;
this.speed = 5;
this.colors = color(255,204,0);
this.live = true;
}
public void Update(){
if(live == true){
Move();
Draw();
}
}
private void Draw(){
fill(colors);
rect(x,y,w,h);
}
private void Move(){
if(y > ball.y && y > 16){
y -= speed;
}
if(y < ball.y && y < 370){
y += speed;
}
}
}//fin clase player cpu
The first question is about collision detection, I am using the collision function that is in the main file “collisionRect” and there seems to be a problem when the ball collides with the top or bottom of the player, it does not bounce but rather It moves downwards when colliding and I don’t know how to fix it.