html+css+js识色

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./download/font_2970444_40zea46u07p/iconfont.css">
    <style>
*{
    margin: 0;
    padding: 0;
}
span{
    padding: 0;
    margin: 0;
}
body{
    font-family: -apple-system, BlinkMacSystemFont,
    "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell",
    "Fira Sans", "Droid Sans", "Helvetica Neue",
    sans-serif;
}
.game{
    width: 480px;
    height: 640px;
    margin: 0 auto;
    text-align: center;
    background-color: #ff6666;
}
/* 游戏开始之前 */
.begin{
    width: 480px;
    height: 640px;
    position: relative;
    display: block;
}

.begin-big-title{
    width: 100%;
    height: 150px;
    font-size: 50px;
    color: white;
    font-weight: 550;
    margin: auto;
    line-height: 150px;
    text-shadow: 2px 3px 0px #cc3300;
}
.begin-icon{
    position: relative;
}
.begin-icon::after{
    content: "识";
    display: inline-block;
    font-size: 20px;
    color: #ffcc00;
    font-weight: bolder;
    transform: rotate(-20deg);
    position: absolute;
    margin-top: -35px;
    margin-left: -15px;
}
.begin-small-title{
    width: 250px;
    height: 50px;
    margin: auto;
    line-height: 50px;
    font-size: 15px;
    color: white;
    font-weight: bold;
    opacity: 0.8;
}
.begin-button{
    width: 200px;
    height: 50px;
    line-height: 50px;
    font-size: 35px;
    font-weight: 900;
    color: white;
    border: 1px solid #ffcc00;
    border-radius: 4%;
    border-bottom: #000;
    background-color: #ffcc00;
    position: absolute;
    padding: 5px;
    bottom: 150px;
    left: 50%;
    transform: translateX(-50%);
    box-shadow: 0px 5px 0px #c9a30e;
}
.score-button{
    width: 200px;
    height: 50px;
    line-height: 50px;
    font-size: 35px;
    font-weight: 900;
    color: white;
    border: 1px solid #ffcc00;
    border-radius: 4%;
    background-color: #ffcc00;
    position: absolute;
    padding: 5px;
    bottom: 60px;
    left: 50%;
    transform: translateX(-50%);
    box-shadow: 0px 5px 0px #c9a30e;
}
/* 游戏开始界面 */
.begining{
    width: 480px;
    height: 640px;
    position: relative;
    display: none;
}
.title{
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    justify-content:space-between;
}
.scoreDiv{
    width: 130px;
    height: 60px;
    font-size: 27px;
    font-weight: bold;
    box-sizing: border-box;
    line-height: 60px;
    color: #ff9898;
}
.score{
    padding-left: 5px;
}
.setting{
    width: 130px;
    height: 60px;
    line-height: 60px;
    font-size: 27px;
    font-weight: bold;
    box-sizing: border-box;
    color: #ff9898;
}
.voice{
    font-size: 20px;
    width: 45px;
    height: 25px;
    line-height: 25px;
    padding: 5px;
    color: azure;
    border-radius: 5px;
    box-sizing: border-box;
    background-color: #ffcc00;
    box-shadow: 0px 3px 0px #c9a30e;
}
.stop{
    width: 30px;
    height: 40px;
    padding: 4px;
    color: azure;
    border-radius: 5px;
    font-size: 23px;
    background-color: #ffcc00;
    box-shadow: 0px 3px 0px #c9a30e;
}
/* 中间游戏区域 */
.main-play{
    width: 95%;
    height: 450px;
    border-radius: 5px;
    margin: 0 auto;
    margin-top: 30px;
    background-color: #ccc;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-evenly; 
    align-items: center;
}
li{
    /* 第一次 */
    width: 48%;
    height: 48%;
    /* 第二次 */
    /* width: 32%;
    height: 32%; */
    padding: 1%;
    list-style: none;
    display: block;
    border: 3px solid #ccc;
    border-radius: 5px;
    background-color: green;
    box-sizing: border-box;
}
/* 底部进度条 */
.footer{
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    bottom: 25px;
    width: 425px;
    height: 50px;
    border-radius: 5px;
    background-color: #ff9999;
}
.bk{
    width: 425px;
    height: 50px;
    float: right;
    border-radius: 5px;
    background-color: #ffcc00;
}
/* 游戏结束 */
.GameOver{
    width: 480px;
    height: 640px;
    position: relative;
    display: none;
}
.game-out-title{
    width: 480px;
    height: 100px;
    font-size: 55px;
    line-height: 100px;
    display: inline-block;
    margin-top: 50px;
    color: #fabc60;
    font-weight: bolder;
    text-indent: 20px;
    letter-spacing: 20px;
    box-sizing: border-box;
    text-shadow: 3px 3px 0px #cc6600;
}
.game-level{
    width: 480px;
    height: 100px;
    font-size: 40px;
    line-height: 100px;
    display: inline-block;
    margin-top: 100px;
    color: #fff;
    font-weight: bolder;
}
.game-ni{
    margin-left: 40px;
}
.game-choice{
    width: 480px;
    height: 200px;
    font-size: 30px;
    display: inline-block;
    margin-top: 40px;
    color: #fff;
    font-weight: bolder;
}
.game-again{
    margin: auto;
    width: 200px;
    height: 50px;
    line-height: 50px;
    border: 1px solid #ffcc00;
    border-radius: 4%;
    background-color: #ffcc00;
    box-shadow: 0px 5px 0px #c9a30e;
}
.game-more{ margin: auto;
    width: 200px;
    height: 50px;
    line-height: 50px;
    border: 1px solid #ffcc00;
    border-radius: 4%;
    background-color: #ffcc00;
    box-shadow: 0px 5px 0px #c9a30e;
    margin-top: 15px;
}
.game-submit{
    margin: auto;
    width: 200px;
    height: 50px;
    line-height: 50px;
    border: 1px solid #ffcc00;
    border-radius: 4%;
    background-color: #ffcc00;
    box-shadow: 0px 5px 0px #c9a30e;
    margin-top: 15px;
}
/* 特殊div块 */
.decorate{
    opacity: 0.75;
}
    </style>
