xxxxxxxxxx
160
let ball;
let paddle;
let bricks = [];
let w, h;
let gameStarted = false;
let gameInfo = true;
let gameOver = false;
let gameWon = false;
let score = 0;
function setup() {
createCanvas(640, 480);
ball = new Ball(width / 2, height - 100);
paddle = new Paddle(width / 2, height - 80, 90, 15);
createBricks();
}
function keyPressed() {
if (keyCode === RIGHT_ARROW) {
paddle.setDir(1);
} else if (keyCode === LEFT_ARROW) {
paddle.setDir(-1);
}
if (keyCode === ENTER) {
gameInfo = true;
gameOver = false;
gameStarted = false;
gameWon = false;
ball.reset();
paddle.reset();
createBricks();
score = 0;
}
if (key === ' ') {
gameStarted = true;
gameInfo = false;
gameWon = false;
gameOver = false;
}
}
function keyReleased() {
paddle.setDir(0);
}
function draw() {
background("steelblue");
textSize(30);
fill("coral");
stroke(0);
strokeWeight(4);
text("Score : " + score, width - 100, height / 4 - 80);
strokeWeight(2);
stroke("limegreen");
text("Score : " + score, width - 100, height / 4 - 80);
textSize(40);
fill(255);
strokeWeight(4);
stroke("navy")
text("Breakout Game!!", width / 2 - 50, height / 4 - 80);
textSize(15);
strokeWeight(2);
fill("darkorange");
text("by : Abhay and Simon!", width - 80, height - 20);
for (let brick of bricks) {
brick.render();
}
ball.render();
paddle.render();
ball.edges();
ball.end();
ball.won();
if (gameInfo && !gameStarted && !gameOver && !gameWon) {
textAlign(CENTER, CENTER);
textSize(20);
fill("LightGoldenRodYellow");
strokeWeight(3);
stroke(0);
text("use the arrow keys to move the paddle", width / 2, height / 2);
fill("Khaki");
text("Press Space to start the game!!", width / 2, height / 2 + 50);
ball.pos.x = paddle.pos.x;
}
//ball.update();
if (gameStarted && !gameInfo && !gameOver && !gameWon) {
paddle.update();
ball.update();
ball.bounce(paddle);
let ABBrick = false;
for (let i = bricks.length - 1; i >= 0; i--) {
let brick = bricks[i];
if (ball.colliding(brick)) {
if (ABBrick === false) {
ball.bounceOff(brick);
ABBrick = true;
}
score += 1;
bricks.splice(i, 1);
}
}
}
if (gameOver && !gameStarted && !gameInfo && !gameWon) {
fill("darkMagenta");
textAlign(CENTER, CENTER);
strokeWeight(5);
stroke("firebrick");
textSize(50);
text("GAME IS OVER!!", width / 2, height / 2);
fill("Khaki");
textSize(20);
text("press enter to play again!", width / 2, height / 2 + 75);
}
if (gameWon && !gameOver && !gameStarted && !gameInfo) {
textAlign(CENTER, CENTER);
textSize(70);
stroke("Chartreuse");
strokeWeight(6);
fill("MediumSpringGreen");
text("YOU WIN!!!!!", width / 2, height / 2);
stroke(0);
strokeWeight(3);
text("YOU WIN!!!!!", width / 2, height / 2);
fill("cyan");
stroke(0);
textSize(20);
text("THAT WAS A GREAT ACHIVEMENT!!!", width / 2, height / 2 - 100);
fill("Khaki");
text("press enter to play again!", width / 2, height / 2 + 50);
}
}
function createBricks() {
bricks.splice(0);
for (let i = 0; i < 14; i++) {
for (let j = 0; j < 7; j++) {
w = width / 14;
h = 15;
bricks.push(new Brick(i * w + w / 2, j * h + h / 2 + 75, w, h));
}
}
}