Android做一个连连看小游戏

一、首先我们将我们所需要用到的连连看图片放到res下的drawable下
在这里插入图片描述
二、我们在相应的layout文件夹下的xml布局文件中写上连连看布局代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity"
    >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/animal_bg"
        tools:context=".MainActivity"
        android:orientation="vertical">


        <LinearLayout

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="180dp"
            tools:context=".MainActivity">

            <ImageButton

                android:id="@+id/button1"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_margin="15dp"
                android:background="@drawable/three"
                android:onClick="myClick"
                tools:ignore="InvalidId" />

            <ImageButton
                android:id="@+id/button2"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_margin="15dp"
                android:onClick="myClick"
                android:background="@drawable/four" />

            <ImageButton
                android:id="@+id/button3"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_margin="15dp"
                android:onClick="myClick"
                android:background="@drawable/one" />

            <ImageButton

                android:id="@+id/button4"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_margin="15dp"
                android:onClick="myClick"
                android:background="@drawable/five" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:context=".MainActivity">
            <ImageButton

                android:id="@+id/button5"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_margin="15dp"
                android:onClick="myClick"
                android:background="@drawable/one"/>
            <ImageButton

                android:id="@+id/button6"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_margin="15dp"
                android:onClick="myClick"
                android:background="@drawable/two"/>
            <ImageButton
                android:id="@+id/button7"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_margin="15dp"
                android:onClick="myClick"
                android:background="@drawable/box"/>
            <ImageButton

                android:id="@+id/button8"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_margin="15dp"
                android:onClick="myClick"
                android:background="@drawable/four" />
        </LinearLayout>


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:context=".MainActivity">
            <ImageButton

                android:id="@+id/button9"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_margin="15dp"
                android:onClick="myClick"
                android:background="@drawable/five"/>
            <ImageButton
                android:id="@+id/button10"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_margin="15dp"
                android:onClick="myClick"
                android:background="@drawable/box"/>
            <ImageButton
                android:id="@+id/button11"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_margin="15dp"
                android:onClick="myClick"
                android:background="@drawable/three"/>
            <ImageButton
                android:id="@+id/button12"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_margin="15dp"
                android:onClick="myClick"
                android:background="@drawable/two"/>

        </LinearLayout>
            <TextView
                android:id="@+id/guanka"
                android:layout_width="350dp"
                android:layout_height="80dp"
                android:autoSizeTextType="uniform"
                android:textColor="@color/pink"
                android:layout_marginLeft="50dp"
                android:layout_marginTop="30dp"
                android:text="🐧第一关🐇"/>
    </LinearLayout>
</RelativeLayout>

上面代码中的 android:onClick代表着点击事件,android:background="@drawable/two"代表着是哪张背景图片,是以按钮的形式来作为连连看的方块
三、接下来就是我们的按钮点击事件也就是xml文件对应的java类

public class MainActivity extends AppCompatActivity {
    private int count = 5;
    private int sorce=0;
    List<ImageButton> buttonList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tvOne = findViewById(R.id.guanka);
        Typeface typeface = Typeface.createFromAsset(getAssets(), "05汉仪乐喵字体.ttf");
        tvOne.setTypeface(typeface);
    }

    public void myClick(View view){
        if (buttonList.size()==2){
            buttonList.clear();
            ImageButton imageButton = (ImageButton)findViewById(view.getId());
            buttonList.add(imageButton);
        }else if (buttonList.size()==1){
            ImageButton imageButton2 = (ImageButton)findViewById(view.getId());
            ImageButton imageButton1 = buttonList.get(0);
            Drawable drawable1 = imageButton1.getBackground();
            Drawable drawable2 = imageButton2.getBackground();
            Bitmap bitmap1 = ((BitmapDrawable) drawable1).getBitmap();
            Bitmap bitmap2 = ((BitmapDrawable) drawable2).getBitmap();
            if (bitmap1.sameAs(bitmap2)){
                Drawable drawable = getResources().getDrawable(R.drawable.box);
                imageButton1.setBackground(drawable);
                imageButton2.setBackground(drawable);
                if (++sorce>=count){

                    startActivity(new Intent(MainActivity.this,TwoLevel.class));
                }
                buttonList.clear();
            }else {
                buttonList.clear();
                Toast.makeText(this, "图案不同", Toast.LENGTH_SHORT).show();
            }
        }else if (buttonList.size()==0){
            ImageButton imageButton = (ImageButton)findViewById(view.getId());
            buttonList.add(imageButton);
        }

    }



}

四、最后实现效果就是
在这里插入图片描述

图片就各位大哥用自己老师或者自己的渠道的我就不发了。

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dont give up

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值