首頁 > 軟體

Java使用定時器編寫一個簡單的搶紅包小遊戲

2022-07-01 18:01:22

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搶紅包遊戲內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com