jq 实现简单版别踩白块儿(40多行jq代码)

html:

<div class="wrap">
<div class="gamePage">

</div>
<p class="countDown_wrap">倒计时:<span class="countDown">0</span></p>
<p class="score_wrap">分数:<span class="score">0</span></p>
</div>

css:

.wrap{
width: 100%;
height: 100%;
position: relative;
overflow: hidden;
}
.gamePage{
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: -20%;
}
.game_ul{
width: 100%;
height: 20%;
display: flex;
transition: all .3s; 
}
.game_li{
flex: 1;
transition: all 1s; 
}
.aqua{
background:aqua;
}
.gray{
background:#cccccc;
}
.red{
background: red;
}
.score_wrap{
position: absolute;
left: 1rem;
top: 3rem;
z-index: 5;
font-size: .84rem;
color: #000000;
}
.countDown_wrap{
position: absolute;
left: 1rem;
top: 1.5rem;
z-index: 5;
font-size: .84rem;
color: #000000;
}

jq :单位标准使用rem和百分比,rem的标准前面的文章有发表过,整个游戏的逻辑纯属个人的,获取不够成熟

$(function(){
var score = 0;//分数
var countDown = 30; //倒计时
var arrColor = ["aqua","gray","red"]; //随机类名(可不用)
//初始化游戏
function init(){
//for循环初始生成一个5个ul,每个ul里面有4个li
for(var i=0; i<6; i++){
$(".gamePage").append("<ul class='game_ul'></ul>");
}
for(var i=0; i<4; i++){
$(".game_ul").append("<li class='game_li'></li>");
}
//让每个ul中随机的一个li添加一个类名表示可以点区域
$(".game_ul").each(function(){
$(this).children().eq(Math.floor(Math.random()*4)).addClass(arrColor[Math.floor(Math.random()*2)]);
})
//倒计时
var timer = setInterval(function(){
countDown--;
if(countDown==0){
clearInterval(timer);
console.log("时间到了,分数为"+score);
}
$(".countDown").html(countDown);
},1000)
//手机触摸事件
$(".gamePage").on("touchstart",".game_ul:eq(-1) li",function(){
if($(this).hasClass("aqua")||$(this).hasClass("gray")||$(this).hasClass("red")){
$(this).removeClass("black");
$(".game_ul").eq(-1).remove();
createRow(); //调用创建行
setTimeout(function(){
$(".game_ul").eq(0).css("height","");
},1)
score++;
$(".score").html(score);
}else{
clearInterval(timer);
alert("点错了,分数为"+score);
}
})
}
//创建行
function createRow(){
var firstLine = "<ul class='game_ul' style='height:0;'></ul>";
$(".game_ul").eq(0).before(firstLine);
for(var i=0; i<4; i++){
$(".game_ul").eq(0).append("<li class='game_li'></li>");
}
$(".game_ul").children().eq(Math.floor(Math.random()*4)).addClass(arrColor[Math.floor(Math.random()*3)]);
}
init();//调用游戏初始化
})




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值