Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html>
- <!--beannshie-->
- <head>
- <meta charset="utf-8">
- <meta http-equiv="x-ua-compatible" content="ie=edge">
- <title>Game Menu</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="https://i.imgur.com/UJ9CaVW.png">
- <style>
- * {
- font-family: sans-serif;
- font-size: 16px;
- }
- body {
- background: url("https://i.pinimg.com/originals/7f/30/66/7f30662655df2ba727a226131755dcf8.png");
- background-repeat: no-repeat;
- background-position: center center;
- }
- .container {
- width: 100%;
- height: 100%;
- padding: 12.5% 0 0 0;
- }
- .btns {
- width: 50%;
- margin: auto;
- background: #000000;
- padding: 1% 3.5% 1% 1%;
- }
- ul {
- list-style-type: none;
- }
- button {
- padding: 1%;
- width: 100%;
- font-size: 20px;
- background: #00CC00;
- border: 2px solid #000000;
- }
- button:hover {
- cursor: pointer;
- background: #00AA00;
- border: 2px solid #008800;
- }
- </style>
- </head>
- <body>
- <canvas id="canvas"></canvas>
- <div class="container">
- <div class="btns">
- <ul>
- <li><button id="atari">Atari Breakout</button></li>
- <li><button id="flappy">Flappy Bird</button></li>
- <li><button id="snake">Snake Game</button></li>
- </ul>
- </div>
- </div>
- <script>
- $("#atari").on("click", function(){
- $("*").css("width","100%");
- $("*").css("height","100%");
- $("*").css("padding","0");
- $("*").css("margin","0");
- $("canvas").css("width","100%");
- $("canvas").css("height","99.6%");
- $("div").css("display","none");
- $("body").css("background","#000000");
- $("title").html("Atari Breakout");
- 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
- };
- }
- });
- $("#flappy").on("click", function(){
- $("*").css("width","100%");
- $("*").css("height","100%");
- $("*").css("padding","0");
- $("*").css("margin","0");
- $("canvas").css("width","100%");
- $("canvas").css("height","99.6%");
- $("div").css("display","none");
- $("body").css("background","#007700");
- $("title").html("Flappy Bird");
- var canvas = document.getElementById("canvas");
- var ctx = canvas.getContext("2d");
- var winW = window.innerWidth;
- var winH = window.innerHeight;
- canvas.width = window.innerWidth;
- canvas.height = window.innerHeight;
- document.onkeydown = checkKey;
- var gameSpeed = 30;
- var x,y,score;
- var scored = false;
- var jumpH = winH/10;
- var gravity = winH/750;
- var maxGrav = 15;
- const pillarCount = 4;
- var pillarX = [];
- var pillarY1 = [];
- var pillarY2 = [];
- var pillarW = winW/(pillarCount*2.75+1);
- var pillarDist = winH/5;
- var ballR = jumpH/3.5;
- setup();
- function timeout() {
- setTimeout(function () {
- main();
- timeout();
- }, gameSpeed);
- };
- timeout();
- function setup()
- {
- x = winW/10;
- y = winH/2;
- score = 0;
- for(var i = 0; i < pillarCount; i++)
- {
- if(i === 0)
- {
- pillarX[i] = winW;
- }
- else
- {
- pillarX[i] = pillarX[(i-1)] + pillarW*4;
- }
- pillarY1[i] = Math.floor(Math.random() * ((winH-winH/5) - (winH/10) + 1)) + (winH/10);
- pillarY2[i] = pillarY1[i] + pillarDist;
- }
- }
- function main()
- {
- logic();
- move();
- draw();
- }
- function move()
- {
- if(gravity < maxGrav/2)
- {
- gravity *= 1.1;
- }
- else if(gravity < maxGrav)
- {
- gravity *= 1.05;
- }
- y += gravity;
- for(var i = 0; i < pillarCount; i++)
- {
- pillarX[i] -= 10;
- }
- }
- function logic()
- {
- if(y < 0 || y > winH)
- {
- alert("You died with a score of " + score);
- setup();
- }
- for(var i = 0; i < pillarCount; i++)
- {
- if(x >= pillarX[i] && x <= pillarX[i]+pillarW && y-ballR/1.5 > pillarY1[i] && y+ballR/1.5 < pillarY2[i])
- {
- if(!scored)
- {
- score++;
- scored = true;
- }
- }
- else if(x+ballR/1.5 >= pillarX[i] && x-ballR/1.5 <= pillarX[i]+pillarW && (y-ballR/1.5 < pillarY1[i] || y+ballR/1.5 > pillarY2[i]))
- {
- alert("You died with a score of " + score);
- setup();
- }
- if(scored)
- {
- if(x > pillarX[i]+pillarW)
- {
- scored = false;
- }
- }
- if(pillarX[i]+pillarW <= 0)
- {
- pillarX[i] += winW + pillarW*4;
- pillarY1[i] = Math.floor(Math.random() * ((winH-winH/5) - (winH/10) + 1)) + (winH/10);
- pillarY2[i] = pillarY1[i] + pillarDist;
- }
- }
- }
- function draw()
- {
- rect(0,0, winW,winH, '#0088CC');
- circle(x,y, ballR,'#CCCC00');
- for(var i = 0; i < pillarCount; i++)
- {
- rect(pillarX[i],0, pillarW,pillarY1[i],'#007700');
- rect(pillarX[i],pillarY2[i], pillarW,(winH-pillarY2[i]),'#007700');
- }
- ctx.fillStyle = 'red';
- ctx.font = 'normal bold 42px sans-serif';
- ctx.textAlign = 'center';
- ctx.fillText(score,winW/2,winH/15);
- }
- function rect(x,y, width,height, color)
- {
- ctx.fillStyle = color;
- ctx.fillRect(x,y, width,height);
- }
- function circle(cX,cY, radius, color)
- {
- ctx.fillStyle = color;
- ctx.beginPath();
- ctx.arc(cX,cY, radius,0,Math.PI*2,true);
- ctx.fill();
- }
- function checkKey(e)
- {
- var keynum;
- if(window.event) { // IE
- keynum = e.keyCode;
- } else if(e.which){ // Netscape/Firefox/Opera
- keynum = e.which;
- }
- switch(keynum)
- {
- case 32:
- gravity = 0;
- j = Math.floor(jumpH/2);
- (function theLoop (i) {
- setTimeout(function () {
- y -= 2;
- if (--i) {
- theLoop(i);
- }
- }, 1);
- })(j);
- setTimeout(function () { gravity = winH/750; }, j);
- break;
- }
- }
- });
- $("#snake").on("click", function(){
- $("*").css("width","100%");
- $("*").css("height","100%");
- $("*").css("padding","0");
- $("*").css("margin","0");
- $("canvas").css("width","100%");
- $("canvas").css("height","99.6%");
- $("div").css("display","none");
- $("body").css("background","#000000");
- $("title").html("Snake Game");
- var canvas = document.getElementById("canvas");
- var canvasContext = canvas.getContext('2d');
- var winW = window.innerWidth;
- var winH = window.innerHeight;
- canvas.width = window.innerWidth;
- canvas.height = window.innerHeight;
- document.onkeydown = checkKey;
- var x,y,fruitX,fruitY,nTail,score;
- var tailX = [];
- var tailY = [];
- const COLUMNS = 15;
- const ROWS = COLUMNS;
- var gameSpeed = 120;
- const directions = {
- UP: 'up',
- DOWN: 'down',
- LEFT: 'left',
- RIGHT: 'right'
- };
- let direciton;
- setup();
- function timeout() {
- setTimeout(function () {
- main();
- timeout();
- }, gameSpeed);
- };
- timeout();
- function main()
- {
- draw();
- move();
- logic();
- }
- function setup()
- {
- x = 7;
- y = 7;
- score = 0;
- nTail = 0;
- tailX = [];
- tailY = [];
- direction = null;
- fruitX = Math.floor(Math.random() * ROWS);
- fruitY = Math.floor(Math.random() * COLUMNS);
- }
- function draw()
- {
- const GAME_HEIGHT = winH - (winH/50);
- const GAME_WIDTH = GAME_HEIGHT;
- const GAME_X = (winW/2)-(GAME_WIDTH/2);
- const GAME_Y = winH/100;
- const TILE_WIDTH = GAME_WIDTH / ROWS;
- const TILE_HEIGHT = GAME_HEIGHT / COLUMNS;
- const HEAD_RADIUS = (TILE_WIDTH - (TILE_WIDTH / 3)) / 2;
- const TAIL_RADIUS = TILE_WIDTH / 4;
- rect(0,0, winW,winH, 'black'); // canvas
- rect(GAME_X,GAME_Y, GAME_WIDTH,GAME_HEIGHT, 'white');
- for(var i = 0; i < COLUMNS; i++)
- {
- for(var j = 0; j < ROWS; j++)
- {
- rect(GAME_X+(i*TILE_WIDTH),GAME_Y+(j*TILE_HEIGHT), TILE_WIDTH,TILE_HEIGHT, 'red');
- if(x === i && y === j)
- {
- var circleX = ((GAME_X+(i*TILE_WIDTH)) - (TILE_WIDTH / 2)) + TILE_WIDTH;
- var circleY = ((GAME_Y+(j*TILE_HEIGHT)) - (TILE_HEIGHT / 2)) + TILE_HEIGHT;
- circle(circleX,circleY, HEAD_RADIUS, '#555555');
- }
- if(fruitX === i && fruitY === j)
- {
- var circleX = ((GAME_X+(i*TILE_WIDTH)) - (TILE_WIDTH / 2)) + TILE_WIDTH;
- var circleY = ((GAME_Y+(j*TILE_HEIGHT)) - (TILE_HEIGHT / 2)) + TILE_HEIGHT;
- circle(circleX,circleY, TAIL_RADIUS, '#00AA00');
- }
- for(var k = 0; k < nTail; k++)
- {
- if (tailX[k] === i && tailY[k] === j)
- {
- var circleX = ((GAME_X+(i*TILE_WIDTH)) - (TILE_WIDTH / 2)) + TILE_WIDTH;
- var circleY = ((GAME_Y+(j*TILE_HEIGHT)) - (TILE_HEIGHT / 2)) + TILE_HEIGHT;
- circle(circleX,circleY, TAIL_RADIUS, '#555555');
- }
- }
- }
- }
- }
- function move()
- {
- switch(direction)
- {
- case directions.UP:
- y--;
- break;
- case directions.DOWN:
- y++;
- break;
- case directions.LEFT:
- x--;
- break;
- case directions.RIGHT:
- x++;
- break;
- }
- }
- function logic()
- {
- if(x < 0 || x >= ROWS || y < 0 || y >= COLUMNS)
- {
- alert("Game Over - you died with a score of " + score + ".");
- setup();
- }
- if(x === fruitX && y === fruitY)
- {
- fruitX = Math.floor(Math.random() * ROWS);
- fruitY = Math.floor(Math.random() * COLUMNS);
- score += 100;
- if(nTail === 0)
- {
- nTail += 2;
- }
- else
- {
- nTail++;
- }
- }
- for(var i = 0; i < nTail; i++)
- {
- if(tailX[i] === x && tailY[i] === y && i !== 1)
- {
- draw();
- alert("Game Over - you bit youself, you died with a score of " + score + ".");
- setup();
- }
- if(tailX[i] === x && tailY[i] === y && i === 1)
- {
- switch(direction)
- {
- case directions.UP:
- y += 2;
- direction = directions.DOWN;
- break;
- case directions.DOWN:
- y -= 2;
- direction = directions.UP;
- break;
- case directions.LEFT:
- x += 2;
- direction = directions.RIGHT;
- break;
- case directions.RIGHT:
- x -= 2;
- direction = directions.LEFT;
- break;
- }
- }
- }
- var prevX = tailX[0];
- var prevY = tailY[0];
- var prevX2, prevY2;
- tailX[0] = x;
- tailY[0] = y;
- for (var i = 1; i < nTail; i++)
- {
- prevX2 = tailX[i];
- prevY2 = tailY[i];
- tailX[i] = prevX;
- tailY[i] = prevY;
- prevX = prevX2;
- prevY = prevY2;
- }
- }
- 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 checkKey(e)
- {
- var keynum;
- if(window.event) { // IE
- keynum = e.keyCode;
- } else if(e.which){ // Netscape/Firefox/Opera
- keynum = e.which;
- }
- switch(keynum)
- {
- case 87: // w
- if(direction !== directions.DOWN)
- {
- direction = directions.UP;
- }
- else if(nTail === 0)
- {
- direction = directions.UP;
- }
- break;
- case 65: // a
- if(direction !== directions.RIGHT)
- {
- direction = directions.LEFT;
- }
- else if(nTail === 0)
- {
- direction = directions.LEFT;
- }
- break;
- case 83: // s
- if(direction !== directions.UP)
- {
- direction = directions.DOWN;
- }
- else if(nTail === 0)
- {
- direction = directions.DOWN;
- }
- break;
- case 68: // d
- if(direction !== directions.LEFT)
- {
- direction = directions.RIGHT;
- }
- else if(nTail === 0)
- {
- direction = directions.RIGHT;
- }
- break;
- }
- }
- });
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment