最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JS寫的貪吃蛇游戲(個人練習(xí))

 更新時間:2013年07月08日 17:51:23   作者:  
本文為大家介紹的是使用JS寫的貪吃蛇游戲,個人練習(xí)之用,感興趣的額朋友可以參考下哈,希望對大家學(xué)習(xí)js有所幫助
JS貪吃蛇游戲,個人練習(xí)之用,放在這備份一下,
 
復(fù)制代碼 代碼如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JS貪吃蛇-練習(xí)</title>
<style type="text/css">
#pannel table {
border-collapse: collapse;
}
#pannel table td {
border: 1px solid #808080;
width: 10px;
height: 10px;
font-size: 0;
line-height: 0;
overflow: hidden;
}
#pannel table .snake {
background-color: green;
}
#pannel table .food {
background-color: blue;
}
</style>
<script type="text/javascript">
var Direction = new function () {
this.UP = 38;
this.RIGHT = 39;
this.DOWN = 40;
this.LEFT = 37;
};
var Common = new function () {
this.width = 20; /*水平方向方格數(shù)*/
this.height = 20; /*垂直方向方格數(shù)*/
this.speed = 250; /*速度 值越小越快*/
this.workThread = null;
};
var Main = new function () {
var control = new Control();
window.onload = function () {
control.Init("pannel");
/*開始按鈕*/
document.getElementById("btnStart").onclick = function () {
control.Start();
this.disabled = true;
document.getElementById("selSpeed").disabled = true;
document.getElementById("selSize").disabled = true;
};
/*調(diào)速度按鈕*/
document.getElementById("selSpeed").onchange = function () {
Common.speed = this.value;
}
/*調(diào)大小按鈕*/
document.getElementById("selSize").onchange = function () {
Common.width = this.value;
Common.height = this.value;
control.Init("pannel");
}
};
};
/*控制器*/
function Control() {
this.snake = new Snake();
this.food = new Food();
/*初始化函數(shù),創(chuàng)建表格*/
this.Init = function (pid) {
var html = [];
html.push("<table>");
for (var y = 0; y < Common.height; y++) {
html.push("<tr>");
for (var x = 0; x < Common.width; x++) {
html.push('<td id="box_' + x + "_" + y + '">&nbsp;</td>');
}
html.push("</tr>");
}
html.push("</table>");
this.pannel = document.getElementById(pid);
this.pannel.innerHTML = html.join("");
};
/*開始游戲 - 監(jiān)聽鍵盤、創(chuàng)建食物、刷新界面線程*/
this.Start = function () {
var me = this;
this.MoveSnake = function (ev) {
var evt = window.event || ev;
me.snake.SetDir(evt.keyCode);
};
try {
document.attachEvent("onkeydown", this.MoveSnake);
} catch (e) {
document.addEventListener("keydown", this.MoveSnake, false);
}
this.food.Create();
Common.workThread = setInterval(function () {
me.snake.Eat(me.food); me.snake.Move();
}, Common.speed);
};
}
/*蛇*/
function Snake() {
this.isDone = false;
this.dir = Direction.RIGHT;
this.pos = new Array(new Position());
/*移動 - 擦除尾部,向前移動,判斷游戲結(jié)束(咬到自己或者移出邊界)*/
this.Move = function () {
document.getElementById("box_" + this.pos[0].X + "_" + this.pos[0].Y).className = "";
//所有 向前移動一步
for (var i = 0; i < this.pos.length - 1; i++) {
this.pos[i].X = this.pos[i + 1].X;
this.pos[i].Y = this.pos[i + 1].Y;
}
//重新設(shè)置頭的位置
var head = this.pos[this.pos.length - 1];
switch (this.dir) {
case Direction.UP:
head.Y--;
break;
case Direction.RIGHT:
head.X++;
break;
case Direction.DOWN:
head.Y++;
break;
case Direction.LEFT:
head.X--;
break;
}
this.pos[this.pos.length - 1] = head;
//遍歷畫蛇,同時判斷游戲結(jié)束
for (var i = 0; i < this.pos.length; i++) {
var isExits = false;
for (var j = i + 1; j < this.pos.length; j++)
if (this.pos[j].X == this.pos[i].X && this.pos[j].Y == this.pos[i].Y) {
isExits = true;
break;
}
if (isExits) { this.Over();/*咬自己*/ break; }
var obj = document.getElementById("box_" + this.pos[i].X + "_" + this.pos[i].Y);
if (obj) obj.className = "snake"; else { this.Over();/*移出邊界*/ break; }
}
this.isDone = true;
};
/*游戲結(jié)束*/
this.Over = function () {
clearInterval(Common.workThread);
alert("游戲結(jié)束!");
}
/*吃食物*/
this.Eat = function (food) {
var head = this.pos[this.pos.length - 1];
var isEat = false;
switch (this.dir) {
case Direction.UP:
if (head.X == food.pos.X && head.Y == food.pos.Y + 1) isEat = true;
break;
case Direction.RIGHT:
if (head.Y == food.pos.Y && head.X == food.pos.X - 1) isEat = true;
break;
case Direction.DOWN:
if (head.X == food.pos.X && head.Y == food.pos.Y - 1) isEat = true;
break;
case Direction.LEFT:
if (head.Y == food.pos.Y && head.X == food.pos.X + 1) isEat = true;
break;
}
if (isEat) {
this.pos[this.pos.length] = new Position(food.pos.X, food.pos.Y);
food.Create(this.pos);
}
};
/*控制移動方向*/
this.SetDir = function (dir) {
switch (dir) {
case Direction.UP:
if (this.isDone && this.dir != Direction.DOWN) { this.dir = dir; this.isDone = false; }
break;
case Direction.RIGHT:
if (this.isDone && this.dir != Direction.LEFT) { this.dir = dir; this.isDone = false; }
break;
case Direction.DOWN:
if (this.isDone && this.dir != Direction.UP) { this.dir = dir; this.isDone = false; }
break;
case Direction.LEFT:
if (this.isDone && this.dir != Direction.RIGHT) { this.dir = dir; this.isDone = false; }
break;
}
};
}
/*食物*/
function Food() {
this.pos = new Position();
/*創(chuàng)建食物 - 隨機位置創(chuàng)建立*/
this.Create = function (pos) {
document.getElementById("box_" + this.pos.X + "_" + this.pos.Y).className = "";
var x = 0, y = 0, isCover = false;
/*排除蛇的位置*/
do {
x = parseInt(Math.random() * (Common.width - 1));
y = parseInt(Math.random() * (Common.height - 1));
isCover = false;
if (pos instanceof Array) {
for (var i = 0; i < pos.length; i++) {
if (x == pos[i].X && y == pos[i].Y) {
isCover = true;
break;
}
}
}
} while (isCover);
this.pos = new Position(x, y);
document.getElementById("box_" + x + "_" + y).className = "food";
};
}
function Position(x, y) {
this.X = 0;
this.Y = 0;
if (arguments.length >= 1) this.X = x;
if (arguments.length >= 2) this.Y = y;
}
</script>
</head>
<body>
<div id="pannel" style="margin-bottom: 10px;"></div>
<select id="selSize">
<option value="20">20*20</option>
<option value="30">30*30</option>
<option value="40">40*40</option>
</select>
<select id="selSpeed">
<option value="500">速度-慢</option>
<option value="250" selected="selected">速度-中</option>
<option value="100">速度-快</option>
</select>
<input type="button" id="btnStart" value="開始" />
</body>
</html>

