xxxxxxxxxx
ArrayList < Ball > pangs = new ArrayList < Ball > ();
​
float WALL_SIDE = 200;
float WALL_TOP = -800;
float WALL_LOSE = 320;
​
float paddleWidth = 100;
float paddleVelz = 0;
float paddleVelx = 0;
​
​
​
PVector paddlePos;
boolean gameOver;
​
int score;
float screenshake = 0;
​
void setup() {
size(500, 500, P3D);
initGame();
noStroke();
}
​
void draw() {
background(12, 48, 128);
​
lights();
PVector screenshakeVector = new PVector();
screenshakeVector.x = random(-screenshake, screenshake);
screenshakeVector.y = random(-screenshake, screenshake);
screenshakeVector.z = random(-screenshake, screenshake);
​
ambientLight(102, 102, 102);
​
camera(-mouseX / 4 + 62.5 + screenshakeVector.x, -300 - (mouseY / 4 - 62.5) + screenshakeVector.y, 750 + screenshakeVector.z, 0, 0, 0, 0, 1, 0);
​
gameLogic();
gameDisplay();
worldDisplay();
}
​
void initGame() {
pangs = new ArrayList < Ball > ();
pangs.add(new Ball(new PVector(0, 0, 0), new PVector(5, 0, -5)));
paddlePos = new PVector(0, 0, 250);
gameOver = false;
score = 0;
}
​
void gameLogic() {
if (gameOver) {
screenshake *= 0.85;
return;
}
for (Ball ball: pangs) {
ball.move();
​
if (abs(ball.pos.x) > WALL_SIDE) {
ball.vel.x *= -1;
ball.vel.z += random(-.5, .5);
screenshake += 5;
}
if (ball.pos.z < WALL_TOP) {
ball.vel.z *= -1;
ball.vel.x += random(-.5, .5);
screenshake += 6;
}
​
if (ball.pos.z > 250 - 10) {
if (ball.pos.x > paddlePos.x - paddleWidth / 2 && ball.pos.x < paddlePos.x + paddleWidth / 2) {
ball.vel.z *= -1;
ball.pos.z -= 30;
ball.vel.mult(1.015);
paddleWidth *= 0.975;
​
score++;
if (score % 5 == 0) {
pangs.add(new Ball(new PVector(0, 0, 0), new PVector(5, 0, -5)));
}
screenshake += 50;
paddlePos.z += 50;
} else {
gameOver = true;
screenshake += 100;
}
}
}
​
// paddl
screenshake *= 0.85;
int targetX = constrain(mouseX - width / 2, -WALL_SIDE + paddleWidth / 2 - 30, WALL_SIDE - paddleWidth / 2 + 30);
paddleVelx += (targetX - paddlePos.x) / 5;
paddlePos.x += paddleVelx;
paddleVelz += (250 - paddlePos.z) / 5;
paddlePos.z += paddleVelz;
paddleVelz *= 0.85;
paddleVelx *= 0.65;
​
}
​
void keyPressed() {
if (key == 'r') {
initGame();
}
}
​
void worldDisplay() {
pushMatrix();
translate(0, 50, -200);
fill(12, 178, 243);
box(WALL_SIDE * 3, 20, WALL_LOSE - WALL_TOP + 600);
popMatrix();
}
​
void gameDisplay() {
pushMatrix();
translate(0, -19, 120);
fill(255);
rotateX(PI / 2);
textSize(120);
text(score, 0, 0)
popMatrix();
​
// the ball.
for (Ball ball: pangs) {
ball.render();
}
​
pushMatrix();
translate(0, 10, WALL_LOSE - 40);
fill(235, 15, 12);
box(500, 30, 20);
popMatrix();
​
// not lose wall
​
pushMatrix();
translate(0, 40, WALL_LOSE - 300);
fill(255);
box(500, 30, 10);
popMatrix();
​
pushMatrix();
translate(WALL_SIDE + 40, 0, -160);
fill(255);
box(20, 50, 1340);
popMatrix();
​
pushMatrix();
translate(-(WALL_SIDE + 40), 0, -160);
fill(255);
box(20, 50, 1340);
popMatrix();
​
pushMatrix();
translate(0, 0, WALL_TOP - 40);
fill(255);
box(500, 50, 20);
popMatrix();
​
pushMatrix();
fill(noise(millis() / 2500) * 255, noise(0, millis() / 2500) * 255, noise(0, 0, millis() / 2500) * 255);
translate(-200, (noise(millis() / 5000) * -250), WALL_TOP - 40);
box(50, noise(millis() / 5000) * 500, 15);
translate(50, 0, 0);
box(50, noise((millis() + 50) / 5000) * 500, 15);
translate(50, 0, 0);
box(50, noise((millis() + 100) / 5000) * 500, 15);
translate(50, 0, 0);
box(50, noise((millis() + 150) / 5000) * 500, 15);
translate(50, 0, 0);
box(50, noise((millis() + 250) / 5000) * 500, 15);
translate(50, 0, 0);
box(50, noise((millis() + 300) / 5000) * 500, 15);
translate(50, 0, 0);
box(50, noise((millis() + 350) / 5000) * 500, 15);
translate(50, 0, 0);
box(50, noise((millis() + 400) / 5000) * 500, 15);
translate(50, 0, 0);
box(50, noise((millis() + 450) / 5000) * 500, 15);
popMatrix();
​
pushMatrix();
translate(paddlePos.x, paddlePos.y, paddlePos.z);
fill(43, 165, 245);
box(paddleWidth, 50, 20);
fill(24, 100, 213);
translate(0, 0, 200);
box(paddleWidth / 5, paddleWidth / 5, 400);
popMatrix();
​
}