а тогда что сделать, чтобы после алерта игра заново началась?
|
Георгий777,
строка 124 <!DOCTYPE html> <html> <head> <style> #output { width:400px; height:400px; border-top: 1px solid gray; border-left: 1px solid gray; } .inner { border-bottom: 1px solid gray; border-right: 1px solid gray; width:19px; height:19px; float: left; } .red{ background-color: #FF0000; } .green{ background-color: #008000; } </style> </head> <body> <div id="output"></div> <script> "use strict" var row = 20, col = 20, len = row * col, direction = "down", ar = [], snake = [], eat = [], timer, score = 0, speed = 500, game_over = false; function createMatrix() { var output = document.getElementById("output"), div, i; for (i = 0; i < len; i++) { div = document.createElement("div"); div.className = "inner"; output.appendChild(div); ar.push(div); } } createMatrix(); function rand(max) { return Math.floor(Math.random() * max); } function generatePoint() { var num = rand(len); snake.push(num); setCell(num, "red"); } generatePoint(); function setCell(num, cls) { cls ? ar[num].classList.add(cls) : ar[num].className = "inner"; } function generateEat() { var temp = ar.reduce(function(a, b, k) { b.classList.contains("red") || b.classList.contains("green") || a.push(k); return a; }, []); var num = temp[rand(temp.length)]; eat.push(num); setCell(num, "green"); } for (var i = 0; i < 50; i++) { //generateEat(); } generateEat(); function move() { var pointNext = snake[0]; if (direction == "right") { pointNext += 1; if (pointNext % col == 0) { pointNext -= col; } } if (direction == "left") { if (pointNext % col == 0) { pointNext += col - 1; } else { pointNext -= 1; } } if (direction == "down") { pointNext += col; if (pointNext > len) { pointNext %= len; } } if (direction == "up") { pointNext -= col; if (pointNext < 0) { pointNext += len; } } if (ar[pointNext].classList.contains("green")) { eat = eat.filter(function(a) { a != pointNext; }); snake.push(0); generateEat(); score++; } switch(score) { case 2: game_over = true; break; case 20: speed = 125; break; case 30: speed = 65; break; } console.log(score, speed) if (ar[pointNext].classList.contains("red")) { game_over = true; } if(game_over == true) { alert("game over"); game_over = false score = 3; // return; } for (var i = 0; i < snake.length; i++) { var point = snake[i]; setCell(point); snake[i] = pointNext; setCell(pointNext, "red"); pointNext = point; } timer = window.setTimeout(move, speed); } move(); document.onkeydown = function(e) { switch(e.keyCode) { case 37: direction = "left"; break; case 38: direction = "up"; break; case 39: direction = "right"; break; case 40: direction = "down"; break; } }; </script> </body> </html> |
рони,
|
Но теперь просто выводится постоянный алерт и игра не останавливается и не начинается заново
|
Георгий777,
строка 126 добавлена |
теперь он вроде начинает заново, но размер змеи - 3, а не один, как должен быть с начала
|
Георгий777,
нужно написать свой resetGame который сотрёт snake |
if(game_over == true) {
alert("game over"); game_over = false score = 3; // return; snake = []; } но так не помогает( |
Георгий777, строка 120
<!DOCTYPE html> <html> <head> <style> #output { width:400px; height:400px; border-top: 1px solid gray; border-left: 1px solid gray; } .inner { border-bottom: 1px solid gray; border-right: 1px solid gray; width:19px; height:19px; float: left; } .red{ background-color: #FF0000; } .green{ background-color: #008000; } </style> </head> <body> <div id="output"></div> <script> "use strict" var row = 20, col = 20, len = row * col, direction = "down", ar = [], snake = [], eat = [], timer, score = 0, speed = 500, game_over = false; function createMatrix() { var output = document.getElementById("output"), div, i; for (i = 0; i < len; i++) { div = document.createElement("div"); div.className = "inner"; output.appendChild(div); ar.push(div); } } createMatrix(); function rand(max) { return Math.floor(Math.random() * max); } function generatePoint() { var num = rand(len); snake.push(num); setCell(num, "red"); } generatePoint(); function setCell(num, cls) { cls ? ar[num].classList.add(cls) : ar[num].className = "inner"; } function generateEat() { var temp = ar.reduce(function(a, b, k) { b.classList.contains("red") || b.classList.contains("green") || a.push(k); return a; }, []); var num = temp[rand(temp.length)]; eat.push(num); setCell(num, "green"); } for (var i = 0; i < 50; i++) { } generateEat(); function move() { var pointNext = snake[0]; if (direction == "right") { pointNext += 1; if (pointNext % col == 0) { pointNext -= col; } } if (direction == "left") { if (pointNext % col == 0) { pointNext += col - 1; } else { pointNext -= 1; } } if (direction == "down") { pointNext += col; if (pointNext > len) { pointNext %= len; } } if (direction == "up") { pointNext -= col; if (pointNext < 0) { pointNext += len; } } if (ar[pointNext].classList.contains("green")) { eat = eat.filter(function(a) { a != pointNext; }); snake.push(0); generateEat(); score++; } switch(score) { case 2: game_over = true; break; case 20: speed = 125; break; case 30: speed = 65; break; } if (ar[pointNext].classList.contains("red")) { game_over = true; } function resetGame() { snake.forEach(function(point) { setCell(point); }); snake.length = 0; eat.forEach(function(point) { setCell(point); }); eat.length = 0; generatePoint(); generateEat(); } if (game_over == true) { alert("game over"); game_over = false; score = 0; resetGame(); } for (var i = 0; i < snake.length; i++) { var point = snake[i]; setCell(point); snake[i] = pointNext; setCell(pointNext, "red"); pointNext = point; } timer = window.setTimeout(move, speed); } move(); document.onkeydown = function(e) { switch(e.keyCode) { case 37: direction = "left"; break; case 38: direction = "up"; break; case 39: direction = "right"; break; case 40: direction = "down"; break; } }; </script> </body> </html> |
спасибо большое, вы супер)
|
Часовой пояс GMT +3, время: 00:45. |