js贪吃蛇源码

1、注意,自己引入jquery,这个demo基于jquery的,我的jquery是写的本地的

2、没有写注释,看不懂的再问我吧,

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
* {
padding: 0;
margin: 0;
}

div.wrap {
text-align: center;
padding-top: 50px;
}

div.container {
display: inline-block;
border: 1px solid rgba(0, 0, 0, 0.2);
}

div.row {
display: flex;
}

div.row div {
width: 20px;
height: 20px;
border: 1px solid rgba(0, 0, 0, 0.2);
}

div.row div.snake {
background: #26376b;
}

div.row div.snakeHeader {
background: red;
}

div.row div.food {
background: greenyellow;
}

div.grade {}
</style>
</head>

<body>
<div class="wrap">
<button class="btn">从新开始</button>
<div class="grade">
0
</div>

<div class="container">

</div>
</div>

</body>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
function snakeCreate(wrap, grade) {
var fn = {},
$wrap = $(wrap),
$row = null;
var direct = "up";
var count = 0;
var data = [];
var food = {
x: 0,
y: 0
};
var gameRun = true;
var snake = [{
x: 9,
y: 10
}, {
x: 10,
y: 10
}, {
x: 11,
y: 10
}, {
x: 12,
y: 10
}];

fn.init = function() {
fn.keyCode();
fn.createDom();
fn.config();
fn.createFood();
fn.display();
};

fn.createDom = function() {
for(var i = 0; i < 21; i++) {
$wrap.append(‘<div class="row"></div>‘);
for(var j = 0; j < 21; j++) {
$wrap.find(".row").last().append("<div></div>");
}
}

$row = $wrap.find(".row");

};

fn.config = function() {
for(var i = 0; i < 21; i++) {
var temp = [];
for(var j = 0; j < 21; j++) {
temp.push(0);
}
data.push(temp);

}

for(var i = 0; i < snake.length; i++) {
data[snake[i].x][snake[i].y] = 2;
}

};

fn.display = function() {
for(var i = 0; i < data.length; i++) {
for(var j = 0; j < data.length; j++) {
var className = "";
if(data[i][j] == 0) {
className = "";
} else if(data[i][j] == 2) {
className = "snake";
} else if(data[i][j] == 1) {
className = "food";
} else {
className = "";
}
$row.eq(i).find("div").eq(j)[0].className = className;
$row.eq(snake[0].x).find("div").eq(snake[0].y)[0].className = "snakeHeader";
}
}

};

fn.keyCode = function() {
$(window).on("keydown", function(event) {
if(gameRun) {
switch(event.key) {
case "w":
fn.moveToUp();
break;
case "s":
fn.moveToDown();
break;
case "a":
fn.moveToLeft();
break;
case "d":
fn.moveToRight();
break;
default:
break;

}
}

});

};
fn.change = function(x, y) {
fn.judegDeth(x, y);
if(gameRun) {
data[snake[snake.length - 1].x][snake[snake.length - 1].y] = 0;
data[x][y] = 2;
if(x == food.x && y == food.y) {
var feed = {
x: x,
y: y
}
$(grade).html($(grade).html() * 1 + 1);
snake.push(feed);
fn.createFood();
}

for(var i = snake.length - 1; i > 0; i--) {
snake[i].x = snake[i - 1].x;
snake[i].y = snake[i - 1].y;
}
for(var i = 0; i < snake.length; i++) {
data[snake[i].x][snake[i].y] = 2;
}

snake[0].x = x;
snake[0].y = y;
fn.display();
}

};

fn.moveToUp = function() {
if(direct != "down") {
direct = "up";
fn.outMove(-1, 0);
}
};

fn.moveToDown = function() {
if(direct != "up") {
direct = "down";
fn.outMove(1, 0);
}

};

fn.moveToLeft = function() {
if(direct != "right") {
direct = "left";
fn.outMove(0, -1);
}

};

fn.moveToRight = function() {
if(direct != "left") {
direct = "right";
fn.outMove(0, 1);
}

};

fn.outMove = function(speedx, speedy) {
clearInterval(fn.timer);
fn.change(snake[0].x + speedx, snake[0].y + speedy);
fn.timer = setInterval(function() {
fn.change(snake[0].x + speedx, snake[0].y + speedy);
}, 400);
};

fn.createFood = function() {
var x = Math.floor(Math.random() * 21);
var y = Math.floor(Math.random() * 21);
while(data[x][y] != 0) {
x = Math.floor(Math.random() * 21);
y = Math.floor(Math.random() * 21);
}
food.x = x;
food.y = y;
data[x][y] = 1;

};

fn.judegDeth = function(x, y) {
if(gameRun) {
fn.judegBound(x, y);
fn.judgeOver(x, y);
}
};

fn.judegBound = function(x, y) {
if(x >= 21 || x < 0 || y >= 21 || y < 0) {
clearInterval(fn.timer);
gameRun = false;
alert("game over1!");
}
};

fn.judgeOver = function(x, y) {
for(var i = 0; i < snake.length; i++) {
if(snake[i].x == x && snake[i].y == y) {
clearInterval(fn.timer);
gameRun = false;
alert("game over2!");
break;
}
}
};

fn.clear = function() {
clearInterval(fn.timer);
count = 0;
direct = "up";
food = {
x: 0,
y: 0
};
$(grade).html(0);
gameRun = true;
snake = [{
x: 9,
y: 10
}, {
x: 10,
y: 10
}, {
x: 11,
y: 10
}, {
x: 12,
y: 10
}];

for(var i = 0; i < data.length; i++) {
for(var j = 0; j < data.length; j++) {
data[i][j] = 0;
}
}

for(var i = 0; i < snake.length; i++) {
data[snake[i].x][snake[i].y] = 2;
}
fn.createFood();
fn.display();
};

return fn;
}

