`
yangsongjing
  • 浏览: 247520 次
  • 性别: Icon_minigender_1
  • 来自: 湖南
社区版块
存档分类
最新评论

js 贪吃蛇

阅读更多
$(document).ready(function(){
//Canvas stuff
var canvas = $("#canvas")[0];
var ctx = canvas.getContext("2d");
var w = $("#canvas").width();
var h = $("#canvas").height();

//Lets save the cell width in a variable for easy control
var cw = 10;
var d;
var food;
var score;

//Lets create the snake now
var snake_array; //an array of cells to make up the snake
var isgameover;
function init()
{
d = "right"; //default direction
create_snake();
create_food(); //Now we can see the food particle
//finally lets display the score
score = 0;
//Lets move the snake now using a timer which will trigger the paint function
//every 60ms
if(typeof game_loop != "undefined") clearInterval(game_loop);
game_loop = setInterval(paint, 60);
isgameover = false;
}
init();

function create_snake()
{
var length = 5; //Length of the snake
snake_array = []; //Empty array to start with
for(var i = length-1; i>=0; i--)
{
//This will create a horizontal snake starting from the top left
snake_array.push({x: i, y:0});
}
}

//Lets create the food now
function create_food()
{
food = {
x: Math.round(Math.random()*(w-cw)/cw),
y: Math.round(Math.random()*(h-cw)/cw),
};
//This will create a cell with x/y between 0-44
//Because there are 45(450/10) positions accross the rows and columns
}
function paint_gameover(){
ctx.fillStyle = 'red';
//ctx.fillText("Game Over",50,h-200);
ctx.font="bold 50px sans-serif";
ctx.strokeText("Game Over",w/2-70,h/2);

ctx.font="bold 20px sans-serif";
ctx.fillText("replay",w/2,h/2+20);
isgameover=true;
}
//Lets paint the snake now
function paint()
{
//To avoid the snake trail we need to paint the BG on every frame
//Lets paint the canvas now
ctx.fillStyle = "white";
ctx.fillRect(0, 0, w, h);
ctx.strokeStyle = "black";
ctx.strokeRect(0, 0, w, h);
isgameover=false;
//The movement code for the snake to come here.
//The logic is simple
//Pop out the tail cell and place it infront of the head cell
var nx = snake_array[0].x;
var ny = snake_array[0].y;
//These were the position of the head cell.
//We will increment it to get the new head position
//Lets add proper direction based movement now
if(d == "right") nx++;
else if(d == "left") nx--;
else if(d == "up") ny--;
else if(d == "down") ny++;


//Lets add the game over clauses now
//This will restart the game if the snake hits the wall
//Lets add the code for body collision
//Now if the head of the snake bumps into its body, the game will restart
if(nx == -1 || nx == w/cw || ny == -1 || ny == h/cw || check_collision(nx, ny, snake_array))
{
paint_gameover();
//restart game
//Lets organize the code a bit now.
return;
}

//Lets write the code to make the snake eat the food
//The logic is simple
//If the new head position matches with that of the food,
//Create a new head instead of moving the tail
if(nx == food.x && ny == food.y)
{
var tail = {x: nx, y: ny};
score++;
//Create new food
create_food();
}
else
{
var tail = snake_array.pop(); //pops out the last cell
tail.x = nx; tail.y = ny;
}
//The snake can now eat the food.

snake_array.unshift(tail); //puts back the tail as the first cell

for(var i = 0; i < snake_array.length; i++)
{
var c = snake_array[i];
//Lets paint 10px wide cells
paint_cell(c.x, c.y);
}

//Lets paint the food
paint_cell(food.x, food.y);
//Lets paint the score
var score_text = "Score: " + score;
ctx.fillText(score_text, 5, h-5);
}

//Lets first create a generic function to paint cells
function paint_cell(x, y)
{
ctx.fillStyle = "blue";
ctx.fillRect(x*cw, y*cw, cw, cw);
ctx.strokeStyle = "white";
ctx.strokeRect(x*cw, y*cw, cw, cw);
}



function check_collision(x, y, array)
{
//This function will check if the provided x/y coordinates exist
//in an array of cells or not
for(var i = 0; i < array.length; i++)
{
if(array[i].x == x && array[i].y == y)
return true;
}
return false;
}

//Lets add the keyboard controls now
$(document).keydown(function(e){
if (isgameover) return;
var key = e.which;
//We will add another clause to prevent reverse gear
if(key == "37" && d != "right") d = "left";
else if(key == "38" && d != "down") d = "up";
else if(key == "39" && d != "left") d = "right";
else if(key == "40" && d != "up") d = "down";
//The snake is now keyboard controllable
})
$("#canvas").click(function(){
if(isgameover){
init();
}
})


})
分享到:
评论

相关推荐

    js 贪吃蛇完美版

    总之,【JS贪吃蛇完美版】不仅展现了JavaScript的动态特性,还涵盖了基础编程、游戏逻辑、用户交互等多个方面的技术。对于想要学习JavaScript或游戏开发的人来说,这是一个很好的实践项目。通过分析和重构这个游戏的...

    Phaser.js 贪吃蛇游戏

    Phaser.js 是一款非常流行的开源JavaScript游戏开发框架,它基于HTML5 Canvas技术,使得开发者能够轻松地创建各种类型的Web游戏,无需复杂的后端处理。在本项目中,我们看到一个用Phaser.js实现的仿Nokia经典贪吃蛇...

    js贪吃蛇源码下载

    通过分析和学习这个JavaScript贪吃蛇游戏的源码,开发者不仅能提升JavaScript编程技能,还能了解游戏开发的基本流程,为今后的Web应用开发打下坚实基础。同时,这个游戏源码也是很好的实战练习,帮助初学者将理论...

    javaScript贪吃蛇源代码

    JavaScript贪吃蛇是一款基于网页的经典小游戏,通过JavaScript编程语言实现。这款游戏的核心原理是利用JavaScript的事件监听、定时器和数组操作来控制蛇的移动、食物的生成以及碰撞检测。下面我们将详细探讨这些知识...

    JavaScript贪吃蛇小游戏.zip

    JavaScript贪吃蛇小游戏是一款基于网页的互动娱乐项目,利用JavaScript编程语言实现。JavaScript是一种轻量级的解释型编程语言,广泛应用于网页和网络应用开发,它允许在浏览器端执行代码,为用户提供动态交互的体验...

    JS 贪吃蛇,快来试试吧

    JS贪吃蛇是一款经典的基于JavaScript实现的小游戏,它结合了jQuery和ExtJS这两个流行的前端框架,为用户带来娱乐的同时,也是学习和提升编程技能的好途径。在这里,我们将深入探讨其背后的实现原理和技术点。 1. **...

    js贪吃蛇源码

    【JavaScript贪吃蛇游戏开发详解】 在编程世界中,JavaScript是一种广泛应用的脚本语言,尤其在Web开发领域占据着核心地位。"js贪吃蛇源码"是一个利用JavaScript实现的经典小游戏,它展示了基础的编程逻辑、事件...

    js 贪吃蛇(面向对象)

    总结来说,"js 贪吃蛇(面向对象)"案例提供了学习和实践JavaScript面向对象编程的好机会。通过分析和实现这个案例,开发者可以深入理解面向对象的设计原则,提高代码复用和维护性,同时也能体会到面向对象编程在...

    JS贪吃蛇闯关卡小游戏.zip

    【JS贪吃蛇闯关卡小游戏】是一款基于JavaScript编写的网页版贪吃蛇游戏,它融合了关卡挑战和速度调节功能,为玩家提供多样的游戏体验。在这款游戏中,玩家可以通过控制蛇的方向来捕食食物,每吃掉一个食物,蛇的长度...

    js贪吃蛇特效zip

    【JS贪吃蛇特效】是基于原生JavaScript实现的一款经典小游戏,它展示了JavaScript在实现动态交互效果方面的强大能力。这个游戏的实现主要涉及到以下几个关键知识点: 1. **HTML结构**: `index.html` 文件是游戏的...

    纯js 贪吃蛇 游戏源码

    纯js 贪吃蛇 游戏源码。用了canvas。没有做手机滑屏功能。有3种难度选择。

    Javascript贪吃蛇游戏源码

    JavaScript贪吃蛇游戏是一款经典的基于Web的休闲游戏,利用JavaScript编程语言实现。在这个游戏中,玩家控制一条蛇在网格中移动,通过吃掉食物来增长蛇的长度,同时避开蛇身,因为碰到自身或边界会导致游戏结束。...

    HTML5 Canvas js贪吃蛇

    在这个“HTML5 Canvas js贪吃蛇”项目中,我们看到的是一个利用Canvas API和JavaScript实现的经典游戏——贪吃蛇。这个项目不仅展示了Canvas的基本用法,还涉及到了JavaScript编程技巧和游戏逻辑的实现。 首先,...

    javascript贪吃蛇(源码)

    【JavaScript贪吃蛇游戏详解】 JavaScript,作为网页开发中的重要脚本语言,具有广泛的用途,不仅可以处理用户交互,还能创建动态、有趣的游戏。本篇将深入解析基于JavaScript编写的贪吃蛇游戏,帮助你理解其背后的...

    js贪吃蛇游戏

    通过以上步骤,你将能够构建一个基础的JavaScript贪吃蛇游戏。不断地迭代和优化,可以让这款游戏变得更加有趣且具有挑战性。与社区交流,分享你的代码和经验,这将有助于你深入理解和掌握JavaScript编程技巧。

    简单javascript贪吃蛇

    总之,"简单javascript贪吃蛇"是一个很好的学习资源,它向我们展示了如何用基础的前端技术构建一个完整的互动游戏。对于想要提升JavaScript编程技能,尤其是对游戏开发感兴趣的开发者来说,这是一个很好的实践项目。...

    javascript贪吃蛇网页小游戏

    JavaScript贪吃蛇是一款基于网页的经典小游戏,利用JavaScript、jQuery等技术实现。JavaScript是浏览器端的主要脚本语言,负责处理游戏的逻辑和用户交互,而jQuery则简化了DOM操作和事件处理,让代码更加简洁易懂。 ...

    原生js贪吃蛇源码

    【原生JS贪吃蛇源码】是一种使用JavaScript编程语言实现的经典小游戏——贪吃蛇。JavaScript,简称JS,是Web开发中的主要脚本语言,它用于处理网页的动态交互,如用户输入、页面动画以及与服务器的数据通信。在这个...

    原生JavaScript贪吃蛇完美实现

    在本项目中,我们将深入探讨如何使用原生JavaScript来实现一个经典的贪吃蛇游戏。JavaScript是一种广泛用于网页和网络应用的编程语言,以其灵活性和强大的功能而受到开发者的喜爱。贪吃蛇游戏作为一款历史悠久、简单...

    纯JS版本贪吃蛇(绝对精美)

    【标题】"纯JS版本贪吃蛇(绝对精美)"所涉及的知识点:...这个"纯JS版本的贪吃蛇"项目展示了JS在游戏开发中的强大能力,涵盖了前端开发的多个重要方面,对于学习和实践JavaScript的开发者来说,是一个很好的实践项目。

Global site tag (gtag.js) - Google Analytics