【原生JS】做一个打字消除小游戏,学习摸鱼两不误

简介: 【原生JS】做一个打字消除小游戏,学习摸鱼两不误

话不多说先看效果!

2023-04-16.png


JS打字消除游戏

页面布局:

<!DOCTYPE html>
<html>
<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>打字消消乐</title>
     <!-- 这里我使用u-reset来清除一些默认样式 -->
    <link rel="stylesheet" href="./css/u-reset.min.css">
    <link rel="stylesheet" href="./css/index.css">
    <script src="./js/index.js"></script>
</head>
<body>
    <!-- 开始界面开始 -->
    <div class="start">
        <div class="start-aom">
            <img src="./img/start/girl.png" alt="" class="girl">
            <img src="./img/start/text.png" alt="" class="title">
        </div>
        <img src="./img/start/play.png" alt="" class="playbtn">
    </div>
    <!-- 开始界面结束 -->
    <!-- 加载界面开始 -->
    <div class="load">
        <div class="loading">
            <div class="loadTiao">
                <div class="progresscon">
                    <span></span>
                </div>
            </div>
        </div>
    </div>
    <!-- 加载界面结束 -->
    <!-- 游戏开始 -->
    <div class="plays">
        <div class="main"></div>
        <div class="fenshu">
            <span></span>
        </div>
        <div class="xuetiao">
            <div class="xuetiao1"></div>
        </div>
        <img src="./img/play/close.png" class="close" alt="">
        <audio src="./于荣光 - 少林英雄.mp3"></audio>
    </div>
    <!-- 游戏结束 -->
</body>
</html>

CSS样式:

html,body{
    width: 100%;
    height: 100%;
}
.start{
    width: 100%;
    height: 100%;
    background: url(../img/start/bg.png) no-repeat center center/cover;
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 10000;
    transition: all 1s;
    /* display: none; */
}
.start-aom{
    width: 400px;
    height: 400px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}
.girl{
    width: 250px;
    height: 250px;
    display: block;
    margin: 0 auto;
    transform: translateY(70px);
    animation: jump 1s;
}
 @keyframes jump{
    0%{
        transform: translateY(0px);
    }
    50%{
        transform: translateY(160px);
    }
    100%{
        transform: translateY(0px);
    }
}
.title{
    width: 400px;
    height: 200px;
    display: block;
    /* margin: 0 auto; */
    position: relative;
    z-index: 1000;
}
.playbtn{
    width: 50px;
    height: 50px;
    display: block;
    position: absolute;
    right: 0;
    left: 0;
    margin: auto;
    bottom: 50px;
    animation: run 1s infinite;
}
.playbtn:hover{
    cursor: pointer;
}
@keyframes run{
    0%{
        transform: translateY(0px);
    }
    50%{
        transform: translateY(20px);
    }
    100%{
        transform: translateY(0px);
    }
}
/* 加载界面开始 */
.load{
    width: 100%;
    height: 100%;
    background: url(../img/load/bg.png) no-repeat center/cover;
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 100;
    transition: all 1s;
    /* display: none; */
    z-index: 1000;
}
.loading{
    width: 500px;
    height: 500px;
    background: url(../img/load/loading1.png) center no-repeat;
    background-size: 100% auto ;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    /* display: none; */
}
.loadTiao{
    width: 303px;
    height: 39px;
    background: url(../img/load/loadingProgress.png) center no-repeat;
    background-size: 100% auto;
    display: block;
    margin: 220px auto 0;
    position: relative;
}
.progresscon{
    width: 0px;
    height: 19px;
    background: url(../img/load/progresscon.png);
    position: absolute;
    left: 5px;
    top: 9px;
    border-radius: 15px;
}
.progresscon span{
    text-align: center;
    width: 303px;
    display: block;
}
/* 加载界面结束 */
/* 游戏开始 */
.plays{
    width: 100%;
    height: 100%;
    background:url(../img/play/bg.png) center/cover no-repeat ;
    position: absolute;
    top: 0;
    left: 0;
    overflow: hidden;
    z-index: 999;
}
.main{
    width: 100%;
    height: 100px;
    position: relative;
    z-index: 10;
    float: left;
    transition: all 5s;
}
.main img{
    transition: all 0.2s;
}
.xuetiao{
    width: 204px;
    height: 48px;
    background: url(../img/play/life.png) center no-repeat;
    background-size: contain;
    z-index: 100;
    float: left;
    position: absolute;
    top: 19px;
    left: 194px;
}
.xuetiao1{
    width: 132px;
    height: 24px;
    background-color: rgb(224, 31, 31);
    position: absolute;
    top: 13px;
    left: 60px;
    border-radius: 7px;
}
.fenshu{
    width: 168px;
    height: 53px;
    background: url(../img/play/fenzhi.png) center no-repeat;
    background-size: contain;
    z-index: 100;
    position: absolute;
    top: 10px;
    left: 10px;
}
.fenshu span{
    font-size: 20px;
    color: #fff;
    display: block;
    margin-top: 21px;
    margin-left: 95px;
}
.close{
    position: absolute;
    top: 10px;
    right: 10px;
    width: 50px;
    height: 50px;
    z-index: 1000;
}
/* 游戏结束 */

JS文件(重点):

html,body{
    width: 100%;
    height: 100%;
}
.start{
    width: 100%;
    height: 100%;
    background: url(../img/start/bg.png) no-repeat center center/cover;
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 10000;
    transition: all 1s;
    /* display: none; */
}
.start-aom{
    width: 400px;
    height: 400px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}
