HarmonyOS小游戲項目—數獨Sudoku(七)
前言
在本期的內容中,我們會為此游戲添加計時功能,同時接入數據庫保存通關記錄。完成這些后,數獨游戲項目便也竣工了。
正文
創建退出按鈕與秒表
首先,我們需要在項目內導入圖片:打開:entry\src\main\resources\base\media,將如下圖片放置在media目錄下(可以以復制粘貼的方式放置):
放置完成后,我們打開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對象的方法。
此時我們打開模擬機并隨機進入一個關卡,可以看到,頁面的左上角多了一個可點擊圖標:
圖標被點擊后,系統會彈出一個提示彈窗:
計時器
成功加入退出的功能后,接下來要做的就是在游戲界面加入一個計時器。
首先,我們在合適的位置編寫一個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效果:
打開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();
}
});
完成上述操作后,打開模擬器試玩游戲:
結語
恭喜你,你已經成功在IDE制作了一個小游戲項目。