I’m am trying to make a pong game where one player controls with the mouse and the other controls with the keyboard. I got my mouse to move the paddle, but it keeps glitching to different places on the screen. Any ideas?
//pong tab
sunBall sunball;
cloudPaddle left;
cloudPaddle right;
int leftscore = 0;
int rightscore = 0;
void setup() {
size(1500, 1500);
sunball = new sunBall();
left = new cloudPaddle(true);
right = new cloudPaddle(false);
}
void draw() {
background(111,180,255);
//puck.checkPaddle(left);
sunball.checkcloudPaddleRight(right);
sunball.checkcloudPaddleLeft(left);
left.show();
right.show();
left.update();
right.update();
sunball.update();
sunball.edges();
sunball.show();
fill(255);
textSize(32);
text(leftscore, 32, 40);
text(rightscore, width-64, 40);
}
void keyReleased() {
left.move(0);
right.move(0);
}
void keyPressed() {
if (key == 'a') {
left.move(-15);
} else if (key == 'd') {
left.move(15);
}
}
void mouseReleased(){
}
void mouseMoved(){
right.move(mouseY);
}
//paddles tab
class cloudPaddle {
float x;
float y = height/2;
float w = 30;
float h = 150;
float ychange = 0;
cloudPaddle(boolean left) {
if (left) {
x = w;
} else {
x = width - w;
}
}
void update() {
y += ychange;
y = constrain(y, h/2, height-h/2);
}
void move(float steps) {
ychange = steps;
y = mouseY;
}
void show() {
fill(255);
rectMode(CENTER);
rect(x, y, w, h);
}
}
//ball tab
class sunBall {
float x = width/2;
float y = height/2;
float xspeed;
float yspeed;
float r = 17;
sunBall() {
reset();
}
void checkcloudPaddleLeft(cloudPaddle p) {
if (y - r < p.y + p.h/2 && y + r > p.y - p.h/2 && x - r < p.x + p.w/2) {
if (x > p.x) {
float diff = y - (p.y - p.h/2);
float rad = radians(45);
float angle = map(diff, 0, p.h, -rad, rad);
xspeed = 5 * cos(angle);
yspeed = 5 * sin(angle);
x = p.x + p.w/2 + r;
//xspeed *= -1;
}
}
}
void checkcloudPaddleRight(cloudPaddle p) {
if (y - r < p.y + p.h/2 && y + r > p.y - p.h/2 && x + r > p.x - p.w/2) {
if (x < p.x) {
//xspeed *= -1;
float diff = y - (p.y - p.h/2);
float angle = map(diff, 0, p.h, radians(225), radians(135));
xspeed = 5 * cos(angle);
yspeed = 5 * sin(angle);
x = p.x - p.w/2 - r;
}
}
}
void update() {
x = x + xspeed;
y = y + yspeed;
}
void reset() {
x = width/2;
y = height/2;
float angle = random(-PI/4, PI/4);
//angle = 0;
xspeed = 5 * cos(angle);
yspeed = 5 * sin(angle);
if (random(1) < 0.5) {
xspeed *= -1;
}
}
void edges() {
if (y < 0 || y > height) {
yspeed *= -1;
}
if (x - r > width) {
leftscore++;
reset();
}
if (x + r < 0) {
rightscore++;
reset();
}
}
void show() {
fill(255,221,60);
ellipse(x, y, r*2, r*2);
}
}