</head>
<body>
    <div class="game">
        <!-- 开始游戏 -->
          <div class="begin">
            <div class="begin-big-title">看你有<span class="begin-icon"></span></div>
            <div class="begin-small-title">找出所有色块里颜色不同的一个</div>
            <div class="begin-button">开始游戏</div>
            <div class="score-button">积分排行榜</div>
        </div>
        <!-- 游戏中 -->
        <div class="begining">
            <div class="title">
                <div class="scoreDiv">
                    <span>得分:LV</span><span class="score">1</span>
                </div>
                <div class="setting">
                    <span class="iconfont icon-shengyin voice"></span>
                    <span class="stop">暂停</span>
                </div>
            </div>
            <div class="main-play">

            </div>
            <div class="footer">
                <div class="bk"></div>
            </div>
        </div>
        <!-- 游戏结束 -->
        <div class="GameOver">
            <div class="game-out-title">
                游戏结束
            </div>
            <div class="game-level">
                <span>LV</span>
                <span class="game-score">0</span>
                <span class="game-ni">青铜</span>
            </div>
            <div class="game-choice">
                <div class="game-again">再来一次</div>
                <div class="game-more">更多游戏</div>
                <div class="game-submit">提交积分</div>
            </div>

        </div>
    </div>
    <script>
        //开始视图
