求一javascript小游戏代码。【注:不要叫我去下载,你直接给我复制过来就可以了】。thank!!

JavaScript010

求一javascript小游戏代码。【注:不要叫我去下载,你直接给我复制过来就可以了】。thank!!,第1张

代码比较长 没有注释

脚本说明:

把如下代码加入<body>区域中

<style>

.bigcell {

background-color:#aa9966

border:4px solid #aa9966

text-align:center

}

.cell {

width:40px

height:40px

font-family:Verdana, Arial

font-size:10pt

font-weight:bold

background-color:#996633

color:#ffff33

border-top:2px solid #aa9966

border-left:2px solid #aa9966

border-right:2px solid #663300

border-bottom:2px solid #663300

text-align:center

}

.hole {

width:40px

height:40px

background-color:#aa9966

text-align:center

}

body,h1,h2,h3,.msg,capt1,capt2 {font-family:Verdana,Comic Sans MS,Arial}

body {margin:0px}

h1 {font-size:28ptfont-weight:boldmargin-bottom:0px}

h2 {font-size:16ptmargin:0pxfont-weight:bold}

h3 {font-size:8ptmargin:0pxfont-weight:bold}

.msg {font-size:8ptfont-weight:bold}

.tab {cursor:hand}

.capt1 {font-size:10ptfont-weight:bold}

.capt2 {font-size:9ptfont-weight:bold}

.capt3 {font-size:14ptfont-weight:boldcolor:yellow}

.capt4 {font-size:10ptfont-weight:boldcolor:yellow}

.but {font-size:9ptfont-weight:boldheight:30pxbackground-color:#aaaa99}

</style>

<BODY onLoad="loadBoard(4)">

<script>

var gsize, ghrow, ghcol, gtime, gmoves, gintervalid=-1, gshuffling

function toggleHelp()

{

if (butHelp.value == "Hide Help")

{

help.style.display = "none"

butHelp.value = "Show Help"

}

else

{

help.style.display = ""

butHelp.value = "Hide Help"

}

}

//random number between low and hi

function r(low,hi)

{

return Math.floor((hi-low)*Math.random()+low)

}

//random number between 1 and hi

function r1(hi)

{

return Math.floor((hi-1)*Math.random()+1)

}

//random number between 0 and hi

function r0(hi)

{

return Math.floor((hi)*Math.random())

}

function startGame()

{

shuffle()

gtime = 0

gmoves = 0

tickTime()

gintervalid = setInterval("tickTime()",1000)

}

function stopGame()

{

if (gintervalid==-1) return

clearInterval(gintervalid)

fldStatus.innerHTML = ""

gintervalid=-1

}

function tickTime()

{

showStatus()

gtime++

}

function checkWin()

{

var i, j, s

if (gintervalid==-1) return//game not started!

if (!isHole(gsize-1,gsize-1)) return

for (i=0i<gsizei++)

for (j=0j<gsizej++)

{

if (!(i==gsize-1 &&j==gsize-1)) //ignore last block (ideally a hole)

{

if (getValue(i,j)!=(i*gsize+j+1).toString()) return

}

}

stopGame()

s = "<table cellpadding=4>"

s += "<tr><td align=center class=capt3>!! CONGRATS !!</td></tr>"

s += "<tr class=capt4><td align=center>You have done it in " + gtime + " secs "

s += "with " + gmoves + " moves!</td></tr>"

s += "<tr><td align=center class=capt4>Your speed is " + Math.round(1000*gmoves/gtime)/1000 + " moves/sec</td></tr>"

s += "</table>"

fldStatus.innerHTML = s

// shuffle()

}

function showStatus()

{

fldStatus.innerHTML = "Time: " + gtime + " secs Moves: " + gmoves

}

function showTable()

{

var i, j, s

stopGame()

s = "<table border=3 cellpadding=0 cellspacing=0 bgcolor='#666655'><tr><td class=bigcell>"

s = s + "<table border=0 cellpadding=0 cellspacing=0>"

for (i=0i<gsizei++)

{

s = s + "<tr>"

for (j=0j<gsizej++)

{

s = s + "<td id=a_" + i + "_" + j + " onclick='move(this)' class=cell>" + (i*gsize+j+1) + "</td>"

}

s = s + "</tr>"

}

s = s + "</table>"

s = s + "</td></tr></table>"

return s

}

function getCell(row, col)

{

return eval("a_" + row + "_" + col)

}

function setValue(row,col,val)

{

var v = getCell(row, col)

v.innerHTML = val

v.className = "cell"

}

function getValue(row,col)

{

// alert(row + "," + col)

var v = getCell(row, col)

return v.innerHTML

}

function setHole(row,col)

{

var v = getCell(row, col)

v.innerHTML = ""

v.className = "hole"

ghrow = row

ghcol = col

}

function getRow(obj)

{

var a = obj.id.split("_")

return a[1]

}

function getCol(obj)

{

var a = obj.id.split("_")

return a[2]

}

function isHole(row, col)

{

return (row==ghrow &&col==ghcol) ? true : false

}

function getHoleInRow(row)

{

var i

return (row==ghrow) ? ghcol : -1

}

function getHoleInCol(col)

{

var i

return (col==ghcol) ? ghrow : -1

}

function shiftHoleRow(src,dest,row)

{

var i

//conversion to integer needed in some cases!

src = parseInt(src)

dest = parseInt(dest)

if (src <dest)

{

for (i=srci<desti++)

{

setValue(row,i,getValue(row,i+1))

setHole(row,i+1)

}

}

if (dest <src)

{

for (i=srci>desti--)

{

setValue(row,i,getValue(row,i-1))

setHole(row,i-1)

}

}

}

function shiftHoleCol(src,dest,col)

{

var i

//conversion to integer needed in some cases!

src = parseInt(src)

dest = parseInt(dest)

if (src <dest)

{//alert("src=" + src +" dest=" + dest + " col=" + col)

for (i=srci<desti++)

{//alert(parseInt(i)+1)

setValue(i,col,getValue(i+1,col))

setHole(i+1,col)

}

}

if (dest <src)

{

for (i=srci>desti--)

{

setValue(i,col,getValue(i-1,col))

setHole(i-1,col)

}

}

}

function move(obj)

{

var r, c, hr, hc

if (gintervalid==-1 &&!gshuffling)

{

alert('请点击"开始游戏"按钮')

return

}

r = getRow(obj)

c = getCol(obj)

if (isHole(r,c)) return

hc = getHoleInRow(r)

if (hc != -1)

{

shiftHoleRow(hc,c,r)

gmoves++

checkWin()

return

}

hr = getHoleInCol(c)

if (hr != -1)

{

shiftHoleCol(hr,r,c)

gmoves++

checkWin()

return

}

}

function shuffle()

{

var t,i,j,s,frac

gshuffling = true

frac = 100.0/(gsize*(gsize+10))

s = "% "

for (i=0i<gsizei++)

{

s += "|"

for (j=0j<gsize+10j++)

{

window.status = "Loading " + Math.round((i*(gsize+10) + j)*frac) + s

if (j%2==0)

{

t = r0(gsize)

while (t == ghrow) t = r0(gsize)//skip holes

getCell(t,ghcol).click()

}

else

{

t = r0(gsize)

while (t == ghcol) t = r0(gsize)//skip holes

getCell(ghrow,t).click()

}

}

}

window.status = ""

gshuffling = false

}

function loadBoard(size)

{

gsize = size

board.innerHTML = showTable(gsize)

setHole(gsize-1,gsize-1)

//shuffle()

}

</script>

<div id=test></div>

<table cellpadding=4>

<tr><td align=center>

<b>请选择难度: </B>

<select id=level onchange="loadBoard(parseInt(level.value))">

<option value='3'>3</option>

<option value='4' selected>4</option>

<script>

for (var i=5i<=10i++)

{

document.write("<option value='" + i + "'>" + i + "</option>")

}

</script>

</select>

</td></tr>

<tr><td align=center>

<input type=button class=but value="开始游戏" onclick="startGame()">

<tr><td align=center id=fldStatus class=capt2>

</td></tr>

</table>

<div id=board></div>

有是有,但并不是很多,而且都是贪吃蛇之类的,非常小的游戏,即便是页游也一样。

能运行在浏览器端的语言,确实只有JS,但在开发阶段,却并不一定要使用JS写。而是用其他语言写,直接使用JS写游戏,实在太自虐了。

JS本身的缺点非常严重,如果只是写DOM的话,其实并没什么感觉,因为代码量太少。

但如果写类似游戏这种复杂逻辑,代码量一变大,瞬间就令人崩溃了。弱类型,回调地狱问题,即便将来版本更新到ES10,也不可能完全解决。

如果你看过一个游戏项目的JS源码,你会发现一个非常恐怖的现象。在代码的最底部,有几百个,甚至几千个大括号。。。。所有大型程序的JS源码,拉到最底部,大概都是长这个样子的:

} } } } } } } } } } } } } } } } } } } } } } } } } } } }} } } } } } }} } } } } } } } } } } } } } } } } } } }.Listen(127.0.0.1) } } } } } } } } } } } } } } } } } } } } }} } } } } } } } } } } } } } } }}

