Java使用定時(shí)器編寫一個(gè)簡(jiǎn)單的搶紅包小游戲
更新時(shí)間:2022年07月01日 14:56:50 作者:秋日的晚霞
這篇文章主要為大家介紹了Java如何使用定時(shí)器編寫一個(gè)簡(jiǎn)單的搶紅包小游戲,文中的示例代碼講解詳細(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è)簡(jiǎn)單的搶紅包小游戲的文章就介紹到這了,更多相關(guān)Java搶紅包游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java多線程之synchronized關(guān)鍵字的使用
這篇文章主要介紹了Java多線程之synchronized關(guān)鍵字的使用,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
Springboot接收?Form?表單數(shù)據(jù)的示例詳解
這篇文章主要介紹了Springboot接收?Form?表單數(shù)據(jù)的實(shí)例代碼,本文通過圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
Java中的強(qiáng)制類型轉(zhuǎn)換 大數(shù)轉(zhuǎn)小數(shù)
這里主要討論一下大數(shù)轉(zhuǎn)小數(shù),比如int類型轉(zhuǎn)short類型。小數(shù)轉(zhuǎn)大數(shù),如short 轉(zhuǎn) int不做討論,需要的朋友可以參考下2020-02-02
Java基礎(chǔ)之動(dòng)態(tài)代理Cglib詳解
這篇文章主要介紹了Java基礎(chǔ)之動(dòng)態(tài)代理Cglib詳解,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-05-05
spring boot整合mybatis使用c3p0數(shù)據(jù)源連接mysql
這篇文章主要為大家詳細(xì)介紹了spring boot整合mybatis使用c3p0數(shù)據(jù)源連接mysql,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03