var beginView=document.querySelector(".begin");
//开始按钮
var btnGame=document.querySelector(".begin-button");
//游戏中视图
var beginingDiv=document.querySelector(".begining");
//进度条
var bkDiv=document.querySelector(".bk");
//游戏结束的视图
var overDiv=document.querySelector(".GameOver");
//等级分数
var gamescore=document.querySelector(".game-score");
//等级名称
var gameni=document.querySelector(".game-ni");
//再来一次按钮
var againBtn=document.querySelector(".game-again");
//更多游戏
var moreBtn=document.querySelector(".game-more");
//提交积分
var submitBtn=document.querySelector(".game-submit");
//游戏空间的视图
var mainGameDiv=document.querySelector(".main-play");
//bigining页面分数
var beginingscore=document.querySelector(".score");
//倒计时时间设置30s
var maxTime=30;
//背景Div的宽度
var widthDiv=425;
//每次减小的宽度
var evDiv=widthDiv/maxTime;
//最终分数
var lastScore=0;
//将beginvView页面的
btnGame.addEventListener('click',function(){
    beginView.style.display="none";
    beginingDiv.style.display="block";
    init(2);
    var timer=setInterval(function(){
        maxTime--;
        widthDiv=widthDiv-evDiv;
        if(lastScore>=30){
            gameni.innerHTML="王者";
            beginingDiv.style.display="none";
            overDiv.style.display="block";
            gamescore.innerHTML=lastScore;
            clearInterval(timer);
        }
        if(maxTime>0){
            bkDiv.style.width=widthDiv+"px";
        }else{
            bkDiv.style.width="0px";
            beginingDiv.style.display="none";
            overDiv.style.display="block";
            gamescore.innerHTML=lastScore;
            if(lastScore>=4&&lastScore<=8){
                gameni.innerHTML="白银";
            }
            if(lastScore>=9&&lastScore<=13){
                gameni.innerHTML="黄金";
            }
            if(lastScore>=14&&lastScore<=18){
                gameni.innerHTML="铂金";
            }
            if(lastScore>=19&&lastScore<=23){
                gameni.innerHTML="钻石";
            }
            if(lastScore>=24&&lastScore<=29){
                gameni.innerHTML="星耀";
            }
            clearInterval(timer);
        }
    },1000);
});
// 再来一次
againBtn.addEventListener('click',function(){
    beginView.style.display="block";
    overDiv.style.display="none";
    maxTime=30;
    widthDiv=425;
    bkDiv.style.width="425px";
    lastScore=0;
    beginingscore.innerHTML=lastScore;
})

//初始化游戏的界面
function init(level){
    //判断level大小
    if(level>9){
        level=9;
    }
    //根据level的1,2,3,4
        //height

    var liHeight=100/level;
        //width
    var liWidth=100/level;
    console.log("liWidth:"+liWidth);
    console.log("liHeight:"+liHeight);
    console.log("level:"+level+":"+Math.pow(level,2));
    //创建外部的div
    //块数
    var str="";
    num=level*level;
    for(var i=0;i<num;i++){
        str=str+"<li></li>";
    }
    mainGameDiv.innerHTML=str;
    //获取每个li的id
    var verLi=document.querySelectorAll("li");
    //创建这几个块谁是特殊的~~~0-4
    var decorate=Math.floor(Math.random()*Math.pow(level,2));
    //设置每个li的样色
    var color=randomColor();
    //添加修饰
    verLi[decorate].style.opacity="0.75";
    //设置每个块的监听
    for(var i=0;i<verLi.length;i++){
        verLi[i].setAttribute("index",i);
        verLi[i].style.backgroundColor=color;
        //width
        verLi[i].style.width=liWidth+"%";
        //heigh
        verLi[i].style.height=liHeight+"%";
        //设置监听
        verLi[i].addEventListener("click",function(){
            var index=this.getAttribute("index");
            if(index==decorate){
                level++;
                init(level);
                lastScore++;
                beginingscore.innerHTML=lastScore;
            }else{
                //加速
                if(maxTime>0){
                maxTime--;
                widthDiv=widthDiv-evDiv;
                bkDiv.style.width=widthDiv+"px";
                }else{
                    bkDiv.style.width="0px";
                    maxTime=-1;
                }
            }
        });
    }
}
//随机颜色
function randomColor(){
    var str='#';
    for(var i=0;i<6;i++){
        //0-15转为16进制
     str+=Math.floor(Math.random()*16).toString(16);
    }
    return str;
}
    </script>
</body>
</html>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值