【散分】花了一晚上写了个JavaScript小游戏

wujinjian2008n wujinjian2008n 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
在 ie7,火狐,谷歌 都测试通过。
源码:


<!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>wujinjian</title>
<script type="text/javascript">
//11 个单元格,每个单元格的大小就 等于 地图的大小(mapWH)/mapSize
var mapSize=11;
//地图的大小
var mapWH=440;
//记录对方的ID
var computerID;
//这个方向是否可走
var isPath=true;
//记录四方位上距离对方的距离
var up=0;
var left=0;
var right=0;
var down=0;
//障碍物的最多个数(可重叠)
var za=3;

window.onerror=function()
{
alert("异常!点击确定重新开始");
window.location.href=window.location.href;
};

function createMap()
{
var x=Math.round(Math.random()*(mapSize-1)); //行
var y=Math.round(Math.random()*(mapSize-1)); //列

if(x==0)
x=x+1;
else if(x==(mapSize-1))
x=x-1;
if(y==0)
y=y+1;
else if(y==(mapSize-1))
y=y-1;

//var x=7;
//var y=2;

computerID=x+"_"+y;

var tabobj=document.createElement("table");
tabobj.style.width=mapWH+"px";
tabobj.style.height=mapWH+"px";

tabobj.border="1";

var tbodyobj=document.createElement("tbody");

for(var i=0;i<mapSize;i++)
{
var trobj=document.createElement("tr");

for(var j=0;j<mapSize;j++)
{
var tdobj=document.createElement("td");
tdobj.style.border="rgb(128,128,255) solid 1px";
tdobj.id=i+"_"+j
tdobj.onclick=tdClick;

if(i+"_"+j==computerID)
{
tdobj.style.backgroundColor="red";
}

var txt=document.createTextNode(" ");
tdobj.appendChild(txt);

trobj.appendChild(tdobj);
}

tbodyobj.appendChild(trobj);
}

tabobj.appendChild(tbodyobj);

document.getElementById("map_div").appendChild(tabobj);

//默认随机障碍物
for(var i=0;i<za;i++)
{
var _id=Math.round(Math.random()*(mapSize-1)) +"_"+ Math.round(Math.random()*(mapSize-1));
if(document.getElementById(_id).style.backgroundColor=="")
document.getElementById(_id).style.backgroundColor="gray";
}

for(var i=0;i<mapSize;i++)
{
document.getElementById(i+"_"+(mapSize-1)).style.border="rgb(223,223,223) solid 1px";
document.getElementById((mapSize-1)+"_"+i).style.border="rgb(223,223,223) solid 1px";
document.getElementById(i+"_0").style.border="rgb(223,223,223) solid 1px";
document.getElementById("0_"+i).style.border="rgb(223,223,223) solid 1px";
}
}

function tdClick()
{
if(this.style.backgroundColor=="")
{
this.style.backgroundColor="gray";

up=0;
left=0;
right=0;
down=0;

computerXZ();
}
}

function computerXZ()
{
var xy=computerID.split("_");
var x=xy[0]-0;
var y=xy[1]-0;

//中心位置
var mid=(mapSize-1)/2;

//左上角
if(x<=mid && y<=mid)
{
//向上
if(x<=y)
{
//向上不通,向左走 //false 表示是判断,true 表示行走
if(!computerUp(x,y,false))
{
//向左不通,向右走
if(!computerLeft(x,y,false))
{
//向右不通,向下走
if(!computerRight(x,y,false))
{
//向下不通,向下走(往最长的方向走)
if(!computerDown(x,y,false))
{

direction(up,left,right,down,x,y)
}
}
}
}
}
else //向左
{
if(!computerLeft(x,y,false))
{
if(!computerUp(x,y,false))
{
if(!computerDown(x,y,false))
{
if(!computerRight(x,y,false))
{
direction(up,left,right,down,x,y)
}
}
}
}
}
}
//右上角
else if(x<=mid && y>=mid)
{
if(x<=(mapSize-1-y)) //向上
{
if(!computerUp(x,y,false))
{
if(!computerRight(x,y,false))
{
if(!computerLeft(x,y,false))
{
if(!computerDown(x,y,false))
{
direction(up,left,right,down,x,y)
}
}
}
}
}
else //向右
{
if(!computerRight(x,y,false))
{
if(!computerUp(x,y,false))
{
if(!computerDown(x,y,false))
{
if(!computerLeft(x,y,false))
{
direction(up,left,right,down,x,y)
}
}
}
}
}
}
//右下角
else if(x>=mid && y>=mid)
{
if(x>=y) //向下
{
if(!computerDown(x,y,false))
{
if(!computerRight(x,y,false))
{
if(!computerLeft(x,y,false))
{
if(!computerUp(x,y,false))
{
direction(up,left,right,down,x,y)
}
}
}
}
}
else //向右
{
if(!computerRight(x,y,false))
{
if(!computerDown(x,y,false))
{
if(!computerUp(x,y,false))
{
if(!computerLeft(x,y,false))
{
direction(up,left,right,down,x,y)
}
}
}
}
}
}
//左下角
else if(x>=mid && y<=mid)
{
if((mapSize-1-x)<=y) //向下
{
if(!computerDown(x,y,false))
{
if(!computerLeft(x,y,false))
{
if(!computerRight(x,y,false))
{
if(!computerUp(x,y,false))
{
direction(up,left,right,down,x,y)
}
}
}
}
}
else //向左
{
if(!computerLeft(x,y,false))
{
if(!computerDown(x,y,false))
{
if(!computerUp(x,y,false))
{
if(!computerRight(x,y,false))
{
direction(up,left,right,down,x,y)
}
}
}
}
}
}

}
...全文

相关推荐

JS和HTML和JAVAScript写的打字小游戏 .html

JS和HTML和JAVAScript写的打字小游戏 .htmlJS和HTML和JAVAScript写的打字小游戏 .htmlJS和HTML和JAVAScript写的打字小游戏 .htmlJS和HTML和JAVAScript写的打字小游戏 .htmlJS和HTML和JAVAScript写的打字小游戏 .htmlJS和HTML和JAVAScript写的打字小游戏 .htmlJS和HTML和JAVAScript写的打字小游戏 .htmlJS和HTML和JAVAScript写的打字小游戏 .htmlJS和HTML和JAVAScript写的打字小游戏 .htmlJS和HTML和JAVAScript写的打字小游戏 .htmlJS和HTML和JAVAScript写的打字小游戏 .html

王大师王文峰

JavaScript实现2048小游戏,我终于赢了一把

作者简介 作者名:编程界明世隐 简介:CSDN博客专家,从事软件开发多年,精通Java、JavaScript,博主也是从零开始一步步把学习成长、深知学习和积累的重要性,喜欢跟广大ADC一起打野升级,欢迎您关注,期待与您一起学习、成长、起飞! 系列目录 1. JavaScript 贪吃蛇游戏 2. JavaScript 俄罗斯方块 3. JavaScript 扫雷小游戏 4. JavaScript 网红太空人表盘 效果图 实现思路 编写页面和画布代码。 绘制背景。 绘制好全部卡片。 随机生成一个卡片(

编程界小明哥

利用js写的见缝插针小游戏(推荐给新手)

标题之所以写推荐给新手,是因为我也是个刚学js一周的新人,想练练手,于是就花了一下午写了这个游戏,所以我写的可能更适合新手,大神勿喷。    这个游戏是利用原生的js以及html5的canvas技术做的一个小游戏,游戏规则跟手机上的见缝插针一样的,这是我的游戏界面: 这个是无限插针的,就是针有无限多,直到你失败了或是点击reset按钮停止. 以下把我的源码贴出来供大家参考: js代

arbitatry

JS和HTML和JAVAScript写简单的翻牌游戏小游戏 .html

JS和HTML和JAVAScript写简单的翻牌游戏小游戏 .htmlJS和HTML和JAVAScript写简单的翻牌游戏小游戏 .htmlJS和HTML和JAVAScript写简单的翻牌游戏小游戏 .htmlJS和HTML和JAVAScript写简单的翻牌游戏小游戏 .htmlJS和HTML和JAVAScript写简单的翻牌游戏小游戏 .htmlJS和HTML和JAVAScript写简单的翻牌游戏小游戏 .htmlJS和HTML和JAVAScript写简单的翻牌游戏小游戏 .htmlJS和HTML和JAVAScript写简单的翻牌游戏小游戏 .htmlJS和HTML和JAVAScript写简单的翻牌游戏小游戏 .htmlJS和HTML和JAVAScript写简单的翻牌游戏小游戏 .htmlJS和HTML和JAVAScript写简单的翻牌游戏小游戏 .htmlJS和HTML和JAVAScript写简单的翻牌游戏小游戏 .htmlJS和HTML和JAVAScript写简单的翻牌游戏小游戏 .htmlJS和HTML和JAVAScript写简单的翻牌游戏小游戏 .htmlJS和HTML和JA

王大师王文峰

纯js写的小游戏,打开就可以玩,类似于是男人就坚持20秒

纯javascript写的小游戏, 打开浏览器就可以玩, 类似于是男人就坚持20秒。

zwl
评论 860
收藏 已收藏
分享
返回顶部
wujinjian2008n
JavaScript 87,983 成员 224,681 内容