Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Альфа версия (Ранний доступ) #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
100 changes: 96 additions & 4 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,64 @@ var snakeGame = (function () {
const WIDTH = 10;
const HEIGHT = 10;

let score = 0;
let headX = 0;
let headY = 0;
let dx = 1;
let dy = 0;
let currentStep = 1;
let mainDir = [1,0];
let amOfFood = 0;
let foodPos = [0,0];

let teleport = (dir,step) => {
if(dir > 9)
dir = 0;
if(dir < 0)
dir = 9;
return dir;
}

let playStep = () => {
headX += dx;
mainDir = changeDir();
dx = mainDir[0];
dy = mainDir[1];
headX += dx;
headX = teleport(headX);
headY += dy;
headY = teleport(headY);
let newHeadCell = getCell(headX, headY);

removeSnakeCells();
addSnakeToCell(newHeadCell);
setCellStep(newHeadCell, currentStep);

if(amOfFood < 1){
foodPos[0]=Math.floor(Math.random()*WIDTH);
foodPos[1]=Math.floor(Math.random()*HEIGHT);
let newFoodCell = getCell(foodPos[0],foodPos[1]);
addFoodToCell(newFoodCell);
amOfFood++;
}

eating();

currentStep++;
return true;
}

let eating = () => {
if((headX == foodPos[0]) && (headY == foodPos[1]))
{
let foodCell = getCell(foodPos[0],foodPos[1]);
removeFoodFromCell(foodCell);
amOfFood--;
score++;
}
}

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

let removeSnakeCells = () => {
for (let snakeCell of getSnakeCells())
Expand All @@ -53,6 +93,11 @@ var snakeGame = (function () {
// Поэтому добавляем пробел.
cell.className += ' snake';
}

let addFoodToCell = cell => {
//Добавим еду
cell.className += ' food';
}

let removeSnakeFromCell = cell => {
cell.className = cell.className.replace('snake', '');
Expand All @@ -70,11 +115,22 @@ var snakeGame = (function () {

// Только этот объект будет доступен извне.
return {
getScore: 0,
getScore: score,
playStep: playStep
}
})(); // Используем IIFE (Immediately Invoked Function Expression) для изоляции.

var changeDir = function(){
if (down)
return [0,1];
if (up)
return [0,-1];
if (left)
return [-1,0];
if (right)
return [1,0];
}

// Главный цикл игры.
let timer = setInterval(function () {
if (!snakeGame.playStep())
Expand All @@ -85,10 +141,46 @@ let timer = setInterval(function () {
}
}, 500);

var up = false;
var down = false;
var left = false;
var right = true;

// Обработка действий пользователя.
document.onkeydown = e => {
switch (e.keyCode)
{
case 38:
down = false;
up = true;
left = false;
right = false;
break;

case 40:
down = true;
up = false;
left = false;
right = false;
break;

case 37:
down = false;
up = false;
left = true;
right = false;
break;

case 39:
down = false;
up = false;
left = false;
right = true;
break;

default:

break;
// http://keycode.info/
}
}
}