Flutter 拼圖小游戲

效果圖

1597904550873.gif

主要實現功能

  • 選取系統相冊圖片
  • 切割圖片
  • 打亂圖片
  • 移動圖片

選取系統相冊

用的是flutter的image_picker的圖片選擇器.自己集成一下就可以。不再多說

image_picker: 0.6.7+4

切割圖片

新建一個類繼承CustomPainter, 去進行圖片操作,

import 'package:flutter/material.dart';
import 'dart:ui' as ui;
/// 圖片裁剪
class ImageClipper extends CustomPainter {
  final ui.Image image;
  final double left;
  final double top;
  final double right;
  final double bottom;
  final int currentIndex;

  ImageClipper(this.image, this.left, this.top, this.right, this.bottom,
      this.currentIndex);
  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint();
    canvas.drawImageRect(
        image,
        Rect.fromLTRB(image.width * left, image.height * top,
            image.width * right, image.height * bottom),
        Rect.fromLTWH(0, 0, size.width, size.height),
        paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return false;
  }
}

然后顯示出來

Container(
      color: Colors.green,
      child: GestureDetector(
        child: CustomPaint(
          painter: clipper,
          size: Size(80, 80),
        ),
        onTap: () {
          tapList.add(clipper.currentIndex);
          showAnimation(clipper.currentIndex);
        },
      ),
    );

上面代碼添加了,點擊方法。用來進行模塊移動, 其中clipper就是切割好的圖片,返回來的對象,
比較重要的一點是選擇的需要對選擇好了的本地圖片處理一下

Future<ui.Image> _loadImge() async {
    ImageStream imageStream = FileImage(imgPath).resolve(ImageConfiguration());
    Completer<ui.Image> completer = Completer<ui.Image>();
    void imageListener(ImageInfo info, bool synchronousCall) {
      ui.Image image = info.image;
      completer.complete(image);
      imageStream.removeListener(ImageStreamListener(imageListener));
    }
    imageStream.addListener(ImageStreamListener(imageListener));
    return completer.future;
  }

  clip(left, top, right, bottom, currentIndex) async {
    ui.Image uiImage;
    _loadImge().then((image) {
      uiImage = image;
    }).whenComplete(() {
      clipperList
          .add(ImageClipper(uiImage, left, top, right, bottom, currentIndex));
      setState(() {});
    });
  }

打亂圖片

這個可以用shuffle()函數去處理,但是打亂之后的數組,不一定能夠通過移動完成拼圖
所以采用了一個相當于比較穩的方法,這里提供一下思路。循環次數去進行點擊移動操作,記錄點擊的圖片模塊,進行移動,這樣自動還原的時候把記錄的數組進行倒敘之后,在進行移動就可以還原了。。完美
感興趣的同學可以用A*搜索算法。

移動圖片

更換數組位置即可。這個比較簡單了

int emptyIndex = 0;
    int currentIndex = 0;
    ImageClipper emptyClipper;
    for (int i = 0; i < clipperList.length; i++) {
      if (clipperList[i].currentIndex == 8) {
        emptyIndex = i;
        emptyClipper = clipperList[i];
      }
      if (clipperList[i].currentIndex == current) {
        currentIndex = i;
      }
    }
  // 判斷是否可以移動
    if (isCanMove(emptyIndex, currentIndex)) {
      // 不移動
      clipperList[emptyIndex] = clipperList[currentIndex];
      clipperList[currentIndex] = emptyClipper;
      if (mounted) {
        setState(() {});
      // 判斷是否完成
        isSuccess();
      }
    }

針對9宮格來說。沒有寫通用的。??

 bool isCanMove(empty, current) {
    bool isCanMove = false;
    switch (empty) {
      case 0:
        {
          if (current == 1 || current == 3) isCanMove = true;
          break;
        }
      case 1:
        {
          if (current == 0 || current == 2 || current == 4) isCanMove = true;
          break;
        }
      case 2:
        {
          if (current == 1 || current == 5) isCanMove = true;
          break;
        }
      case 3:
        {
          if (current == 0 || current == 4 || current == 6) isCanMove = true;
          break;
        }
      case 4:
        {
          if (current == 1 || current == 3 || current == 5 || current == 7)
            isCanMove = true;
          break;
        }
      case 5:
        {
          if (current == 2 || current == 4 || current == 8) isCanMove = true;
          break;
        }
      case 6:
        {
          if (current == 3 || current == 7) isCanMove = true;
          break;
        }
      case 7:
        {
          if (current == 4 || current == 6 || current == 8) isCanMove = true;
          break;
        }
      case 8:
        {
          if (current == 5 || current == 7) isCanMove = true;
          break;
        }
      default:
    }
    return isCanMove;
  }
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,923評論 6 535
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,740評論 3 420
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 176,856評論 0 380
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,175評論 1 315
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,931評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,321評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,383評論 3 443
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,533評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,082評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,891評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,067評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,618評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,319評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,732評論 0 27
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,987評論 1 289
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,794評論 3 394
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,076評論 2 375