邹阳|zouyang's blog学无止境,聚沙成塔,无所畏惧

位置: 首页 » 学习 » 简化版打地鼠游戏  [阅读繁体版本]

简化版打地鼠游戏

 今日无课,研究程序

罗维老师写的打地鼠程序如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>打地鼠-未进行美化</title>
</head>
<body  onkeydown='moveRole();'>
<font color=red size="20">您现在的得分为:<div id="score">0</div></font>
<script type="text/javascript">
//以下代码演示javascript数组
var role=new Array;//定义角色数组
var maxWidth = document.documentElement.clientWidth-50;
//alert(maxWidth);
var maxHeight = document.documentElement.clientHeight-50;
var activeRole='';//当前角色,此角色可移动
var step=5;//步长
var score=0;
createRole(1);
/*createRole(2);
createRole(3);
createRole(4);*/
//创建游戏角色
function createRole(id){
 role[id]=document.createElement('img');
 role[id].id=id+'role';
 role[id].src='face.gif';
 document.body.appendChild(role[id]);
 role[id].onclick=setActiveRole1;
 role[id].style.position='absolute';
 role[id].style.top=Math.round(Math.random()*maxHeight)+'px';//设定y
 role[id].style.left=Math.round(Math.random()*maxWidth)+'px';//设定x
}
//随机移动角色
function moveRole1(){
 //alert(role.length);
 for(i=1;i<role.length;i++){
  //var id=i+'role';
  role[i].style.top=Math.round(Math.random()*maxHeight)+'px';//设定y
  role[i].style.left=Math.round(Math.random()*maxWidth)+'px';//设定x
 }
 window.setTimeout(moveRole1,2000);
}
window.setTimeout(moveRole1,2000);
//当角色被点击时的动作
function setActiveRole1(){
 //event.srcElement;
 alert(event.srcElement.id);
 score=score+1;//加一分
 document.getElementById('score').innerText=score;
}
</script>
</body>
</html>

  • 相关文章:

发表评论:

最近发表

最新评论及回复

Copyright xxxx-xxxx Your WebSite. Some Rights Reserved.渝ICP备08000190号
Powered By Z-Blog 1.8 Spirit Build 80605  Skin By Stephen