自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

HarmonyOS小游戲項(xiàng)目—數(shù)獨(dú)Sudoku(七)

系統(tǒng) OpenHarmony
在本期的內(nèi)容中,我們會(huì)為此游戲添加計(jì)時(shí)功能,同時(shí)接入數(shù)據(jù)庫(kù)保存通關(guān)記錄。完成這些后,數(shù)獨(dú)游戲項(xiàng)目便也竣工了。

??想了解更多關(guān)于開源的內(nèi)容,請(qǐng)?jiān)L問:??

??51CTO 開源基礎(chǔ)軟件社區(qū)??

??https://ost.51cto.com??

前言

在本期的內(nèi)容中,我們會(huì)為此游戲添加計(jì)時(shí)功能,同時(shí)接入數(shù)據(jù)庫(kù)保存通關(guān)記錄。完成這些后,數(shù)獨(dú)游戲項(xiàng)目便也竣工了。

正文

創(chuàng)建退出按鈕與秒表

首先,我們需要在項(xiàng)目?jī)?nèi)導(dǎo)入圖片:打開:entry\src\main\resources\base\media,將如下圖片放置在media目錄下(可以以復(fù)制粘貼的方式放置):

【木棉花】#打卡不停更#HarmonyOS小游戲項(xiàng)目——數(shù)獨(dú)Sudoku(7)-開源基礎(chǔ)軟件社區(qū)

放置完成后,我們打開GameAbilitySlice,并在onstart()函數(shù)的合適位置放置如下代碼:

//退出的彈窗
CommonDialog Dialog_exit=new CommonDialog(getContext());
Dialog_exit.setSize(800,400);
Dialog_exit.setTitleText(" 提示");
Dialog_exit.setContentText(" 游戲未完成,確認(rèn)退出?");
Dialog_exit.setButton(IDialog.BUTTON1,"確定",(iDialog, i) ->Dialog_exit.destroy() );
Dialog_exit.setButton(IDialog.BUTTON2,"取消",(iDialog, i) ->Dialog_exit.hide() );
Dialog_exit.setDestroyedListener(new CommonDialog.DestroyedListener() {
@Override
public void onDestroy() { //組件銷毀監(jiān)聽器
terminate(); //當(dāng)彈窗被銷毀后,GameAbilitySlice也會(huì)被銷毀,以實(shí)現(xiàn)頁(yè)面退出的功能
}
});
//創(chuàng)建Image對(duì)象,并設(shè)置點(diǎn)擊監(jiān)聽器
Image exit=new Image(this);
exit.setPixelMap(ResourceTable.Media_exit);
exit.setScaleMode(Image.ScaleMode.CLIP_CENTER);
exit.setComponentSize(130,130);
exit.setPosition(50,25);
exit.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
Dialog_exit.show(); //點(diǎn)擊Image組件后,Dialog_exit會(huì)彈出
}
});
layout1.addComponent(exit);

這里我們是先創(chuàng)建Dialog對(duì)象,再創(chuàng)建Image對(duì)象的,原因是:只有Dialog對(duì)象先生成,Image的點(diǎn)擊監(jiān)聽器內(nèi)才能調(diào)用已創(chuàng)建的Dialog對(duì)象的方法。

此時(shí)我們打開模擬機(jī)并隨機(jī)進(jìn)入一個(gè)關(guān)卡,可以看到,頁(yè)面的左上角多了一個(gè)可點(diǎn)擊圖標(biāo):

【木棉花】#打卡不停更#HarmonyOS小游戲項(xiàng)目——數(shù)獨(dú)Sudoku(7)-開源基礎(chǔ)軟件社區(qū)

圖標(biāo)被點(diǎn)擊后,系統(tǒng)會(huì)彈出一個(gè)提示彈窗:

【木棉花】#打卡不停更#HarmonyOS小游戲項(xiàng)目——數(shù)獨(dú)Sudoku(7)-開源基礎(chǔ)軟件社區(qū)

計(jì)時(shí)器

成功加入退出的功能后,接下來要做的就是在游戲界面加入一個(gè)計(jì)時(shí)器。

首先,我們?cè)诤线m的位置編寫一個(gè)run()函數(shù):

import ......
public class GameAbilitySlice extends AbilitySlice {
......
//同步計(jì)時(shí)器,用于輸出時(shí)間
private Timer timer;
int sec,min;
public void running(){
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
getUITaskDispatcher().asyncDispatch(()->{
sec++;
if (sec >= 60) {
min++;
sec = sec % 60;
if (min >= 60) {
min = min % 60;
}
}
});
}
},0,1000);
}
......
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
......

加入計(jì)時(shí)器的功能:

