我是中工的简向奎,我的鸿蒙大作业是数字华容道,以下是我的作业内容。
一、简介
鸿蒙小游戏APP在手机上的编译在项目中我们所使用到的软件为DevEco Studio,在项目中我们要实现的内容为数字华容道APP的开发。
1.1 首先打开并进入小游戏,来到初始界面,点击开始游戏。
1.2 进入游戏,进入小游戏界面会显示4*4的方阵,方阵中分布有已经打乱的1到15的数字和一个空白方格,界面下方会显示一个“重新开始”和一个“返回”按钮,点击“重新开始”按钮即会重新生成随机打乱的1到15的数字和一个空白方格的方阵,点击“返回”就会返回到小游戏初始界面,最下方有四个指向不同方向箭头的按钮,点击下方方向箭头的按钮,方阵内的数字方块就会向对应方向移动。
1.3 游戏结束,经过不断的移动数字方块,当游戏胜利后,界面上面就会出现”游戏成功“的四个字,此时游戏就会结束,点击重新开始会重新生成新的对局。
二、正文
2.1 在entry>src>main>config.json文件中最下方"launchType": “standard"的后面添加以下代码,并且将上方的“label”:“MyPhoneApplication”修改成"label”: “数字华容道”,这样就实现去掉应用上方的标题栏和将应用名称改为数字华容道了。
部分代码:
"orientation": "unspecified",
"name": "com.example.myphoneapplication.MainAbility",
"icon": "$media:icon",
"description": "$string:mainability_description",
"label": "数字华容道",
"type": "page",
"launchType": "standard",
"metaData": {
"customizeData": [
{
"name": "hwc-theme",
"value": "androidhwext:style/Theme.Emui.Light.NoTitleBar",
"extra": ""
}
]
}
2.2 在entry>src>main>resources>base>layout>ability_main.xml中添加布局,先将事先存在的Text组件删去,添加Image图片组件,引入我们刚才复制粘贴的图片,再添加一个Button按钮组件,加入唯一标识符id并配置好其他相应的属性。
部分代码:
<?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:orientation="vertical">
<Image
ohos:height="match_parent"
ohos:width="match_parent"
ohos:image_src="$media:game"
ohos:layout_alignment="center"
/>
<Button
ohos:id="$+id:button_game"
ohos:height="150"
ohos:width="515"
ohos:text_alignment="center"
ohos:top_margin="-810"
ohos:left_margin="250"
/>
</DirectionalLayout>
2.3 在entry>src>main>java>com.example.myphoneapplication>slice>MainAbilitySlice中的onStart函数中添加一个按钮指向我们(2)中添加的按钮,按钮添加一个响应点击事件的函数,用parsent函数跳转到SecondAbilitySlice
部分代码:
package com.example.myphoneapplication.slice;
import com.example.myphoneapplication.ResourceTable;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
public class MainAbilitySlice extends ohos.aafwk.ability.AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
Button button = (Button) findComponentById(ResourceTable.Id_button_game);
button.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
present(new SecondAbilitySlice(),new Intent());
}
});
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
2.4 数字打乱,定义函数createGrids(int[][] grids)用于随机生成一个表示方向的数字,循环调用函数changeGrids(grids,direction)用于随机打乱二维数组对应的数字。
部分代码:
public void createGrids(int[][] grids){
int[] array = {-1,-2,1,2};
for(int i = 0; i < 100; i++){
int random = (int)Math.floor(Math.random()*4);
int direction = array[random];
changeGrids(grids,direction);
}
}
最后在initialize()函数中调用createGrids(grids)函数和drawGrids(grids)函数
public void initialize(){
layout = new DirectionalLayout(this);
grids = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8,}, {9, 10, 11, 12}, {13, 14, 15, 0}};
createGrids(grids);
drawGrids(grids);
}
2.5 定义一个函数gameover()用于判断二维数组的数字是否按顺序排列,当二维数组的数字按顺序排列时返回true,否则返回false
部分代码:
public boolean gameover() {
int[][] gameoverGrids = {{1, 2, 3, 4}, {5, 6, 7, 8,}, {9, 10, 11, 12}, {13, 14, 15, 0}};
for (int row = 0; row < 4; row++) {
for (int column = 0; column < 4; column++) {
if (grids[row][column] != gameoverGrids[row][column]) {
return false;
}
}
}
return true;
}