华容道小游戏

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
            text-decoration: none;
        }

        h1 {
            text-align: center;
        }

        h2 {
            text-align: center;
            position: absolute;
            left: 70px;
            top: 100px;
            cursor: pointer;
        }

        .container {
            width: 300px;
            height: 320px;
            border: 2px solid gold;
            margin: 10px auto;
            background-color: cadetblue;
        }

        .time {
            margin: 5px 100px;
        }

        .img {
            background-image: url(img/1.jpg);
            width: 240px;
            height: 240px;
        }

        .suc {
            background-image: url(img/2.jpg);
            width: 240px;
            height: 240px;
        }

        .box {
            position: relative;
            width: 240px;
            height: 240px;
            margin: 5px auto;
        }


        .box ul.disappear {
            display: none;
        }

        .box li {
            float: left;
            width: 60px;
            height: 60px;
            border: 1px solid silver;
            box-sizing: border-box;
            font-size: 35px;
            text-align: center;
            cursor: pointer;
            color: #fff;
        }

        .button {
            margin: 15px 90px;
        }

        .button input {
            background-color: burlywood;
            color: #3c3c3c;
            width: 35px;
            height: 25px;
            cursor: pointer;
        }
    </style>
</head>

<body>
    <h1>数字华容道</h1>
    <div class="container">
        <div class="time">
            时间:<span>00:00</span>
        </div>
        <div class="content">
            <div class="box img">
                <ul class="disappear">
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                </ul>
            </div>
        </div>
        <div class="button">
            <input type="button" value="开始">
            <input type="button" value="重置">
            <input type="button" value="退出">
        </div>
    </div>
    <script>
        var div = document.getElementsByClassName('box')[0];
        var time = document.getElementsByTagName('span')[0];
        var start = document.getElementsByTagName('input')[0];
        var init = document.getElementsByTagName('input')[1];
        var over = document.getElementsByTagName('input')[2];
        var ul = document.getElementsByTagName('ul')[0];
        var li = document.getElementsByTagName('li');
        var h = document.getElementsByTagName('h2');
        var timer;
        var minute = 0;
        var second = 0;
        var startZt = true;
        var initZt = true;
        var space = 0;
        //格式化时间
        function fromat(v) {
            return v < 10 ? '0' + v : v;
        }
        //计时函数
        function getTime() {
            timer = setInterval(function () {
                second++;
                if (second == 60) {
                    minute++;
                    second = 0;
                }
                time.innerHTML = fromat(minute) + ":" + fromat(second);
            }, 1000)
        }
        //点击开始按钮,开始计时,并调用centent方法产生数字
        start.onclick = function () {
            if (startZt !== true) {
                return;
            }
            getTime();
            startZt = false;
            ul.classList.remove('disappear');
            main();
            initZt = true;
            div.classList.remove('img');
            div.classList.remove('suc');
        }
        //判断能否点击
        function pd(i, space) {
            if (i != space) {
                if (i <= 3) {
                    if (i % 4 == 0) {
                        if (space == (i + 1) || space == (i + 4)) {
                            return true;
                        }
                    }
                    if (i % 4 == 1 || i % 4 == 2) {
                        if (space == (i - 1) || space == (i + 1) || space == (i + 4)) {
                            return true;
                        }
                    }
                    if (i % 4 == 3) {
                        if (space == (i - 1) || space == (i + 4)) {
                            return true;
                        }
                    }
                }
                if (i >= 4 && i <= 11) {
                    if (i % 4 == 0) {
                        if (space == (i - 4) || space == (i + 4) || space == (i + 1)) {
                            return true;
                        }
                    }
                    if (i % 4 == 1 || i % 4 == 2) {
                        if (space == (i - 1) || space == (i + 1) || space == (i - 4) || space == (i + 4)) {
                            return true;
                        }
                    }
                    if (i % 4 == 3) {
                        if (space == (i - 1) || space == (i - 4) || space == (i + 4)) {
                            return true;
                        }
                    }
                }
                if (i >= 12) {
                    if (i % 4 == 0) {
                        if (space == (i - 4) || space == (i + 1)) {
                            return true;
                        }
                    }
                    if (i % 4 == 1 || i % 4 == 2) {
                        if (space == (i - 1) || space == (i + 1) || space == (i - 4)) {
                            return true;
                        }
                    }
                    if (i % 4 == 3) {
                        if (space == (i - 1) || space == (i - 4)) {
                            return true;
                        }
                    }
                }
            }

        }
        //点击重置按钮,数字重置,时间重置
        init.onclick = function () {
            if (initZt == false) {
                return;
            }
            main();
            second = 0;
            minute = 0;
            time.innerHTML = "00:00";
            clearInterval(timer);
            getTime();
            startZt = false;
        }
        //点击退出按钮,回到主页面
        over.onclick = function () {
            for (var i = 0; i < li.length; i++) {
                li[i].innerHTML = "";
            }
            second = 0;
            minute = 0;
            time.innerHTML = "00:00";
            clearInterval(timer);
            ul.classList.add('disappear');
            startZt = true;
            initZt = false;
            div.classList.add('img');
            div.classList.remove('suc');
        }
        //判断游戏是否胜利
        function gameOver() {
            var total = 0;
            for (var i = 0; i < li.length - 1; i++) {
                if (li[i].value == (li[i].innerHTML - 1)) {
                    total++;
                }
            }
            if (total == 15) {
                ul.classList.add('disappear');
                div.classList.add('suc');
                time.innerHTML = "00:00";
                clearInterval(timer);
                startZt = true;
                initZt = false;
            }
        }
        function main() {
            //随机产生1-8的数字
            var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
            arr.sort(function () {
                return Math.random() - 0.5;
            })
            for (var i = 0; i < li.length; i++) {
                li[i].innerHTML = arr[i];
                li[i].value = i;
            }
            space = 15;
            li[15].innerHTML = "";
            li[15].value = 15;
            //绑定事件 进行数字的移动
            ul.onclick = function (e) {
                var target = e.target;
                var i = target.value;
                if (pd(i, space)) {
                    var a = li[i].innerHTML;
                    li[space].innerHTML = a;
                    li[i].innerHTML = "";
                    space = i;
                }
                gameOver();
            }
        }
    </script>