@Override
protected void onStart(Intent intent) {
super.onStart(intent);
......
//計(jì)時(shí)器
Text time=new Text(this);
time.setText("用時(shí):");
time.setTextSize(75);
time.setTextColor(Color.RED);
time.setPosition(260+360,40);
layout1.addComponent(time);
TickTimer ticktimer=new TickTimer(this);
ticktimer.start();
running();
ticktimer.setTextSize(75);
ticktimer.setTextColor(Color.BLACK);
ticktimer.setPosition(465+360,40);
layout1.addComponent(ticktimer);

數(shù)據(jù)庫(kù)

首先實(shí)現(xiàn)頁(yè)面設(shè)計(jì)。

在graphic目錄下,創(chuàng)建一個(gè)背景元素文件,并命名為background_ability_record.xml,之后加入如下代碼:

<?xml version="1.0" encoding="UTF-8" ?>
<shape
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:shape="rectangle">
<solid
ohos:color="#FFFFFF"/>
</shape>

在layout目錄下,創(chuàng)建一個(gè)xml文件,并命名為background_ability.xml,之后加入如下代碼:

<?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">
<Text
ohos:id="$+id:text_record1"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="$graphic:background_ability_record"
ohos:layout_alignment="horizontal_center"
ohos:text_size="30vp"
ohos:margin="10vp"
/>
<Text
ohos:id="$+id:text_record2"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="$graphic:background_ability_record"
ohos:layout_alignment="horizontal_center"
ohos:text_size="30vp"
ohos:margin="10vp"
/>
<Text
ohos:id="$+id:text_record3"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="$graphic:background_ability_record"
ohos:layout_alignment="horizontal_center"
ohos:text_size="30vp"
ohos:margin="10vp"
/>
<Text
ohos:background_element="$graphic:select_text_exit"
ohos:id="$+id:back_of_record"
ohos:height="match_content"
ohos:width="match_parent"
ohos:text="返回"
ohos:top_margin="90vp"
ohos:text_alignment="horizontal_center"
ohos:text_size="30vp"
ohos:text_font="sans-serif"/>
</DirectionalLayout>

打開RecordAbilitySlice,制作基本的AbilitySlice框架:

import ...
public class RecordAbilitySlice extends AbilitySlice { //繼承
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_record); //與xmi文件綁定
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}

然后,在合適的位置定義一個(gè)數(shù)據(jù)庫(kù):

public class RecondAbilitySlice extends AbilitySlice {  
private Context context; //
//定義數(shù)據(jù)庫(kù)
private Preferences center(){
DatabaseHelper databaseHelper=new DatabaseHelper(this);
Preferences preferences=databaseHelper.getPreferences("DBM");
return preferences;
}
@Override
public void onStart(Intent intent) {
......

接著,設(shè)計(jì)UI界面:

......
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_record); //與xmi文件綁定
Preferences preferences=center();
int Min1=preferences.getInt("MIN_1",999);
int Sec1=preferences.getInt("SEC_1",999);
int Min2=preferences.getInt("MIN_2",999);
int Sec2=preferences.getInt("SEC_2",999);
int Min3=preferences.getInt("MIN_3",999);
int Sec3=preferences.getInt("SEC_3",999);
Text text1 = (Text) findComponentById(ResourceTable.Id_text_record1);
if (Min1==999){
text1.setText("初級(jí)" + " " + "無記錄");
}else {
text1.setText("初級(jí)" + " " + String.valueOf(Min1) + "分" + String.valueOf(Sec1) + "秒");
}
Text text2 = (Text) findComponentById(ResourceTable.Id_text_record2);
if (Min2==999){
text2.setText("中級(jí)" + " " + "無記錄");
}else {
text2.setText("中級(jí)" + " " + String.valueOf(Min2) + "分" + String.valueOf(Sec2) + "秒");
}
Text text3 = (Text) findComponentById(ResourceTable.Id_text_record3);
if (Min3==999){
text3.setText("高級(jí)" + " " + "無記錄");
}else {
text3.setText("高級(jí)" + " " + String.valueOf(Min3) + "分" + String.valueOf(Sec3) + "秒");
}
Text text4=(Text) findComponentById(ResourceTable.Id_back_of_record);
text4.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
terminate();
}
});
}
......

最后打開MainAbilitySlice,實(shí)現(xiàn)“游戲記錄”按鈕的頁(yè)面導(dǎo)航的邏輯:

@Override
public void onActive() {
super.onActive();
......
Button button2=(Button)findComponentById(ResourceTable.Id_record);
button2.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
present(new RecordAbilitySlice(),new Intent());
}
});
}

打開模擬器,在主界面點(diǎn)擊“游戲記錄”,可查看UI效果:

【木棉花】#打卡不停更#HarmonyOS小游戲項(xiàng)目——數(shù)獨(dú)Sudoku(7)-開源基礎(chǔ)軟件社區(qū)

打開GameAbilitySlice,先定義一些對(duì)象:

