课程设计:使用JavaScript制作一个网页上的贪吃蛇游戏

JavaScript025

课程设计:使用JavaScript制作一个网页上的贪吃蛇游戏,第1张

<html>

<head>

<title>贪吃蛇 Snake v2.4</title>

<style>

body{

font-size:9pt

}

table{

border-collapse: collapse

border:solid #333 1px

}

td{

height: 10px

width: 10px

font-size: 0px

}

.filled{

background-color:blue

}

</style>

</head>

<script>

function $(id){return document.getElementById(id)}

/**************************************************************

* javascript贪吃蛇 v2.4 <br />

* author: sunxing007 05/14/2009<br />

* 转载请注明来自http://blog.csdn.net/sunxing007 谢谢!<br />

* v2.4修正了蛇身颜色可以随着蛇前进而移动

**************************************************************/

//贪吃蛇类

var Snake = {

tbl: null,

/**

* body: 蛇身,数组放蛇的每一节,

* 数据结构{x:x0, y:y0, color:color0},

* x,y表示坐标,color表示颜色

**/

body: [],

//当前移动的方向,取值0,1,2,3, 分别表示向上,右,下,左, 按键盘方向键可以改变它

direction: 0,

//定时器

timer: null,

//速度

speed: 250,

//是否已经暂停

paused: true,

//行数

rowCount: 30,

//列数

colCount: 30,

//初始化

init: function(){

var colors = ['red','orange','yellow','green','blue','purple','#ccc']

this.tbl = $("main")

var x = 0

var y = 0

var colorIndex = 0

//产生初始移动方向

this.direction = Math.floor(Math.random()*4)

//构造table

for(var row=0row<this.rowCountrow++){

var tr=this.tbl.insertRow(-1)

for(var col=0col<this.colCountcol++) {

var td=tr.insertCell(-1)

}

}

//产生20个松散节点

for(var i=0i<10i++){

x = Math.floor(Math.random()*this.colCount)

y = Math.floor(Math.random()*this.rowCount)

colorIndex = Math.floor(Math.random()*7)

if(!this.isCellFilled(x,y)){

this.tbl.rows[y].cells[x].style.backgroundColor = colors[colorIndex]

}

}

//产生蛇头

while(true){

x = Math.floor(Math.random()*this.colCount)

y = Math.floor(Math.random()*this.rowCount)

if(!this.isCellFilled(x,y)){

this.tbl.rows[y].cells[x].style.backgroundColor = "black"

this.body.push({x:x,y:y,color:'black'})

break

}

}

this.paused = true

//添加键盘事件

document.onkeydown= function(e){

if (!e)e=window.event

switch(e.keyCode | e.which | e.charCode){

case 13: {

if(Snake.paused){

Snake.move()

Snake.paused = false

}

else{

//如果没有暂停,则停止移动

Snake.pause()

Snake.paused = true

}

break

}

case 37:{//left

//阻止蛇倒退走

if(Snake.direction==1){

break

}

Snake.direction = 3

break

}

case 38:{//up

//快捷键在这里起作用

if(event.ctrlKey){

Snake.speedUp(-20)

break

}

if(Snake.direction==2){//阻止蛇倒退走

break

}

Snake.direction = 0

break

}

case 39:{//right

if(Snake.direction==3){//阻止蛇倒退走

break

}

Snake.direction = 1

break

}

case 40:{//down

if(event.ctrlKey){

Snake.speedUp(20)

break

}

if(Snake.direction==0){//阻止蛇倒退走

break

}

Snake.direction = 2

break

}

}

}

},

//移动

move: function(){

this.timer = setInterval(function(){

Snake.erase()

Snake.moveOneStep()

Snake.paint()

}, this.speed)

},

//移动一节身体

moveOneStep: function(){

if(this.checkNextStep()==-1){

clearInterval(this.timer)

alert("Game over!\nPress Restart to continue.")

return

}

if(this.checkNextStep()==1){

var _point = this.getNextPos()

var _x = _point.x

var _y = _point.y

var _color = this.getColor(_x,_y)

this.body.unshift({x:_x,y:_y,color:_color})

//因为吃了一个食物,所以再产生一个食物

this.generateDood()

return

}

//window.status = this.toString()

var point = this.getNextPos()

//保留第一节的颜色

var color = this.body[0].color

//颜色向前移动

for(var i=0i<this.body.length-1i++){

this.body[i].color = this.body[i+1].color

}

//蛇尾减一节, 蛇尾加一节,呈现蛇前进的效果

this.body.pop()

this.body.unshift({x:point.x,y:point.y,color:color})

//window.status = this.toString()

},

//探寻下一步将走到什么地方

pause: function(){

clearInterval(Snake.timer)

this.paint()

},

getNextPos: function(){

var x = this.body[0].x

var y = this.body[0].y

var color = this.body[0].color

//向上

if(this.direction==0){

y--

}

//向右

else if(this.direction==1){

x++

}

//向下

else if(this.direction==2){

y++

}

//向左

else{

x--

}

//返回一个坐标

return {x:x,y:y}

},

//检查将要移动到的下一步是什么

checkNextStep: function(){

var point = this.getNextPos()

var x = point.x

var y = point.y

if(x<0||x>=this.colCount||y<0||y>=this.rowCount){

return -1//触边界,游戏结束

}

for(var i=0i<this.body.lengthi++){

if(this.body[i].x==x&&this.body[i].y==y){

return -1//碰到自己的身体,游戏结束

}

}

if(this.isCellFilled(x,y)){

return 1//有东西

}

return 0//空地

},

//擦除蛇身

erase: function(){

for(var i=0i<this.body.lengthi++){

this.eraseDot(this.body[i].x, this.body[i].y)

}

},

//绘制蛇身

paint: function(){

for(var i=0i<this.body.lengthi++){

this.paintDot(this.body[i].x, this.body[i].y,this.body[i].color)

}

},

//擦除一节

eraseDot: function(x,y){

this.tbl.rows[y].cells[x].style.backgroundColor = ""

},

paintDot: function(x,y,color){

this.tbl.rows[y].cells[x].style.backgroundColor = color

},

//得到一个坐标上的颜色

getColor: function(x,y){

return this.tbl.rows[y].cells[x].style.backgroundColor

},

//用于调试

toString: function(){

var str = ""

for(var i=0i<this.body.lengthi++){

str += "x:" + this.body[i].x + " y:" + this.body[i].y + " color:" + this.body[i].color + " - "

}

return str

},

//检查一个坐标点有没有被填充

isCellFilled: function(x,y){

if(this.tbl.rows[y].cells[x].style.backgroundColor == ""){

return false

}

return true

},

//重新开始

restart: function(){

if(this.timer){

clearInterval(this.timer)

}

for(var i=0i<this.rowCounti++){

this.tbl.deleteRow(0)

}

this.body = []

this.init()

this.speed = 250

},

//加速

speedUp: function(time){

if(!this.paused){

if(this.speed+time<10||this.speed+time>2000){

return

}

this.speed +=time

this.pause()

this.move()

}

},

//产生食物。

generateDood: function(){

var colors = ['red','orange','yellow','green','blue','purple','#ccc']

var x = Math.floor(Math.random()*this.colCount)

var y = Math.floor(Math.random()*this.rowCount)

var colorIndex = Math.floor(Math.random()*7)

if(!this.isCellFilled(x,y)){

this.tbl.rows[y].cells[x].style.backgroundColor = colors[colorIndex]

}

}

}

