Java使用定時(shí)器編寫一個(gè)簡單的搶紅包小游戲
更新時(shí)間:2022年07月01日 14:56:50 作者:秋日的晚霞
這篇文章主要為大家介紹了Java如何使用定時(shí)器編寫一個(gè)簡單的搶紅包小游戲,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下
1.新建項(xiàng)目
2. 添加 計(jì)時(shí)器,按鈕組件
<?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="準(zhǔn)備!" ohos:text_color="red" ohos:text_size="50vp" ohos:text_alignment="center" ohos:layout_alignment="center"/> </DirectionalLayout>
3.搶紅包業(yè)務(wù)邏輯
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); // 獲取定時(shí)器組件 TickTimer tickTimer = (TickTimer) findComponentById(ResourceTable.Id_tick_1); //獲取按鈕組件 Button bt = (Button) findComponentById(ResourceTable.Id_bt_1); tickTimer.setCountDown(false); tickTimer.start(); // 10S 準(zhǔn)備時(shí)間 int countDwonTime = 3; tickTimer.setTickListener(tickTimer1 -> { Long aLong = string2Long(tickTimer1.getText()); Long time = countDwonTime - aLong; if (aLong >= 10) { bt.setText(" 恭喜你 搶到 " + money + " 元 "); bt.setMultipleLine(true); //關(guān)閉定時(shí)器 tickTimer.setText(" 00 : 00 "); tickTimer.stop(); return; } if (time <= 0) { bt.setText("點(diǎn)我瘋狂搶紅包"); } else { if (aLong == 0) { } else { bt.setText(" 倒計(jì)時(shí) " + 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.效果演示
到此這篇關(guān)于Java使用定時(shí)器編寫一個(gè)簡單的搶紅包小游戲的文章就介紹到這了,更多相關(guān)Java搶紅包游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Netty分布式ByteBuf使用subPage級(jí)別內(nèi)存分配剖析
這篇文章主要為大家介紹了Netty分布式ByteBuf使用subPage級(jí)別內(nèi)存分配剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03MyBatis使用自定義TypeHandler轉(zhuǎn)換類型的實(shí)現(xiàn)方法
這篇文章主要介紹了MyBatis使用自定義TypeHandler轉(zhuǎn)換類型的實(shí)現(xiàn)方法,本文介紹使用TypeHandler 實(shí)現(xiàn)日期類型的轉(zhuǎn)換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10Java調(diào)用shell命令涉及管道、重定向時(shí)不生效問題及解決
這篇文章主要介紹了Java調(diào)用shell命令涉及管道、重定向時(shí)不生效問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12Java虛擬機(jī)常見內(nèi)存溢出錯(cuò)誤匯總
這篇文章主要匯總了Java虛擬機(jī)常見的內(nèi)存溢出錯(cuò)誤,警示大家,避免出錯(cuò),感興趣的朋友可以了解下2020-09-09SpringBoot集成minio實(shí)現(xiàn)文件上傳和刪除功能
這篇文章主要介紹了SpringBoot集成minio實(shí)現(xiàn)文件上傳和刪除功能,詳細(xì)介紹每個(gè)功能的實(shí)現(xiàn)步驟和代碼示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11