xxxxxxxxxx
131
function setup() {
createCanvas(560, 560);
background(0);
textAlign(CENTER, CENTER);
resetBall();
paddlesize = PI / 16;
}
let playing = false;
let balla;
let ballx, bally;
const ringsize = 450;
const ballsize = 20;
let paddlesize;
let paddangle;
let score = 0;
const paddlewidth = 16;
function draw() {
fill(0,100);
rect(0,0,width,height);
textSize(40);
fill(255);
if (!playing) {
text("ROTATO SOLO PONG", width / 2, height*2 / 10);
text("CLICK TO PLAY", width / 2, (height * 4) / 10);
text("AVOID MISSING BALL", width / 2, (height * 7) / 10);
text("FOR HIGH SCORE", width / 2, (height * 8) / 10);
}
if (
ballAngledLikePaddle(paddangle) &&
ballOnCircle()
) {
score++;
ballspeed += .1;
//figure which one we hit....
const pa = paddangle ;
const angleFromCenter = atan2(bally - height / 2, ballx - width / 2);
ballx = width / 2 + (cos(angleFromCenter) * (ringsize - ballsize * 2)) / 2;
bally = height / 2 + (sin(angleFromCenter) * (ringsize - ballsize * 2)) / 2;
balla = angleFromCenter + PI + (angleFromCenter - pa);
}
push();
translate(width / 2, height / 2);
strokeWeight(1);
text(score, 0, 0);
strokeWeight(paddlewidth);
stroke(255);
noFill();
paddangle = mouseX / 50;
arc(0, 0, ringsize, ringsize, paddangle - paddlesize, paddangle + paddlesize);
//circle(cos(balla) * ringsize/2,sin(balla) * ringsize/2, ballsize );
pop();
fill(255);
noStroke();
if (playing) {
circle(ballx, bally, ballsize);
moveball();
if(dist(width / 2, height / 2, ballx, bally) > width*3/4){
playing = false;
}
}
}
function mousePressed() {
score = 0;
resetBall();
playing = true;
}
function moveball() {
ballx += cos(balla) * ballspeed;
bally += sin(balla) * ballspeed;
}
function ballOnCircle() {
const distanceFromCenter = dist(width / 2, height / 2, ballx, bally);
return abs(ringsize / 2 - distanceFromCenter) < ballsize;
}
function resetBall() {
ballx = width / 2;
bally = height / 2;
ballspeed = 2;
balla = random(2 * PI);
}
function ballAngledLikePaddle(pa) {
const balla = normalizeAngle(atan2(bally - height / 2, ballx - width / 2));
const [pad1Start, pad1Stop] = normalizeAngles(
pa - paddlesize,
pa + paddlesize
);
const ballb = balla + PI * 2;
return (
(balla >= pad1Start && balla <= pad1Stop) ||
(ballb >= pad1Start && ballb <= pad1Stop)
);
}
function normalizeAngle(a) {
while (a < 0) a += PI * 2;
return a;
}
function normalizeAngles(a, b) {
if (a < 0 || b < 0) {
return [a + 2 * PI, b + 2 * PI];
}
return [a, b];
}