// , var canvas, context, first_x, first_y, rabbit_pos = new Array(), rabbit_on_field = false, start = true, state = false, g_over = false, direction = 'right'; // canvas = document.getElementById('mycanvas'); canvas.width = 310; canvas.height = 310; context = canvas.getContext('2d'); // context.fillStyle = "#CE3429"; // var snake_sectors = [[10, 50], [20, 50], [30, 50], [40, 50]];
// function field(context){ context.strokeStyle = "#546DEA"; context.lineWidth = 1; context.strokeRect(7, 7, 295, 295); }
// function snake(){ if(state){ // "" // undefined context.clearRect(first_x, first_y, 9, 9); // "" : , / if(start){ // , // , 9px 10 for(var i in snake_sectors){ context.fillRect(snake_sectors[i][0], snake_sectors[i][1], 9, 9); } start = false; } else{ // - "" context.fillRect(snake_sectors.slice(-1)[0][0], snake_sectors.slice(-1)[0][1], 9, 9); } // if(!rabbit_on_field){ rabbit(); } // "" "" var last_x = snake_sectors.slice(-1)[0][0]; var last_y = snake_sectors.slice(-1)[0][1]; first_x = snake_sectors[0][0]; first_y = snake_sectors[0][1]; // if(direction == 'right'){ var next_x = last_x + 10; if(next_x > 290){ // , window.setTimeout(game_over, 700); return false; } snake_sectors.push([next_x, last_y]); } if(direction == 'down'){ var next_y = last_y + 10; if(next_y > 290){ // , window.setTimeout(game_over, 700); return false; } snake_sectors.push([last_x, next_y]); // ("" ) } if(direction == 'up'){ var next_y = last_y - 10; if(next_y < 10) { // , window.setTimeout(game_over, 700); return false; } snake_sectors.push([last_x, next_y]); } if(direction == 'left'){ var next_x = last_x - 10; if(next_x < 10) { // , window.setTimeout(game_over, 700); return false; } snake_sectors.push([next_x, last_y]); } // // .. "" for(var i = 0; i < snake_sectors.length - 1; i++){ if(snake_sectors.slice(-1)[0][0] == snake_sectors[i][0] && snake_sectors.slice(-1)[0][1] == snake_sectors[i][1]){ // window.setTimeout(game_over, 700); return false; } } // // "" if(!is_catching()) snake_sectors.splice(0, 1); else rabbit(); // , - setTimeout(snake, 200); // 200 ms } }
// function rabbit(){ // rabbit_pos[0] = math_rand(); rabbit_pos[1] = math_rand(); // "" for(var i in snake_sectors){ if(rabbit_pos[0] == snake_sectors[i][0]){ rabbit_pos[0] = math_rand(); } if(rabbit_pos[1] == snake_sectors[i][1]){ rabbit_pos[1] = math_rand(); } } // , context.fillRect(rabbit_pos[0], rabbit_pos[1], 9, 9); rabbit_on_field = true; }
// // function math_rand(){ return Math.ceil((Math.random() * 2.9) * 10) * 10; }
// "" // .. "" function is_catching(){ if(rabbit_pos[0] == snake_sectors.slice(-1)[0][0] && rabbit_pos[1] == snake_sectors.slice(-1)[0][1]) return true; else return false; }
// " ", - // function game_over(){ // , , // "Game Over" g_over = true; // context.clearRect(8, 8, 293, 293); // context.font='35px Verdana'; context.strokeStyle="#DB733B"; context.strokeText('Game Over!',47,160); // - "Game Over!" setTimeout(function(){context.clearRect(8, 8, 293, 293); g_over = false}, 1500); // - snake_sectors = [[10, 50], [20, 50], [30, 50], [40, 50]]; state = false; direction = 'right'; start = true; rabbit_on_field = false; }
// "" document.onkeydown = function(event){ var keyCode; if(event == null){ keyCode = window.event.keyCode; } else{ keyCode = event.keyCode; } switch(keyCode){ // space/ case 32: // if(!g_over){ // (continue) state = (!state) ? true : false; // snake(); } break; // left/ case 37: // "" if(direction == 'right') return; direction = 'left'; break; // up/ case 38: // "" if(direction == 'down') return; direction = 'up'; break; // right/ case 39: // "" if(direction == 'left') return; direction = 'right'; break; // down/ case 40: // "" if(direction == 'up') return; direction = 'down'; break; default: break; } }
Source: https://habr.com/ru/post/202014/
All Articles