</body>

</html>
  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package 华容道; import java.awt.*; import java.awt.event.*; //主函数 public class Main { public static void main(String[] args) { new Hua_Rong_Road(); } } //人物按钮颜色 class Person extends Button implements FocusListener{ int number; Color c=new Color(255,245,170); Person(int number,String s) { super(s); setBackground(c);//人物的颜色背景是黄色 this.number=number; c=getBackground(); addFocusListener(this);//好像是焦点监听器 } public void focusGained(FocusEvent e) { setBackground(Color.red);//只要单击该按钮则按钮变颜色 } public void focusLost(FocusEvent e) { setBackground(c);//上一个按钮回复原先的颜色 } } //华容道总类 class Hua_Rong_Road extends Frame implements MouseListener,KeyListener,ActionListener{ Person person[] = new Person[10]; Button left,right,above,below; Button restart = new Button("Start");//重新开始按钮 public Hua_Rong_Road() { init(); setBounds(100,100,320,360); setVisible(true);//设置Frame为可见,默认为不可见 validate(); addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); } public void init() { setLayout(null); add(restart); restart.setBounds(100, 320, 120, 25); restart.addActionListener(this); String name[]={"我","陆逊","姜维","陈宫","许攸","邓艾","周瑜","庞统","诸葛亮","贾诩"}; for(int k=0;k<name.length;k++) { person[k]=new Person(k,name[k]); person[k].addMouseListener(this); person[k].addKeyListener(this); add(person[k]); }//为所有的按钮注册所需的东西 person[0].setBounds(104, 54, 100, 100); person[1].setBounds(104,154, 100, 50); person[2].setBounds(54, 154, 50, 100); person[3].setBounds(204, 154, 50, 100);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值