public class GameAbilitySlice extends AbilitySlice {
......
private Context context;//
private DatabaseHelper databaseHelper;//
private Preferences preferences;//
....
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
......

之后,創(chuàng)建一個(gè)輕量級(jí)數(shù)據(jù)庫(kù):

......
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
//數(shù)據(jù)庫(kù)
context=getContext();
databaseHelper=new DatabaseHelper(context);
preferences=databaseHelper.getPreferences("DBM");
......

制作一個(gè)返回布爾值的比較函數(shù)compare():

int Min_record;
int Sec_record;
private int strsec;
private int strmin;
private String timing;
//比較函數(shù)
private boolean compare(){
int nowtime=strmin+strsec*60;
int mintime= Min_record +Sec_record*60;
if(nowtime<mintime){
return true;
}
return false;
}

找到button_pr,完成記錄時(shí)間的邏輯:

Button button_pr=new Button(this);
......
button_pr.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
if (Gamesuccess()){
ticktimer.stop(); //讓計(jì)時(shí)器停止
timing=ticktimer.getText();//獲得通關(guān)時(shí)間
strsec=sec;
strmin=min;
Dialog_win.setContentText(" 用時(shí):"+timing);//讓通過時(shí)間在彈窗中顯示
Dialog_win.show();
}else {
ticktimer.stop(); //讓計(jì)時(shí)器停止
Dialog_fail.setContentText(" 游戲未完成或答案不正確" );
Dialog_fail.show();
}
}
});
......

找到對(duì)話框,加入指令:

//對(duì)話框
CommonDialog Dialog_win=new CommonDialog(getContext());
Dialog_win.setSize(800,400);
Dialog_win.setTitleText(" 游戲解答成功!");
Dialog_win.setButton(IDialog.BUTTON1,"返回主菜單",(iDialog, i) -> Dialog_win.destroy());
Dialog_win.setDestroyedListener(new CommonDialog.DestroyedListener() {
@Override
public void onDestroy() {
//銷毀監(jiān)聽器內(nèi)加入指令
if(L==1){
Min_record=preferences.getInt("MIN_1",60);
Sec_record=preferences.getInt("SEC_1",60);
if (compare()) {
preferences.putInt("MIN_1", strmin);
preferences.putInt("SEC_1",strsec);
preferences.flush();//清除緩沖區(qū)的緩存
}
}
if(L==2||L==3){
Min_record=preferences.getInt("MIN_2",60);
Sec_record=preferences.getInt("SEC_2",60);
if (compare()) {
preferences.putInt("MIN_2", strmin);
preferences.putInt("SEC_2",strsec);
preferences.flush(); //清除緩沖區(qū)的緩存
}
}
if(L==4){
Min_record=preferences.getInt("MIN_3",60);
Sec_record=preferences.getInt("SEC_3",60);
if (compare()) {

preferences.putInt("MIN_3", strmin);
preferences.putInt("SEC_3",strsec);
preferences.flush();
}
}
terminate();
}
});

完成上述操作后,打開模擬器試玩游戲:

【木棉花】#打卡不停更#HarmonyOS小游戲項(xiàng)目——數(shù)獨(dú)Sudoku(7)-開源基礎(chǔ)軟件社區(qū)

結(jié)語

恭喜你,你已經(jīng)成功在IDE制作了一個(gè)小游戲項(xiàng)目。

??想了解更多關(guān)于開源的內(nèi)容,請(qǐng)?jiān)L問:??

??51CTO 開源基礎(chǔ)軟件社區(qū)??

??https://ost.51cto.com??。

責(zé)任編輯:jianghua 來源: 51CTO開源基礎(chǔ)軟件社區(qū)
相關(guān)推薦

2022-07-29 14:47:34

數(shù)獨(dú)Sudoku鴻蒙

2022-10-19 15:19:53

數(shù)獨(dú)Sudoku鴻蒙

2022-10-18 15:45:17

數(shù)獨(dú)Sudoku鴻蒙

2021-08-23 11:03:54

鴻蒙HarmonyOS應(yīng)用

2013-06-17 12:44:38

WP7開發(fā)Windows Pho數(shù)獨(dú)游戲

2023-08-07 15:18:29

游戲開發(fā)鴻蒙Arkts

2024-07-31 09:46:13

2011-09-16 10:35:13

Android應(yīng)用數(shù)獨(dú)經(jīng)典游戲

2022-03-24 08:33:58

小游戲項(xiàng)目cmdvue3

2021-01-15 12:15:36

鴻蒙HarmonyOS游戲

2022-11-01 15:17:48

JS鴻蒙小游戲

2022-08-25 21:41:43

ArkUI鴻蒙

2021-01-12 12:16:55

鴻蒙HarmonyOS游戲

2012-01-10 12:48:52

Java

2023-11-06 11:33:15

C++數(shù)獨(dú)

2021-09-06 08:26:08

JavaScript數(shù)獨(dú) LeetCode

2015-09-29 09:38:50

Java程序猜大小

2022-07-08 14:53:46

掃雷小游戲鴻蒙

2024-11-06 16:45:39

Python游戲開發(fā)代碼

2022-02-11 14:39:11

游戲JS鴻蒙
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)