九宫格,拼图游戏完整源码


代码如下:(需要 引入 jquery .js和 vue.js):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        /*#piclist {
            width: 600px;
            height: 600px;
            background-color: green;
        }*/
        .nitem {
            /*width: 200px;*/
            /*height: 200px;*/
            float: left;
            background: url(imgs/fengnv.jpg) 0px 0px no-repeat;
            -webkit-background-size: 600px 600px;
            background-size: 600px 600px;
            font-size: 33px;
            color: red;
            font-weight: bold;
            cursor: pointer;
        }

        /*.nitem:nth-child(2){
            background-position: -200px 0;
        }
        .nitem:nth-child(3){
            background-position: -400px 0;
        }
        .nitem:nth-child(4){
            background-position: 0px -200px;
        }
        .nitem:nth-child(5){
            background-position: -200px -200px;
        }
        .nitem:nth-child(6){
            background-position: -400px -200px;
        }
        .nitem:nth-child(7){
            background-position: 0px -400px;
        }
        .nitem:nth-child(8){
            background-position: -200px -400px;
        }
        .nitem:nth-child(9){
            background-position: -400px -400px;
        }*/

        .fn-clear {
            clear: both;
            height: 0;
            line-height: 0px;
            font-size: 0px;
        }
    </style>
</head>
<body>
<div id="appbox" :style="{ width:width+'px',height:height+'px' }">
    <div id="piclist">
        <div class="nitem"
             v-for="(item,index) in itemlist"
             :class="{remove : index === 0}"
             :index="index "
             v-bind:style="{
                'backgroundPosition':-px(index)+'px -' + py(index) + 'px',
                 width : width / rows + 'px',
                 height : height / cols + 'px'}">{{index}}
        </div>
    </div>
</div>
</body>
<script src=js/jquery.min.js></script>
<script src=js/vue.min.js></script>
<script>
    var olen = 0, oi = 0, cindex = 0, oa = new Array(), oindex = 0, listarray = new Array();
    var vm = new Vue({
        el: "#appbox",
        data: {
            itemlist: [],
            rows: 3,
            cols: 3,
            width: 600,
            height: 600,
        },
        methods: {
            px(index){
                return (index % this.rows) * (this.width / this.rows)
            },
            py (index){
                return parseInt((index / this.cols)) * (this.height / this.cols);
            }
        }
    });
    for (var i = 0; i < vm.rows * vm.cols; i++) {
        vm.itemlist.push(i);
    }

    function getrow(index) {
        return parseInt(index / vm.cols);
    }
    function getcols(index) {
        return index % vm.rows;
    }
    function getBound(index) {
        var left = index - 1;
        var right = index + 1;
        var top = index - vm.rows;
        var bottom = index + vm.rows;
        var len = vm.itemlist.length; //总长度

        var currentRow = getrow(index);
        var currentCol = getcols(index);
        var roundArr = new Array();

        if (left >= 0 && left < len && getrow(left) == currentRow) {
            roundArr.push(left);
        }
        if (right >= 0 && right < len && getrow(right) == currentRow) {
            roundArr.push(right);
        }
        if (top >= 0 && top < len && getcols(top) == currentCol) {
            roundArr.push(top);
        }
        if (bottom >= 0 && bottom < len && getcols(bottom) == currentCol) {
            roundArr.push(bottom);
        }

        return roundArr;
    }
    function box_switch(i, j) {
        var iobj = $('#piclist .nitem').eq(i);
        var jobj = $('#piclist .nitem').eq(j);
        var tobj = iobj.clone();

        jobj.after(tobj);
        iobj.replaceWith(jobj);
    }

    vm.$nextTick(function () {
        $('.remove').css({
            background: 'none',
            backgroundColor: 'green'
        });
    });

    function box_rand(times) {
        for (var i = 0; i < times; i++) {
            oindex = $('.remove').index();
            oa = getBound(oindex);
            olen = oa.length;
            oi = Math.floor(Math.random() * olen);
            cindex = oa[oi];
            box_switch(cindex, oindex);
        }
        listarray.length = 0;
        $.each($('.nitem'), function (i, item) {
            listarray.push($(item).attr('index'));
        });
        if (listarray.join(',') == vm.itemlist.join(',')) {
            box_rand(times);
        }
    }
    $(function () {
        //打乱图片
        box_rand(20);

        $('.nitem').on('click   ', function () {
            var cindex = $(this).index();
            var oindex = $('.remove').index();
            var oRound = getBound(oindex); //空盒子四周的索引

            if ($.inArray(cindex, oRound) < 0) { //不在
                return false;
            } else {
                box_switch(oindex, cindex);

                var listArray = new Array();
                $.each($('.nitem'), function (i, item) {
                    listArray.push($(item).attr('index'));
                })
                if (listArray.join(',') == vm.itemlist.join(',')) {
                    alert('success')
                } else {
                    console.log('失败')
                }
            }
        });
    })
</script>
</html>



  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Flash拼图游戏源码是一种用于制作和开发拼图游戏的编程代码。拼图游戏是一种具有挑战性和娱乐性的游戏,目标是通过重新排列碎片或拼图将图像或图案组合在一起。Flash拼图游戏源码可以通过Adobe Flash软件进行编程和设计。 在编写Flash拼图游戏源码时,可以使用ActionScript语言编写交互逻辑和游戏功能。这种源码可以包括以下关键功能: 1. 图片加载:源码可以包含从本地计算机或互联网加载图像的功能。这样,玩家可以选择自己喜欢的图片作为拼图的来源。 2. 拼图切割:游戏源码可以将所选图片切割成多个碎片或拼图块。这些碎片将被随机散布或排列在游戏场景中,需要玩家重新排列以恢复原始图像。 3. 拖放交互:这种源码还可以包含拖放功能,使玩家能够通过将拼图块拖到适当的位置来重新排列拼图。 4. 成功验证:源码可以包含用于验证玩家是否成功完成拼图的逻辑。一旦玩家成功重新排列拼图块,游戏可以显示祝贺消息或播放动画。 5. 计时器和计分:源码还可以包括一个计时器和计分系统,以追踪玩家完成拼图所花费的时间和分数。 Flash拼图游戏源码可以根据程序员的需求和游戏设计进行自定义。通过调整代码和设计游戏界面,可以创建出具有不同难度级别和风格的拼图游戏。此外,源码还允许玩家自定义游戏的其他方面,如图像选择、难度设置和游戏提示。 总之,Flash拼图游戏源码使得开发者可以创建具有各种功能和特色的拼图游戏,为玩家提供挑战和娱乐。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ITzhongzi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值