why does the score keep counting after gameover? - processing

Everything works perfectly as it should except the fact that even when the person has lost the game, the score keeps counting. The score should freeze when the game is lost so that when the method gameover (); is called the score can be given for that game.
It also does not add a specific amount, the score just raises from 10 to 210 to 430 - it just all over the place.
Can someone find the reason for it?
// variables for the ball
int ball_width = 15,
ball_height = 15;
float ballX,
ballY;
// // variables for the paddles
int paddle_width = 20,
paddle_height = 200;
int paddle1,
paddle2=0;
// // direction variables
float directionX = 12,
directionY = 12;
// // variables for the score
int scorecounter = 0;
// //game states
boolean playing = false,
gameover = false,
finalscore = false,
score = true;
// ----------------------------------------------------
void setup () {
size (1900, 800); // the field game is going to be 1900x800 px big
background (0); // black background
rectMode (CENTER);
paddle1 = 60;
paddle2 = width - 60;
}
void draw () {
background (0); // black background
playing ();
contact ();
gameover ();
}
// ----------------------------------------------------
void playing () {
if (keyPressed) { // starts the game and makes the restart possible
playing = true;
scorecounter = 0 ;
gameover = false;
ballX = width/2;
ballY = height/2;
}
if (!playing) {
// playing = false
fill(255);
textSize(80);
textAlign(CENTER);
text("Press Space to Play", width/2, height/4);
fill (255);
ellipse (width/2, height/2, ball_width, ball_height); // this is the starting point of the ball
fill (255, 10, 20);
rect(paddle1, (height/2), paddle_width, paddle_height); // red pong
fill (60, 255, 0);
rect(paddle2, (height/2), paddle_width, paddle_height); // green pong
}
if (playing) { // playing = true
score(); // does what i wrote down in void score () -- keeps track of the score
ballX = ballX + directionX;
ballY = ballY + directionY; // gives the directions of the ball
fill (255);
ellipse (ballX, ballY, ball_width, ball_height); // ball itself
fill ( 255, 10, 20 );
rect(paddle1, mouseY, paddle_width, paddle_height); // red pong
fill ( 60, 255, 0 );
rect(paddle2, mouseY, paddle_width, paddle_height); // green pong
// top and bottom of screen --------------------
if ( ballY + ball_width/2 > height ) {
directionY = -directionY;
} // if the ball reaches the lower wall it will bounce off
if ( ballY - ball_width/2 < 0 ) {
directionY = -directionY;
} // if the ball reaches the upper wall it will bounce off
if ( ballX > width || ballX < 0 ) {
gameover = true;
} // if the ball reaches one of the bounderies it will be game over
}
}
void contact () {
// right paddle = paddle2
if (ballY - ball_width/2 > mouseY - paddle_height/2 &&
ballY + ball_width/2 < mouseY + paddle_height/2 &&
ballX + ball_width/2 > paddle2-4 ) {
directionX = -abs(directionX);
scorecounter = scorecounter + 10;
}
// left paddle = paddle1
if (ballY - ball_width/2 > mouseY - paddle_height/2 &&
ballY + ball_width/2 < mouseY + paddle_height/2 &&
ballX + ball_width/2 < paddle1+ paddle_width+4 ) {
directionX = abs(directionX);
scorecounter = scorecounter + 10; // if the ball touches the paddles it will bounce off
}
score ();
}
void gameover () {
if (gameover) {
background (0);
finalscore (); // gives me the final score + play again option
score = false;
} // it overrides the scorecounter with a black background
}
void score () {
if (playing) {
fill(255);
textSize(45);
textAlign(CENTER);
text ( scorecounter, width/2, height/4); // keeps the score on the display while playing
}
}
void finalscore () {
if (gameover) {
score = false; // the scorecounter will stop counting
fill(255);
textSize(45);
textAlign(CENTER);
text("Game Over. Press a key to play again.", width/2, height/4); // game over message
fill(255);
textSize(80);
textAlign(CENTER);
text("You scored " + scorecounter+ " points", width/2, (height/4) * 3); // the final score will appear here
}
}

