js实现打地鼠
2017-05-19 23:10
232 查看
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
.content{
width: 330px;
height: 330px;
margin: 50px auto;/*草坪margin为50*/
background: url(img/5.jpg) no-repeat;
background-size: 100% 100%;
}
.content img{
width: 100px;
height: 100px;
}
#score{
width: 100px;
height: 50px;
margin: 0 auto;
background: lightpink;
text-align: center;
line-height: 50px;
}
#score1{
width: 100px;
height: 50px;
margin: 0 auto;
background: lightpink;
text-align: center;
line-height: 50px;
}
#btn{
/*display: block;*/
width: 80px;
height: 50px;
margin: 20px auto;
font-family: forte;/*漂亮字体*/
font-size:30px;
color: greenyellow;
}
.title{
position:relative;
width: 330px;
height: 30px;
background-color: lightblue;
margin: 20px auto;
font-family: arial;
font-size: 20px;
color: lightcoral;
text-align:center;
line-height: 30px;
}
</style>
</head>
<body>
<div class="title">
<marquee direction="right" behavior="alternate" scrollamount="10" scrolldelay="10">一起打地鼠呀</marquee>
</div>
<div class="content" id="game">
<img src="img/5.j
4000
pg" onclick="beat(this)">
<img src="img/5.jpg" onclick="beat(this)">
<img src="img/5.jpg" onclick="beat(this)">
<img src="img/5.jpg" onclick="beat(this)">
<img src="img/5.jpg" onclick="beat(this)">
<img src="img/5.jpg" onclick="beat(this)">
<img src="img/5.jpg" onclick="beat(this)">
<img src="img/5.jpg" onclick="beat(this)">
<img src="img/5.jpg" onclick="beat(this)">
</div>
<p id="score">得分:0分</p>
<p id="score1">得分:0分</p>
<div id="btn" onclick="start()">Start</div>
<script>
// 定义全局变量
var lastTemp=0;
//实现开始按钮的点击函数
function start(obj){
// alert("可以游戏了");
setInterval(timer,1000);
}
function timer(){
//产生随机数0-8
var temp=Math.round(Math.random()*8);
//获取最外层的div
var contentDiv=document.getElementById("game");
//找到相应整数的子类
var child=contentDiv.children[temp];
child.src="img/2.gif";
if(lastTemp!=temp){
// 将上一秒的地鼠变为草坪
var lastChild=contentDiv.children[lastTemp];
lastChild.src="img/5.jpg";
}
lastTemp=temp;
}
//设置一个参数,记录分数
var x=0,k=0;
function beat(obj){
//取出图片路径
var srcstr=obj.src;
//截取字符串,若倒数第五位字符串为2,则为地鼠,否则为草坪
if(srcstr.charAt(srcstr.length-5)=="2"){
x++;
obj.src="img/5.jpg";
document.getElementById("score").innerHTML="得分:"+x;
}else if(srcstr.charAt(srcstr.length-5)=="5"){
k++;
document.getElementById("score1").innerHTML="失分:"+k;
}
}
</script>
</body>
</html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
.content{
width: 330px;
height: 330px;
margin: 50px auto;/*草坪margin为50*/
background: url(img/5.jpg) no-repeat;
background-size: 100% 100%;
}
.content img{
width: 100px;
height: 100px;
}
#score{
width: 100px;
height: 50px;
margin: 0 auto;
background: lightpink;
text-align: center;
line-height: 50px;
}
#score1{
width: 100px;
height: 50px;
margin: 0 auto;
background: lightpink;
text-align: center;
line-height: 50px;
}
#btn{
/*display: block;*/
width: 80px;
height: 50px;
margin: 20px auto;
font-family: forte;/*漂亮字体*/
font-size:30px;
color: greenyellow;
}
.title{
position:relative;
width: 330px;
height: 30px;
background-color: lightblue;
margin: 20px auto;
font-family: arial;
font-size: 20px;
color: lightcoral;
text-align:center;
line-height: 30px;
}
</style>
</head>
<body>
<div class="title">
<marquee direction="right" behavior="alternate" scrollamount="10" scrolldelay="10">一起打地鼠呀</marquee>
</div>
<div class="content" id="game">
<img src="img/5.j
4000
pg" onclick="beat(this)">
<img src="img/5.jpg" onclick="beat(this)">
<img src="img/5.jpg" onclick="beat(this)">
<img src="img/5.jpg" onclick="beat(this)">
<img src="img/5.jpg" onclick="beat(this)">
<img src="img/5.jpg" onclick="beat(this)">
<img src="img/5.jpg" onclick="beat(this)">
<img src="img/5.jpg" onclick="beat(this)">
<img src="img/5.jpg" onclick="beat(this)">
</div>
<p id="score">得分:0分</p>
<p id="score1">得分:0分</p>
<div id="btn" onclick="start()">Start</div>
<script>
// 定义全局变量
var lastTemp=0;
//实现开始按钮的点击函数
function start(obj){
// alert("可以游戏了");
setInterval(timer,1000);
}
function timer(){
//产生随机数0-8
var temp=Math.round(Math.random()*8);
//获取最外层的div
var contentDiv=document.getElementById("game");
//找到相应整数的子类
var child=contentDiv.children[temp];
child.src="img/2.gif";
if(lastTemp!=temp){
// 将上一秒的地鼠变为草坪
var lastChild=contentDiv.children[lastTemp];
lastChild.src="img/5.jpg";
}
lastTemp=temp;
}
//设置一个参数,记录分数
var x=0,k=0;
function beat(obj){
//取出图片路径
var srcstr=obj.src;
//截取字符串,若倒数第五位字符串为2,则为地鼠,否则为草坪
if(srcstr.charAt(srcstr.length-5)=="2"){
x++;
obj.src="img/5.jpg";
document.getElementById("score").innerHTML="得分:"+x;
}else if(srcstr.charAt(srcstr.length-5)=="5"){
k++;
document.getElementById("score1").innerHTML="失分:"+k;
}
}
</script>
</body>
</html>
相关文章推荐
- 无聊的人用JS实现了一个简单的打地鼠游戏
- Day30 - JS实现打地鼠游戏
- JavaScript强化教程 —— JS实现一个基本的打地鼠游戏
- js实现打地鼠小游戏
- web前端学习--用js实现计算器、迷宫、打地鼠
- 用简单的代码实现简易的rxjs的filter功能,帮助rxjs新手了解rxjs的操作符的工作原理。
- js 采用apply实现继承
- html页面js实现多个div定时轮流出现
- 图片倒计时实现原理--js
- js实现文件上传自定义样式
- js实现省市二级联动
- JS+canvas实现的五子棋游戏【人机大战版】
- 走马灯效果代码js appendChild实现的无缝滚动
- js实现加载页面
- js实现Tooltip
- JS实现标签滚动切换效果
- 原生js实现自适应宽度瀑布流以及加载效果——李帅醒博客
- js如何实现继承
- js 实现倒计时
- jquery结合js实现动态添加内容,并给动态添加的内容添加事件