安卓实现简单砸地鼠游戏

效果

布局

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);
    }
}

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

相关推荐
wanhengidc7 小时前
云手机 多开无忧 科技运行
运维·服务器·科技·游戏·智能手机
牛掰是怎么形成的15 小时前
性能优化:线程数量、CPU绑定、负载均衡——游戏多线程场景详解与C#实战
游戏·性能优化·负载均衡
_大学牲1 天前
Flutter 勇闯2D像素游戏之路(二):绘制加载游戏地图
flutter·游戏·游戏开发
top_designer1 天前
PS 样式参考:3D 白模直接出原画?概念美术的“光影魔术手”
游戏·3d·prompt·aigc·技术美术·建模·游戏美术
汝生淮南吾在北1 天前
SpringBoot+Vue游戏攻略网站
前端·vue.js·spring boot·后端·游戏·毕业设计·毕设
weixin_307779131 天前
满足游戏应用低延迟和历史查询需求的解决方案
游戏·云原生·架构·云计算·aws
da_vinci_x1 天前
PS 图案预览 + Sampler:告别“修接缝”,AI 量产 4K 无缝 PBR
人工智能·游戏·aigc·贴图·技术美术·游戏美术·法线贴图
摘星编程1 天前
从云游戏到Mac远程操控:UU远程游戏、办公场景全覆盖
游戏·macos·uu远程
wanhengidc2 天前
云手机 网络连接与持续性的表现如何
运维·服务器·科技·游戏·智能手机
无限进步_2 天前
C语言实现贪吃蛇游戏详解
c语言·开发语言·数据结构·c++·后端·算法·游戏