JavaScript和JQuery

JavaScript和JQuery

工具网站:
https://jquery.cuishifeng.cn/index.html

JQuery:
使用变量

  1. $(‘img:nth-child(’+i+’)’)
    选中当前img元素的父级元素的第i个img元素
    注意点:使用变量的时候变量前后有个‘+’。
    $(‘img’).eq(i)也可选中第i个img元素。

img

  1. $(‘img:nth-child(’+i+’)’).attr(‘src’,’./imgs/mouse.png’)
    设置img的路径

JS:
计时相关

  1. let timer = setInterval(function(){…}, 1000)
    clearInterval(timer)
    setInterval是每1000ms执行一次function的内容。
    clearInterval是清空计时器。
  2. setTimeout(function(){…}, 1000)
    在1000ms后执行function的内容。

模仿源码之家小游戏:

  1. 打地鼠
    https://www.mycodes.net/166/10359.htm
    源码之家用JavaScript Dom,我的是用JQuery
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>打地鼠</title>
<style>
body, div, img, button{
	margin:0;
	padding:0;
}

.glass{
	margin:0 auto;
	height: 900px;
	width: 800px;
	background: url("./imgs/timg.jpg") no-repeat center;
	position: relative;
	bottom: 157px;
}

img{
	height: 100px;
	width: 100px;

}

img:nth-child(1){
	position: absolute;
	left: 152px;
	top: 295px;
}

img:nth-child(2){
	position: absolute;
	left: 359px;
	top: 295px;
}

img:nth-child(3){
	position: absolute;
	left: 562px;
	top: 295px;
}

img:nth-child(4){
	position: absolute;
	left: 152px;
	top: 429px;
}

img:nth-child(5){
	position: absolute;
	left: 359px;
	top: 429px;
}

img:nth-child(6){
	position: absolute;
	left: 562px;
	top: 429px;
}

img:nth-child(7){
	position: absolute;
	left: 152px;
	top: 567px;
}

img:nth-child(8){
	position: absolute;
	left: 359px;
	top: 567px;
}

img:nth-child(9){
	position: absolute;
	left: 562px;
	top: 567px;
}

#time{
	width:200px;
	height:30px;
	background:#efb113;
	border-radius:10px;
	position: absolute;
	left: 103px;
	top: 251px;
	text-align: center;
	line-height : 30px; 
	color: #794c20;
	font-weight: bolder;
}

#score{
	width:200px;
	height:30px;
	background:#efb113;
	border-radius:10px;
	position: absolute;
	left: 496px;
	top: 251px;
	text-align: center;
	line-height : 30px; 
	color: #794c20;
	font-weight: bolder;
}

button{
	width:200px;
	height:30px;
	background:#ef4113;
	border: none;
	border-radius: 10px;
	position: absolute;
	left: 310px;
	top: 482px;
	font-weight: bolder;
	color: white;
	text-align: center;
	line-height : 30px; 
	
}

</style>
<script src="lib/jquery-3.6.0.js"></script>

</head>
<body>
<div class="glass">
	<img src="./imgs/mouse.png">
	<img src="./imgs/mouse.png">
	<img src="./imgs/mouse.png">
	
	<img src="./imgs/mouse.png">
	<img src="./imgs/mouse.png">
	<img src="./imgs/mouse.png">
	
	<img src="./imgs/mouse.png">
	<img src="./imgs/mouse.png">
	<img src="./imgs/mouse.png">
	<button style="visibility:visible;"></button>
	<div id="time"></div>
	<div id="score"></div>
</div>

<script src="mouse.js"></script>

</body>
</html>
/**
 * 游戏实现的效果:
 * 1. 显示剩余时间,目前得分;
 * 2. 点击按钮后按钮消失游戏开始,游戏结束按钮出现;
 * 3. 游戏内容:
 * 	3.1 地鼠随机出现在某个洞中;
 *  3.2 点击地鼠,得分,地鼠显示出被打的状态;
 *  3.3 时间到,游戏结束。
 */

//初始化
var time = $('#time');
var score = $('#score');
var btn = $('button');
time.text('剩余时间');
score.text('得分: 0');
btn.text('开始游戏');
for(let i=1;i<=9;i++){
	$('img:nth-child('+i+')').hide();
}



btn.click(function(){
	$(this).hide();
	time.text('剩余时间');
	score.text('得分: 0');
	//游戏时间10s
	let count = 10;
	let currentScore = 0;
	let timer1 = setInterval(function(){
		time.text('剩余时间: '+count+'s');
		count--;
		
		if(count<0){
			clearInterval(timer1);
			clearInterval(timer2);
			time.text('游戏结束');
			btn.show();
		}
		
	}, 1000);
	

	let timer2 = setInterval(function(){
		for(let i=1;i<=9;i++){
			$('img:nth-child('+i+')').attr('src','./imgs/mouse.png');
			$('img:nth-child('+i+')').hide();
		}
		let cur = Math.floor(Math.random()*9);
		$('img:nth-child('+cur+')').show();
		$('img:nth-child('+cur+')').click(function(){
			$('img:nth-child('+cur+')').attr('src','./imgs/mouse2.png');
			currentScore++;
			score.text('当前得分:'+currentScore);
			setTimeout(function(){
				$('img:nth-child('+cur+')').hide();
			}, 200);
		})
		
		
	}, 2000);
	
})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值