“paint by pong” by Jia
https://openprocessing.org/sketch/161377
License CreativeCommons Attribution ShareAlike
https://creativecommons.org/licenses/by-sa/3.0
{{filePath}}
{{width}} x {{height}}
Report Sketch
Oh, that naughty sketch! Please let us know what the issue is below.
Apply Template
Applying this template will reset your sketch and remove all your changes. Are you sure you would like to continue?
Report Sketch
Report Comment
Please confirm that you would like to report the comment below.
We will review your submission and take any actions necessary per our Community Guidelines. In addition to reporting this comment, you can also block the user to prevent any future interactions.
Please report comments only when necessary. Unnecessary or abusive use of this tool may result in your own account being suspended.
Are you sure you want to delete your sketch?
Any files uploaded will be deleted as well.
Delete Comment?
This will also delete all the replies to this comment.
Delete this tab? Any code in it will be deleted as well.
Select a collection to submit your sketch
We Need Your Support
Since 2008, OpenProcessing has provided tools for creative coders to learn, create, and share over a million open source projects in a friendly environment.
Niche websites like ours need your continued support for future development and maintenance, while keeping it an ad-free platform that respects your data and privacy!
Please consider subscribing below to show your support with a "Plus" badge on your profile and get access to many other features!
CC Attribution ShareAlike
paint by pong
xxxxxxxxxx
//PGraphics pg;
int cols = 600;
int rows = 600;
int i, j;
color b1, b2;
//float pi = 3.14;
// for ball
float ballX = 300;
float ballY = 300;
float ballRadius = 20;
// for making ball move
float speed = 2;
float directionX = 1;
float directionY = 1;
// for calculating distance
float longestDistance, shortestDistance;
float distance1, distance2;
// for paddles
int paddle1X;
int paddle2X;
int paddleY;
// Declare pixel Array
int[][] pixelRed = new int[cols][rows];
int[][] pixelGreen = new int[cols][rows];
int[][] pixelBlue = new int[cols][rows];
void setup() {
size (600, 600);
// distances between paddle and ball
longestDistance = sqrt(50*50 + 30*30);
shortestDistance = 15;
// Initialize colors
b1 = color(203, 102, 0);
b2 = color(0, 102, 153);
// Initialize pixel RGB values
for (j = 0; j < cols; j++) {
for (i = 0; i < rows; i++) {
pixelRed[i][j] = 255;
pixelGreen[i][j] = i/3;
pixelBlue[i][j] = j/3;
}
}
//pg = createGraphics(40,40);
//Draw background gradience
// for (i = 0; i < cols; i++) {
// for (j = 0; j < rows; j++) {
// fill(0, pixelGreen[i][j]/3, pixelBlue[i][j]/3);
// noStroke();
// ellipse(i, j, 20, 20);
// }
// }
//setGradient(0,0,width/2,height,b1,b2,1);
background(0);
}
void draw () {
/* draws ball
fill(255);
noStroke();
ellipse(ballX, ballY, ballRadius*2, ballRadius*2); */
// draws gradience based on ball location
if (ballY > 0 && ballY < cols && ballX > 0 && ballX < rows) {
i = int(ballX);
j = int(ballY);
fill(pixelRed[i][j], pixelGreen[i][j], pixelBlue[i][j]);
noStroke();
ellipse(i, j, ballRadius*2, ballRadius*2);
}
/**************** trying PGraphics */
// pg.beginDraw();
// pg.fill(255, 255, 255);
// pg.noStroke();
// pg.ellipse(ballX, ballY, ballRadius*2, ballRadius*2);
// pg.endDraw();
move(); // make ball move
noStroke();
fill(0);
rect(0, 0, 20, 1200); // cover left paddle trail
fill(0);
rect(cols, 0, 20, 1200); // cover right paddle trail
// draws paddles
fill(255);
noStroke();
paddleY = mouseY;
paddle1X = 0;
paddle2X = width;
rectMode(CENTER);
rect(paddle1X, paddleY, 20, 100); // left paddle
rect(paddle2X, paddleY, 20, 100); // right paddle
bounce(); // make ball bounce
}
/******************************* Functions */
// function make ball move
void move () {
if (!mousePressed) {
ballX = ballX + speed * directionX;
//ballY = ballY + (pow(speed, 4) * directionY);
ballY = ballY + speed * directionY;
} else {
//ballY = ballY + speed * directionY;
//ballY = ballY + (sin(speed*2) * directionY);
//ballY = ballY + (pow(speed, 2) * directionY);
ballX = ballX + speed;
ballY = ballY + (pow(speed, 2) * directionY);
// fill(255,0,0);
// ellipse(i, j, 40, 40);
}
}
// function make ball bounce
void bounce () {
// distances between paddle and ball
distance1 = sqrt((paddleY - ballY) * (paddleY - ballY) + (paddle1X - ballX) * (paddle1X - ballX));
distance2 = sqrt((paddleY - ballY) * (paddleY - ballY) + (paddle2X - ballX) * (paddle2X - ballX));
// ball bounce if hit top or bottom of screen
if (ballY < 0+20 || ballY > height-20) {
directionY = directionY * -1 + random(-.3, .3);
ballRadius = ballRadius * 1.1;
}
// ball bounce if touch paddles
else if (distance1 <= longestDistance || distance2 <= longestDistance) {
if (ballX == 30 || ballX == width - 30) {
directionX = directionX * -1;
directionY = directionY + random(-.3, .3);
ballRadius = ballRadius * 1.1;
//counter++;
}
}
}
// void mousePressed() {
// ballY = ballY + (pow(speed, 5) * directionY);
// }
See More Shortcuts