Android-小游戏

2023-11-19

Android:打地鼠游戏

前端界面(布局文件) :TableLayout(表格布局)+TableRow(行)+TextView(文本框)+ImageView(图片框)
java代码:Handler(消息处理)+Runnable(建子线程)+setOnClickListener(开始结束按钮监听事件)+CountDownTimer(倒计时类)
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//设置横屏

布局文件
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:layout_marginLeft="30dp">

    <TableRow>

    <TextView
        android:id="@+id/tvCount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打到个数:" />

    <ImageView
        android:background="#0000"
        android:id="@+id/img12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/d1"/>

    <ImageView
        android:background="#0000"
        android:id="@+id/img13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/d1"/>

    <ImageView
        android:background="#0000"
        android:id="@+id/img14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/d1"/>

        <ImageView
            android:background="#0000"
            android:id="@+id/img15"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

    <TextView
        android:id="@+id/tvRemainTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="倒计时:" />
</TableRow>



    <TableRow>

        <ImageView
            android:id="@+id/img21"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img22"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img23"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img24"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img25"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>
        <ImageView
            android:id="@+id/img26"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>
    </TableRow>


    <TableRow>

        <ImageView
            android:id="@+id/img31"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img32"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img33"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img34"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img35"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>
        <ImageView
            android:id="@+id/img36"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>
    </TableRow>

    <TableRow>

        <ImageView
            android:id="@+id/img41"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img42"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img43"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img44"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img45"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img46"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>
    </TableRow>


    <TableRow>

        <Button
            android:id="@+id/btnPlay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="开始"/>

        <ImageView
            android:id="@+id/img52"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img53"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>
        <ImageView
            android:id="@+id/img54"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <ImageView
            android:id="@+id/img55"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/d1"/>

        <Button
            android:id="@+id/btnFinish"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="结束"/>
    </TableRow>


</TableLayout>
Java代码
package com.example.qq.myfirstgame;

import android.content.pm.ActivityInfo;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    //声明组件
    private TextView tvCount, tvRemainTime;
    private Button btnPlay, btnFinish;
    private ImageView[] images = new ImageView[26];
    public int[] ids = {R.id.img12, R.id.img13, R.id.img14, R.id.img15,
            R.id.img21, R.id.img22, R.id.img23, R.id.img24, R.id.img25, R.id.img26,
            R.id.img31, R.id.img32, R.id.img33, R.id.img34, R.id.img35, R.id.img36,
            R.id.img41, R.id.img42, R.id.img43, R.id.img44, R.id.img45, R.id.img46,
            R.id.img52, R.id.img53, R.id.img54, R.id.img55};

    //flag判断游戏是否结束,count记录打中地鼠的个数,oldID,newID记录位置
    boolean flag = true;
    int count = 0;
    int oldID = 0, newID = 0;
    //子线程与主线程通过Handler来进行通信。子线程可以通过Handler来通知主线程进行UI更新
    Handler handler = new Handler() {//创建一个Handler,消息处理,在handler中捕获所需消息,实现响应
        @Override
        //设置地洞,地鼠随机出洞
        public void handleMessage(Message message) {
            images[oldID].setImageResource(R.mipmap.d1);//设置背景图片为地洞
            newID = (int) (Math.random() * 26);//地鼠随机出洞位置
            images[newID].setImageResource(R.mipmap.d3);//设置背景图片为地鼠
            oldID=newID;

        }
    };

    //创建子线程用于数秒,计时器
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            while (flag) {
                try {
                    Message message = Message.obtain();//当前的Handler中获取指定的Message以供再次使用,得到msg不会产生内存开销
                    handler.sendMessage(message);//从子线程中发出消息
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//设置横屏
        initView();//初始化页面
        //按钮,组件绑定监听事件
        btnPlay.setOnClickListener(this);
        btnFinish.setOnClickListener(this);
        for (int j = 0; j < images.length; j++) {
            images[j].setOnClickListener(this);
        }

    }


    @Override
    public void onClick(View v) {//按钮重玩/开始的监听方法重写
        switch (v.getId()) {
            case R.id.btnPlay://若单击开始按钮就开始倒计时(新建线程)
                new MyCount(20 * 1000, 1000).start();
                Thread thread1 = new Thread(runnable);
                thread1.start();
                flag=true;
                break;


            case R.id.btnFinish://若单击重玩按钮就就开始倒计时(新建线程)
                images[oldID].setBackgroundResource(R.mipmap.d1);
                flag=false;
                break;

            default://游戏未结束时,点击到有地鼠的图片位置击中个数就+1
                if (images[oldID].getId() == v.getId() && flag == true) {
                    count++;
                    tvCount.setText("击中数:" + count);
                }
        }
    }

    //倒计时功能
    private class MyCount extends CountDownTimer {//继承倒计时类并重写倒计时方法
        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long l) {//每个时间间隔会回调一次(上面设置的是1秒)
            long hour = l / 1000 / 3600;
            long minute = l / 1000 % 3600 / 60;
            long second = l / 1000 % 3600 % 60;
            tvRemainTime.setText("倒计时" + hour + ":" + minute + ":" + second);
        }

        @Override
        public void onFinish() {//在整个计时器结束之后回调该方法,并将flag置为false,游戏结束。
            tvRemainTime.setText("游戏时间到了!");
            flag = false;
            images[oldID].setBackgroundResource(R.mipmap.d1);//图片背景置为地洞
            count=0;

        }
    }

    public void initView(){
        //初始化组件
        tvCount= (TextView) this.findViewById(R.id.tvCount);
        tvRemainTime=(TextView) this.findViewById(R.id.tvRemainTime);
        btnPlay=(Button)this.findViewById(R.id.btnPlay);
        btnFinish=(Button)this.findViewById(R.id.btnFinish);
        for(int i=0;i<images.length;i++){
            images[i]=(ImageView)this.findViewById(ids[i]);
        }
    }
}

