Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta http-equiv="x-ua-compatible" content="ie=edge">
- <title>Atari Breakout</title>
- <meta name="description" content="">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
- <link rel="icon" href="">
- <style>
- * {
- font-family: sans-serif;
- font-size: 16px;
- width: 100%;
- height: 100%;
- padding: 0;
- margin: 0;
- }
- canvas {
- width: 100%;
- height: 99.6%;
- }
- </style>
- </head>
- <body>
- <canvas id="canvas"></canvas>
- <script>
- var canvas = document.getElementById("canvas");
- var canvasContext = canvas.getContext('2d');
- var winW = window.innerWidth;
- var winH = window.innerHeight;
- canvasContext.canvas.width = window.innerWidth;
- canvasContext.canvas.height = window.innerHeight;
- const contX = canvas.width/30; // container X
- const contY = canvas.height/7; // container Y
- const contW = canvas.width - canvas.width/15; // container width
- const contH = canvas.height - canvas.height/5; // container height
- const gameX = contX + contW/100;
- const gameY = contY + contH/50;
- const gameW = contW - contW/50;
- const gameH = contH - contH/25;
- const borderTop = contY + contH/50; // top border of container
- const borderBot = borderTop + (contH - contH/25); // bottom border of container
- const borderLeft = contX + contW/100; // left border of container
- const borderRight = borderLeft + (contW - contW/50); // right border of container
- const paddleW = borderLeft*4; // paddle width
- const paddleH = gameH/25; // paddle height
- const paddleY = borderBot - contH/10; // paddle Y
- var paddleX; // paddle X
- const ballR = 10;
- const maxBallSpeed = 10;
- var ballX,ballY,ballSpeedX,ballSpeedY,firstHit,bounceVAR;
- var score,lives;
- var gameWon,gameOver = false;
- const ROWS = 10;
- const COLUMNS = 5;
- const MAX_SCORE = ROWS*COLUMNS*100;
- var brickDistX = (gameW/ROWS)/(ROWS*5);
- var brickDistY = (gameH/ROWS)/(ROWS*2);
- var brickW = gameW/ROWS - brickDistX;
- var brickH = gameH/(COLUMNS*4) - brickDistY;
- var bricksX = []; // array with X coordinate of each brick
- var bricksY = []; // array with Y coordinate of each brick
- var bricksBroken = []; // array that holds information about which bricks are broken
- var currentColumn = 1;
- for(var j = 0; j < (COLUMNS*ROWS); j += ROWS)
- {
- for(var i = 0; i < ROWS; i++)
- {
- if(i === 0 || (i !== 1 && i%ROWS === 0))
- {
- bricksX[(i+j)] = borderLeft + (brickW * i) + brickDistX*3;
- }
- else
- {
- bricksX[(i+j)] = bricksX[(i-1)] + brickW + (brickDistX/2);
- }
- bricksY[(i+j)] = (borderTop + gameH/10) + ((brickDistY + brickH) * currentColumn);
- bricksBroken[(i+j)] = false;
- }
- currentColumn++;
- }
- canvas.addEventListener('mousemove', // checks if the mouse moves
- function(evt) {
- var mousePos = calculateMousePos(evt);
- if(mousePos.x < borderLeft+(paddleW/2))
- {
- paddleX = gameX+1;
- }
- else if(mousePos.x >= borderRight-(paddleW/2))
- {
- paddleX = (borderRight-paddleW)-1;
- }
- else
- {
- paddleX = mousePos.x - paddleW/2;
- }
- }
- );
- setup();
- function timeout() {
- setTimeout(function () {
- main();
- timeout();
- }, 30);
- };
- timeout();
- function setup()
- {
- firstHit = true;
- score = 0;
- lives = 3;
- ballX = winW/2;
- ballY = gameH/1.5;
- ballSpeedX = 0;
- ballSpeedY = 5;
- paddleX = gameX + gameW/2 - paddleW/2;
- for(var i = 0; i < (ROWS*COLUMNS); i++)
- {
- bricksBroken[i] = false;
- }
- }
- function main()
- {
- move();
- logic();
- draw();
- }
- function draw()
- {
- rect(0,0, winW,winH, 'black'); // canvas
- rect(contX,contY, contW,contH,'white'); // game area container
- rect(gameX,gameY, gameW,gameH,'black'); // game area
- rect(paddleX,paddleY, paddleW,paddleH,'white'); // paddle
- if(!gameOver && !gameWon)
- {
- circle(ballX,ballY, ballR,'white'); // ball
- for(var i = 0; i < (ROWS*COLUMNS); i++)
- {
- if(!bricksBroken[i])
- {
- rect(bricksX[i],bricksY[i], brickW,brickH,'white');
- }
- }
- }
- text("Score : " + score, canvas.width/4,canvas.height/12, canvas.height/16,'white');
- text("Lives : " + lives, canvas.width/4 + 2*(canvas.width/6),canvas.height/12, canvas.height/16,'white');
- }
- function move()
- {
- ballX += ballSpeedX;
- ballY += ballSpeedY;
- }
- function logic()
- {
- // reset the ball when it hits the bottom border
- if(ballY >= borderBot)
- {
- lives--;
- ballX = winW/2;
- ballY = gameH/1.5;
- ballSpeedX = 0;
- ballSpeedY = 5;
- firstHit = true;
- }
- // bounce the ball when it hits a wall
- if(ballY <= borderTop)
- {
- ballSpeedY *= -1;
- }
- if((ballX <= borderLeft) || (ballX >= borderRight))
- {
- ballSpeedX *= -1;
- }
- // bounce the ball when it hits the paddle
- if((ballY >= paddleY) && (ballY <= (paddleY+paddleH)) && (ballX >= paddleX) && (ballX <= (paddleX+paddleW)))
- {
- if(firstHit)
- {
- if(ballX < (paddleX+paddleX/2))
- {
- ballSpeedX = 5;
- }
- else if(ballX >= (paddleX+paddleX/2))
- {
- ballSpeedX = -5;
- }
- ballSpeedY = 10;
- firstHit = false;
- }
- bounceVAR = ballX - (paddleX+paddleW/2);
- ballSpeedX = bounceVAR * 0.35;
- ballSpeedY *= -1;
- if(ballSpeedX > maxBallSpeed)
- {
- ballSpeedX = maxBallSpeed;
- }
- }
- // check if a brick is hit
- for(var i = 0; i < (ROWS*COLUMNS); i++)
- {
- if((ballX >= bricksX[i]) && (ballX <= (bricksX[i]+brickW)) && (ballY >= bricksY[i]) && (ballY <= (bricksY[i]+brickH)) && (!bricksBroken[i]))
- {
- if((ballX - ballSpeedX) < bricksX[i])
- {
- ballX = bricksX[i];
- ballSpeedX *= -1;
- }
- if((ballX - ballSpeedX) > (bricksX[i] + brickW))
- {
- ballX = bricksX[i] + brickW;
- ballSpeedX *= -1;
- }
- if(((ballY - ballSpeedY) < bricksY[i]))
- {
- ballY = bricksY[i];
- ballSpeedY *= -1;
- }
- if((ballY - ballSpeedY) > (bricksY[i] + brickH))
- {
- ballY = bricksY[i] + brickH;
- ballSpeedY *= -1;
- }
- bricksBroken[i] = true;
- score += 100;
- }
- }
- // checks if the player won the game and if the player has no lives left
- if(score >= MAX_SCORE)
- {
- draw();
- alert("Congratulations, you've won the game with " + lives + " lives left!!!");
- setup();
- gameWon = true;
- }
- else if(lives <= 0)
- {
- var prependAlert = "";
- for(var i = 0; i < (score.toString().length); i += 2)
- {
- prependAlert += ' ';
- }
- alert(prependAlert + "\t Game Over\nYou died with a score of " + score);
- setup();
- }
- }
- function rect(x,y, width,height, color)
- {
- canvasContext.fillStyle = color;
- canvasContext.fillRect(x,y, width,height);
- }
- function circle(cX,cY, radius, color)
- {
- canvasContext.fillStyle = color;
- canvasContext.beginPath();
- canvasContext.arc(cX,cY, radius,0,Math.PI*2,true);
- canvasContext.fill();
- }
- function text(txt, x,y, size,color)
- {
- canvasContext.fillStyle = color;
- canvasContext.font = size + "px Arial";
- canvasContext.fillText(txt,x,y);
- }
- function calculateMousePos(evt) { // calculates the position of the mouse
- var rect = canvas.getBoundingClientRect();
- var root = document.documentElement;
- var mouseX = evt.clientX - rect.left - root.scrollLeft;
- var mouseY = evt.clientY - rect.top - root.scrollTop;
- return {
- x:mouseX,
- y:mouseY
- };
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment