Игра "Змейка" — различия между версиями

Материал из Department of Theoretical and Applied Mechanics
Перейти к: навигация, поиск
(Код программы)
(Код программы)
Строка 34: Строка 34:
 
  var rightCode = 39;
 
  var rightCode = 39;
 
  var pause = false;
 
  var pause = false;
function get_mouse_coords(e){  
+
function get_mouse_coords(e){  
var m = {};
+
var m = {};
var rect = SnakeS.getBoundingClientRect();
+
var rect = SnakeS.getBoundingClientRect();
m.x = e.clientX - rect.left;
+
m.x = e.clientX - rect.left;
m.y = e.clientY - rect.top;
+
m.y = e.clientY - rect.top;
return m;
+
return m;
}
+
}
SnakeS.onclick = function(e){
+
SnakeS.onclick = function(e){
var m = get_mouse_coords(e);
+
  var m = get_mouse_coords(e);
console.log(m.x, m.y);
+
  console.log(m.x, m.y);
if ((m.x>(w/2-95))&&(m.x<(w/2+95))&&(m.y>(h/2+30))&&(m.y<(h/2+70))) {
+
  if ((m.x>(w/2-95))&&(m.x<(w/2+95))&&(m.y>(h/2+30))&&(m.y<(h/2+70))) {
console.log(1);
+
  console.log(1);
location.reload();
+
  location.reload();
}
+
  }
}
+
}
 
  var sl=1;
 
  var sl=1;
 
  var nofood = false;
 
  var nofood = false;
Строка 55: Строка 55:
 
  var y = h/n;
 
  var y = h/n;
 
  console.log(x,y);
 
  console.log(x,y);
function box(){
+
function box(){
this.conteins = "empty";
+
  this.conteins = "empty";
}
+
}
+
function Snake(){
function Snake(){
+
  this.head = null;
this.head = null;
+
  this.tail = null;
this.tail = null;
+
  this.body = [];
this.body = [];
+
  this.length = 1;
this.length = 1;
+
}
}
+
var field = [];
var field = [];
+
for (var i=0; i<n; i++) {
for (var i=0; i<n; i++) {
+
  var m = [];
var m = [];
+
  for (var j=0; j<n; j++){
for (var j=0; j<n; j++){
+
  m.push(new box());
m.push(new box());
+
  }
}
+
  field.push(m);
field.push(m);
+
}
}
+
var snake = new Snake();
var snake = new Snake();
+
function finish() {
function finish() {
+
  ctx.textAlign = 'center';
ctx.textAlign = 'center';
+
  ctx.font = '30px Georgia';
ctx.font = '30px Georgia';
+
  pause = true;
pause = true;
+
  console.error("Вы проиграли");
console.error("Вы проиграли");
+
  clearInterval(intervalID);
clearInterval(intervalID);
+
  ctx.fillStyle = 'rgb(180, 234, 255)';
ctx.fillStyle = 'rgb(180, 234, 255)';
+
  ctx.fillRect(0.25*w, 0.25*h, 0.5*w, 0.5*h);
ctx.fillRect(0.25*w, 0.25*h, 0.5*w, 0.5*h);
+
  ctx.strokeStyle = 'black';
ctx.strokeStyle = 'black';
+
  ctx.strokeRect(0.25*w, 0.25*h, 0.5*w, 0.5*h);
ctx.strokeRect(0.25*w, 0.25*h, 0.5*w, 0.5*h);
+
  ctx.fillStyle = 'black';
ctx.fillStyle = 'black';
+
  ctx.fillText('Вы проиграли!', w/2, h/2-40);
ctx.fillText('Вы проиграли!', w/2, h/2-40);
+
  ctx.fillText('Ваш счет: '+score, w/2, h/2);
ctx.fillText('Ваш счет: '+score, w/2, h/2);
+
  ctx.fillStyle = 'rgb(14, 151, 184)';
ctx.fillStyle = 'rgb(14, 151, 184)';
+
  ctx.fillRect(w/2-95, h/2+30, 190, 40);
ctx.fillRect(w/2-95, h/2+30, 190, 40);
+
  ctx.strokeRect(w/2-95, h/2+30, 190, 40);
ctx.strokeRect(w/2-95, h/2+30, 190, 40);
+
  ctx.fillStyle = 'black';
ctx.fillStyle = 'black';
+
  ctx.fillText('Сыграть еще', w/2, h/2+60);
ctx.fillText('Сыграть еще', w/2, h/2+60);
+
  console.info('rtr');
+
}
console.info('rtr');
+
function CreateItem(item){
}
+
  var i=Math.round(Math.random()*(n - 1));
function CreateItem(item){
+
  var j=Math.round(Math.random()*(n - 1));
var i=Math.round(Math.random()*(n - 1));
+
  while (field [i][j].conteins != "empty"){
var j=Math.round(Math.random()*(n - 1));
+
    var i=Math.round(Math.random()*(n - 1));
while (field [i][j].conteins != "empty"){
+
    var j=Math.round(Math.random()*(n - 1));
var i=Math.round(Math.random()*(n - 1));
+
  }
var j=Math.round(Math.random()*(n - 1));
+
  field [i][j].conteins = item;
}
+
}
field [i][j].conteins = item;
+
function Init(){
}
+
  var i=Math.round(Math.random()*(n - 1));
function Init(){
+
  var j=Math.round(Math.random()*(n - 1));
var i=Math.round(Math.random()*(n - 1));
+
  field [i][j].conteins = "food";
var j=Math.round(Math.random()*(n - 1));
+
  var i=Math.round(Math.random()*(n - 1));
field [i][j].conteins = "food";
+
  var j=Math.round(Math.random()*(n - 1));
var i=Math.round(Math.random()*(n - 1));
+
  while (field [i][j].conteins == "food"){
var j=Math.round(Math.random()*(n - 1));
+
    var i=Math.round(Math.random()*(n - 1));
while (field [i][j].conteins == "food"){
+
    var j=Math.round(Math.random()*(n - 1));
var i=Math.round(Math.random()*(n - 1));
+
  }
var j=Math.round(Math.random()*(n - 1));
+
  field [i][j].conteins = "snake";
}
+
  snake.head = {x: i, y: j};
field [i][j].conteins = "snake";
+
  snake.tail = {x: i, y: j};
snake.head = {x: i, y: j};
+
  snake.body.push({x: i, y: j}) ;
snake.tail = {x: i, y: j};
+
  console.log(direction, snake.body, snake.head);
snake.body.push({x: i, y: j}) ;
+
  CreateItem('nofood');
console.log(direction, snake.body, snake.head);
+
}
+
function Draw() {
CreateItem('nofood');
+
  for (var i=0; i<n; i++) {
}
+
  for (var j=0; j<n; j++){
function Draw() {
+
    if (field[i][j].conteins == "empty") {
for (var i=0; i<n; i++) {
+
    ctx.fillStyle = 'white';
for (var j=0; j<n; j++){
+
    ctx.fillRect(x*i, y*j, x, y);
if (field[i][j].conteins == "empty") {
+
    }
ctx.fillStyle = 'white';
+
    if (field[i][j].conteins == "food") {
ctx.fillRect(x*i, y*j, x, y);
+
    ctx.fillStyle = 'red';
}
+
    ctx.fillRect(x*i, y*j, x, y);
if (field[i][j].conteins == "food") {
+
    ctx.strokeStyle = 'black';
ctx.fillStyle = 'red';
+
    ctx.strokeRect(x*i, y*j, x, y);
ctx.fillRect(x*i, y*j, x, y);
+
    }
ctx.strokeStyle = 'black';
+
    if (field[i][j].conteins == "snake") {
ctx.strokeRect(x*i, y*j, x, y);
+
    ctx.fillStyle = 'green';
}
+
    ctx.fillRect(x*i, y*j, x, y);
if (field[i][j].conteins == "snake") {
+
    ctx.strokeStyle = 'black';
ctx.fillStyle = 'green';
+
    ctx.strokeRect(x*i, y*j, x, y);
ctx.fillRect(x*i, y*j, x, y);
+
    }
ctx.strokeStyle = 'black';
+
    if (field[i][j].conteins == "nofood") {
ctx.strokeRect(x*i, y*j, x, y);
+
    ctx.fillStyle = 'blue';
}
+
    ctx.fillRect(x*i, y*j, x, y);
if (field[i][j].conteins == "nofood") {
+
    ctx.strokeStyle = 'black';
ctx.fillStyle = 'blue';
+
    ctx.strokeRect(x*i, y*j, x, y);
ctx.fillRect(x*i, y*j, x, y);
+
    }
ctx.strokeStyle = 'black';
+
    if (field[i][j].conteins == "nofoodno") {
ctx.strokeRect(x*i, y*j, x, y);
+
    ctx.fillStyle = 'yellow';
}
+
    ctx.fillRect(x*i, y*j, x, y);
if (field[i][j].conteins == "nofoodno") {
+
    ctx.strokeStyle = 'black';
ctx.fillStyle = 'yellow';
+
    ctx.strokeRect(x*i, y*j, x, y);
ctx.fillRect(x*i, y*j, x, y);
+
    }
ctx.strokeStyle = 'black';
+
  }
ctx.strokeRect(x*i, y*j, x, y);
+
  }
}
+
  document.getElementById('scre').innerHTML = score;
}
+
}
}
+
window.onkeydown = function(evt) {  
document.getElementById('scre').innerHTML = score;
+
evt = evt || window.event;
}
+
var charCode = evt.keyCode || evt.which;
window.onkeydown = function(evt) {  
+
console.log(charCode);  
evt = evt || window.event;
+
if (((charCode == wCode)||(charCode == upCode)) && direction != "down") {
var charCode = evt.keyCode || evt.which;
+
  direction = "up";
console.log(charCode);  
+
}
if (((charCode == wCode)||(charCode == upCode)) && direction != "down") {
+
if (((charCode == sCode)||(charCode == downCode)) && direction != "up") {
direction = "up";
+
  direction = "down";  
}
+
}
if (((charCode == sCode)||(charCode == downCode)) && direction != "up") {
+
if (((charCode == aCode)||(charCode == leftCode)) && direction != "right") {
direction = "down";  
+
  direction = "left";
}
+
}
if (((charCode == aCode)||(charCode == leftCode)) && direction != "right") {
+
if (((charCode == dCode)||(charCode == rightCode)) && direction != "left") {
direction = "left";
+
  direction = "right";
}
+
}
if (((charCode == dCode)||(charCode == rightCode)) && direction != "left") {
+
}
direction = "right";
+
function CheckLength() {
}
+
  if ((score == 3) && (sl != 2)){
}
+
  sl = 2;
function CheckLength() {
+
  ms = ms-100;
if ((score == 3) && (sl != 2)){
+
  clearInterval(intervalID);
sl = 2;
+
  intervalID = setInterval(control, ms);
ms = ms-100;
+
  return ;
clearInterval(intervalID);
+
  }
intervalID = setInterval(control, ms);
+
  if ((score == 5 ) && (sl != 3)){
return ;
+
  sl = 3;
}
+
  ms = ms-50;
if ((score == 5 ) && (sl != 3)){
+
  clearInterval(intervalID);
sl = 3;
+
  intervalID = setInterval(control, ms);
ms = ms-50;
+
  }
clearInterval(intervalID);
+
}
intervalID = setInterval(control, ms);
+
function Calcul(){
}
+
  var next  
}
+
  try {
function Calcul(){
+
  if (direction == "left") {
var next  
+
  next = {x: snake.head.x-1, y: snake.head.y}
try {
+
  }
if (direction == "left") {
+
  if (direction == "right") {
next = {x: snake.head.x-1, y: snake.head.y}
+
  next = {x: snake.head.x+1, y: snake.head.y}
}
+
  }
if (direction == "right") {
+
  if (direction == "up") {
next = {x: snake.head.x+1, y: snake.head.y}
+
  next = {x: snake.head.x, y: snake.head.y-1}
}
+
  }
if (direction == "up") {
+
  if (direction == "down" && snake.length == 1) {
next = {x: snake.head.x, y: snake.head.y-1}
+
  next = {x: snake.head.x, y: snake.head.y+1}
}
+
  }
if (direction == "down" && snake.length == 1) {
+
  if (direction == "down" && snake.length > 1) {
next = {x: snake.head.x, y: snake.head.y+1}
+
  next = {x: snake.head.x, y: snake.head.y}
}
+
  }
if (direction == "down" && snake.length > 1) {
+
  if (score > 5) {
next = {x: snake.head.x, y: snake.head.y}
+
  if (next.x<0) {
}
+
    next.x = n-1
if (score > 5) {
+
    console.log('TP')
if (next.x<0) {
+
    }
next.x = n-1
+
  if (next.y<0) {
console.log('TP')
+
    next.y = n-1
}
+
    console.log('TP')
if (next.y<0) {
+
   
next.y = n-1
+
  if (next.x>n-1) {
console.log('TP')
+
    next.x = 0
}
+
    console.log('TP')
if (next.x>n-1) {
+
    }
next.x = 0
+
  if (next.y>n-1) {
console.log('TP')
+
    next.y = 0
}
+
    console.log('TP')
if (next.y>n-1) {
+
    }
next.y = 0
+
  } else {
console.log('TP')
+
  if (next.x<0) {
}
+
    finish();
} else {
+
    return;
if (next.x<0) {
+
    }
finish();
+
  if (next.y<0) {
return;
+
    finish();
}
+
    return;
if (next.y<0) {
+
    }
finish();
+
  if (next.x>n-1) {
return;
+
    finish();
}
+
    return;
if (next.x>n-1) {
+
    }
finish();
+
  if (next.y>n-1) {
return;
+
    finish();
}
+
    return;
if (next.y>n-1) {
+
    }
finish();
+
  }
return;
+
  if (field[next.x][next.y].conteins == "snake") {
}
+
  finish();
}
+
  return;
+
  }
if (field[next.x][next.y].conteins == "snake") {
+
  if (field[next.x][next.y].conteins == "food") {
finish();
+
  snake.body.push({x:next.x, y:next.y});
return;
+
  snake.head = {x:next.x, y:next.y};
}
+
  field[next.x][next.y].conteins = "snake";
if (field[next.x][next.y].conteins == "food") {
+
  CreateItem ('food');
snake.body.push({x:next.x, y:next.y});
+
  score++;
snake.head = {x:next.x, y:next.y};
+
  }
field[next.x][next.y].conteins = "snake";
+
  if (field[next.x][next.y].conteins == "nofoodno") {
CreateItem ('food');
+
  snake.body.push({x:next.x, y:next.y});
score++;
+
  snake.head = {x:next.x, y:next.y};
}
+
  field[next.x][next.y].conteins = "snake";
if (field[next.x][next.y].conteins == "nofoodno") {
+
  field[snake.body[0].x][snake.body[0].y].conteins = "empty";
snake.body.push({x:next.x, y:next.y});
+
  snake.body.shift();
snake.head = {x:next.x, y:next.y};
+
  if(snake.body.length!=1)
field[next.x][next.y].conteins = "snake";
+
    field[snake.body[0].x][snake.body[0].y].conteins = "empty";
field[snake.body[0].x][snake.body[0].y].conteins = "empty";
+
    snake.body.shift();
snake.body.shift();
+
  score++;
if(snake.body.length!=1)
+
  nofoodno=false;
field[snake.body[0].x][snake.body[0].y].conteins = "empty";
+
  }
snake.body.shift();
+
  if (field[next.x][next.y].conteins == "nofood") {
score++;
+
  snake.body.push({x:next.x, y:next.y});
nofoodno=false;
+
  snake.head = {x:next.x, y:next.y};
}
+
  field[next.x][next.y].conteins = "snake";
if (field[next.x][next.y].conteins == "nofood") {
+
  field[snake.body[0].x][snake.body[0].y].conteins = "empty";
snake.body.push({x:next.x, y:next.y});
+
  snake.body.shift();
snake.head = {x:next.x, y:next.y};
+
  score++;
field[next.x][next.y].conteins = "snake";
+
  nofood=false;
field[snake.body[0].x][snake.body[0].y].conteins = "empty";
+
  }
snake.body.shift();
+
  if (field[next.x][next.y].conteins == "empty") {
score++;
+
  snake.body.push({x:next.x, y:next.y});
nofood=false;
+
  snake.head = {x:next.x, y:next.y};
}
+
  field[next.x][next.y].conteins = "snake";
if (field[next.x][next.y].conteins == "empty") {
+
  field[snake.body[0].x][snake.body[0].y].conteins = "empty";
snake.body.push({x:next.x, y:next.y});
+
  snake.body.shift();
snake.head = {x:next.x, y:next.y};
+
  }
field[next.x][next.y].conteins = "snake";
+
  } catch (err){
field[snake.body[0].x][snake.body[0].y].conteins = "empty";
+
  console.error("Что то пошло не так; X=", next.x, "Y=", next.y);
snake.body.shift();
+
  clearInterval(intervalID);
}
+
  }
} catch (err){
+
  if (((score+1) % 5 == 0)&&(!nofood)){
console.error("Что то пошло не так; X=", next.x, "Y=", next.y);
+
  CreateItem ('nofood');
clearInterval(intervalID);
+
  nofood=true;
}
+
  }
if (((score+1) % 5 == 0)&&(!nofood)){
+
  if (((score+1) % 8 == 0)&&(!nofoodno)){
CreateItem ('nofood');
+
  CreateItem ('nofoodno');
nofood=true;
+
  nofoodno=true;
}
+
  }
if (((score+1) % 8 == 0)&&(!nofoodno)){
+
}
CreateItem ('nofoodno');
+
function control (){
nofoodno=true;
+
  if (!pause)
}
+
  Calcul ();
}
+
  if (!pause)
+
  Draw();
function control (){
+
  CheckLength();
if (!pause)
+
}
Calcul ();
+
Init();
if (!pause)
+
Draw ();
Draw();
+
intervalID = setInterval(control, ms);
CheckLength();
 
}
 
Init();
 
Draw ();
 
intervalID = setInterval(control, ms);
 
 
}
 
}

