Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 43 additions & 10 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,54 @@ var snakeGame = (function () {
let dx = 1;
let dy = 0;
let currentStep = 1;
let foodCounter = 0;

let playStep = () => {
headX += dx;
headY += dy;

let playStep = (direction) => {
dx = (direction - 38) % 2;
dy = (direction - 39) % 2;
headX = (WIDTH - 1) * (WIDTH - (headX + dx)) % WIDTH;
headY = (HEIGHT - 1) * (HEIGHT - (headY + dy)) % HEIGHT;
let newHeadCell = getCell(headX, headY);
let newFoodCell = getCell(Math.ceil(Math.random() * WIDTH), Math.ceil(Math.random() * HEIGHT));

removeSnakeCells();
removeSnakeCells();
addSnakeToCell(newHeadCell);
removeFoodCells();
addFoodToCell(newFoodCell);
setCellStep(newHeadCell, currentStep);

currentStep++;
return true;
}

let removeFoodCells = () => {
for (let foodCell of document.getElementsByClassName('food snake')) {
removeFoodFromCell(foodCell);
foodCounter--;
}
}

let getCellClass = (cell) => {
return cell.className;
}

let removeFoodFromCell = cell => {
cell.className = cell.className.replace('food', '');
}

let getFoodCells = () => {
// Можно находить элементы DOM по классу
return document.getElementsByClassName('food');
}

let addFoodToCell = cell => {
if (foodCounter == 0 && cell.className == '') {
cell.className += ' food';
foodCounter++;
}
}

let removeSnakeCells = () => {
for (let snakeCell of getSnakeCells())
removeSnakeFromCell(snakeCell);
Expand Down Expand Up @@ -63,7 +97,7 @@ var snakeGame = (function () {
cell.setAttribute('data-step', step);
}

let getCellStep = cell => {
let getCellStep = cell => {
// Явное преобразование строки к числу через объект-обертку.
return new Number(cell.getAttribute('data-step'));
}
Expand All @@ -77,18 +111,17 @@ var snakeGame = (function () {

// Главный цикл игры.
let timer = setInterval(function () {
if (!snakeGame.playStep())
{
if (!snakeGame.playStep(direction)) {
// Игра закончена - останавливаем цикл.
clearInterval(timer);
alert('Game Over! Score: ' + snakeGame.getScore());
}
}, 500);

var direction = 39;
// Обработка действий пользователя.
document.onkeydown = e => {
switch (e.keyCode)
{
// http://keycode.info/
if (e.keyCode < 41 && e.keyCode > 36) {
direction = e.keyCode;
}
}