Du må være en sjov fætter, som tror du får foræret det hele på et sølvfad og kan tjene på det ?
Men snake er gammelt.
- var snake = {
- /**
- *
- * 0 = Free
- * 1 = Wall
- *
- */
- map : [
- [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
- [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
- ],
- gameOver : false, //To stop the game if player is dead
- points : 0, //Player points
- snakeDir : "right", //Current direction of snake
- snakeCur : [1,1], //Current position of the snakes head
- snakePos : [], //Array holding snake body
- baitPos : [], //Bait position
-
- /**
- * Function called to create game, this will start the main game loop
- *
- */
- init: function () {
- this.fieldRows = this.map.length;
- this.fieldCols = this.map[0].length;
- this.newBait();
- this.gameLoop();
- },
-
- /**
- * Main loop running the game
- *
- */
- gameLoop : function () {
- if(!this.gameOver) {
- this.moveSnake();
- this.show();
-
- window.setTimeout('snake.gameLoop();', 100);
- }
- },
-
- /**
- * Function to move snake in it's current direction
- *
- */
- moveSnake : function () {
-
- //Creating holder for temporary position
- var tmpPos = [0,0];
-
-
- //Setting temporary position
- //Up
- if(this.snakeDir=="up") {
- tmpPos = [this.snakeCur[0]-1, this.snakeCur[1]];
- //Right
- } else if(this.snakeDir=="right") {
- tmpPos = [this.snakeCur[0], this.snakeCur[1]+1];
- //Down
- } else if(this.snakeDir=="down") {
- tmpPos = [this.snakeCur[0]+1, this.snakeCur[1]];
- //Left
- } else if(this.snakeDir=="left") {
- tmpPos = [this.snakeCur[0], this.snakeCur[1]-1];
- }
-
- //If the temporary position is available - move the snake
- if(this.fieldAvailable(tmpPos)) {
-
- //If the snake hit some bait!
- if(tmpPos[0]==this.baitPos[0] && tmpPos[1]==this.baitPos[1]) {
- this.newBait();
- this.points++;
- document.getElementById('points').innerHTML = this.points;
- //Else remove last element from snake
- } else {
- this.snakePos.shift();
- }
-
- //Push new element to snake
- this.snakePos.push(this.snakeCur);
- this.snakeCur = tmpPos;
-
- //If field isn't available it means the snake hit wall or it self
- } else {
- alert("You died!");
- this.gameOver = true;
- }
- },
- /**
- * Function that prints the entire game board for each this.gameLoop() run
- *
- */
- show : function () {
- var output = '';
-
- //Running throughj map
- for(var i=0; i<this.fieldRows; i++) {
- for(var j=0; j<this.fieldCols; j++) {
- var field = this.map[i][j];
-
- //If it's a wall
- if(field==1) {
- output += "#";
-
- //If it's part of snake body
- } else if(this.hasSnake([i,j])) {
- output += "*";
-
- //If it's snakes head
- } else if(i==this.snakeCur[0] && j==this.snakeCur[1]) {
- output += "@";
-
- //If it's bait
- } else if(i==this.baitPos[0] && j==this.baitPos[1]) {
- output += "o";
-
- //Or else just an empty field
- } else {
- output += " ";
- }
- }
- output += "<br />";
- }
- document.getElementById('gameField').innerHTML = output;
- },
-
- /**
- * Function to determine if a field has some part of the snake in it
- *
- */
- hasSnake : function(pos) {
- //Running through snake body
- for(var i in this.snakePos) {
- if(this.snakePos[i][0]==pos[0] && this.snakePos[i][1]==pos[1]) {
- return true;
- }
- }
- return false;
- },
- /**
- * Funtion to check if a field DOES NOT have wall and DOES NOT have snake
- *
- */
- fieldAvailable : function (pos) {
- if(this.map[pos[0]][pos[1]]==0 && !this.hasSnake([pos[0],pos[1]])) {
- return true;
- }
- return false;
- },
-
- /**
- * Function to place a bait at a random place on the map
- *
- */
- newBait : function () {
-
- //Getting random position within field size
- var y = Math.floor(Math.random()*(this.fieldRows-1))+1;
- var x = Math.floor(Math.random()*(this.fieldCols-1))+1;
-
- //If field availabel, set bait
- if(this.fieldAvailable([y,x])) {
- this.baitPos = [y,x];
- //else try again
- } else {
- this.newBait();
- }
- }
- }
-
- /**
- * Moving snake
- * Preventing from moving in the direction you come from - cause that'll kill you!
- */
- window.onkeydown = function (e) {
- e = e ? e : window.event;
- //Left
- if(e.keyCode==37 && snake.snakeDir!="right") {
- snake.snakeDir = "left";
- } else if(e.keyCode==38 && snake.snakeDir!="down") {
- snake.snakeDir = "up";
- } else if(e.keyCode==39 && snake.snakeDir!="left") {
- snake.snakeDir = "right";
- } else if(e.keyCode==40 && snake.snakeDir!="up") {
- snake.snakeDir = "down";
- }
- }
-
- //Start game when window is loaded
- window.onload = function () {
- snake.init();
- }
- body {
- font-family: courier new;
- }
-
- #gameField {
- font-size: 20px;
- position: relative;
- width: 360px;
- height: 440px;
- margin: 0 auto;
- border: 1px solid #000;
- background-color: black;
- color: white;
-
- }
- <!DOCTYPE html>
- <html>
- <head>
- <link rel="stylesheet" href="style.css" />
- <script type="text/javascript" src="snake.js"></script>
- </head>
- <body>
- <h1 style="text-align: center;">Snake</h1>
- <div style="text-align: center;">Points: <span id="points">0</span></div>
- <div id="gameField"></div>
- </body>
- </html>