Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>stairfax temperatures</title>
- <style type="text/css">
- #drawCanvas{
- width: 600px;
- height: 600px;
- /* border: 2px solid; */
- border-radius: 30px;
- padding: 15px;
- background-color: white;
- }
- </style>
- </head>
- <body style="background-color: rgb(200,200,200);">
- <div style="width: 100%;">
- <div style="display: table; margin: 15px auto;">
- <canvas id="drawCanvas"/>
- <script>
- var canvas = document.getElementById("drawCanvas");
- canvas.width=600;
- canvas.height=600;
- var ctx = canvas.getContext("2d");
- var snake = {};
- var dot = {};
- var then;
- var gameMode;
- addEventListener("keydown", function (e) {
- if((e.keyCode>=37) && (e.keyCode<=40)){
- if(gameMode!=1) { if(snake.pieces.length>1) { reset(); } gameMode=1; }
- if(snake.direction.length==1)
- snake.direction.pop();
- snake.direction.unshift(e.keyCode-37); // 0-left, 1-up, 2-right, 3-down
- }
- }, false);
- addEventListener("keyup", function (e) {
- if((e.keyCode>=37) && (e.keyCode<=40)){
- var tmp = snake.direction.indexOf(e.keyCode-37);
- if(tmp>-1&&snake.direction.length>1)
- snake.direction.splice(tmp,1); // 0-left, 1-up, 2-right, 3-down
- }
- }, false);
- var reset = function () {
- run=true;
- then=Date.now();
- snake.length=5;
- snake.direction=[0];
- snake.pieces = [[Math.floor(Math.random()*10),Math.floor(Math.random()*10)]];
- do
- dot.location = [Math.floor(Math.random()*10),Math.floor(Math.random()*10)];
- while((dot.location[0] == snake.pieces[0][0]) && (dot.location[1] == snake.pieces[0][1]));
- };
- var update = function () {
- var oldPiece = snake.pieces[0];
- switch(snake.direction[0]){
- case 0: // left
- snake.pieces.unshift([oldPiece[0]-1,oldPiece[1]]);
- break;
- case 1: // up
- snake.pieces.unshift([oldPiece[0],oldPiece[1]-1]);
- break;
- case 2: // right
- snake.pieces.unshift([oldPiece[0]+1,oldPiece[1]]);
- break;
- case 3: // down
- snake.pieces.unshift([oldPiece[0],oldPiece[1]+1]);
- break;
- }
- var p = snake.pieces[0];
- if(p[0]<0||p[0]>9||p[1]<0||p[1]>9)
- gameMode=0;
- if((dot.location[0] == p[0]) && (dot.location[1] == p[1])){
- snake.length++;
- do
- dot.location = [Math.floor(Math.random()*10),Math.floor(Math.random()*10)];
- while((dot.location[0] == snake.pieces[0][0]) && (dot.location[1] == snake.pieces[0][1]));
- }
- while(snake.pieces.length>snake.length)
- snake.pieces.pop();
- };
- var render = function () {
- switch(gameMode){
- case 1:
- ctx.fillStyle="rgb(200,200,200)";
- break;
- default:
- ctx.fillStyle="rgb(220,220,220)";
- break;
- }
- for(var y=0;y<10;y++){
- for(var x=0;x<10;x++){
- ctx.fillRect(10+x*60,10+y*60,40,40);
- }
- }
- if(gameMode==1){
- ctx.fillStyle="rgb(10,20,255)";
- ctx.fillRect(10+dot.location[0]*60,10+dot.location[1]*60,40,40);
- }
- switch(gameMode){
- case 1:
- ctx.fillStyle="rgb(255,20,10)";
- break;
- default:
- ctx.fillStyle="rgb(255,110,100)";
- break;
- }
- for (var i=0;i<snake.pieces.length;i++)
- ctx.fillRect(10+snake.pieces[i][0]*60,10+snake.pieces[i][1]*60,40,40);
- };
- var main = function(){
- if(gameMode==1){
- var now = Date.now();
- if(run&&(now-then)>100){
- then=now;
- update();
- }
- }
- render();
- window.requestAnimationFrame(main);
- };
- gameMode=0;
- reset();
- main();
- </script>
- </div>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement