来自于 千年-today
var curCount;
var ranCount;
var curTotal=0;
var Count = document.getElementsByTagName("td");
var time = 10; //初始时间为10
var overRandom;
var overClear;
var overSub;
function randomCount(){
var ran = Math.random()*9|0;//随机一个1到9的数
curCount = document.getElementById(ran);
curCount.style.backgroundImage="url(images/1.jpg)"; //背景图片为地鼠出现
curCount.style.backgroundSize="cover";
curCount.state="yes";
ranCount=ran;
}
//清空9个方格
function clear(){
for(var i=0;i<9;i++){
var forCount = document.getElementById(i);
forCount.style.backgroundImage="url(images/2.jpg)";
forCount.style.backgroundSize="cover";
forCount.state="no";
}
}
//点击砸中地鼠
function onClick(v){
if(v.state=="yes"){
v.style.backgroundImage="url(images/3.jpg)";
v.style.backgroundSize="cover";
curTotal++;
document.getElementById("put").value="打到"+curTotal+"个";
}
}
//时间减少
function timeSub(){
time--;
document.getElementById("time").value="剩余时间"+time+"s";
if(time<=0){
window.clearInterval(overRandom);
window.clearInterval(overClear);
window.clearInterval(overSub);
clear();
alert("游戏结束,你打到了"+curTotal+"个");
}
}
//用定时器周期生成和清空地鼠
function start(){
time=10;
curTotal=0;
overRandom = window.setInterval("randomCount()",600);
overClear = window.setInterval("clear()",2100);
overSub = window.setInterval("timeSub()",1000);
}
哪位大神能详细的解读一下,感激不尽!在线等,挺急的
var curCount;
var ranCount;
var curTotal=0;
var Count = document.getElementsByTagName("td");
var time = 10; //初始时间为10
var overRandom;
var overClear;
var overSub;
function randomCount(){
var ran = Math.random()*9|0;//随机一个1到9的数
curCount = document.getElementById(ran);
curCount.style.backgroundImage="url(images/1.jpg)"; //背景图片为地鼠出现
curCount.style.backgroundSize="cover";
curCount.state="yes";
ranCount=ran;
}
//清空9个方格
function clear(){
for(var i=0;i<9;i++){
var forCount = document.getElementById(i);
forCount.style.backgroundImage="url(images/2.jpg)";
forCount.style.backgroundSize="cover";
forCount.state="no";
}
}
//点击砸中地鼠
function onClick(v){
if(v.state=="yes"){
v.style.backgroundImage="url(images/3.jpg)";
v.style.backgroundSize="cover";
curTotal++;
document.getElementById("put").value="打到"+curTotal+"个";
}
}
//时间减少
function timeSub(){
time--;
document.getElementById("time").value="剩余时间"+time+"s";
if(time<=0){
window.clearInterval(overRandom);
window.clearInterval(overClear);
window.clearInterval(overSub);
clear();
alert("游戏结束,你打到了"+curTotal+"个");
}
}
//用定时器周期生成和清空地鼠
function start(){
time=10;
curTotal=0;
overRandom = window.setInterval("randomCount()",600);
overClear = window.setInterval("clear()",2100);
overSub = window.setInterval("timeSub()",1000);
}
哪位大神能详细的解读一下,感激不尽!在线等,挺急的
<script type="text/javascript">
/* 应该是一个9宫格打地鼠游戏 */
// 当前地鼠出现的格子的dom
var curCount;
// 当前地鼠出现的格子数
var ranCount;
// 总共打倒的地鼠个数
var curTotal=0;
// 格子数量
var Count = document.getElementsByTagName("td");
var time = 10; //初始时间为10 // 定义三个循环定时器的变量
var overRandom;
var overClear;
var overSub;
// 获取1-9随机数,得到的随机数即地鼠出现的格子
function randomCount(){
var ran = Math.random()*9|0;//随机一个1到9的数
// 获取指定格子的地鼠dom
curCount = document.getElementById(ran);
// 设置该格子的背景为地鼠背景图,即地鼠出现
curCount.style.backgroundImage="url(images/1.jpg)"; //背景图片为地鼠出现
// 设置为背景图覆盖样式
curCount.style.backgroundSize="cover";
// 设置自定义的属性state状态为yes,表示该格子没有地鼠
curCount.state="yes";
// 全局变量ranCount赋值,代表当前地鼠出现的格子
ranCount=ran;
}
//清空9个方格
function clear(){
for(var i=0;i<9;i++){
// 循环获取地鼠格子
var forCount = document.getElementById(i);
// 把所有背景替换成默认图片
forCount.style.backgroundImage="url(images/2.jpg)";
forCount.style.backgroundSize="cover";
// 设置自定义的属性state状态为no
forCount.state="no";
}
}
//点击砸中地鼠
function onClick(v){
// 根据当前格子是否存在地鼠来进入条件
if(v.state=="yes"){
// 这里应该是地鼠被砸中了
v.style.backgroundImage="url(images/3.jpg)";
v.style.backgroundSize="cover";
// 打到的地鼠个数叠加
curTotal++;
document.getElementById("put").value="打到"+curTotal+"个";
}
}
//时间减少
function timeSub(){
time--;
// 获取显示时间的dom
document.getElementById("time").value="剩余时间"+time+"s";
// 当时间结束
if(time<=0){
// 清除循环定时器调用生成地鼠的函数 randomCount
window.clearInterval(overRandom);
// 清除循环定时器调用清理格子的函数 clear
window.clearInterval(overClear);
// 清除循环定时器调用计时的函数 timeSub
window.clearInterval(overSub);
// 顺便清理一下所有格子为初始化状态
clear();
alert("游戏结束,你打到了"+curTotal+"个");
}
}
//用定时器周期生成和清空地鼠
function start(){
time=10;
curTotal=0;
// 开启循环定时器 randomCount, 600毫秒执行一次
overRandom = window.setInterval("randomCount()",600);
// 开启循环定时器 clear, 2100毫秒执行一次
overClear = window.setInterval("clear()",2100);
// 开启循环定时器 timeSub, 1000毫秒执行一次
overSub = window.setInterval("timeSub()",1000);
}
</script>