运行效果图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android-小游戏 的相关文章

  • SSLContext 初始化

    我正在看JSSE参考指南 我需要获取一个实例SSLContext为了创建一个SSLEngine 所以我可以使用它Netty以启用安全性 获取实例SSLContext I use SSLContext getInstance 我看到该方法被重
  • ThreadPoolExecutor 和队列

    我以为使用线程池执行器 http docs oracle com javase 6 docs api java util concurrent ThreadPoolExecutor html我们可以提交Runnables 要在以下位置执行B
  • 如何在 Java 中使用 HTML 解析器和 Apache Tika 来提取所有 HTML 标签?

    我下载了 tika core 和 tika parser 库 但找不到将 HTML 文档解析为字符串的示例代码 我必须删除网页源的所有 html 标签 我能做些什么 如何使用 Apache Tika 进行编码 您想要 html 文件的纯文本
  • Java 8 Stream - 并行执行 - 不同的结果 - 为什么?

    假设我有一个List
  • Java ArrayList 和 HashMap 动态

    有人可以提供一个创建Java的例子吗ArrayList and HashMap在飞行中 所以而不是做一个add or put 实际上在类实例化时为数组 哈希提供种子数据 举个例子 类似于 PHP 的例子 array array 3 1 2
  • Android studio 找不到 Theme.AppCompat.Light.DarkActionBar [重复]

    这个问题在这里已经有答案了 我已经导入了一个Android Studio项目来自Github into Android Studio 虽然我的build gradle有dependencies dependencies compile fi
  • java中永远不会出现的异常

    我为点和向量编写一个类 我想用它们来计算向量的点和范数 这些是点类和向量类 public class Point public float x y public class MyVector public Point start end 我
  • 如何在Android 11中获取dir文件列表

    我想编写自己的精简版文件浏览器 文件 API 现在不适用于外部存储 该版本还提供了对范围存储的改进 这使得开发人员可以更轻松地迁移到使用此存储模型 我不明白如何使用范围存储来访问 sdcard 如果您正在寻找文件选择器体验 存储访问框架 h
  • python 和 android 中通过 AES 算法加密和解密

    我有用于 AES 加密的 python 和 android 代码 当我在android中加密文本时 它在python上成功解密 但无法在android端解密 有人有想法吗 Python代码 import base64 import hash
  • Log4j 2.0 中发现 ClassNotFoundException

    我已经设置了 log4j12 api beta2 jar 的构建路径 但它给出了 以下错误请帮我解决这个问题我的代码如下 java 文件 package com sst log4j class Product private int pro
  • 找不到R类

    当我打开 Eclipse 时 R class在我的项目中消失了 为什么 我有 eclipse juno 和最新版本的 android SDK The R class不会重新生成 因为代码中有错误 我怎么解决这个问题 Try Project
  • Eclipse Juno 指标插件

    Eclipse JUNO 版本有哪些 Eclipse 指标插件 我尝试了一些通用指标插件 但没有一个能够在 Eclipse 的 JUNO 版本中正常运行 差点忘了 我们正在使用 Java 作为编程语言 我想要诸如圈复杂度 代码行数 方法长度
  • “强制更新快照/版本” - 这是什么意思

    在 Maven 项目中 选择 更新项目 时 有一个名为 强制更新快照 版本 的选项 它有什么作用 强制更新快照 版本 就像运行以下命令 mvn U install U 也可以用作 update snapshot 看here http boo
  • 未捕获的引用错误:cordova 未定义

    这是我的 HelloPlugin js 文件 var HelloPlugin callNativeFunction function success fail resultType return cordova exec success f
  • 按钮悬停和按下效果 CSS Javafx

    我是 CSS 新手 为按钮定义了以下 CSS 样式 其中id并且应用了自定义样式 但不应用悬停和按下效果 bevel grey fx background color linear gradient f2f2f2 d6d6d6 linear
  • Android 10 通知彩色操作按钮

    I m trying to color the buttons Action in Notification like this So far this is what i m achieved so far 下面是我正在使用的代码 通知服
  • 在Java内存管理中,“PS”代表什么?

    每当我看到 Java 中对内存的引用时 各种空格总是以 PS 为前缀 PS 是什么意思 它开始困扰我 到目前为止我唯一的猜测是 泳池空间 但这将是多余的 例子 PS伊甸园空间 PS 幸存者空间 PS 终身空间 老一代 PS Perm Gen
  • 背景图像隐藏其他组件,例如按钮标签等,反之亦然

    如何解决此代码中组件的隐藏问题 代码运行没有错误 但背景图片不显示 如何更改代码以获取背景图像 使用验证方法时 它在validation 中创建错误 public class TEST public TEST String strm Jan
  • 如何在顶部工具栏的购物车图标上添加商品编号?安卓

    I saw some apps made it So I am intersting at how to do it I believe there must be some trick way to do it Example 我在菜单文
  • 将其元素添加到另一个列表后清除列表

    我正在做一个程序 它获取更多句子作为参数 我制作了 2 个列表 一个称为 propozitie 其中包含每个句子 另一个称为 propozitii 其中包含所有句子 问题是 当我在遇到 后清除 propozitie 列表时 它也会清除 pr

