安卓实现简单砸地鼠游戏

news2025/6/28 8:38:45

效果

布局 

<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>

实现代码,

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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1453549.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

开年炸裂-Sora/Gemini

最新人工智能消息 谷歌的新 Gemini 模型 支持多达 1M的Token&#xff0c;可以分析长达一小时的视频 1M Token可能意味着分析700,000 个单词、 30,000 行代码或11 小时的音频、总结、改写和引用内容。 Comment&#xff1a;google公司有夸大的传统&#xff0c;所以真实效果需要上…

【自然语言处理】实验3,文本情感分析

清华大学驭风计划课程链接 学堂在线 - 精品在线课程学习平台 (xuetangx.com) 代码和报告均为本人自己实现&#xff08;实验满分&#xff09;&#xff0c;只展示主要任务实验结果&#xff0c;如果需要详细的实验报告或者代码可以私聊博主 有任何疑问或者问题&#xff0c;也欢…

2024/2/17 图论 最短路入门 dijkstra 1

目录 算法思路 Dijkstra求最短路 AcWing 849. Dijkstra求最短路 I - AcWing 850. Dijkstra求最短路 II - AcWing题库 最短路 最短路 - HDU 2544 - Virtual Judge (vjudge.net) 【模板】单源最短路径&#xff08;弱化版&#xff09; P3371 【模板】单源最短路径&#xf…

文生视频提示词:故事与主题

内容创意 --故事与主题 Story & Theme 这些词汇覆盖了从基本的故事类型到特定的主题和元素&#xff0c;可用于激发创意和定义视频内容的核心主题。 Adventure 冒险 Romance 浪漫 Mystery 神秘 Fantasy 幻想 Science Fiction 科幻 Horror 恐怖 Thriller 惊悚 Comedy 喜剧 Dr…

EXCEL中不错的xlookup函数

excel中一般要经常用vlookup函数&#xff0c;但其实经常麻烦要正序&#xff0c;从左边到右边&#xff0c;还要数列&#xff0c;挺麻烦的&#xff0c;xlookup的函数还不错&#xff0c;有个不错的一套视频介绍,B站的&#xff0c;地址是&#xff1a;XLOOKUP函数基础用法&#xff0…

Python vars函数

在Python编程中&#xff0c;vars()函数是一个常用的内置函数&#xff0c;用于返回对象的__dict__属性。该属性存储了对象的命名空间&#xff0c;包括对象的所有属性和方法。本文将深入探讨Python中的vars()函数&#xff0c;包括基本用法、适用对象、返回结果、实际应用场景&…

MySQL数据库⑪_C/C++连接MySQL_发送请求

目录 1. 下载库文件 2. 使用库 3. 链接MySQL函数 4. C/C链接示例 5. 发送SQL请求 6. 获取查询结果 本篇完。 1. 下载库文件 要使用C/C连接MySQL&#xff0c;需要使用MySQL官网提供的库。 进入MySQL官网选择适合自己平台的mysql connect库&#xff0c;然后点击下载就行…

线程库接口模拟封装(使用参数包接受参数,2种方法)

目录 引入 模拟实现 思路 传递参数包 代码 thread.hpp main.cpp 示例 引入 之前我们一直使用的都是linux中的原生线程库,但c中其实是有提供封装好的线程库的 -- <thread> 下面我们也来试着封装一下线程接口 模拟实现 思路 首先,明确线程库的核心操作: 创建和销毁…

标签结构比目录结构更易用 | Obsidian实践

当我顿悟了标签结构&#xff08;标签树&#xff09;的构建逻辑&#xff0c;彻底摆脱了目录结构的限制&#xff0c;从此可按任意维度管理和检索笔记。 对于每一个新入坑Obsidian的小白菜来说&#xff0c;通过创建目录结构&#xff0c;对笔记进行管理是最符合直觉的方式。但是&am…

【AIGC】大语言模型

大型语言模型&#xff0c;也叫大语言模型、大模型&#xff08;Large Language Model&#xff0c;LLM&#xff1b;Large Language Models&#xff0c;LLMs&#xff09; 什么是大型语言模型 大型语言模型&#xff08;LLM&#xff09;是指具有数千亿&#xff08;甚至更多&#xf…