</script>

<body onload="Snake.init()">

/*************************************************************<br />

* javascript贪吃蛇 v2.4<br />

* author: sunxing007 05/14/2009<br />

* 转载请注明来自 <a href="http://blog.csdn.net/sunxing007">http://blog.csdn.net/sunxing007</a>谢谢!<br />

**************************************************************/<br />

<table id="main" border="1" cellspacing="0" cellpadding="0"></table>

<input type="button" id="btn" value="开始/暂停" />点左边按钮或按Enter开始/暂停游戏<br />

<input type="button" id="reset" value="重新开始" /><br />

<input type="button" id="upSpeed" value="加速" />点左边按钮或按Ctrl + ↑加速<br />

<input type="button" id="downSpeed" value="减速" />点左边按钮或按Ctrl + ↓减速

<script>

$('btn').onclick = function(){

if(Snake.paused){

Snake.move()

Snake.paused = false

}

else{

Snake.pause()

Snake.paused = true

}

}

$("reset").onclick = function(){

Snake.restart()

this.blur()

}

$("upSpeed").onclick = function(){

Snake.speedUp(-20)

}

$("downSpeed").onclick = function(){

Snake.speedUp(20)

}

</script>

</body>

</html>

我原来用C语言,借助curses库实现了linux 终端下的贪吃蛇游戏。

