打字游戏

打字游戏:
开始状态时,暂停按钮禁用,开始按钮可用,点击一次开始按钮后,开始按钮禁用,暂停按钮可用,
然后开始调用计时器不断生成字母,每清除一次后加一分,每通过10个,速度会逐渐递增,掉落5个后游戏结束。点击重新开始刷新页面。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style>
			#container{
				position:absolute;
				top:0;
				left: 0;
				width:100%;
				height: 100%;
				background: url(img/bg.jpg) no-repeat;
				background-size: 100% 100%;
			}		
			#counter{
				position: absolute;	
				width: 230px;
				height: 250px;
				right: 15px;
				bottom: 15px;
				background: url(img/score.png) no-repeat;
				background-size: 100% 100%;
			}			
			button{
				position: absolute;
				width: 100px;
				height: 100px;
				border:none;
				color: transparent;
			}		
			#start,#restart,#pause,#continue{
				position: absolute;
				display: block;
				width: 200px;
				height: 75px;
				bottom: 50px;
				left:30px;
				background: url(img/stop.png) no-repeat;
				background-size: 100% 100%;			
			}
			#restart{
				display: none;
			}			
			#continue{
				display: none;
			}	
			#pause,#continue{
				bottom: 50px;*/
				left:250px;
			}
			#count{
				display: block;
				width: 30px;
				height: 30px;
				position: absolute;
				top:32%;
				right:40%;
				font-size: 25px;
				background: url(img/score.png) no-repeat;
			}
			.start,.restart,.pause,.continue{
				background-color: transparent;
				position: relative;
				bottom: -20px;
				left: 50px;
				border: none;
				color: #8B0000;
				font-size: 20px;
			}
			#gameover span{
				position: absolute;
				width: 600px;
				height: 150px;
				margin: auto;
				top: 0;
				right: 0;
				bottom: 0;
				left: 0;
				font-size:150px;
				color: red;
				display: none;
			}
		</style>
	</head>
	<body>
		<div id="container">
			<div id="start">
				<input class="start" type="button" value="开始游戏"/>
			</div>
			<div id="restart">
				<input class="restart" type="button" value="重新游戏"/>
			</div>
			<div id="pause">
				<input class="pause" type="button" value="暂停游戏"/>
			</div>
			<div id="continue">
				<input class="continue" type="button" value="继续游戏"/>
			</div>
			<div id="counter">
				<span id="count">
					0
				</span>
			</div>
			<div id="gameover">
				<span class="gameover">你输啦!</span>
			</div>
		</div>
		<script>
			var letterArrs = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
			var btnArr = new Array();
			var startInterval;
			var moveInterval;
			var speed = 0.5;
			var con = document.getElementById("container");		
			var c = document.getElementById("count");
			var over = document.getElementsByClassName("gameover")[0];
			var startdiv = document.getElementById("start");
			var restartdiv = document.getElementById("restart");
			var pausediv = document.getElementById("pause");
			var continuediv = document.getElementById("continue");
			var startBtn = document.getElementsByClassName("start")[0];
			var restartBtn = document.getElementsByClassName("restart")[0];
			var pauseBtn = document.getElementsByClassName("pause")[0];
			var continueBtn = document.getElementsByClassName("continue")[0];
			
			startBtn.addEventListener("click",start,false);
			restartBtn.addEventListener("click",restart,false);
			pauseBtn.addEventListener("click",pause,false);	
			continueBtn.addEventListener("click",continue_,false);	
			console.log(startdiv);
			var over_count = 0;
			var count = 0;
			pauseBtn.disabled = true;
			
			
			function start(){
				startBtn.disabled = true;
				pauseBtn.disabled = false;
				startInterval = window.setInterval(create,1000);
				moveInterval = setInterval(move,10);
			}
			
			function restart(){
				window.location.reload();
			}
			
			function pause(){
				window.clearInterval(startInterval);
				clearInterval(moveInterval);
				pausediv.style.display = 'none';
				continuediv.style.display = 'block';
			}
			
			function continue_(){
				pausediv.style.display = 'block';
				continuediv.style.display = 'none';
				start();
			}
			
			function create(){		
				var btn = document.createElement("button");
				var index = parseInt(Math.random()*26);
				btn.innerHTML = letterArrs[index];
				btn.style.background = 'url(img/'+letterArrs[index]+'.png)';
				
				var distance = (Math.random()*1201+100)+'px';
				btn.style.left = distance;
				btn.style.top = 0;
				con.appendChild(btn);
				btnArr.push(btn);		
			}	
			
			function move(){
				for(var i=0;i<btnArr.length;i++){	
					var btn = btnArr[i];
					btn.style.top = btn.offsetTop+speed+'px';
					if(btn.offsetTop>=600){
						btnArr.shift();
						con.removeChild(btn);
						over_count++;
						if(over_count>=5){
							stopGame();
						}
					}
					if(count>=10){
						speed = parseInt(count/10);
					}
				}
			}	
					
			window.onkeyup = function(e){
				console.log(e.keyCode);
				for(var i=0;i<btnArr.length;i++){					
					var btn = btnArr[i];
					if(e.keyCode==btn.innerHTML.charCodeAt()){
						count++;
						con.removeChild(btn);
						btnArr.splice(i,1);
						c.innerText = count;
						break;
					}
				}
			}
			
			function stopGame(){
				clearInterval(startInterval);
				clearInterval(moveInterval);
				startdiv.style.display = 'none';
				restartdiv.style.display = 'block';
				over.style.display = 'block';		
				pauseBtn.disabled = true;
			}
		</script>
	</body>
</html>

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值