You should have
playing = false;
somewhere in game over method or somewhere else. As I see you haven't such line of code.
I ask a question. Where did you change playing to false so that
You expect to get
If(!playing)
Condition instead of
If(playing)
You press the key. Game begins. Now you reach
If(playing)
Then the ball get to boundaries and the code reach
if ( ballX > width || ballX < 0 ) {
gameover = true; } // if the ball reaches one of the bounderies it will be game over
In this situation. Playing var and gameover var both are true. Right?
And under
If(playing)
You have score() method.so even when gameover is true the score method still is performed.
You can set playing to false under:
if ( ballX > width || ballX < 0 ) { gameover = true;
Playing =false ; }
Or you can modify the score method like this:
void score () { if (playing && !gameover) { fill(255); textSize(45); textAlign(CENTER); text(scorecounter, width/2,height/4);
// keeps the score on the display while playing }

Related

p5js sketch slows down over time

I'm new to p5js and I'm trying to plot incoming x y data by drawing circles which initially expand and then shrink becoming 'fixed' , but the speed at which the circles expand slows down over time - and I can't work out why. I have one array which stores the animated expanding / shrinking circle and another which stores the final state of the fixed circle.
let circles = [];
let circlesStatic = [];
function setup() {
createCanvas(640, 480);
frameRate(60);
smooth();
// e1 = new Ellipse(320,240);
// e2 = new Ellipse(20,20);
background(0);
}
function mouseClicked() {
let e = new Ellipse(mouseX, mouseY);
circles.push(e);
}
function draw() {
for (let i = 0; i < circles.length; i++) {
circles[i].render();
}
for (let i = 0; i < circlesStatic.length; i += 2) {
// noFill();
fill('rgba(50,50,50, 0.1)');
strokeWeight(1);
stroke('rgba(100,100,100, 0.25)');
circle(circlesStatic[i], circlesStatic[i + 1], 20);
}
}
class Ellipse {
// constructor
constructor(x, y) {
this.x = x; // copy x argument value to the instance (this) x property
this.y = y; // copy x argument value to the instance (this) x property
// current size - continuously updated
this.size = 10;
// minimum size
this.minSize = 10;
// maximum size
this.maxSize = random(30, 35);
// change speed for size (how much will the size increase/decrease each frame)
this.sizeSpeed = 3;
// internal frameCount replacement
this.tick = 0;
// this.fill=(255,0,0);
}
render() {
// if the size is either too small, or too big, flip the size speed sign (if it was positive (growing) - make it negative (shrink) - and vice versa)
if (this.size < this.minSize || this.size > this.maxSize) {
this.sizeSpeed *= -1;
}
// increment the size with the size speed (be it positive or negative)
this.size += this.sizeSpeed;
console.log(this.sizeSpeed);
if (this.size < this.minSize) {
this.sizeSpeed = 0;
circlesStatic.push(this.x);
circlesStatic.push(this.y);
}
background(0);
// noStroke();
fill(200, 50, 0);
ellipse(this.x, this.y, this.size, this.size);
// this.tick++;
// this.size = map(sin(this.tick * this.sizeSpeed),-1.0,1.0,this.minSize,this.maxSize);
// fill(random(255), random(255), random(255));
// ellipse(this.x,this.y, this.size,this.size);
}
}
<script src="https://cdn.jsdelivr.net/npm/p5#1.4.1/lib/p5.js"></script>
The problem is that every time you render a circle it checks if it should become "static," and if it should it adds new values to circlesStatic. However, there is nothing to stop the animating circle to continue being rendered on subsequent frames. This results in massive numbers of entries being added to circlesStatic which means longer and longer render times. I've implemented one possible fix below. I also made it so that if you hold the shift key down you will see the previous behavior (watch the length of circlesStatic skyrocket after a few clicks). As soon as you release the shift key the fix will run and the number of entries in circlesStatic will plateau.
let circles = [];
let circlesStatic = [];
function setup() {
createCanvas(640, 480);
frameRate(60);
smooth();
// e1 = new Ellipse(320,240);
// e2 = new Ellipse(20,20);
textSize(16);
}
function mouseClicked() {
let e = new Ellipse(mouseX, mouseY);
circles.push(e);
}
function draw() {
background(0);
push();
noStroke();
fill('red')
text(`circles: ${circles.length}; circlesStatic: ${circlesStatic.length}`, 20, 20);
pop();
for (let i = 0; i < circles.length; i++) {
circles[i].render();
if (!keyIsDown(SHIFT)) {
if (circles[i].isStatic()) {
circles.splice(i, 1);
i--;
}
}
}
for (let i = 0; i < circlesStatic.length; i += 2) {
// noFill();
fill('rgba(50,50,50, 1)');
strokeWeight(1);
stroke('rgba(100,100,100, 1)');
circle(circlesStatic[i], circlesStatic[i + 1], 20);
}
}
class Ellipse {
// constructor
constructor(x, y) {
this.x = x; // copy x argument value to the instance (this) x property
this.y = y; // copy x argument value to the instance (this) x property
// current size - continuously updated
this.size = 10;
// minimum size
this.minSize = 10;
// maximum size
this.maxSize = random(30, 35);
// change speed for size (how much will the size increase/decrease each frame)
this.sizeSpeed = 3;
// internal frameCount replacement
this.tick = 0;
// this.fill=(255,0,0);
}
isStatic() {
return this.sizeSpeed === 0;
}
render() {
// if the size is either too small, or too big, flip the size speed sign (if it was positive (growing) - make it negative (shrink) - and vice versa)
if (this.size < this.minSize || this.size > this.maxSize) {
this.sizeSpeed *= -1;
}
// increment the size with the size speed (be it positive or negative)
this.size += this.sizeSpeed;
console.log(this.sizeSpeed);
if (this.size < this.minSize) {
this.sizeSpeed = 0;
circlesStatic.push(this.x);
circlesStatic.push(this.y);
}
// background(0);
// noStroke();
fill(200, 50, 0);
ellipse(this.x, this.y, this.size, this.size);
// this.tick++;
// this.size = map(sin(this.tick * this.sizeSpeed),-1.0,1.0,this.minSize,this.maxSize);
// fill(random(255), random(255), random(255));
// ellipse(this.x,this.y, this.size,this.size);
}
}
<script src="https://cdn.jsdelivr.net/npm/p5#1.4.1/lib/p5.js"></script>

p5.js draw a rectangle at cursor position on mouse click

I have a very simple 9x9 grid. I know how to handle the rectangles on this grid. What I want is when I click on one of the grid rectangles, this rectangle should now be marked with a fill or border around it.
I can draw a rectangle, with a blue fill at exact the correct rectangle on the grid.
But with the next click on the grid, the new rectangle is drawn but the old rectangle is still there and will stay there. And so on.
My question is now, how can I paint always exact on rectangle at the click position?
Is maybe a class the right way?
Creating every time a new rectangle and destroy the old one?
var nullx = 140;
var nully = 50;
var breite = 65;
var hoehe = 65;
var pressed = false;
function setup() {
createCanvas(800, 1200);
}
function draw() {
//background(220);
noFill();
//strokeWeight(2);
sudokulines();
numberstorect();
if(pressed == true){
sqMark();
}
if (mousePressed == true){
console.log("click...")
sqMark();
}
}
function mousePressed(){
pressed = true;
}
function sqMark(){
var tempX;
var tempY;
//console.log(tempX,tempY);
tempX = (floor((mouseX - nullx) / breite) * breite) + nullx;
tempY = (floor((mouseY - nully) / hoehe) * hoehe) + nully;
console.log(tempX,tempY);
if (tempX > 139 && tempY > 49 ){
if (tempX < 661 && tempY < 571){
strokeWeight(0.7);
fill(0,0,255);
rect(tempX+2,tempY+2,breite-4,hoehe-4);
pressed = false;
}
}
}
function sudokulines(){
//The vertical lines
for (var i = 1; i <= 8; i++){
if (i == 3 || i == 6){
strokeWeight(3);
}else{
strokeWeight(1);
}
line(nullx + breite * i, nully, nullx + breite * i, nully+ 9*hoehe);
}
//The horizontal lines
for (var i = 1; i <= 8; i++){
if (i == 3 || i == 6){
strokeWeight(3);
}else{
strokeWeight(1);
}
//The big rectangle around
line(nullx , nully + hoehe * i, nullx + 9*breite, nully + hoehe * i);
}
strokeWeight(3);
rect(nullx,nully,9*breite,9*hoehe);
}
function numberstorect(){
textAlign(CENTER,CENTER);
fill(0);
textSize(breite/1.3);
text(2,nullx + breite/2, nully + hoehe/2);
}
It's important to understand that p5.js draws in immediate mode, so each element, once drawn, is not removable unless intentionally drawn over or cleared. This simplest solution to your specific problem is to simply save to location to be highlighted when the user presses the mouse, and then always clear the canvas and redraw everything (including the selected square) in the draw function. Because there's no animation, you can optimize this by disabling looping in your setup function, and then explicitly calling redraw when something happens that effects the display (i.e. the mouse is pressed).
var nullx = 140;
var nully = 50;
var breite = 65;
var hoehe = 65;
let selectedX = -1;
let selectedY = -1;
function setup() {
createCanvas(800, 1200);
// By default don't re draw every frame
noLoop();
}
function draw() {
background(255);
noFill();
//strokeWeight(2);
sudokulines();
numberstorect();
sqMark();
}
function mousePressed() {
// Set the selection
selectedX = (floor((mouseX - nullx) / breite) * breite) + nullx;
selectedY = (floor((mouseY - nully) / hoehe) * hoehe) + nully;
// Only redraw when something changes.
redraw();
}
function sqMark() {
if (selectedX > 139 && selectedY > 49) {
if (selectedX < 661 && selectedY < 571) {
strokeWeight(0.7);
fill(0, 0, 255);
rect(selectedX + 2, selectedY + 2, breite - 4, hoehe - 4);
pressed = false;
}
}
}
function sudokulines() {
//The vertical lines
for (var i = 1; i <= 8; i++) {
if (i == 3 || i == 6) {
strokeWeight(3);
} else {
strokeWeight(1);
}
line(nullx + breite * i, nully, nullx + breite * i, nully + 9 * hoehe);
}
//The horizontal lines
for (var i = 1; i <= 8; i++) {
if (i == 3 || i == 6) {
strokeWeight(3);
} else {
strokeWeight(1);
}
//The big rectangle around
line(nullx, nully + hoehe * i, nullx + 9 * breite, nully + hoehe * i);
}
strokeWeight(3);
rect(nullx, nully, 9 * breite, 9 * hoehe);
}
function numberstorect() {
textAlign(CENTER, CENTER);
fill(0);
textSize(breite / 1.3);
text(2, nullx + breite / 2, nully + hoehe / 2);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>

Ball should bounce off the paddles and game should restart once it is gameover

So this is my entire code and what it does not do, but should definitely do is first bounce off when colliding with the paddles and second if it does not do so then once the game enters in gameover mode once a key is pressed it should restart the game. Now I've tried several things and nothing seems to work.
Can someone please find a solution and try to explain what I did wrong?
// variables for the ball
int ball_width = 15, ball_height = 15;
int ballX = width/2, ballY = height/2;
//
// variables for the paddles
int paddle_width = 20, paddle_height = 150;
int paddle1 = 60, paddle2;
//
// direction variables
int directionX = 15, directionY = 15;
//
// variables for the score
int scorecounter = 0;
//
//game states
boolean playing = false, gameover = false, finalscore = false, score = true;
void setup () {
size (1900, 1300); // the field game is going to be 1900x1300 px big
rectMode (CENTER);
paddle2 = width - 60;
}
void draw () {
background (0); // black background
playing ();
gameover ();
finalscore();
}
//
void playing () {
if (keyPressed) {
playing = true;
}
if (!playing) { // playing = false
fill(255);
textSize(80);
textAlign(CENTER);
text("Press Space to Play", width/2, height/4);
fill (255);
ellipse (width/2, height/2, ball_width, ball_height); // this is the starting point of the ball
fill (255, 10, 20);
rect(paddle1, (height/2), paddle_width, paddle_height); // red pong
fill (60, 255, 0);
rect(paddle2, (height/2), paddle_width, paddle_height); // green pong
}
if (playing) { // playing = true
score();
ballX = ballX + directionX;
ballY = ballY + directionY;
fill (255);
ellipse (ballX, ballY, ball_width, ball_height);
fill ( 255, 10, 20 );
rect(paddle1, mouseY, paddle_width, paddle_height); // red pong
fill ( 60, 255, 0 );
rect(paddle2, mouseY, paddle_width, paddle_height); // green pong
if ( ballY > height ) {
directionY = -directionY;
} // if the ball reaches the lower wall it will bounce off
if ( ballY < 0 ) {
directionY = -directionY;
} // if the ball reaches the upper wall it will bounce off
if ( ballX > width || ballX < 0 ) {
gameover = true; }
}
if (ballX == paddle1 && ballY <= paddle_height) {
directionX = -directionX;
directionY = -directionY;
}
if (ballX == paddle2 && ballY <= paddle_height) {
directionX = -directionX;
directionY = -directionY;
}
}
void gameover () {
if (gameover) {
background (0);
}
finalscore ();
score = false;
if (keyPressed) {
playing = true;
}
}
void score () {
if (playing) {
fill(255);
textSize(45);
textAlign(CENTER);
text ( scorecounter, width/2, height/4);
if (ballX == paddle1 && ballY <= paddle_height) {
scorecounter = scorecounter + 10;
}
if (ballX == paddle2 && ballX <= paddle_height) {
scorecounter = scorecounter + 10;
}
}
if (!playing) {
score = false;
}
}
void finalscore () {
if (gameover) {
score = false;
fill(255);
textSize(45);
textAlign(CENTER);
text("Game Over. Press a key to play again.", width/2, height/4);
fill(255);
textSize(80);
textAlign(CENTER);
text("You scored " + scorecounter + " points", width/2, (height/4) * 3);
if (keyPressed) {
playing = playing;
}
}
}
Not sure if that's the problem, but
if (keyPressed) {
playing = playing;
}
looks not very useful. You have both a function and a variable named playing. Reusing identifiers is usually a recipe for confusion.
For your first issue of the ball not bouncing off the paddle, take a look at these lines:
if (ballX == paddle1 && ballY <= paddle_height) {
directionX = -directionX;
directionY = -directionY;
}
if (ballX == paddle2 && ballY <= paddle_height) {
directionX = -directionX;
directionY = -directionY;
}
Notice that you're checking whether ballX == paddle1 or ballX == paddle2. You're checking whether the exact pixel position of the ball matches either of the paddles.
However, also notice that you're moving the ball by 15 pixels each time, so the chances of it hitting an exact pixel are pretty low. In other words, your ball is going to move through the exact pixel position and be inside your paddles. That is what you have to check for.
Furthermore, you're only checking whether the ball's y position is less than paddleHeight. That's not enough. You also have to check whether it's greater than the paddle position!
Even after that, I'm not sure you want to reverse directionY when it hits a paddle, but I'll let you decide that for yourself.
For your other question about the gameover not reseting, notice that you never reset the ball position! That means you start playing, but then instantly get a game over. You need to reset the position of the ball before you restart the game.
Honestly, if I were you, I would start with a more basic sketch. Your code has a lot of extra stuff in it that doesn't make a lot of sense, so I'm not surprised that you're getting confused. Start with something much simpler: can you create a sketch that simply detects when the mouse is inside a rectangle? Start from there (or even simpler!) and take small steps instead of trying to program your end goal all at once. Good luck.
The reason why this is not reseting is because you are never updating the ballX o gameover variable. Every time you press a key you are falling into this conditional.
if ( ballX > width || ballX < 0 ) {
gameover = true;
}
I got around this by adding your inital values to the playing method:
void playing () {
if (keyPressed) {
playing = true;
gameover = false;
ballX = width/2;
ballY = height/2;
}
// rest of playing() code
}

Can someone find the reason why my rectangles do not follow my mouse?

So down below I have my entire code for a pong game I am programming and although I used mouseY in the y coordinate
fill ( 255, 10, 20 ); rect(x, mouseY + y, 20, 100); // red pong
fill ( 60, 255, 0 ); rect(paddleX, mouseY + y, 20, 100); // green pong
it does not work and I can simply not find my mistake.
Also I am struggling with the Score () part because everytime I try to put
if ( ballX = paddleX ) { score = score + 1;}
processing tells me that it is not possible to convert int to Boolean. Can someone please tell me how I can rewrite my code so this error does not appear anymore. What my goal with that part of the code is, is that I want the counter to increase by one every time the ball hits one of the paddles.
full code:
float ballX = 15, ballY = 15, dX = 15, dY = 15; // variables for the ball
float x = 40, y, score = 0;
float paddleX, paddleY = 0; // variables for the paddles
float mouseY, mouseX;
boolean playing = false, gameover = false, finalscore = false, Score = true; // game states
boolean keyPressed, key;
void setup() {
size (1500,1100); // the field is going to be 1500x1100px big
paddleX = width - 40;
}
void keyPressed () { // the game will only start when a key is pressed
playing = true;
}
void draw() {
background(0); // black background
if (!playing) { // playing = false
fill(255); textSize(80); textAlign(CENTER); text("Press Space to Play",width/2, height/4);
fill (255); ellipse (width/2, height/2, 15, 15); // this is the starting point of the ball
fill (255,10,20); rect(paddleX/58, (height/2)-50, 20, 100); // red pong
fill (60,255,0); rect(paddleX, (height/2)-50, 20, 100); // green pong
}
if (playing) { // playing = true
Score();
fill ( 0, 255, 0 ); ellipse (ballX, ballY, 15, 15);
fill ( 255, 10, 20 ); rect(x, mouseY + y, 20, 100); // red pong
fill ( 60, 255, 0 ); rect(paddleX, mouseY + y, 20, 100); // green pong
if ( ballY > height ) { dY = -dY; } // if the ball reaches the lower wall it will bounce off
if ( ballY < 0 ) { dY = -dY; } // if the ball reaches the upper wall it will bounce off
ballX = ballX + dX; ballY = ballY + dY;
if (ballX > width || ballX < 0 ) { gameover = true;}
}
if (gameover) { // gameover = true
finalscore = true;
if (keyPressed) { playing = false; }
else if (playing) { playing = true; }
if (finalscore) { // score = true
fill(255); textSize(45); textAlign(CENTER); text("Game Over. Press a letter to play again.",width/2, height/4);
fill(255); textSize(80); textAlign(CENTER); text("You scored " + score + " points" ,width/2, (height/4) * 3);
if (keyPressed) { playing = true;}
}
}
}
void Score() {
fill(255); textSize(45); textAlign(CENTER); text(score,width/2, height/4);
if (playing) {
if (paddleX <= ballX){ score = score + 1;}
}
}
You're declaring your own mouseX and mouseY variables at the top of your sketch:
float mouseY, mouseX;
Don't do this. Processing automatically creates mouseX and mouseY variables for you. You don't create them yourself. Get rid of this line so that you're using Processing's mouseX and mouseY variables, not your own.
Also I am struggling with the Score () part because everytime I try to put
if ( ballX = paddleX ) { score = score + 1;}
Look at that if statement. You're only using a single =, which is an assignment. You need to use two equals ==, or better yet an inequality like <= or >=, since the chances of the ball hitting the exact pixel value of the paddle is pretty low.

Moving Paddle with mouse (Arkanoid)

I am having a slight problem. I'm creating a basic Arkanoid using processing but I can't figure out how to make the paddle move with my mouse. I have it currently set up to move on a click for this example.
void setup() {
size(400, 400);
rectMode(CENTER);
ellipseMode(CENTER);
}
void draw() {
background(#EFF8FB);
batMain();
ballMain();
}
float ballX = 200, ballY = 200;
float direction = PI * .3f;
byte speed = 4;
final static byte ballSize = 20;
final static color ballColor = #2EFE2E;
void ballMain() {
// Draw the ball
fill(ballColor);
ellipse(ballX, ballY, ballSize, ballSize);
ballX += speed * cos(direction);
ballY += speed * sin(direction);
walls();
if (direction > TWO_PI) direction -= TWO_PI;
else if (direction < -TWO_PI) direction += TWO_PI;
}
short a = 200;
final static color batColor = #5858FA;
void batMain() {
// Draw the bat
fill(batColor);
rect(a, 380, 60, 10);
noStroke();
if (mousePressed) {
// Right side of the screen
if (mouseX > width>>1) a += 4;
// Other side of the screen
else a -= 4;
}
if (ballY + 10 > 375 && ballY + 10 < 385 && ballX + 10 > a - 30 && ballX - 10 < a + 30) {
if (ballX + 10 > a - 30 && ballX < a - 15) { // Left part
direction = TWO_PI - direction;
++speed;
}
else if (ballX - 10 < a + 30 && ballX < a + 15) {
direction = TWO_PI - direction;
++speed;
}
else {
direction = TWO_PI - direction;
speed = 4;
}
}
}
void walls() {
if (ballX < 10) {
ballX = 11;
direction = PI - direction;
}
if (ballX > 390) {
ballX = 389;
direction = PI - direction;
}
if (ballY < 10) {
ballY = 11;
direction = TWO_PI - direction;
}
if (ballY > 390) {
ballY = 389;
direction = TWO_PI - direction;
}
}
You already have the logic here:
if (mousePressed) {
// Right side of the screen
if (mouseX > width>>1) a += 4;
// Other side of the screen
else a -= 4;
}
If you want the paddle to follow the mouse even when you aren't pressing it, just get rid of that outer if statement:
// Right side of the screen
if (mouseX > width>>1){
a += 4;
}
// Other side of the screen
else{
a -= 4;
}
Or if you want the paddle to be at the mouse's position, just use that directly:
a = mouseX;

Resources