Android项目---拼图小游戏(上)

原创
小哥 3年前 (2022-12-19) 阅读数 4 #大杂烩

每个人都一定接触过类似的小游戏。进入节目时,画面会无序排列,共有9张乱序画面。必须转移到提供模板图片的同一方才能成功。在比赛的过程中,会有时间安排。当拼图成功时,时间就会停止。单击再次开始,图片将再次中断,计时将再次开始。!

实施步骤:1.拼图图,2.拼图打乱了设置,3.拼图块开关位置,4.拼图成功条件,5.拼图游戏又开始了

这只能实现以下步骤1和步骤2

实施过程中使用的部分知识,使用的时间安排handler线程机制,图片无序排列的实现原理是以数字形式书写,然后在相应的控件上用两个或两个数值进行交换!

已在代码段中进行了相应的注释。如果你不明白,可以在评论区留言或私信。如果文章内容错误或不足,请原谅我并纠正我!

代码如下:

xml拼图布局代码:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">

<LinearLayout android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/pt_line1" android:layout_gravity="center" android:orientation="horizontal"> <ImageButton android:src="@drawable/pt_id_00x00" android:padding="0dp" android:id="@+id/pt_id_00x00" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick"/> <ImageButton android:src="@drawable/pt_id_00x01" android:padding="0dp" android:id="@+id/pt_id_00x01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick"/> <ImageButton android:src="@drawable/pt_id_00x02" android:padding="0dp" android:id="@+id/pt_id_00x02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/pt_line2" android:layout_gravity="center" android:orientation="horizontal"> <ImageButton android:src="@drawable/pt_tv_01x00" android:padding="0dp" android:id="@+id/pt_id_01x00" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick"/> <ImageButton android:src="@drawable/pt_tv_01x01" android:padding="0dp" android:id="@+id/pt_id_01x01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick"/> <ImageButton android:src="@drawable/pt_tv_01x02" android:padding="0dp" android:id="@+id/pt_id_01x02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/pt_line3" android:layout_gravity="center" android:orientation="horizontal"> <ImageButton android:src="@drawable/p1" android:padding="0dp" android:id="@+id/pt_id_02x00" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick"/> <ImageButton android:src="@drawable/p2" android:padding="0dp" android:id="@+id/pt_id_02x01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick"/> <ImageButton android:src="@drawable/p3" android:padding="0dp" android:id="@+id/pt_id_02x02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick"/> <Button android:layout_marginTop="10dp" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/pt_btn_restart" android:onClick="restart" android:text="重新开始"/> <ImageView android:layout_gravity="center" android:layout_marginTop="10dp" android:src="@drawable/yuantu" android:id="@+id/pt_iv" android:layout_width="210dp" android:layout_height="210dp"/>

activity功能实现代码:

package com.example.jigsaw;

import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.ImageButton; import android.widget.TextView;

public class MainActivity extends AppCompatActivity { ImageButton ib00,ib01,ib02,ib10,ib11,ib12,ib20,ib21,ib22; Button restartBtn; TextView timeTv; //定义时间的变量 int time = 0; //用于存储碎片以进行统一管理的阵列 private int[]image={R.drawable.pt_id_00x00,R.drawable.pt_id_00x01,R.drawable.pt_id_00x02, R.drawable.pt_tv_01x00,R.drawable.pt_tv_01x01,R.drawable.pt_tv_01x02,R.drawable.p1,R.drawable.p2,R.drawable.p3}; //为图片数组声明下标数组并随机排列数组 private int[]imageIndex=new int[image.length]; Handler handler=new Handler(){ @Override public void handleMessage(@NonNull Message msg) { if (msg.what==1){ time++; timeTv.setText("时间:"+time+" 秒"); handler.sendEmptyMessageDelayed(1,1000); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); //打乱碎片 disruptRandpm(); handler.sendEmptyMessageDelayed(1,1000); } //随机镦粗不规则 private void disruptRandpm() { for(int i=0;i<imageIndex.length;i++){ imageIndex[i]=i; } //规定20随机选择与两个角标记相对应的值进行交换。 int rand1,rand2; for(int j=0;j<20;j++){ //随机生成角标记,0-8的数值 rand1=(int)(Math.random()(imageIndex.length-1)); //第二个随机生成的角标记不能与第一个相同 do{ rand2=(int)(Math.random()(imageIndex.length-1)); if(rand1!=rand2){ break; } }while (true); //交换两个角标签上的相应值 swap(rand1,rand2); } //随机排列在指定控件上 ib00.setImageResource(image[imageIndex[0]]); ib01.setImageResource(image[imageIndex[1]]); ib02.setImageResource(image[imageIndex[2]]); ib10.setImageResource(image[imageIndex[3]]); ib11.setImageResource(image[imageIndex[4]]); ib12.setImageResource(image[imageIndex[5]]); ib20.setImageResource(image[imageIndex[6]]); ib21.setImageResource(image[imageIndex[7]]); ib22.setImageResource(image[imageIndex[8]]); } //交换 private void swap(int rand1, int rand2) { int temp=imageIndex[rand1]; imageIndex[rand1]=imageIndex[rand2]; imageIndex[rand2]=temp; }

private void initView() {
    ib00=findViewById(R.id.pt\_id\_00x00);
    ib01=findViewById(R.id.pt\_id\_00x01);
    ib02=findViewById(R.id.pt\_id\_00x02);
    ib10=findViewById(R.id.pt\_id\_01x00);
    ib11=findViewById(R.id.pt\_id\_01x01);
    ib12=findViewById(R.id.pt\_id\_01x02);
    ib20=findViewById(R.id.pt\_id\_02x00);
    ib21=findViewById(R.id.pt\_id\_02x01);
    ib22=findViewById(R.id.pt\_id\_02x02);
    timeTv=findViewById(R.id.pt\_tv\_time);
    restartBtn=findViewById(R.id.pt\_btn\_restart);

}

public void onClick(View view) {
}

public void restart(View view) {
    //拼图打乱

    handler.removeMessages(1);
    //该回来了。0和re-⏲
    time=0;
    timeTv.setText("时间:"+time+" 秒");
    handler.sendEmptyMessageDelayed(1,1000);

}

} 效果截图:

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除