大括号的数量还必须绝对精准,少一个,或者多一个,都无法正常运行。。。这就是平时所说的回调地狱。由于JS项目总是函数里面套函数,层层相套,这叫做回调函数。层数一多,就算你是N年的老手,也照样懵比。。。。

所有的游戏项目,都比网页特效的代码量要多的多。。。比如写一个斗地主,就需要4,5万行的JS代码。。。。。最底部的大括号数量,轻松上千。。。。

弱类型的缺陷更严重,但由于解释起来篇幅会很长,所以这里就不提了。

所以为了避开JS本身太多的语法缺陷,一般游戏项目,都是使用其他语言编写,最后再通过一些手段,编译成JS。。。就如同你用一般编程语言编写,最终运行的时候,只有1和0的道理一样。。。在制作页游的时候,一般都是用强类型语言编写,最后开发完成之后,把那些强类型语言编写的代码,通过一些手段“转换”成JS代码。

“转换”成JS代码的方法有很多,其中在游戏行业比较主流的,一共有三种:

1,ActionScript语言,简称AS语言。也就是当年FLASH使用的那个语言。。。当年也曾辉煌过,后来随着FLASH的没落而逐渐没落。。。但有很多H5游戏引擎,也同样使用AS语言。比如LayaAir引擎等。

2,TypeScript语言,简称TS语言。由微软出品,微软和谷歌共同维护的一门完全符合ECMA标准的语言,可以视作JS的超集。超集这个概念怎么理解呢?就是“所有的JS语言,同时也是TS语言,而TS比今天的JS,更像未来的JS”。就比如目前的JS版本只出到了ES6或ES7。那么ES10是啥样?现在并没人见过,连ECMA组织也不知道。。。但有一点可以确定的是,它和TypeScript长的很像。而TS是包含JS的。换言之,JS本身也可以视作是TS的一部分。只是TS里的内容要远比JS多的多。这语言主要有两种用法,一是像AS语言一样结合游戏引擎,比如cocos creator,白鹭等引擎都支持。还有一种用法就是。。。结合Three.JS之类的库,完全按照JS本身的用法去使用。

3,C#语言。虽然JS得名字里面带个Java。但和它长的最像的语言,却并不是JAVA,而是C#。简单说就是:“JS的名字和JAVA有多像,语法就和C#有多像”。所以C#也比较容易转换成JS。但这并不是重点,重点是有一个超级牛的游戏引擎,是使用C#作为开发语言的。就是大名鼎鼎的Unity3D。Unity3D可以直接把C#编写的游戏项目,虚拟现实项目等,编译发布到WebGL。

<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>