.girl{
    width: 250px;
    height: 250px;
    display: block;
    margin: 0 auto;
    transform: translateY(70px);
    animation: jump 1s;
}
 @keyframes jump{
    0%{
        transform: translateY(0px);
    }
    50%{
        transform: translateY(160px);
    }
    100%{
        transform: translateY(0px);
    }
}
.title{
    width: 400px;
    height: 200px;
    display: block;
    /* margin: 0 auto; */
    position: relative;
    z-index: 1000;
}
.playbtn{
    width: 50px;
    height: 50px;
    display: block;
    position: absolute;
    right: 0;
    left: 0;
    margin: auto;
    bottom: 50px;
    animation: run 1s infinite;
}
.playbtn:hover{
    cursor: pointer;
}
@keyframes run{
    0%{
        transform: translateY(0px);
    }
    50%{
        transform: translateY(20px);
    }
    100%{
        transform: translateY(0px);
    }
}
/* 加载界面开始 */
.load{
    width: 100%;
    height: 100%;
    background: url(../img/load/bg.png) no-repeat center/cover;
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 100;
    transition: all 1s;
    /* display: none; */
    z-index: 1000;
}
.loading{
    width: 500px;
    height: 500px;
    background: url(../img/load/loading1.png) center no-repeat;
    background-size: 100% auto ;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    /* display: none; */
}
.loadTiao{
    width: 303px;
    height: 39px;
    background: url(../img/load/loadingProgress.png) center no-repeat;
    background-size: 100% auto;
    display: block;
    margin: 220px auto 0;
    position: relative;
}
.progresscon{
    width: 0px;
    height: 19px;
    background: url(../img/load/progresscon.png);
    position: absolute;
    left: 5px;
    top: 9px;
    border-radius: 15px;
}
.progresscon span{
    text-align: center;
    width: 303px;
    display: block;
}
/* 加载界面结束 */
/* 游戏开始 */
.plays{
    width: 100%;
    height: 100%;
    background:url(../img/play/bg.png) center/cover no-repeat ;
    position: absolute;
    top: 0;
    left: 0;
    overflow: hidden;
    z-index: 999;
}
.main{
    width: 100%;
    height: 100px;
    position: relative;
    z-index: 10;
    float: left;
    transition: all 5s;
}
.main img{
    transition: all 0.2s;
}
.xuetiao{
    width: 204px;
    height: 48px;
    background: url(../img/play/life.png) center no-repeat;
    background-size: contain;
    z-index: 100;
    float: left;
    position: absolute;
    top: 19px;
    left: 194px;
}
.xuetiao1{
    width: 132px;
    height: 24px;
    background-color: rgb(224, 31, 31);
    position: absolute;
    top: 13px;
    left: 60px;
    border-radius: 7px;
}
.fenshu{
    width: 168px;
    height: 53px;
    background: url(../img/play/fenzhi.png) center no-repeat;
    background-size: contain;
    z-index: 100;
    position: absolute;
    top: 10px;
    left: 10px;
}
.fenshu span{
    font-size: 20px;
    color: #fff;
    display: block;
    margin-top: 21px;
    margin-left: 95px;
}
.close{
    position: absolute;
    top: 10px;
    right: 10px;
    width: 50px;
    height: 50px;
    z-index: 1000;
}
/* 游戏结束 */
目录
相关文章
|
19天前
|
JavaScript 前端开发 开发者
VUE 开发——Node.js学习(一)
VUE 开发——Node.js学习(一)
47 3
|
1月前
|
JavaScript
ES6学习(9)js中的new实现
ES6学习(9)js中的new实现
|
11天前
|
JavaScript
js学习--制作猜数字
js学习--制作猜数字
32 4
js学习--制作猜数字
|
10天前
|
JavaScript
webpack学习五:webpack的配置文件webpack.config.js分离,分离成开发环境配置文件和生产环境配置文件
这篇文章介绍了如何将webpack的配置文件分离成开发环境和生产环境的配置文件,以提高打包效率。
21 1
webpack学习五:webpack的配置文件webpack.config.js分离,分离成开发环境配置文件和生产环境配置文件
|
28天前
|
算法 JavaScript 前端开发
第一个算法项目 | JS实现并查集迷宫算法Demo学习
本文是关于使用JavaScript实现并查集迷宫算法的中国象棋demo的学习记录,包括项目运行方法、知识点梳理、代码赏析以及相关CSS样式表文件的介绍。
第一个算法项目 | JS实现并查集迷宫算法Demo学习
|
29天前
|
JavaScript 前端开发 API
紧跟月影大佬的步伐,一起来学习如何写好JS(上)
该文章跟随月影老师的指导,探讨了编写优质JavaScript代码的三大原则:各司其职、组件封装与过程抽象,通过具体示例讲解了如何在实际开发中应用这些原则以提高代码质量和可维护性。
紧跟月影大佬的步伐,一起来学习如何写好JS(上)
|
11天前
|
JavaScript
js学习--制作选项卡
js学习--制作选项卡
30 4
|
10天前
|
JavaScript
js学习--商品列表商品详情
js学习--商品列表商品详情
12 2
|
10天前
|
JavaScript
js学习--九宫格抽奖
js学习--九宫格抽奖
9 2
|
10天前
|
JavaScript
js学习--开屏弹窗
js学习--开屏弹窗
21 1