首页 > 其他分享 >2048简单游戏网页版

2048简单游戏网页版

时间:2023-03-23 23:55:06浏览次数:26  
标签:网页 游戏 list 2048 background push const preV

失业在家,做几个小游戏看看

玩法:按键盘的上下左右操作

 

 

 

2048.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>2048小游戏</title>
</head>
<style>
    html,body{
        width: 100%;
        height: 100%;
        overflow: hidden;
        color: #776e65;
        font-family: "Clear Sans", "Helvetica Neue", Arial, sans-serif;
    }
    #game{
        display: flex;
        flex-wrap: wrap;
        width: 400px;
        background: #bbada0;
        padding: 10px;
        border-radius:10px;
        justify-content: space-between;
    }
    .item{
        background: rgba(238, 228, 218, 0.35);
        width: 90px;
        height: 90px;
        display: block;
        text-align: center;
        line-height: 90px;
        font-size: 35px;
        margin-bottom: 10px;
    }
    .item.active{
        background: #eee4da;
    }
</style>
<body>
<div id="game">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
</div>

</body>
<script>

    const rect=new Array(16).fill(0);

    function getRandomNum(){
        return Math.random()>0.7?4:2
    }
    function getRandomIndex(){
        const emptyArr=[]
        for(let i=0;i<16;i++){
            if(!rect[i]){
                emptyArr.push(i)
            }
        }
        const n1=0|Math.random()*emptyArr.length
        const n2=getRandomNum()
        rect[emptyArr[n1]]=n2
        console.log(emptyArr[n1],n2)
    }
    const childNodes=document.querySelectorAll("#game .item")
    function render(){
        for(let i=0;i<16;i++){
            const num=rect[i]
            if(num){
                childNodes[i].textContent=num
                childNodes[i].classList.add('active')
            }else{
                childNodes[i].textContent=''
                childNodes[i].classList.remove('active')
            }
        }
    }
    getRandomIndex()
    getRandomIndex()
    render()
    document.addEventListener('keydown',function (event){
        if(event.keyCode===38){
            up()
        }
        if(event.keyCode===40){
            down()
        }
        if(event.keyCode===37){
            left()
        }
        if(event.keyCode===39){
            right()
        }
    })
    function up(){
        let has=false
        //移动
        for(let x=0;x<4;x++){
            const prelist=[]
            const list=[]
            let preV=0
            for(let y=0;y<4;y++){
                const v=rect[y*4+x]
                prelist.push(v)
                if(v){
                    if(preV===v){
                        list[list.length-1]=2*v
                        preV=0
                    }else{
                        list.push(v)
                        preV=v
                    }
                }
            }
            for(let y=0;y<4;y++){
                if(!list[y]){
                    list[y]=0
                }
                rect[y*4+x]=list[y]
            }
            console.log(prelist,list)
            if(prelist.join(',')!==list.join(',')){
                has=true
            }
        }
        if(has){
            getRandomIndex()
            render()
        }

    }
    function down(){
        let has=false
        for(let x=0;x<4;x++){
            const prelist=[]
            const list=[]
            let preV=0
            for(let y=3;y>=0;y--){
                const v=rect[y*4+x]
                prelist.push(v)
                if(v){
                    if(preV===v){
                        list[list.length-1]=2*v
                        preV=0
                    }else{
                        list.push(v)
                        preV=v
                    }
                }
            }
            for(let i=0;i<4;i++){
                if(!list[i]){
                    list[i]=0
                }
                rect[(3-i)*4+x]=list[i]
            }
            if(prelist.join(',')!==list.join(',')){
                has=true
            }
        }
        if(has){
            getRandomIndex()
            render()
        }
    }
    function left(){
        let has=false
        for(let y=0;y<4;y++){
            const prelist=[]
            const list=[]
            let preV=0
            for(let x=0;x<4;x++){
                const v=rect[y*4+x]
                prelist.push(v)
                if(v){
                    if(preV===v){
                        list[list.length-1]=2*v
                        preV=0
                    }else{
                        list.push(v)
                        preV=v
                    }
                }
            }
            for(let i=0;i<4;i++){
                if(!list[i]){
                    list[i]=0
                }
                rect[y*4+i]=list[i]
            }
            if(prelist.join(',')!==list.join(',')){
                has=true
            }
        }
        if(has){
            getRandomIndex()
            render()
        }
    }
    function right(){
        let has=false
        for(let y=0;y<4;y++){
            const prelist=[]
            const list=[]
            let preV=0
            for(let x=3;x>=0;x--){
                const v=rect[y*4+x]
                prelist.push(v)
                if(v){
                    if(preV===v){
                        list[list.length-1]=2*v
                        preV=0
                    }else{
                        list.push(v)
                        preV=v
                    }
                }
            }
            for(let i=0;i<4;i++){
                if(!list[i]){
                    list[i]=0
                }
                rect[y*4+3-i]=list[i]
            }
            if(prelist.join(',')!==list.join(',')){
                has=true
            }
        }
        if(has){
            getRandomIndex()
            render()
        }
    }
