xxxxxxxxxx
163
var ballx = 200;
var bally = 30;
var move = 5;
var speed = 5;
var changeX = 150;
var changeY = 350;
var platformWidth = 100;
var platformHeight = 30;
var diameter = 50;
var score = 0;
var first = 300;
var bally2 = 50;
var diameter2 = 50;
var speed2 = 5;
var move2 = 5;
var ball3 = 100;
var bally3 = 200;
var speed3 = 3;
var move3 = 3;
function setup() {
createCanvas(400, 400);
}
function draw() {
background(0);
stroke(255);
points();
balls();
}
function balls() {
ellipse(ballx, bally, diameter, diameter);
rect(changeX, changeY, platformWidth, platformHeight);
//Bouncing Ball
ballx = ballx + move;
if (ballx > width) {
move = -5;
}
if (ballx < 0) {
move = 5;
}
bally = bally + speed;
if (bally > 400) {
speed = -speed;
//basically losing
score = 0;
}
if (bally < 0) {
speed = 5;
}
//moving the platform
if (keyIsDown(LEFT_ARROW)) {
changeX -= 5;
}
if (keyIsDown(RIGHT_ARROW)) {
changeX += 5;
}
//bouncing off platform
if (
(ballx > changeX &&
ballx < changeX + platformWidth) &&
(bally + diameter / 2 > changeY &&
bally < changeY + platformHeight)
) {
speed = -speed;
//adding score
score = score + 1;
}
//more
if (score > 5) {
ellipse(ball3, bally3, diameter2, diameter2);
ball3 = ball3 + move3;
bally3 = bally3 + speed3;
if (ball3 > width) {
move3 = move3 - 3;
}
if (ball3 < 0) {
move3 = move3 + 3;
}
if (bally3 > 400) {
speed3 = -speed3;
bally3 = bally3 - bally3;
score = 0;
}
if (bally3 < 0) {
speed3 = speed3 + 3;
}
if (
ball3 > changeX &&
ball3 < changeX + platformWidth &&
bally3 + diameter / 2 > changeY &&
bally3 < changeY + platformHeight
) {
speed3 = -speed3;
//adding score
score = score + 1;
}
}
if (score > 20) {
ellipse(first, bally2, diameter2, diameter2);
first = first + move2;
bally2 = bally2 + speed2;
if (first > width) {
move2 = move2 - 5;
}
if (first < 0) {
move2 = move2 + 5;
}
if (bally2 > 400) {
speed2 = -speed2;
bally2 = bally2 - bally2;
score = 0;
}
if (bally2 < 0) {
speed2 = speed2 + 5;
}
if (
first > changeX &&
first < changeX + platformWidth &&
bally2 + diameter / 2 > changeY &&
bally2 < changeY + platformHeight
) {
speed2 = -speed2;
//adding score
score = score + 1;
}
}
}
function points() {
textSize(32);
text(score, 200, 100);
if (score > 100) {
background(255);
textSize(40);
speed = 0;
move = 0;
speed2 = 0;
move2 = 0;
speed3 = 0;
move3 = 0;
bally = 0;
bally2 = 0;
bally3 = 0;
text("YOU WIN", 110, 200);
textSize(30);
text("To play again, press enter", 20, 300);
if (keyIsDown(ENTER)) {
score = 0;
move = 5;
speed = 5;
speed2 = 5;
move2 = 5;
speed3 = 3;
move3 = 3;
bally2 = 50;
bally3 = 90;
bally = 200;
}
}
}