var snakeOne = snakeCreate(".container", ".grade");

snakeOne.init();
$(".btn").on("click", function() {
snakeOne.clear();
});
</script>

</html>

时间: 2024-12-21 05:05:59

js贪吃蛇源码的相关文章

c# 贪吃蛇源码

using UnityEngine; using System.Collections;using System.Diagnostics;using UnityEngine.SceneManagement;using System.Collections.Generic;using System.Linq;using UnityEngine.UI; public class SnakeMove : MonoBehaviour {    List<Transform> Body = new Li

C语言实现贪吃蛇源码

先放效果 源代码 //2016-2-12 //zhaoyu //Gmail:[email protected] //Language: C //Platform:Code::Blocks #include <stdio.h> #include <windows.h> #include <stdlib.h> #include <time.h> typedef struct snake { int x; int y; struct snake *next; }S

python小游戏贪吃蛇源码下载

回车键:开始游戏,空格键:暂停 / 继续,方向键 或 WSAD 键:控制移动方向 下载地址 原文地址:https://www.cnblogs.com/pythongood/p/11173838.html

JS贪吃蛇游戏

<!DOCTYPE html><html> <head>    <meta charset="utf-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <title>JS贪吃蛇游戏</title>    <style>    * {        margin: 0;    

js调试系列: 源码定位与调试[基础篇]

js调试系列目录: - 如果看了1, 2两篇,你对控制台应该有一个初步了解了,今天我们来个简单的调试.昨天留的三个课后练习,差不多就是今天要讲的内容.我们先来处理第一个问题:1. 查看文章下方 推荐 这个功能所调用的函数源码其实非常简单,点放大镜选中那个推荐即可.这个  votePost(cb_entryId,'Digg')  就是推荐按钮所调用的函数了,是不是非常简单. 第二个问题,定位到函数所在文件位置.其实也是非常简单的,当然,不熟悉控制台的朋友也许不知道怎么看.我在控制台输入 voteP

基于wke封装的duilib的webkit浏览器控件,可以c++与js互交,源码及demo下载地址

转载请说明原出处,谢谢~~ 前些日子用wke内核封装了duilib的webkit浏览器控件,好多群里朋友私聊我希望可以我公布源码,今天把这个控件的源码和使用demo公布.其实这个控件封装起来没什么难度,我只是按照原来作者的demo,把相应的消息封装成duilib对应的. 在此首先要感谢wke内核的作者BlzFans以及soui2界面库的作者flyhigh,BlzFans精简了webkit内核后封装为wke并公布了源码,flyhigh对wke进行处理让他更容易移植到dui工程中.wke内核10M大

KRPano JS 场景编辑器源码

KRPano JS编辑器,可以运行在Node环境中. 源码地址:https://github.com/xxweimei/krpano-editor-js 或者下载zip包:http://pan.baidu.com/s/1c2Il5Pu 使用说明: clone项目到本地: git clone https://github.com/xxweimei/krpano-editor-js 安装nodejs: https://nodejs.org/dist/v6.11.1/node-v6.11.1-win-

js贪吃蛇 纯手打 代码比较复杂 自己瞎弄的 bug 比较多 自己一点一点排除弄 完全原创

//虽然代码没有别人写的简单,但是这是自己的思想,通过这次实例,学到了很多,也发现了很多不足,努力学习啊,小小的贪吃蛇也包含了很多东西还有 //好多问题需要解决,比如,如何判断蛇头碰到蛇身,如何让食物不出现在蛇的身上等,欢迎大家一起探讨 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt

JS贪吃蛇小游戏

效果图展示: 具体实现代码如下: (1)html部分 1 !DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8" /> 5 <title>贪吃蛇</title> 6 <link rel="stylesheet" type="text/css" href="main.css"> 7 </h