xxxxxxxxxx
140
let playerPaddle, computerPaddle;
let ball;
let gameStarted = false;
let gameOver = false;
let gameInfo = true;
let score = 0;
let edges;
function setup() {
const canvas = createCanvas(640, 480);
playerPaddle = createSprite(625, height / 2, 17, 90);
playerPaddle.shapeColor = "cyan";
computerPaddle = createSprite(15, height / 2, 17, 90);
computerPaddle.shapeColor = "red";
ball = createSprite(width / 2, height / 2, 20, 20);
ball.shapeColor = "lime";
edges = createEdgeSprites();
}
function keyPressed() {
if (key === ' ') {
gameStarted = true;
gameInfo = false;
ball.velocityX = 6;
ball.velocityY = 6;
}
if (keyCode === ENTER && gameOver) {
reset();
}
}
function draw() {
background(0);
textSize(30);
fill("violet");
text("Score : " + score, 500, 50);
for (var i = 0; i < 480; i+= 15) {
push();
strokeWeight(3);
stroke(255);
line(width / 2, i, width / 2, i + 7.5);
pop();
}
if (gameInfo && !gameStarted && !gameOver) {
textAlign(CENTER, CENTER);
textSize(30);
fill("yellow");
text("press space to start the game!!", width / 2, height / 2 - 100);
text("score ten points to Win the Game!", width / 2, height / 2 + 100);
}
if (gameStarted && !gameOver) {
playerPaddle.bounceOff(edges);
//computerPaddle.bounceOff(edges);
computerPaddle.y = ball.y;
ball.bounceOff(edges);
//ball.bounceOff (playerPaddle);
//ball.bounceOff (computerPaddle);
if (ball.bounceOff(playerPaddle)) {
ball.velocityX *= 1.0888;
ball.velocityY *= 1.0888;
score += 2;
} else if (ball.bounceOff(computerPaddle)) {
ball.velocityX *= 1.0888;
ball.velocityY *= 1.0888;
}
if (keyCode === UP_ARROW && keyIsPressed) {
playerPaddle.y -= 10;
} else if (keyCode === DOWN_ARROW && keyIsPressed) {
playerPaddle.y += 10;
}
}
if (gameOver) {
textAlign(CENTER, CENTER);
textSize(40);
fill("orange");
text("GAME OVER!!!", width / 2, height / 2 + 100);
fill("lime");
text("press enter to Play Again!", width / 2, height / 2 - 100);
gameStarted = false;
gameInfo = false;
}
if (score >= 10) {
GameWon();
}
GameOver();
drawSprites();
}
function GameOver() {
if (ball.x > 620) {
gameOver = true;
gameStarted = false;
}
}
function reset() {
score = 0;
gameOver = false;
gameStarted = true;
gameInfo = false;
ball.x = width / 2;
ball.y = width / 2;
ball.velocityX = 6;
ball.velocityY = 6;
playerPaddle.y = height / 2;
}
function GameWon() {
textAlign(CENTER, CENTER);
textSize(50);
fill("violet");
strokeWeight(8);
stroke("cyan");
text("YOU WON!!!", width / 2, height / 2);
gameStarted = false;
gameOver = true;
}