本文实例为大家分享了Android实现拼图游戏的具体代码,供大家参考,具体内容如下
本人是用 android studio 完成的
源码
package packageName;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import MyImg;
public class MainActivity extends AppCompatActivity {
// 显示图片的宽度
public static final int W = 250;
// 左上边距
public static final int MARGIN = 200;
// 空图片的索引
public static final int NULLINDEX = 0;
private MyImg[] imgs = new MyImg[9];
// 存储图片位置的地图
private int[] map = new int[9];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 主布局没设置啥东西
setContentView(R.layout.activity_main);
// 用于设置生成 view 对象的宽高
ViewGroup.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
initImg(params);
randomMap();
addImg(params);
// 开始新游戏
Button newBtn = new Button(this);
newBtn.setText("新游戏");
newBtn.setTextSize(16);
newBtn.setX(40);
// 添加控件要用的
ViewGroup.LayoutParams p1= new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
addContentView(newBtn, p1);
newBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
newGame();
}
});
}
// 添加图片到布局中并设置点击事件
private void addImg(ViewGroup.LayoutParams params) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int index = i * 3 + j;
// 计算x, y坐标
int x = j * W + MARGIN;
int y = i * W + MARGIN;
ImageView imgView = imgs[map[index]].getImg();
imgView.setX(x);
imgView.setY(y);
addContentView(imgView, params);
imgView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageView tempImg = imgs[NULLINDEX].getImg();
int x = (int) v.getX();
int y = (int) v.getY();
// goal image
int x1 = (int) tempImg.getX();
int y1 = (int) tempImg.getY();
// move top
if (y - y1 == W && x == x1) {
top((ImageView) v);
} else if (y - y1 == -W && x == x1) {
down((ImageView) v);
} else if (x - x1 == W && y == y1) {
left((ImageView) v);
} else if (x - x1 == -W && y == y1) {
right((ImageView) v);
}
if (isWin()) {
Toast.makeText(MainActivity.this, "You Win!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
}
private void newGame() {
randomMap();
// 设置图片的 x, y坐标
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int index = i * 3 + j;
int x = j * W + MARGIN;
int y = i * W + MARGIN;
ImageView imgView = imgs[map[index]].getImg();
imgView.setX(x);
imgView.setY(y);
}
}
}
private void left(ImageView img) {
img.setX(img.getX() - W);
imgs[NULLINDEX].getImg().setX(img.getX() + W);
}
private void right(ImageView img) {
img.setX(img.getX() + W);
imgs[NULLINDEX].getImg().setX(img.getX() - W);
}
private void top(ImageView img) {
img.setY(img.getY() - W);
imgs[NULLINDEX].getImg().setY(img.getY() + W);
}
private void down(ImageView img) {
img.setY(img.getY() + W);
imgs[NULLINDEX].getImg().setY(img.getY() - W);
}
private boolean isWin() {
// 根据 x, y的坐标算出图片的位置,假如一一对应的话,那么久是赢了
for (int i = 0; i < 9; i++) {
ImageView img = imgs[i].getImg();
int x = (int) img.getX();
int y = (int) img.getY();
int index = (y - MARGIN) / W * 3 + (x - MARGIN) / W;
// 有一个没对上,就是没赢
if (index != imgs[i].getType()) {
return false;
}
}
return true;
}
private void randomMap() {
// 打乱地图的位置
int a, b;
for (int i = 0; i < 50; i++) {
a = (int) (Math.random() * 9);
b = (int) (Math.random() * 9);
int t = map[a];
map[a] = map[b];
map[b] = t;
}
}
// 安排图片数组
private void initImg(ViewGroup.LayoutParams params) {
int[] imgId = {R.drawable.img10, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5,
R.drawable.img6, R.drawable.img7, R.drawable.img8, R.drawable.img9};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int index = i * 3 + j;
imgs[index] = new MyImg(index);
MyImg img = imgs[index];
ImageView image = new ImageView(this);
params.width = W;
params.height = W;
image.setLayoutParams(params);
image.setImageResource(imgId[index]);
img.setImg(image);
// 让地图初始化
map[index] = index;
}
}
}
}
MyImg类
package packageName;
import android.widget.ImageView;
public class MyImg {
// 用于存储图片位置的索引
private int type;
private ImageView img;
public MyImg(int type) {
this.type = type;
}
public void setImg(ImageView img) {
this.img = img;
}
public ImageView getImg() {
return img;
}
// 获取图片索引
public int getType() {
return type;
}
}