Версия 01:03, 2 июня 2019

Описание

В этой игре пользователь управляет змейкой, которая ползает по плоскости, ограниченной стенками, и собирает еду, избегая столкновения с собственным хвостом и краями игрового поля.

Исполнители: Демина Ксения, Прохоренкова И.Г., Малышева В.Н.

Группа 13632/1 Кафедра Теоретической механики.

Файл: Текст курсового проекта

Математическая модель

Поле представляет собой прямоугольник, в пределах которого движется зеленая ячейка, являющаяся змейкой, и красные, синие и желтые ячейки, которые представляют собой еду для нее.

Визуализация

Код программы

<syntaxhighlight lang=javascript> window.addEventListener("load", main_code, false); function main_code(){

var ctx = SnakeS.getContext("2d");
var h = SnakeS.height;
var w = SnakeS.width;
var n = 45;
var intervalID;
var direction = "left";
var ms =200;
var score = 0;
var wCode = 87;
var aCode = 65;
var sCode = 83;
var dCode = 68;
var upCode = 38;
var leftCode = 37;
var downCode = 40;
var rightCode = 39;
var pause = false;
function get_mouse_coords(e){ 
var m = {};
var rect = SnakeS.getBoundingClientRect();
m.x = e.clientX - rect.left;
m.y = e.clientY - rect.top;
return m;
}
SnakeS.onclick = function(e){
 var m = get_mouse_coords(e);
 console.log(m.x, m.y);
 if ((m.x>(w/2-95))&&(m.x<(w/2+95))&&(m.y>(h/2+30))&&(m.y<(h/2+70))) {
  console.log(1);
  location.reload();
 }
}
var sl=1;
var nofood = false;
var nofoodno = false;
var x = w/n;
var y = h/n;
console.log(x,y);
function box(){
 this.conteins = "empty";		
}
function Snake(){
 this.head = null;
 this.tail = null;
 this.body = [];
 this.length = 1;
}
var field = [];
for (var i=0; i<n; i++) {
 var m = [];
 for (var j=0; j<n; j++){
  m.push(new box());
 }
 field.push(m);
}
var snake = new Snake();
function finish() {
 ctx.textAlign = 'center';
 ctx.font = '30px Georgia';
 pause = true;
 console.error("Вы проиграли");
 clearInterval(intervalID);
 ctx.fillStyle = 'rgb(180, 234, 255)';
 ctx.fillRect(0.25*w, 0.25*h, 0.5*w, 0.5*h);
 ctx.strokeStyle = 'black';
 ctx.strokeRect(0.25*w, 0.25*h, 0.5*w, 0.5*h);
 ctx.fillStyle = 'black';
 ctx.fillText('Вы проиграли!', w/2, h/2-40);
 ctx.fillText('Ваш счет: '+score, w/2, h/2);
 ctx.fillStyle = 'rgb(14, 151, 184)';
 ctx.fillRect(w/2-95, h/2+30, 190, 40);
 ctx.strokeRect(w/2-95, h/2+30, 190, 40);
 ctx.fillStyle = 'black';
 ctx.fillText('Сыграть еще', w/2, h/2+60);
 console.info('rtr');
}
function CreateItem(item){
 var i=Math.round(Math.random()*(n - 1));
 var j=Math.round(Math.random()*(n - 1));
 while (field [i][j].conteins != "empty"){
   var i=Math.round(Math.random()*(n - 1));
   var j=Math.round(Math.random()*(n - 1));
  }
 field [i][j].conteins = item;
}
function Init(){
  var i=Math.round(Math.random()*(n - 1));
  var j=Math.round(Math.random()*(n - 1));
  field [i][j].conteins = "food";
  var i=Math.round(Math.random()*(n - 1));
  var j=Math.round(Math.random()*(n - 1));
  while (field [i][j].conteins == "food"){
   var i=Math.round(Math.random()*(n - 1));
   var j=Math.round(Math.random()*(n - 1));
  }
  field [i][j].conteins = "snake";
  snake.head = {x: i, y: j};
  snake.tail = {x: i, y: j};
  snake.body.push({x: i, y: j}) ;
  console.log(direction, snake.body, snake.head);
  CreateItem('nofood');
}
function Draw() {
 for (var i=0; i<n; i++) {
  for (var j=0; j<n; j++){
   if (field[i][j].conteins == "empty") {
   ctx.fillStyle = 'white';
   ctx.fillRect(x*i, y*j, x, y);
   }
   if (field[i][j].conteins == "food") {
   ctx.fillStyle = 'red';
   ctx.fillRect(x*i, y*j, x, y);
   ctx.strokeStyle = 'black';
   ctx.strokeRect(x*i, y*j, x, y);
   }
   if (field[i][j].conteins == "snake") {
   ctx.fillStyle = 'green';
   ctx.fillRect(x*i, y*j, x, y);
   ctx.strokeStyle = 'black';
   ctx.strokeRect(x*i, y*j, x, y);
   }
   if (field[i][j].conteins == "nofood") {
   ctx.fillStyle = 'blue';
   ctx.fillRect(x*i, y*j, x, y);
   ctx.strokeStyle = 'black';
   ctx.strokeRect(x*i, y*j, x, y);
   }
   if (field[i][j].conteins == "nofoodno") {
   ctx.fillStyle = 'yellow';
   ctx.fillRect(x*i, y*j, x, y);
   ctx.strokeStyle = 'black';
   ctx.strokeRect(x*i, y*j, x, y);
   }
  }
 }
 document.getElementById('scre').innerHTML = score;
}
window.onkeydown = function(evt) { 
evt = evt || window.event;
var charCode = evt.keyCode || evt.which;
console.log(charCode); 
if (((charCode == wCode)||(charCode == upCode)) && direction != "down") {
 direction = "up";
}
if (((charCode == sCode)||(charCode == downCode)) && direction != "up") {
 direction = "down"; 
}
if (((charCode == aCode)||(charCode == leftCode)) && direction != "right") {
 direction = "left";
}
if (((charCode == dCode)||(charCode == rightCode)) && direction != "left") {
 direction = "right";
}
}
function CheckLength() {
 if ((score == 3) && (sl != 2)){
  sl = 2;
  ms = ms-100;
  clearInterval(intervalID);
  intervalID = setInterval(control, ms);
  return ;
 }
 if ((score == 5 ) && (sl != 3)){
  sl = 3;
  ms = ms-50;
  clearInterval(intervalID);
  intervalID = setInterval(control, ms);
 }
}
function Calcul(){
 var next 
 try {
 if (direction == "left") {
  next = {x: snake.head.x-1, y: snake.head.y}
 }
 if (direction == "right") {
  next = {x: snake.head.x+1, y: snake.head.y}
 }
 if (direction == "up") {
  next = {x: snake.head.x, y: snake.head.y-1}
 }
 if (direction == "down" && snake.length == 1) {
  next = {x: snake.head.x, y: snake.head.y+1}
 }
 if (direction == "down" && snake.length > 1) {
  next = {x: snake.head.x, y: snake.head.y}
 }
 if (score > 5) {
  if (next.x<0) {
   next.x = n-1
   console.log('TP')
   }
  if (next.y<0) {
   next.y = n-1
   console.log('TP')
   
  if (next.x>n-1) {
   next.x = 0
   console.log('TP')
   }
  if (next.y>n-1) {
   next.y = 0
   console.log('TP')
    }	
 } else {
  if (next.x<0) {
   finish();
   return;
   }
  if (next.y<0) {
   finish();
   return;
   }
  if (next.x>n-1) {
   finish();
   return;
   }
  if (next.y>n-1) {
   finish();
   return;
   }	
 }
 if (field[next.x][next.y].conteins == "snake") {
  finish();
  return;
 }
 if (field[next.x][next.y].conteins == "food") {
  snake.body.push({x:next.x, y:next.y});
  snake.head = {x:next.x, y:next.y};
  field[next.x][next.y].conteins = "snake";
  CreateItem ('food');
  score++;
 }
 if (field[next.x][next.y].conteins == "nofoodno") {
  snake.body.push({x:next.x, y:next.y});
  snake.head = {x:next.x, y:next.y};
  field[next.x][next.y].conteins = "snake";
  field[snake.body[0].x][snake.body[0].y].conteins = "empty";
  snake.body.shift();
  if(snake.body.length!=1)
   field[snake.body[0].x][snake.body[0].y].conteins = "empty";
   snake.body.shift();
  score++;
  nofoodno=false;
 }
 if (field[next.x][next.y].conteins == "nofood") {
  snake.body.push({x:next.x, y:next.y});
  snake.head = {x:next.x, y:next.y};
  field[next.x][next.y].conteins = "snake";
  field[snake.body[0].x][snake.body[0].y].conteins = "empty";
  snake.body.shift();
  score++;
  nofood=false;
 }
 if (field[next.x][next.y].conteins == "empty") {
  snake.body.push({x:next.x, y:next.y});
  snake.head = {x:next.x, y:next.y};
  field[next.x][next.y].conteins = "snake";
  field[snake.body[0].x][snake.body[0].y].conteins = "empty";
  snake.body.shift();
 }
 } catch (err){
  console.error("Что то пошло не так; X=", next.x, "Y=", next.y);
  clearInterval(intervalID);
 }
 if (((score+1) % 5 == 0)&&(!nofood)){
  CreateItem ('nofood');
  nofood=true;
 }
 if (((score+1) % 8 == 0)&&(!nofoodno)){
  CreateItem ('nofoodno');
  nofoodno=true;
 }
}
function control (){
 if (!pause)
  Calcul ();
 if (!pause)
  Draw();
 CheckLength();
}
Init();
Draw ();
intervalID = setInterval(control, ms);
}