成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

HarmonyOS小游戲項目—數獨Sudoku(七)

系統 OpenHarmony
在本期的內容中,我們會為此游戲添加計時功能,同時接入數據庫保存通關記錄。完成這些后,數獨游戲項目便也竣工了。

??想了解更多關于開源的內容,請訪問:??

??51CTO 開源基礎軟件社區??

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

前言

在本期的內容中,我們會為此游戲添加計時功能,同時接入數據庫保存通關記錄。完成這些后,數獨游戲項目便也竣工了。

正文

創建退出按鈕與秒表

首先,我們需要在項目內導入圖片:打開:entry\src\main\resources\base\media,將如下圖片放置在media目錄下(可以以復制粘貼的方式放置):

【木棉花】#打卡不停更#HarmonyOS小游戲項目——數獨Sudoku(7)-開源基礎軟件社區

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

//退出的彈窗
CommonDialog Dialog_exit=new CommonDialog(getContext());
Dialog_exit.setSize(800,400);
Dialog_exit.setTitleText(" 提示");
Dialog_exit.setContentText(" 游戲未完成,確認退出?");
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() { //組件銷毀監聽器
terminate(); //當彈窗被銷毀后,GameAbilitySlice也會被銷毀,以實現頁面退出的功能
}
});
//創建Image對象,并設置點擊監聽器
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(); //點擊Image組件后,Dialog_exit會彈出
}
});
layout1.addComponent(exit);

這里我們是先創建Dialog對象,再創建Image對象的,原因是:只有Dialog對象先生成,Image的點擊監聽器內才能調用已創建的Dialog對象的方法。

此時我們打開模擬機并隨機進入一個關卡,可以看到,頁面的左上角多了一個可點擊圖標:

【木棉花】#打卡不停更#HarmonyOS小游戲項目——數獨Sudoku(7)-開源基礎軟件社區

圖標被點擊后,系統會彈出一個提示彈窗:

【木棉花】#打卡不停更#HarmonyOS小游戲項目——數獨Sudoku(7)-開源基礎軟件社區

計時器

成功加入退出的功能后,接下來要做的就是在游戲界面加入一個計時器。

首先,我們在合適的位置編寫一個run()函數:

import ......
public class GameAbilitySlice extends AbilitySlice {
......
//同步計時器,用于輸出時間
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);
......

加入計時器的功能:

@Override
protected void onStart(Intent intent) {
super.onStart(intent);
......
//計時器
Text time=new Text(this);
time.setText("用時:");
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);

數據庫

首先實現頁面設計。

在graphic目錄下,創建一個背景元素文件,并命名為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目錄下,創建一個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);
}
}

然后,在合適的位置定義一個數據庫:

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

接著,設計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("初級" + " " + "無記錄");
}else {
text1.setText("初級" + " " + String.valueOf(Min1) + "分" + String.valueOf(Sec1) + "秒");
}
Text text2 = (Text) findComponentById(ResourceTable.Id_text_record2);
if (Min2==999){
text2.setText("中級" + " " + "無記錄");
}else {
text2.setText("中級" + " " + String.valueOf(Min2) + "分" + String.valueOf(Sec2) + "秒");
}
Text text3 = (Text) findComponentById(ResourceTable.Id_text_record3);
if (Min3==999){
text3.setText("高級" + " " + "無記錄");
}else {
text3.setText("高級" + " " + 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,實現“游戲記錄”按鈕的頁面導航的邏輯:

@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());
}
});
}

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

【木棉花】#打卡不停更#HarmonyOS小游戲項目——數獨Sudoku(7)-開源基礎軟件社區

打開GameAbilitySlice,先定義一些對象:

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

之后,創建一個輕量級數據庫:

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

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

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

找到button_pr,完成記錄時間的邏輯:

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

找到對話框,加入指令:

//對話框
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() {
//銷毀監聽器內加入指令
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();//清除緩沖區的緩存
}
}
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(); //清除緩沖區的緩存
}
}
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小游戲項目——數獨Sudoku(7)-開源基礎軟件社區

結語

恭喜你,你已經成功在IDE制作了一個小游戲項目。

??想了解更多關于開源的內容,請訪問:??

??51CTO 開源基礎軟件社區??

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

責任編輯:jianghua 來源: 51CTO開源基礎軟件社區
相關推薦

2022-07-29 14:47:34

數獨Sudoku鴻蒙

2022-10-19 15:19:53

數獨Sudoku鴻蒙

2022-10-18 15:45:17

數獨Sudoku鴻蒙

2013-06-17 12:44:38

WP7開發Windows Pho數獨游戲

2021-08-23 11:03:54

鴻蒙HarmonyOS應用

2023-08-07 15:18:29

游戲開發鴻蒙Arkts

2011-09-16 10:35:13

Android應用數獨經典游戲

2024-07-31 09:46:13

2023-11-06 11:33:15

C++數獨

2021-09-06 08:26:08

JavaScript數獨 LeetCode

2022-03-24 08:33:58

小游戲項目cmdvue3

2022-08-25 21:41:43

ArkUI鴻蒙

2022-11-01 15:17:48

JS鴻蒙小游戲

2021-01-12 12:16:55

鴻蒙HarmonyOS游戲

2021-01-15 12:15:36

鴻蒙HarmonyOS游戲

2015-09-29 09:38:50

Java程序猜大小

2022-07-08 14:53:46

掃雷小游戲鴻蒙

2012-01-10 12:48:52

Java

2024-11-06 16:45:39

Python游戲開發代碼

2009-04-12 08:52:52

Symbian諾基亞移動OS
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲成人第一页 | 在线视频一区二区三区 | 日韩毛片网 | 久久人人网 | 91精品国产色综合久久不卡98 | 久久久亚洲精品视频 | 国产精品999 | 99在线视频观看 | 久久小视频 | 九九国产在线观看 | 91社区在线观看播放 | 久久国产欧美日韩精品 | 国产成人免费一区二区60岁 | 欧美一级在线免费观看 | 精品久久久一区二区 | a级毛片免费高清视频 | 91精品一区二区三区久久久久 | 成人精品在线观看 | 亚洲成人免费视频在线观看 | 精品一区电影 | 91精品国产高清一区二区三区 | 97高清国语自产拍 | 国产视频91在线 | 日韩精品成人av | 精品国产精品国产偷麻豆 | 高清黄色| 91直接看| 成人天堂噜噜噜 | 午夜激情一区 | 成年人免费在线视频 | 国产乱码精品一区二区三区忘忧草 | 亚洲精品久久久一区二区三区 | 欧美一级欧美三级在线观看 | 99国产精品99久久久久久 | 久久国产精品久久国产精品 | 久久久久免费 | 国产一二区视频 | 亚洲精品视频一区二区三区 | av一级一片 | 天堂在线中文字幕 | 国产精品永久免费视频 |