使用最新版本Android Studio生成一个简单的拼图游戏
时间: 2023-12-20 15:50:37 浏览: 183
基于Android的拼图游戏APP源码,Android拼图游戏APP源码,Android小游戏源码
5星 · 资源好评率100%
好的,下面是一份简单的拼图游戏的代码,供您参考:
1. 首先,在Android Studio中创建一个新项目,并在XML布局文件中添加一个GridView,用于显示拼图。
2. 创建一个名为PuzzleAdapter的自定义适配器类来管理GridView中的拼图:
```java
public class PuzzleAdapter extends BaseAdapter {
private Context mContext;
private int[] mThumbIds;
public PuzzleAdapter(Context c, int[] thumbIds) {
mContext = c;
mThumbIds = thumbIds;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// 创建一个新的ImageView来显示GridView中的一个图片
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// 如果convertView没被使用,创建一个新的ImageView
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
}
```
3. 创建一个名为PuzzleActivity的活动类,用于管理游戏逻辑和UI:
```java
public class PuzzleActivity extends Activity {
private Integer[] mThumbIds = {
R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.image4,
R.drawable.image5, R.drawable.image6,
R.drawable.image7, R.drawable.image8,
R.drawable.image9
};
private int mColumnWidth, mColumnHeight;
private GridView mGridView;
private ArrayList<Bitmap> mBitmaps = new ArrayList<>();
private Bitmap mLastBitmap;
private int mBlankPosition;
private int mLevel = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_puzzle);
init();
shuffle();
}
private void init() {
mGridView = findViewById(R.id.gridview);
mGridView.setNumColumns(mLevel);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
mColumnWidth = width / mLevel;
mColumnHeight = height / mLevel;
for (int i = 0; i < mThumbIds.length; i++) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), mThumbIds[i]);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, mColumnWidth, mColumnHeight, true);
mBitmaps.add(scaledBitmap);
}
mLastBitmap = mBitmaps.get(mThumbIds.length - 1);
mBitmaps.remove(mThumbIds.length - 1);
mBlankPosition = mThumbIds.length - 1;
mGridView.setAdapter(new PuzzleAdapter(this, mThumbIds));
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int x = position % mLevel;
int y = position / mLevel;
int blankX = mBlankPosition % mLevel;
int blankY = mBlankPosition / mLevel;
if ((x == blankX && Math.abs(y - blankY) == 1) || (y == blankY && Math.abs(x - blankX) == 1)) {
swap(position, mBlankPosition);
mBlankPosition = position;
}
checkFinish();
}
});
}
private void shuffle() {
Random random = new Random();
for (int i = 0; i < mThumbIds.length * 2; i++) {
int position = random.nextInt(mThumbIds.length - 1);
swap(position, mBlankPosition);
mBlankPosition = position;
}
}
private void swap(int position1, int position2) {
Bitmap bitmap1 = mBitmaps.get(position1);
Bitmap bitmap2 = mBitmaps.get(position2);
mBitmaps.set(position1, bitmap2);
mBitmaps.set(position2, bitmap1);
mThumbIds[position1] = mThumbIds[mThumbIds.length - 1];
mThumbIds[mThumbIds.length - 1] = mThumbIds[position2];
PuzzleAdapter puzzleAdapter = new PuzzleAdapter(this, mThumbIds);
mGridView.setAdapter(puzzleAdapter);
}
private void checkFinish() {
for (int i = 0; i < mThumbIds.length - 1; i++) {
Bitmap bitmap = mBitmaps.get(i);
Bitmap thumbBitmap = BitmapFactory.decodeResource(getResources(), mThumbIds[i]);
if (!bitmap.sameAs(thumbBitmap)) {
return;
}
}
Toast.makeText(this, "恭喜你完成了拼图游戏!", Toast.LENGTH_SHORT).show();
mBitmaps.add(mLastBitmap);
mThumbIds[mThumbIds.length - 1] = R.drawable.image_blank;
PuzzleAdapter puzzleAdapter = new PuzzleAdapter(this, mThumbIds);
mGridView.setAdapter(puzzleAdapter);
}
}
```
4. 在布局文件中添加GridView:
```xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:gravity="center"
android:numColumns="3"
android:stretchMode="columnWidth" />
```
以上就是一个简单的拼图游戏的代码实现。
阅读全文