Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <!--
- Original Snake Game coded by Chris DeLeon of Gamkedo https://www.youtube.com/channel/UC15WSgcM_5tmN6JRbhA4UaQ
- Button taken from Ottis Kelleghan on CodePen https://codepen.io/cheeriottis/pen/inluv
- Put together and changed by Chad McAdams
- -->
- <title>Snek Game!</title>
- <body style="background-color:white;"></body>
- <p id="score"></p>
- <canvas id="gc" width="400" height="400"></canvas>
- <style>
- body
- {
- padding: 50px;
- }
- .animate
- {
- transition: all 0.1s;
- -webkit-transition: all 0.1s;
- }
- .action-button
- {
- position: relative;
- padding: 10px 40px;
- margin: 0px 10px 10px 0px;
- float: left;
- border-radius: 10px;
- font-family: 'Pacifico', cursive;
- font-size: 25px;
- color: #FFF;
- text-decoration: none;
- }
- .blue
- {
- background-color: #3498DB;
- border-bottom: 5px solid #2980B9;
- text-shadow: 0px -2px #2980B9;
- }
- .red
- {
- background-color: #E74C3C;
- border-bottom: 5px solid #BD3E31;
- text-shadow: 0px -2px #BD3E31;
- }
- .green
- {
- background-color: #82BF56;
- border-bottom: 5px solid #669644;
- text-shadow: 0px -2px #669644;
- }
- .yellow
- {
- background-color: #F2CF66;
- border-bottom: 5px solid #D1B358;
- text-shadow: 0px -2px #D1B358;
- }
- .action-button:active
- {
- transform: translate(0px,5px);
- -webkit-transform: translate(0px,5px);
- border-bottom: 1px solid;
- }
- </style>
- <a class="action-button shadow animate blue" id="play" onclick="playAgain()" style="display: none;">Play Again?</a>
- <style>
- #gc
- {
- border-style: solid;
- border-color: black;
- }
- </style>
- <script>
- window.onload=function()
- {
- canv = document.getElementById("gc");
- ctx = canv.getContext("2d");
- document.addEventListener("keydown", keyPush);
- setInterval(game, 1000/15);
- setVars();
- }
- function setVars()
- {
- px=py=10;
- gs=tc=20;
- ax=ay=15;
- bx=by=5;
- xv=yv=0;
- trail=[];
- tail = 1;
- score = 0;
- console.log(score);
- }
- function playAgain()
- {
- var x = document.getElementById("gc");
- x.style.display = "block";
- var y = document.getElementById("play");
- y.style.display = "none";
- setVars();
- }
- function gameOver()
- {
- var x = document.getElementById("gc");
- x.style.display = "none";
- var y = document.getElementById("play");
- y.style.display = "block";
- //window.location.href = "./gameOver.html";
- }
- function checkZero()
- {
- if(tail <= 0)
- {
- gameOver();
- }
- }
- function reloadBackground()
- {
- ctx.fillStyle="white";
- ctx.fillRect(0, 0, canv.width, canv.height);
- }
- function reloadTail()
- {
- ctx.fillStyle="lime";
- for (var i=0; i < trail.length; i++)
- {
- ctx.fillRect(trail[i].x*gs, trail[i].y*gs, gs-2, gs-2);
- if(trail[i].x == px && trail[i].y == py)
- {
- tail = i + 1;
- score = i + 1;
- console.log(score);
- document.getElementById("score").innerHTML = score.toString();
- checkZero();
- //different version that I don't like
- //tail = tail - 1;
- }
- }
- trail.push({x:px,y:py});
- while(trail.length > tail)
- {
- trail.shift();
- }
- }
- function reloadBombs() //add suopport for multiple bombs?
- {
- if(bx == px && by == py)
- {
- --tail;
- --score;
- console.log(score);
- document.getElementById("score").innerHTML = score.toString();
- checkZero();
- bx = Math.floor(Math.random()*tc);
- by = Math.floor(Math.random()*tc);
- }
- ctx.fillStyle="black";
- ctx.fillRect(bx*gs, by*gs, gs-2, gs-2);
- }
- function reloadApples() //add support for multiple apples?
- {
- if(ax == px && ay == py)
- {
- tail++;
- score++;
- console.log(score);
- document.getElementById("score").innerHTML = score.toString();
- ax = Math.floor(Math.random()*tc);
- ay = Math.floor(Math.random()*tc);
- }
- ctx.fillStyle="red";
- ctx.fillRect(ax*gs, ay*gs, gs-2, gs-2);
- }
- function outOfBounds()
- {
- tail = 0;
- score = 0;
- console.log(score);
- document.getElementById("score").innerHTML = score.toString();
- checkZero();
- }
- function moving()
- {
- px += xv;
- py += yv;
- if(px < 0)
- {
- //px = tc-1;
- outOfBounds();
- }
- if(px > tc - 1)
- {
- //px = 0;
- outOfBounds();
- }
- if(py < 0)
- {
- //py = tc-1;
- outOfBounds();
- }
- if(py > tc - 1)
- {
- //py = 0;
- outOfBounds();
- }
- }
- function game()
- {
- moving();
- reloadBackground();
- reloadTail();
- reloadApples();
- reloadBombs();
- }
- function keyPush(evt)
- {
- switch(evt.keyCode)
- {
- case 37:
- xv=-1; yv=0;
- break;
- case 38:
- xv=0; yv=-1;
- break;
- case 39:
- xv=1; yv=0;
- break;
- case 40:
- xv=0; yv=1;
- break;
- }
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment