【原生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;
}
/* 游戏结束 */
目录
相关文章
|
2月前
|
JavaScript
JS实现简单的打地鼠小游戏源码
这是一款基于JS实现简单的打地鼠小游戏源码。画面中的九宫格中随机出现一个地鼠,玩家移动并点击鼠标控制画面中的锤子打地鼠。打中地鼠会出现卡通爆破效果。同时左上角统计打地鼠获得的分数
154 1
|
2月前
|
JavaScript 前端开发
js+jquery实现贪吃蛇经典小游戏
本项目采用HTML、CSS、JavaScript和jQuery技术,无需游戏框架支持。通过下载项目文件至本地,双击index.html即可启动贪吃蛇游戏。游戏界面简洁,支持方向键控制蛇移动,空格键实现游戏暂停与恢复功能。
74 14
|
2月前
|
JavaScript
原生JS实现斗地主小游戏
这是一个原生的JS网页版斗地主小游戏,代码注释全。带有斗地主游戏基本的地主、选牌、提示、出牌、倒计时等功能。简单好玩,欢迎下载
54 7
|
2月前
|
Web App开发 JavaScript 前端开发
如何学习JavaScript?
如何学习JavaScript?
57 5
|
2月前
|
JavaScript 前端开发 索引
JavaScript学习第二章--字符串
本文介绍了JavaScript中的字符串处理,包括普通字符串和模板字符串的使用方法及常见字符串操作方法如`charAt`、`concat`、`endsWith`等,适合前端学习者参考。作者是一位热爱前端技术的大一学生,专注于分享实用的编程技巧。
35 2
|
2月前
|
存储 JavaScript 前端开发
JavaScript学习第一章
本文档介绍了JavaScript的基础知识,包括其在网页中的作用、如何通过JavaScript动态设置HTML元素的CSS属性,以及JavaScript中的变量类型(`var`、`let`、`const`)和数据类型(基本数据类型与引用数据类型)。通过实例代码详细解释了JavaScript的核心概念,适合初学者入门学习。
59 1
|
2月前
|
JavaScript
JS趣味打字金鱼小游戏特效源码
hi fish是一款打字趣味小游戏,捞出海里的鱼,捞的越多越好。这款游戏用于电脑初学者练习打字。初学者可以根据自己的水平设置游戏难度。本段代码可以在各个网页使用,有需要的朋友可以直接下载使用,本段代码兼容目前最新的各类主流浏览器,是一款非常优秀的特效源码!
38 3
|
2月前
|
移动开发 HTML5
html5+three.js公路开车小游戏源码
html5公路开车小游戏是一款html5基于three.js制作的汽车开车小游戏源代码,在公路上开车网页小游戏源代码。
73 0
html5+three.js公路开车小游戏源码
|
2月前
|
JavaScript
JS趣味打字金鱼小游戏特效源码
hi fish是一款打字趣味小游戏,捞出海里的鱼,捞的越多越好。这款游戏用于电脑初学者练习打字。初学者可以根据自己的水平设置游戏难度。本段代码可以在各个网页使用,有需要的朋友可以直接下载使用,本段代码兼容目前最新的各类主流浏览器,是一款非常优秀的特效源码!
43 0
JS趣味打字金鱼小游戏特效源码
|
3月前
|
JavaScript
js学习--商品列表商品详情
js学习--商品列表商品详情
36 2