这个javascript版本的贪吃蛇是http://www.veryhuo.com/game/tanchishe.html 的学习笔记,实现的原理和C版本基本一样。

--------------------

1.怎样表示一条snake

用一个二维数组存snake的各个点(x,y),同时标记这些点(x,y)为“cover”,这是用于以后检查snake的头是否撞到了snake的body。

//initialize snake

function initSnake() {

var pointer = randomPointer(len-1, len-1, WIDTH/2)

for(var i = 0 i < len i++) {

var x = pointer[0] - i,

y = pointer[1]

snake.push([x,y])

carrier[x][y] = "cover" //标记snake body

}

}

2.用js画出格子

用document.createElent()方法创建出table->tr->td, 然后用document.appendChild()方法追加到id为“snakewrap”的元素上:

//initialize grid 

function initGrid() {

var body = document.getElementsByTagName("body")[0]

var table = document.createElement("table"),

tbody = document.createElement("tbody")

for(var j = 0 j < HEIGHT j++) {  

var col = document.createElement("tr")  

for(var i = 0 i < WIDTH i++) {  

var row = document.createElement("td")

gridElems[i][j] = col.appendChild(row)  

}

tbody.appendChild(col)  

}

table.appendChild(tbody)

document.getElementById("snakewrap").appendChild(table)

}

3.生成食物的随机坐标

function randomPointer(startX,startY,endX,endY) {

startX = startX || 0

startY = startY || 0

endX = endX || WIDTH

endY = endY || HEIGHT

var p = [],

x = Math.floor(Math.random()*(endX - startX)) + startX,

y = Math.floor(Math.random()*(endY - startY)) + startY

//如果(x,y)有物体,则重新生成坐标

if(carrier[x][y]) {

return randomPointer(startX,startY,endX,endY)

}

p[0] = x

p[1] = y

return p

}

添加新的食物:

//addObject("food")

function addObject(name) {

var p = randomPointer() //get random position

var x = p[0]

var y = p[1]

carrier[x][y] = name

gridElems[x][y].className = name

}

4.方向键按下动作事件监听:允许左上右下这4个按键来改变snake的运动方向,注意,如果方向相反的话,不生效。

对于键盘上的每一个按键,都有一个key cord,我的这篇博客记录了javascript的key cord,可看到:

left arrow    37    

up arrow    38    

right arrow    39    

down arrow    40    

//keyboard event listener

function attachEvents(e) {

e = e || event

directkey = Math.abs(e.keyCode - directkey) != 2 && e.keyCode > 36 && e.keyCode < 41 ? e.keyCode : directkey 

return false

}

