Hello everyone! I’m new at coding and processing. I’m looking for an urgent answer for a problem occured while i’m trying to do a Bricker Game. I’m at the phase where i try to the interactions with the ball and the paddle. I tried so hard but couldn’t come with a solution. I’m working with classes and adding codes below. Please help, thank you.
Paddle paddle;
Ball ball;
boolean gameStart = false;
boolean gameFinished = false;
int streak = 0;
void setup(){
size(900, 900);
paddle = new Paddle();
ball = new Ball();
}
void draw(){
background(5, 18, 155);
paddle.display();
paddle.setLocation(mouseX, height-50);
ball.display();
ball.move();
if (paddle.intersect(ball))
{
ball.yspeed *= -1;
}
}
class Ball {
float x, y, xspeed, yspeed, r;
Ball() {
x = width/2;
y = height/2;
xspeed = 4;
yspeed = -4;
r = 15;
}
void display(){
fill(255);
ellipse(x, y, r*2, r*2);
}
void move(){
x += xspeed;
y += yspeed;
if (x > width-r && x > 0) {
xspeed *= -1;
}
if (x < r && x < 0) {
xspeed *= -1;
}
if (y < r && y<0) {
yspeed *= -1;
}
} //move
} //ball
class Paddle {
float x, y;
float a, b;
Paddle() {
x = width/2;
y = height-50;
a = 140;
b = 25;
}
void setLocation(float x, float y) {
this.x = x;
this.y = y;
this.x = constrain(this.x, a/2, width-a/2);
}
void display() {
noStroke();
fill(245, 0, 170);
rectMode(CENTER);
rect(x, y, a, b, 28);
}
}