安卓实现简单砸地鼠游戏

效果

布局

XML 复制代码
<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">
    <TextView
        android:id="@+id/scoreTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="得分:0"
        android:textSize="18sp" />
    <GridLayout
        android:id="@+id/gridLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnCount="3"
        android:rowCount="3">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:background="@mipmap/laohu" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:background="@mipmap/laohu" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:background="@mipmap/laohu" />

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:background="@mipmap/laohu" />

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:background="@mipmap/laohu" />

        <ImageView
            android:id="@+id/imageView6"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:background="@mipmap/laohu" />

        <ImageView
            android:id="@+id/imageView7"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:background="@mipmap/laohu" />

        <ImageView
            android:id="@+id/imageView8"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:background="@mipmap/laohu" />

        <ImageView
            android:id="@+id/imageView9"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:background="@mipmap/laohu" />

    </GridLayout>

</LinearLayout>

实现代码,

java 复制代码
public class AttentionQuestionsActivity extends AppCompatActivity {
    private ImageView[] imageViews; // 地鼠图片数组
    private ImageView currentImageView; // 当前显示的地鼠图片
    private int score = 0; // 得分
    private TextView scoreTextView; // 显示得分的文本视图

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_attention_questions);

        initImageViews(); // 初始化地鼠图片数组

        scoreTextView = findViewById(R.id.scoreTextView);
        showNextImageView(); // 显示第一个地鼠
    }

    // 初始化地鼠图片数组
    private void initImageViews() {
        imageViews = new ImageView[9];
        for (int i = 0; i < imageViews.length; i++) {
            imageViews[i] = findViewById(getResources().getIdentifier("imageView" + (i + 1), "id", getPackageName()));
            imageViews[i].setVisibility(View.INVISIBLE); // 初始设置地鼠图片为不可见
            imageViews[i].setOnClickListener(onClickListener);
        }
    }

    // 点击事件监听器
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == currentImageView) { // 如果点击的是地鼠
                increaseScore(); // 增加得分
                hideCurrentImageView(); // 隐藏当前地鼠
                showNextImageView(); // 显示下一个地鼠
            }
        }
    };

    // 增加得分
    private void increaseScore() {
        score++;
        scoreTextView.setText("得分:" + score); // 更新得分显示
    }

    // 隐藏当前显示的地鼠
    private void hideCurrentImageView() {
        if (currentImageView != null) {
            currentImageView.setVisibility(View.INVISIBLE);
            currentImageView = null;
        }
    }

    // 显示下一个地鼠
    private void showNextImageView() {
        hideCurrentImageView();

        SecureRandom random = new SecureRandom();
        int nextIndex;
        do {
            nextIndex = random.nextInt(imageViews.length);
        } while (imageViews[nextIndex].getVisibility() == View.VISIBLE);

        currentImageView = imageViews[nextIndex];
        currentImageView.setVisibility(View.VISIBLE);
    }
}

备注 以上只是简单把功能实现出来,大家有需要可以拿来改为自己想要的

相关推荐
IDOlaoluo1 天前
vc_redist.x64.exe安装方法,解决软件游戏缺少运行库问题
游戏
AA陈超1 天前
虚幻引擎5 GAS开发俯视角RPG游戏 P06-28 构建属性菜单小部件控制器
c++·游戏·ue5·游戏引擎·虚幻
da_vinci_x1 天前
Substance Designer“程序化地形”与“Splat Map”生成管线
游戏·aigc·设计师·贴图·技术美术·游戏美术·substance designer
远程软件小帮手2 天前
哪个云电脑最好用?适合玩游戏的云电脑测评!
运维·服务器·游戏·电脑
嘀咕博客2 天前
h5游戏免费下载:小飞鱼?
游戏
da_vinci_x3 天前
在Substance Designer里“预演”你的游戏着色器(Shader)
人工智能·游戏·技术美术·着色器·游戏策划·游戏美术·substance designer
reddingtons3 天前
ZBrush细节烘焙全“糊”了?Painter“平均法线”+“偏斜贴图”的“无笼”烘焙管线
游戏·设计师·贴图·技术美术·substance painter·游戏美术·zbrush
嘀咕博客4 天前
h5游戏免费下载:HTML5拉杆子过关小游戏
前端·游戏·html5
FairGuard手游加固4 天前
Cocos资源加密方案解析
安全·游戏·cocos2d
UWA5 天前
为什么Android游戏画面在30帧运行时有抖动现象
android·游戏