以前做的一个 部分注释 核心部分由注释 其实就是一个最后一个跟随前面一个位置的算法

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title></title>

    <style type="text/css">

        .body{width:600pxmargin:100px autotext-align:center}

        #main{height:300pxborder:1px solid #f00position:relative}

        #main div{width:20pxheight:20pxbackground-color:greenposition:absolute}

        #main div.snake{background-color:#ff0}

        .score{font-size:14px}

        .score strong{color:Red}

    </style>

    <script type="text/javascript">

        window.onload = function () {

            var omain = document.getElementById("main")

            var r = 15, c = 30

            var asnake = []

            var speed = 150

            var odiv = document.createElement("div")

            asnake.push({ l: Math.floor(Math.random() * c), t: Math.floor(Math.random() * r), div: odiv, d: "l" })

            omain.appendChild(odiv)

            setPosition(asnake[0])

            //设置位置

            function setPosition(obj) {

                obj.div.style.left = obj.l * 20 + "px"

                obj.div.style.top = obj.t * 20 + "px"

            }

            var aEat = null

            var d = "l"

            //创建吃的

            function createEat() {

                var l = Math.floor(Math.random() * c)

                var t = Math.floor(Math.random() * r)

                var isexist = false

                //判断是否存在在设数组里面

                for (var i = 0 i < asnake.length i++) {

                    if (l == asnake[i].l && t == asnake[i].t) {

                        isexist = true

                        break

                    }

                }

                //如果存在再创建一个蛇

                if (isexist) {

                    createEat()

                    return

                }

                var onewdiv = document.createElement("div")

                onewdiv.className = "snake"

                aEat = { l: l, t: t, div: onewdiv, d: "l" }

                omain.appendChild(onewdiv)

                setPosition(aEat)

            }

            createEat()

            var obtn = document.getElementById("btn")

            var oscore = document.getElementById("score")

            var oaddspeed = document.getElementById("addspeed")

            var otimer = null

            var isitem = true

            oaddspeed.onclick = function () {

                clearInterval(otimer)

                speed = speed < 50 ? 50 : speed - 20

                setasnke()

            }

            obtn.onclick = function () {

                if (!isitem) {

                    clearInterval(otimer)

                    obtn.value = "继续游戏"

                    isitem = true

                    return false

                }

                obtn.value = "暂停游戏"

                setasnke()

                isitem = false

            }

            function setasnke() {

                otimer = setInterval(function () {

                    for (var i = asnake.length - 1 i > 0 i--) {

                        asnake[i].l = asnake[i - 1].l

                        asnake[i].t = asnake[i - 1].t

                        asnake[i].d = asnake[i - 1].d

                    }

                    switch (d) {

                        case "l":

                            asnake[0].l--

                            break

                        case "r":

                            asnake[0].l++

                            break

                        case "t":

                            asnake[0].t--

                            break

                        case "b":

                            asnake[0].t++

                            break

                    }

                    //判断蛇是否撞墙了

                    if (asnake[0].l < 0 || asnake[0].l >= c || asnake[0].t < 0 || asnake[0].t >= r) {

                        alert("你死掉了")

                        clearInterval(otimer)

                        return

                    }

                    //判断蛇是否撞到自己了

                    for (var n = 1 n < asnake.length n++) {

                        if (asnake[0].l == asnake[n].l && asnake[0].t == asnake[n].t) {

                            alert("你已经死掉了")

                            clearInterval(otimer)

                            return

                        }

                    }

                    //判断蛇是否吃到东西了

                    if (asnake[0].l == aEat.l && asnake[0].t == aEat.t) {

                        aEat.div.className = ""

                        oscore.innerHTML = parseInt(oscore.innerHTML) + 1

                        asnake.push(aEat)

                        createEat()

                    }

                    //重新设置蛇的位置

                    for (var j = 0 j < asnake.length j++) {

                        setPosition(asnake[j])

                    }

                }, speed)

            }

            //键盘改变蛇的方向

            document.onkeydown = function (event) {

                var oEn = event || window.event

                var oCode = oEn.keyCode

                switch (oCode) {

                    case 37:

                        d = "l"

                        break

                    case 38:

                        d = "t"

                        break

                    case 39:

                        d = "r"

                        break

                    case 40:

                        d = "b"

                        break

                }

            }

        }

    </script>

</head>

<body>

    <div class="body">

        <input type="button" value="加速" id="addspeed" /> <input type="button" id="btn" value="开始游戏" />&nbsp&nbsp<span class="score">积分:<strong id="score">0</strong></span>

        <div id="main">

        

        </div>

    </div>

</body>

</html>