int stageX = 400; int stageY = 400; float ballX = 150; // ball X coordinate float ballY = 150; // ball Y coordinate float ballSpeed = 10; // ball Speed value float ballDirection = 120; // direction of the ball float ballSpeedX = sin(radians(ballDirection)); float ballSpeedY = cos(radians(ballDirection)); float ballSize = 40; float racketSizeX = 100; float racketSizeY = 20; float racketX = 150; float racketY = 380; // array for pixelating pong results int fieldSize = 20; float fieldPixels[] = new float[fieldSize*fieldSize]; void setup(){ size(400,400); framerate(30); //smooth(); stroke(0); } void draw(){ background(60); playField(); dragRacket(); bounceBall(); } // ballBounce calculates and draws a bouncing ball void bounceBall(){ fill(150); // check for walls if (ballX >= stageX || ballX <= 0){ ballSpeedX = -ballSpeedX; } else if (ballY <= 0){ ballSpeedY = -ballSpeedY; } // check for racket if (ballX >= racketX && ballX <= racketX + (racketSizeX / 1) && ballY >= racketY - (ballSize / 2) && ballY <= racketY){ ballSpeedY = -ballSpeedY; } // move ball ballX += ballSpeedX*ballSpeed; ballY += ballSpeedY*ballSpeed; // draw ball // ellipse(ballX, ballY, ballSize, ballSize); } // drag racket with mouse void dragRacket(){ fill(200); stroke(0); racketX -= (racketX-(mouseX - (racketSizeX / 2)))/3; //rect(racketX, racketY, racketSizeX, racketSizeY); for (int i = 0; i<15; i++){ float randomX = random(100); float randomY = random(20); rect(racketX + randomX, racketY - randomY, 10, 10); } }; // reset game on mouse press void mousePressed (){ ballX = 150; // ball X coordinate ballY = 150; // ball Y coordinate ballSpeed = 10; // ball Speed value ballDirection = 120; // direction of the ball ballSpeedX = sin(radians(ballDirection)); ballSpeedY = cos(radians(ballDirection)); } // try 2d field effect void playField(){ stroke(100); for (int i = 0; i < fieldSize; i++){ for (int j = 0; j < fieldSize; j++){ int pixelPos = i + (10 * j); float pixelSize = stageX / fieldSize; if ( abs( (i * pixelSize) - ballX) < 20 && abs(j * pixelSize - ballY) < 20){ fieldPixels[pixelPos] = 1; } else { fieldPixels[pixelPos] = 0; } // draw walls fieldPixels[i] = 1; fill(fieldPixels[i+10*j]*255); rect(2+(i * pixelSize), 2+( j * pixelSize), (pixelSize/3), (pixelSize/3)); } } }