相關(guān)文章

  • 基于JavaScript實現(xiàn)百度搜索框效果

    基于JavaScript實現(xiàn)百度搜索框效果

    這篇文章主要為大家詳細(xì)介紹了基于JavaScript實現(xiàn)百度搜索框效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • JavaScript計算值然后把值嵌入到html中的實現(xiàn)方法

    JavaScript計算值然后把值嵌入到html中的實現(xiàn)方法

    下面小編就為大家?guī)硪黄狫avaScript計算值然后把值嵌入到html中的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-10-10
  • JS中promise特點與信任問題解決

    JS中promise特點與信任問題解決

    大家都知道Promise解決了回調(diào)地獄的問題,“回調(diào)地獄”所說的嵌套其實是指異步的嵌套,它帶來了兩個問題:可讀性的問題和信任問題,下面這篇文章主要給大家介紹了關(guān)于JS中promise特點與信任問題解決的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • ES11屢試不爽的新特性,你用上了幾個

    ES11屢試不爽的新特性,你用上了幾個

    這篇文章主要介紹了ES11屢試不爽的新特性,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • 最簡單的JavaScript圖片輪播代碼(兩種方法)

    最簡單的JavaScript圖片輪播代碼(兩種方法)

    基于javascript代碼實現(xiàn)最簡單的圖片輪播效果,非常簡單,本文通過兩種方式給大家介紹最簡單的圖片輪播,感興趣的朋友一起學(xué)習(xí)吧
    2015-12-12
  • js讀取被點擊次數(shù)的簡單實例(從數(shù)據(jù)庫中讀取)

    js讀取被點擊次數(shù)的簡單實例(從數(shù)據(jù)庫中讀取)

    這篇文章主要介紹了js讀取被點擊次數(shù)的簡單實例(從數(shù)據(jù)庫中讀取)。需要的朋友可以過來參考下,希望對大家有所幫助
    2014-03-03
  • 微信小程序?qū)崿F(xiàn)搜索商品和歷史記錄的功能

    微信小程序?qū)崿F(xiàn)搜索商品和歷史記錄的功能

    這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)搜索商品和歷史記錄的功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • Web安全測試之XSS實例講解

    Web安全測試之XSS實例講解

    本文主要介紹Web安全測試之XSS,這里詳細(xì)整理了測試XSS的資料,并附示例代碼和詳細(xì)講解,有需要的小伙伴可以參考下
    2016-08-08
  • JavaScript EventEmitter 背后的秘密 完整版

    JavaScript EventEmitter 背后的秘密 完整版

    在這里,我們的目標(biāo)創(chuàng)建屬于我們自己的 Event Emitter 去理解背后的秘密。所以,讓我們看一下下面的代碼是怎么工作的,需要的朋友可以參考下
    2018-03-03
  • js 實現(xiàn)在離開頁面時提醒未保存的信息(減少用戶重復(fù)操作)

    js 實現(xiàn)在離開頁面時提醒未保存的信息(減少用戶重復(fù)操作)

    在離開頁面時判斷是否有未保存的輸入值,然后進行提醒,接下來介紹實現(xiàn)步驟,感興趣的朋友可以了解下
    2013-01-01

最新評論

略阳县| 冷水江市| 枣阳市| 红原县| 疏勒县| 抚顺市| 汶上县| 新竹县| 卢湾区| 会同县| 青铜峡市| 大埔区| 牡丹江市| 寿光市| 高州市| 怀集县| 新干县| 原平市| 宁陕县| 新乡县| 德清县| 汾阳市| 商洛市| 什邡市| 来安县| 灵丘县| 北碚区| 齐齐哈尔市| 富蕴县| 团风县| 新竹县| 平原县| 湘西| 湖州市| 织金县| 松溪县| 康马县| 扎赉特旗| 屯昌县| 中西区| 拉萨市|