<!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" xml:lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<script type="text/javascript">
// window.οnlοad=init;
var dingshiqi;
var sj;//时间定时器名
var count=0;
var n=0;//记录游戏时间的变量
//点击开始游戏的函数
function startGame()
{
window.clearInterval(dingshiqi);
window.clearInterval(sj);
sj=window.setInterval("shijian()",1000);
dingsiqi=window.setInterval("star()",400);
}
function star()
{
//创建星星
var obj=document.createElement("img");
//给星星添加src属性
obj.src="xx.jpeg";
//随机星星大小
var w=Math.floor(Math.random()*80+20);
obj.width=w;
//随机位置
var x=Math.floor(Math.random()*1166+100);
var y=Math.floor(Math.random()*500+100);
obj.style.position="absolute";
obj.style.top=y+"px";
obj.style.left=x+"px";
//添加点击事件
obj.οnclick=removestar;
count++;
var span=document.getElementById("d3");
span.style.width=count*5+"px";
if(count>20)
{
alert("停止游戏");
window.clearInterval(dingshiqi);
location.reload();
}
//放到body中
document.body.appendChild(obj);
}
//点击删除星星的函数
function removestar()
{
this.parentNode.removeChild(this);
count--;
var sp=document.getElementById("d3");
sp.style.width=count*5+"px";
}
//点击暂停游戏的函数
function zanting()
{
alert("暂停游戏");
}
//记录游戏时间的函数
function shijian()
{
n++;
var obj=document.getElementById("d1");
obj.innerHTML="游戏进行了"+n+"秒";
}
</script>
<style type="text/css">
#d2
{
border:1px solid red;
width:100px;
height:20px;
display:inline-block;
}
#d3
{
display: inline-block;
background:yellow;
height:20px;
}
</style>
</head>
<body>
<input type="button" value="开始游戏" οnclick="startGame()" />
<input type="button" value="暂停游戏" οnclick="zanting()" />
<span id="d1">游戏进行的0秒</span>
<span id="d2"><span id="d3"></span></span>
</body>
</html>