打地鼠小游戏

这里写自定义目录标题


在这里插入图片描述
html和css样式代码

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>打地鼠</title>
<style type="text/css">
	html{
		height: 100%;
	}
	body{
		margin: 0;
		background-image: url("image/bg.jpg");
		/* 需要绑定鼠标的样式 */
		cursor: url("image/cursor.png"),auto;
		/* 禁止用户选中html文档中的内容 */
		-moz-user-select: none;
		-webkit-user-select: none;
	}
	table{
		width: 600px;
		height: 600px;
		margin: 0 auto;
	}
	td{
		background-image: url("image/hole.png");
		height: 150px;
		background-size: 120px 50px;
		background-position: center bottom;
		text-align: center;
		vertical-align: bottom;
		background-repeat: no-repeat;
	}
	img{
		width: 80px;
		height: 0;/* 默认让老鼠隐藏起来 */
		position: relative;
		bottom: 8px;
		/* 添加动画属性 */
		animation-timing-function: linear;/* 运动规律 */
		/* 当动画结束时,保持当前状态不恢复到开始状态 */
		animation-fill-mode: both;
	}
	.mouseUp{
		animation-name: up;
		animation-duration: 0.3s;/* 完成一次动画需要0.3s */
	}
	.mouseDown{
		animation-name: down;
		animation-duration: 0.1s;
	}
	/* 添加两个动画关键帧 */
	@keyframes down{
		0%{
			height: 70px;
		}
		100%{
			height: 0;
		}
	}
	@keyframes up{
		0%{
			height: 0;
		}
		100%{
			height: 70px;
		}
	}
	#score{
		font-size: 30px;
		position: absolute;
		top: 50px;
		background-color: rgba(100,100,100,0.6);
		line-height: 50px;
		padding-left: 10px;
		padding-right:15px;
		border-top-right-radius: 5px;
		border-bottom-right-radius: 5px;
	}
</style>
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
</head>
<body>
	<!-- 添加音频audio
		 添加视频  video
		 
		 autoplay自动播放
		 loop循环播放 -->
	<audio src="audio/music.mp3" autoplay loop></audio>
	<audio id="dazhong" src=""></audio>
	<p id="score">得分:0</p>
	<table>
		<tr>
			<td><img alt="" src="image/mouse.png"></td>
			<td><img alt="" src="image/mouse.png"></td>
			<td><img alt="" src="image/mouse.png"></td>
			<td><img alt="" src="image/mouse.png"></td>
		</tr>
		<tr>
			<td><img alt="" src="image/mouse.png"></td>
			<td><img alt="" src="image/mouse.png"></td>
			<td><img alt="" src="image/mouse.png"></td>
			<td><img alt="" src="image/mouse.png"></td>
		</tr>
		<tr>
			<td><img alt="" src="image/mouse.png"></td>
			<td><img alt="" src="image/mouse.png"></td>
			<td><img alt="" src="image/mouse.png"></td>
			<td><img alt="" src="image/mouse.png"></td>
		</tr>
		<tr>
			<td><img alt="" src="image/mouse.png"></td>
			<td><img alt="" src="image/mouse.png"></td>
			<td><img alt="" src="image/mouse.png"></td>
			<td><img alt="" src="image/mouse.png"></td>
		</tr>
	</table>
	<script type="text/javascript" src="dadishu.js"></script>
</body>
</html>

js代码

//声明变量,记录得分值
var score = 0;
//让老鼠显示出来
function show(){
	//从16个同随机出来一个老鼠
	var a = Math.floor(Math.random()*16);
	//$("img").eq(a)等同于$("img:eq(" + a +")")
	$("img:eq(" + a +")").addClass("mouseUp").removeClass("mouseDown");
	//在2.5秒之后删除mouseUp
	setTimeout(function() {
		$("img:eq(" + a + ")").addClass("mouseDown").removeClass("mouseUp");
	},2500);
	
}
//让老鼠无限的生成,怎么生成多个,并且无限生成
function playing(){
	show();show();show();show();show();show();show();show();
}

setInterval(playing,2000);

//添加点击事件 打中老鼠
$("img").click(function(){
//	当老鼠被击中的时候,我们添加一个打中的声音
	$("#dazhong").attr("src","audio/dazhong.wav").get(0).play();
	//同时需要让打中的老鼠隐藏掉
//	this指代被击中的老鼠
	$(this).addClass("mouseDown").removeClass("mouseUp");
	//同时分数相应的增加10
	score+=10;
	//集中老鼠得分之后的值重新插入到页面中
	$("#score").text("得分:"+score+"分");
})
//添加单击鼠标左键的时候,锤子下落,并且迅速上升
$("body").mousedown(function(){
	//当鼠标下落的时候,添加下落的锤子
	$("body").css("cursor","url(image/cursor-down.png),auto");
}).mouseup(function(){
	//当鼠标松开的时候,锤子上升
	$("body").css("cursor","url(image/cursor.png),auto");
})
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值