<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
最近想業餘做一款android遊戲,發現我國一款古老好玩的智力遊戲-九格智慧拼圖挺好玩的,相信大多80後小時玩過,因此有了開發的想法。
遊戲規則:將一副圖片分割為9個子圖片,其中一個為空白圖片,隨機打亂。通過兩兩圖片的交換,合併為一張圖片,最後遊戲完成。
1、將一個圖片分割為9個子圖片,放入ArrayList中。
利用Bitmap.createBitmap()進行圖片切割, 根據引數座標的不同,可以切圖一張圖片的任意部分。
2、採用自定義view,隨機打亂的畫出9個子圖片
選生成0-9的隨機排列。然後根據排列值畫出對應的圖片
3、在自定義view中響應點選圖片是否可以移動。
遍歷左右方塊數位,如果為0,則可以移動。同時交換相連數位和陣列中的位置。
4、判斷遊戲完成的結束演演算法。
依次遍歷各個方塊,如何數位呈依次遞增排列,則遊戲結束
程式碼:
package org.diudiululu.magicSquare; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Point; import android.os.Bundle; import android.util.Log; /** * @author a1623z * */ public class MagicSquareActivity extends Activity { private static final String TAG = "MagicSquare"; public static final int SQUARE_WIDTH = 3; private int square[] = new int[SQUARE_WIDTH * SQUARE_WIDTH]; private int steps = 0; private MagicSquareView magicSquareView; public int getTitleNumber(int x, int y) { return square[y * SQUARE_WIDTH + x]; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(this.TAG, "OnCreate"); initGame(); magicSquareView = new MagicSquareView(this); this.setContentView(magicSquareView); magicSquareView.requestFocus(); } private void initGame() { generateMagicSquare(); steps = 0; } private void generateMagicSquare() { java.util.ArrayList<Integer> numArray = new java.util.ArrayList<Integer>(); for (int i = 0; i < square.length; i++) { numArray.add(new Integer(i)); } int i = 0; while (numArray.size() > 0) { int index = (int) (Math.random() * numArray.size()); Integer integer = numArray.get(index); square[i] = integer.intValue(); i++; numArray.remove(index); } } public boolean moveable(int x, int y) { if (x < 0 || x >= SQUARE_WIDTH) return false; if (y < 0 || y >= SQUARE_WIDTH) return false; for (int i = x - 1; i <= x + 1; i++) { for (int j = y - 1; j <= y + 1; j++) { if (i == x && j == y) // it's myself, skip continue; if (i != x && j != y) continue; if (i < 0 || i >= SQUARE_WIDTH) continue; if (j < 0 || j >= SQUARE_WIDTH) continue; if (square[j * SQUARE_WIDTH + i] == 0) return true; } } return false; } public Point move(int x, int y) { Log.d(TAG, "move" + ",x=" + x + ",y=" + y); if (!moveable(x, y)) return new Point(-1, -1); steps++; for (int i = x - 1; i <= x + 1; i++) { for (int j = y - 1; j <= y + 1; j++) { if (i == x && j == y) // it's myself, skip continue; if (i != x && j != y) continue; if (i < 0 || i >= SQUARE_WIDTH) continue; if (j < 0 || j >= SQUARE_WIDTH) continue; if (square[j * SQUARE_WIDTH + i] == 0) { int temp = square[j * SQUARE_WIDTH + i]; square[j * SQUARE_WIDTH + i] = square[y * SQUARE_WIDTH + x]; square[y * SQUARE_WIDTH + x] = temp; return new Point(i, j); } } } return new Point(-1, -1); } public boolean win() { for (int i = 0; i < square.length - 1; i++) { if (square[i] != i + 1) return false; } return true; } public class Pic{ private int id; private Bitmap subbitmap; } }
MagicSquarView.java
/** * */ package org.diudiululu.magicSquare; import java.util.ArrayList; import org.diudiululu.magicSquare.R; import android.app.Dialog; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.*; import android.graphics.*; import android.graphics.Paint.FontMetrics; import android.graphics.Paint.Style; import android.util.*; /** * @author a1623z * */ public class MagicSquareView extends View { private static final String TAG = "MagicSquare"; private final MagicSquareActivity magicSquareActivity; private float width; private float height; private int selX; private int selY; private final Rect selRect = new Rect(); private final int pingtuheight = 200; private ArrayList<Pic> pieces; /** * @param context */ public MagicSquareView(Context context) { super(context); this.magicSquareActivity = (MagicSquareActivity) context; this.setFocusable(true); this.setFocusableInTouchMode(true); // TODO Auto-generated constructor stub } // 切割圖片,放入ArrayList中 { pieces = new ArrayList<MagicSquareView.Pic>(); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pingtu); int bitmapwidth = bitmap.getWidth(); int bitmapheight = bitmap.getHeight(); int pieceWidth = bitmapwidth / 3; int pieceHeight = bitmapheight / 3; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { Pic piece = new Pic(); piece.index = j + i * 3; int xValue = j * pieceWidth; int yValue = i * pieceHeight; piece.piece = Bitmap.createBitmap(bitmap, xValue, yValue, pieceWidth, pieceHeight); pieces.add(piece); } } } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { width = w / 3f; height = (h - pingtuheight) / 3f; getRect(selX, selY, selRect); Log.d(TAG, "onSizeChanged: width=" + width + ", height=" + height + ",selX=" + selX + ",selY=" + selY); super.onSizeChanged(w, h, oldw, oldh); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_DPAD_UP: select(selX, selY - 1); break; case KeyEvent.KEYCODE_DPAD_DOWN: select(selX, selY + 1); break; case KeyEvent.KEYCODE_DPAD_LEFT: select(selX - 1, selY); break; case KeyEvent.KEYCODE_DPAD_RIGHT: select(selX + 1, selY); break; case KeyEvent.KEYCODE_ENTER: case KeyEvent.KEYCODE_DPAD_CENTER: Point point = magicSquareActivity.move(selX, selY); if (point.x >= 0 && point.y >= 0) { this.invalidate(selRect); Rect targetRect = new Rect(); this.getRect(point.x, point.y, targetRect); this.invalidate(targetRect); } break; default: return super.onKeyDown(keyCode, event); } return true; } @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() != MotionEvent.ACTION_DOWN) return super.onTouchEvent(event); if (event.getY() <= pingtuheight) return false; Log.i(TAG, "event.getX()=" + event.getX() + ",event.getY=" + event.getY()); select((int) (event.getX() / width), (int) ((event.getY() - pingtuheight) / height)); Point point = magicSquareActivity.move(selX, selY); if (point.x >= 0 && point.y >= 0) { this.invalidate(selRect); Rect targetRect = new Rect(); this.getRect(point.x, point.y, targetRect); this.invalidate(targetRect); } return true; } @Override protected void onDraw(Canvas canvas) { Paint backround = new Paint(); backround.setColor(getResources().getColor(R.color.ms_backgroud)); canvas.drawRect(0, 0, this.getWidth(), this.getHeight(), backround); // 畫出原圖及圖片的介紹 Rect dst = new Rect();// 螢幕 >>目標矩形 Bitmap pic = BitmapFactory.decodeResource(getResources(), R.drawable.pingtu); dst.left = 0; dst.top = 0; dst.right = (int) (width * 3) / 2; dst.bottom = pingtuheight; canvas.drawBitmap(pic, null, dst, null); // 繪製出圖片的介紹 Paint textpaint = new Paint(); textpaint.setTextSize(25); canvas.drawText("一副美麗的圖片,", dst.right, 30, textpaint); canvas.drawText("但已支離破碎......", dst.right, 70, textpaint); // draw the board Paint dark = new Paint(); dark.setColor(getResources().getColor(R.color.ms_dark)); Paint hilite = new Paint(); hilite.setColor(getResources().getColor(R.color.ms_hilite)); Paint light = new Paint(); light.setColor(getResources().getColor(R.color.ms_light)); // draw the minor grid lines for (int i = 0; i < 3; i++) { canvas.drawLine(0, i * height + pingtuheight, getWidth(), i * height + pingtuheight, light); canvas.drawLine(0, i * height + 1 + pingtuheight, getWidth(), i * height + pingtuheight + 1, hilite); canvas.drawLine(i * width, pingtuheight, i * width, getHeight() + pingtuheight, light); canvas.drawLine(i * width + 1, pingtuheight, i * width + 1, getHeight() + pingtuheight, hilite); } Rect picrect = new Rect(); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { int n = this.magicSquareActivity.getTitleNumber(i, j);//根據座標依次取出0,1,2,3,4,5,6,7,8下標陣列對應的值 if (n == 0) continue; picrect.left = (int) (i * width); picrect.top = pingtuheight + (int) (j * height); picrect.right = (int) (i * width + width); picrect.bottom = (int) (pingtuheight + j * height + height); canvas.drawBitmap(pieces.get(n).getPiece(), null, picrect, null); } } Paint selected = new Paint(); selected.setColor(getResources().getColor(R.color.ms_selected)); canvas.drawRect(selRect, selected); if (magicSquareActivity.win()) { Dialog winDlg = new Win(magicSquareActivity); winDlg.show(); magicSquareActivity.finish(); } } private void getRect(int x, int y, Rect rect) { Log.i(TAG, "getRect" + x + "y" + y); rect.set((int) (x * width), (int) (y * height + pingtuheight), (int) (x * width + width), (int) (y * height + height + pingtuheight)); } private void select(int x, int y) { invalidate(selRect); selX = Math.min(Math.max(x, 0), 2); selY = Math.min(Math.max(y, 0), 2); getRect(selX, selY, selRect); invalidate(selRect); } public class Pic { public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } public Bitmap getPiece() { return piece; } public void setPiece(Bitmap piece) { this.piece = piece; } int index; Bitmap piece; } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45