</script>
</html>
View Code

 

标签:网页,游戏,list,2048,background,push,const,preV
From: https://www.cnblogs.com/caoke/p/17249960.html

相关文章

  • 使用chrome ABC JS-CSS Injector插件,劫持网页js文件改写调试
    通过保存网站JS文件,然后阻止源本该访问的JS文件,通过ABCJS-CSSInjector讲需要访问的JS转到本地的JS,进行劫持,就可以进行修改调试1.需要插件:ABCJS-CSSInjector  ......
  • P8255 [NOI Online 2022 入门组] 数学游戏
    题目链接一道比较简单的数学题,但我仍然没做出来。首先,若\(x\nmidz\)则无解。设\(d=\gcd(x,y)\),则\(x=da,y=db\),\(z=x\cdoty\cdot\gcd(x,y)=d^3\cdotab\),其中\(......
  • Quicker快速开发,简单的网页数据爬取(示例,获取天眼查指定公司基础工商数据)
    前言有某个线上项目,没有接入工商接口,每次录入公司的时候,都要去天眼查、企查查或者其他公开数据平台,然后手动录入,一两个还好说,数量多了的重复操作就很烦,而且,部分数据是包含......
  • 网页跳转
    我们经常会看到底部导航栏,比如首页、发布、购物车三个功能,当我们点击的时候就可以跳转过去,在这里是包含一个网址链接的,今天就在第二大学远程实习中学到了,写一个学习笔记。......
  • chatgpt写程序-python小游戏-2048-pygame
    闲的没事,用chatpgt弄了个小游戏,2048,利用pygame实现,无额外贴图。只需要告诉他写个python游戏2048,只用pygame实现,不要额外贴图。然后在他暂停后说请继续,最后会有一些bug,把报......
  • 核心网页指标 WebVitals 优化遇到的问题
    1.FCP时间太久。首屏不应该包含动态内容,内容在动,可能会被认作没有完成FCP的渲染。2.关于CLS。CSS不应该包含在页面中间,如果页面中间有CSS,建议移动到页面头部Head里面。3.......
  • 10 款开源的在线游戏,点开就能玩的那种
    我早前写过一篇介绍GitHub上开源游戏的文章:《误入GitHub游戏区,结果意外地收获颇丰》,文中介绍了5款有趣、好玩的开源游戏,虽然当时那篇文章收获了广大读者的肯定,但也......
  • 【场景设计】游戏场景设计
     一.游戏场景设计概述1.1游戏场景设计师的工作职责    1.2游戏场景美术设计的类型     1.3游戏场景类型            ......
  • 服务器部署简单的个人网页
    这是一个最最最最简单的下载nginxyuminstallnginx80端口需要开启(服务器开启80端口且不能被其它占用)netstat-nplt启动nginx重启nginxservicenginxrestart......
  • 使用unity构建射击小游戏
    使用unity构建射击小游戏成果图参考例程www.manning.com/hocking问题汇总1、renderer.material方法过时换成使用this.GetComponent()参考网址:https......