随机推荐

  • C# 系统应用之ListView控件 (三).添加ContextMenuStrip右键菜单打开删除文件

    在前面讲述过使用TreeView控件和ListView控件显示磁盘目录信息 但仅仅是显示信息是不够的 我们还需要具体的操作 在 个人电脑使用历史痕迹 项目中我还需要添加 打开文件 和 删除文件 两种方法 具体如下 在第一篇文章 C 系统应用
  • 【已解决】因为计算机丢失D3DCOMPILER_47.dll 的解决方法

    因为计算机丢失D3DCOMPILER 47 dll 的解决方法 D3DCompiler 47 dll是电脑系统中一个非常重要的文件 许多的游戏或者软件运行都需要它的支持 如果没有这个文件可能会造成一些游戏或者软件运行不了 假如您的系统提示
  • unity 停止调用InvokeRepeating()的方法

    可以使用CancelInvoke 函数来停止InvokeRepeating 的调用 示例如下 声明一个float类型的变量用来存储调用Invokerepeating 的时间间隔 public float repeatTime 2 0f vo
  • nginx超时设置:配置某次请求的超时时间

    nginx比较强大 可以针对单个域名请求做出单个连接超时的配置 可以动态解释和静态解释可以根据业务的需求配置 proxy connect timeout 后端服务器连接的超时时间 发起握手等候响应超时时间 proxy read timeou
  • 解决Error occurred during initialization of VMjava/lang/NoClassDefFoundError: java/lang/Object问题

    问题描述 初次学习java时出现 Error occurred during initialization of VM java lang NoClassDefFoundError java lang Object 前提 环境变量如何配置
  • 函数计算简介

    什么是函数计算 大家都了解 Serverless 并不是没有服务器 而是开发者不再需要关心服务器 下图是一个应用从开发到上线的对比图 在传统 Serverful 架构下 部署一个应用需要购买服务器 部署操作系统 搭建开发环境 编写代码 构建
  • SQL关联表更新

    存在表数据 per user per emp info 需要将per emp info 中的name和post name更新到per user表中 mysql数据库 update per user u JOIN per emp info e
  • 我的英雄之路--转自HERO2008会刊

    HERO2007仍落落在目 HERO2008即将来临 能参加两届英雄大会 甚是荣幸 于是想写一写我的英雄之路 提笔易 落笔难 反复问自己是否配得上 英雄 二字 打开社区 看看自己的排名 虽排名速降 尚位居前列 于是厚起脸皮说说自己的在CSD
  • 【Vue项目实践】实现在线预览word文件、excel文件

    JUST DO IT 目录 一 查看word 1 引用mammoth js 2 页面布局 3 请求URL显示数据 二 查看Excel 1 引用sheetjs 2 页面布局 3 请求URL显示数据 三 项目应用 根据详情后缀分情况显示word
  • 修改CheckBox选择框、设置选择框颜色

    使用组件自带的属性 android buttonTint 可直接设置选择框的颜色 修改选择框选中与未选中时的状态 需设置选择器 selector XML
  • 医学图像公开数据集

    一 医学图像 1 阿里云 天池数据集 https tianchi aliyun com dataset spm 5176 14154004 J 4642415020 16 167b5699EO8yrc https tianchi aliyu
  • IDEA为spring-boot添加热部署

    在IDEA中 可以为spring boot添加热部署 一旦修改了java文件 spring boot会重新编译修改的文件 而不用重启 一 打开pom xml 添加依赖
  • 关于LC电路中电磁振荡过程的一个问题,向各位求教

    关于LC电路中电磁振荡过程的一个问题 向各位求教 复制链接 LC电路关键是产生能量在电感 L 和电容器 C 之间的转换 电感可以维持电流保持不变 电容可以维持两端电压不变 能量就在这个电压与电流之间转换 当电感中有电流时就会产生磁场 电容器
  • 若依框架自定义登录(免密登录)

    1 继承DaoAuthenticationProvider package com ruoyi framework config import org springframework security authentication BadC
  • iOS

    我们有的时候在创建UIView的时候 想要使用xib进行创建视图发现 xib文件不能和UIView文件一起创建 所以 我们要单独创建xib文件 我们选择Empty文件 而不要选择View文件 记得文件名和你之前创建的UIView文件名要一致
  • 简单了解默克尔(Merkle)树

    Merkle树是Ralph Merkle在1988年发明的 旨在构建更好的数字签名 原文是A DIGITAL SIGNATURE BASED ON A CONVENTIONAL ENCRYPTION FUNCTION本篇论文在Weki百科中
  • 各种平台下Perl模块的安装方法

    Perl到了第五版增加了模块的概念 用来提供面向对象编程的能力 这是Perl语言发展史上 的一个里程碑 此后 广大自由软件爱好者开发了大量功能强大 构思精巧的Perl模块 极大地 扩展了Perl语言的功能 CPAN Comprehensiv
  • 交换机access与trunk口

    交换机access与trunk口 转载自 https www cnblogs com weiyikang p 4945914 html 理论知识 以太网端口二种链路类型 Access 和Trunk Access 类型的端口 只能属于1 个V
  • 攻防世界-fileclude

  • Android-小游戏

    Android 打地鼠游戏 前端界面 布局文件 TableLayout 表格布局 TableRow 行 TextView 文本框 ImageView 图片框 java代码 Handler 消息处理 Runnable 建子线程 setOnCl