I’m trying to do the pong, I haven’t added the collision with the rackets yet because I’m trying to understand the collisions.
What I would like to know is how to make the racket that controls the cpu have a little intelligence so that it can move on its own and return the ball.
//player------------------------------------------
class Player{
private float x = 32.0;
private float y = 160.0;
private float speed = 5.0;
private float p_width = 16.0;
private float p_height = 125.0;
public void draw(){
move();
noFill();
strokeWeight(2.0);
stroke(200.0,200.0,200.0);
rect(x,y,p_width,p_height);
}
private void move(){
if(keyPressed && key == CODED && keyCode == UP && y > 8){
y -= speed;
}else if(keyPressed && key == CODED && keyCode == DOWN && y < 348){
y += speed;
}
}
} //player----------------------------------------
//cpu---------------------------------------------
class Player_cpu{
private float x = 588.0;
private float y = 160.0;
private float speed = 5.0;
private float p_width = 16.0;
private float p_height = 125.0;
public void draw(){
noFill();
strokeWeight(2.0);
stroke(200.0,200.0,0.0);
rect(x,y,p_width,p_height);
}
} //player cpu--------------------------------------
//ball-------------------------------------------
class Ball{
private float x = width/2;
private float y = height/2;
private float xspeed = 2.5,yspeed = 2.5;
private float b_width = 16.0;
private float b_height = 16.0;
public void draw(){
move();
noFill();
strokeWeight(2.0);
stroke(0.0,200.0,200.0);
rect(x,y,b_width,b_height);
}
private void move(){
x += xspeed;
if(x < 0 || x > width-16){
xspeed *= -1;
}
y += yspeed;
if(y < 0 || y > height-16){
yspeed *= -1;
}
}
} //ball-----------------------------------------
Player player;
Player_cpu cpu;
Ball ball;
void settings(){
size(640,480);
noSmooth();
}
void setup(){
frameRate(60);
player = new Player();
cpu = new Player_cpu();
ball = new Ball();
}
void draw(){
background(0,0,0);
player.draw();
cpu.draw();
ball.draw();
}