php switch、for、foreach、while、do...while

php switch 1. switch2. for循环3. foreach4. while、do...while 1. switch <?php$height 190;switch ($height) {case 160:echo 太矮了;break; //跳出本次循环case 170:echo 还行吧;break; //跳出本次循环case 180:echo 帅哥;break; //跳出本次循环default:echo 迷; }2.…

初始Git及Linux Centos下安装Git

文章目录 前言版本控制器注意Git安装 前言 不知道你⼯作或学习时&#xff0c;有没有遇到这样的情况&#xff1a;我们在编写各种⽂档时&#xff0c;为了防⽌⽂档丢失&#xff0c;更改失误&#xff0c;失误后能恢复到原来的版本&#xff0c;不得不复制出⼀个副本&#xff0c;⽐如…

springboot196高校教师科研管理系统

Spring Boot高校教师科研管理系统设计与实现 摘 要 社会发展日新月异&#xff0c;用计算机应用实现数据管理功能已经算是很完善的了&#xff0c;但是随着移动互联网的到来&#xff0c;处理信息不再受制于地理位置的限制&#xff0c;处理信息及时高效&#xff0c;备受人们的喜…

(八)【Jmeter】线程(Threads(Users))之bzm - Concurrency Thread Group

简述 操作路径如下: 作用:模拟一定时间段内达到指定并发数的用户访问。配置:设置目标并发数、启动时间、持续时间等参数。使用场景:测试应用程序在达到一定并发用户数时的性能表现。优点:能够模拟实际并发访问的增长和稳定过程。缺点:主要关注并发数,可能无法模拟真实…

C高级D5作业

1.#!/bin/bash read -p "请输入一个字符>>" -n 1 c echo case $c in [[:lower:]]) echo "小写" ;; [[:upper:]]) echo "大写" ;; [1-9]) echo "数字" ;; …

如何实现Vuex数据持久化

Vuex是一个非常流行的状态管理工具&#xff0c;它可以帮助我们在Vue.js应用中管理和共享数据。然而&#xff0c;当应用重新加载或刷新时&#xff0c;Vuex的状态会被重置&#xff0c;这就导致了数据的丢失。那么&#xff0c;如何才能实现Vuex的数据持久化呢&#xff1f;让我们一…

【自然语言处理】seq2seq模型—机器翻译

清华大学驭风计划课程链接 学堂在线 - 精品在线课程学习平台 (xuetangx.com) 代码和报告均为本人自己实现&#xff08;实验满分&#xff09;&#xff0c;只展示主要任务实验结果&#xff0c;如果需要详细的实验报告或者代码可以私聊博主 有任何疑问或者问题&#xff0c;也欢…

SECS/GEM的HSMS通讯?金南瓜方案

High Speed SECS Message Service (HSMS) 是一种基于 TCP/IP 的协议&#xff0c;它使得 SECS 消息通信更加快速。这通常用作设备间通信的接口。 HSMS 状态逻辑变化&#xff08;序列&#xff09;&#xff1a; 1.Not Connected&#xff1a;准备初始化 TCP/IP 连接&#xff0c;但尚…

使用Autodl云服务器或其他远程机实现在本地部署知识图谱数据库Neo4j

本篇博客的目的在于提高读者的使用效率 温馨提醒&#xff1a;以下操作均可在无卡开机状态下就可完成 一.安装JDK 和 Neo4j 1.1 ssh至云服务器 打开你的pycharm或者其他IDE工具或者本地终端&#xff0c;ssh连接到autodl的服务器。(这一步很简单如下图) 1.2 安装JDK 由于我…

入门OpenCV:图像阈值处理

基本概念 图像阈值是一种简单、高效的图像分割方法&#xff0c;目的是将图像转换成二值图像。这个过程涉及比较像素值和阈值&#xff0c;根据比较结果来确定每个像素点的状态&#xff08;前景或背景&#xff09;。图像阈值在处理二维码、文本识别、物体跟踪等领域中非常有用。…