Java使用定时器编写一个简单的抢红包小游戏

 更新时间:2022年07月01日 14:56:50   作者:秋日的晚霞  
这篇文章主要为大家介绍了Java如何使用定时器编写一个简单的抢红包小游戏,文中的示例代码讲解详细,感兴趣的小伙伴可以尝试一下

1.新建项目

2. 添加 计时器,按钮组件

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:alignment="center"
    ohos:orientation="vertical">

    <TickTimer
        ohos:id="$+id:tick_1"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text_color="red"
        ohos:text_size="50vp"
        ohos:text_alignment="center"
        ohos:layout_alignment="center"
        />

    <Button
        ohos:id="$+id:bt_1"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:margin="30vp"
        ohos:clickable="false"
        ohos:text="准备!"
        ohos:text_color="red"
        ohos:text_size="50vp"
        ohos:text_alignment="center"
        ohos:layout_alignment="center"/>

</DirectionalLayout>

3.抢红包业务逻辑

package com.sgg.hongbao.slice;

import com.sgg.hongbao.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.TickTimer;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.SimpleFormatter;

public class MainAbilitySlice extends AbilitySlice {

    Long money = 0L;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        // 获取定时器组件
        TickTimer tickTimer = (TickTimer) findComponentById(ResourceTable.Id_tick_1);
        //获取按钮组件
        Button bt = (Button) findComponentById(ResourceTable.Id_bt_1);


        tickTimer.setCountDown(false);

        tickTimer.start();

        // 10S 准备时间
        int countDwonTime = 3;

        tickTimer.setTickListener(tickTimer1 -> {
            Long aLong = string2Long(tickTimer1.getText());
            Long time = countDwonTime - aLong;

            if (aLong >= 10) {
                bt.setText(" 恭喜你 抢到 " + money + " 元 ");
                bt.setMultipleLine(true);
                //关闭定时器
                tickTimer.setText(" 00 : 00 ");
                tickTimer.stop();
                return;
            }

            if (time <= 0) {
                bt.setText("点我疯狂抢红包");
            } else {

                if (aLong == 0) {

                } else {
                    bt.setText(" 倒计时 " + time + "  秒");
                }
            }
        });


        bt.setClickedListener(component -> {
            money+=1000;
        });

    }


    private Long string2Long(String str) {

        long time = 0;
        try {
            time = new SimpleDateFormat("mm:ss").parse(str).getSeconds();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

4.效果演示

到此这篇关于Java使用定时器编写一个简单的抢红包小游戏的文章就介绍到这了,更多相关Java抢红包游戏内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java中泛型的接口、通配符使用详解

    Java中泛型的接口、通配符使用详解

    这篇文章主要介绍了Java中泛型的接口、通配符使用详解,编译时的类型安全监测机制,也可以把这个数据类型理解成是一种可以传递的参数,需要的朋友可以参考下
    2023-08-08
  • struts2框架的登录制作图文教程

    struts2框架的登录制作图文教程

    下面小编就为大家带来一篇struts2框架的登录制作图文教程。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • Spring Boot多数据源及其事务管理配置方法

    Spring Boot多数据源及其事务管理配置方法

    本篇文章主要介绍了Spring Boot多数据源及其事务管理配置方法,具有一定的参考价值,有兴趣的可以了解一下。
    2017-04-04
  • java中的常见关键字解析

    java中的常见关键字解析

    这篇文章主要介绍了java中的常见关键字,需要的朋友可以参考下
    2014-08-08
  • SpringBoot里使用Servlet进行请求的实现示例

    SpringBoot里使用Servlet进行请求的实现示例

    这篇文章主要介绍了SpringBoot里使用Servlet进行请求的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • idea新建maven项目没有src目录的操作方法

    idea新建maven项目没有src目录的操作方法

    这篇文章主要介绍了idea新建maven项目没有src目录的两种操作方法,需要的朋友可以参考下
    2018-03-03
  • SpringBoot自定义启动器Starter流程详解

    SpringBoot自定义启动器Starter流程详解

    SpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在maven中引入starter依赖,SpringBoot就能自动扫描到要加载的信息并启动相应的默认配置。starter让我们摆脱了各种依赖库的处理,需要配置各种信息的困扰
    2022-11-11
  • java 生成二维码实例

    java 生成二维码实例

    这篇文章主要介绍了java 生成二维码的实例,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • spring Cloud微服务阿里开源TTL身份信息的线程间复用

    spring Cloud微服务阿里开源TTL身份信息的线程间复用

    这篇文章主要为大家介绍了spring Cloud微服务中使用阿里开源TTL身份信息的线程间复用,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • IDEA启动报错Internal error. Please refer to https://jb.gg/ide/critical-startup-errors解决办法

    IDEA启动报错Internal error. Please refer to https://jb.gg/i

    这篇文章主要介绍了IDEA启动报错Internal error. Please refer to https://jb.gg/ide/critical-startup-errors解决办法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-04-04

最新评论