0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

分享一个有趣的鸿蒙分布式小游戏

OpenHarmony技术社区 ? 来源:鸿蒙技术社区 ? 作者:维京战斧 ? 2021-11-01 14:29 ? 次阅读
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

今天给大家分享一个有趣的鸿蒙分布式小游戏:你画我猜。

开发心得(如有错误还请大佬及时指正):

  • 分布式流转:一个 APP 应用在设备之间互相拉起迁移,只在一个终端上运行。

  • 分布式协同:一个 APP 同时在多个设备上运行,画面实时共享,数据实时传输。

在工程创立之后,首先有个很重要的事情那就是验权。

①这个分布式协同会用到一个权限接口,去 MainAbilitySlice 里面申请

分布式数据管理 ohos.permission.DISTRIBUTED_DATASYNC 允许不同设备间的数据交换。
voidgrantPermission(){//获取验证权限数据交互允许不同设备间的数据交换。
if(verifySelfPermission(DISTRIBUTED_DATASYNC)!=IBundleManager.PERMISSION_GRANTED){
if(canRequestPermission(DISTRIBUTED_DATASYNC)){
requestPermissionsFromUser(newString[]{DISTRIBUTED_DATASYNC},PERMISSION_CODE);
}
}
}

权限接口文档链接

https://developer.harmonyos.com/cn/docs/documentation/doc-guides/security-permissions-available-0000001051089272

②权限申请

开发者需要在 config.json 文件中的“reqPermissions”字段中声明所需要的权限。

{
"module":{
"reqPermissions":[
{
"name":"ohos.permission.CAMERA",
"reason":"$string:permreason_camera",
"usedScene":
{
"ability":["com.mycamera.Ability","com.mycamera.AbilityBackground"],
"when":"always"
}
},{
...
}
]
}
}
}

351c9594-3ac5-11ec-82a9-dac502259ad0.png
{
"name":"ohos.permission.DISTRIBUTED_DEVICE_STATE_CHANGE"//允许获取分布式组网内设备的状态变化。
},
{
"name":"ohos.permission.GET_DISTRIBUTED_DEVICE_INFO"//允许获取分布式组网内的设备列表和设备信息。
},
{
"name":"ohos.permission.GRT_BUNDLE_INFO"//查询其他应用的信息。
},
{
"name":"ohos.permission.INTERNET"//允许使用网络socket。
}

应用权限列表文档链接:

https://developer.harmonyos.com/cn/docs/documentation/doc-guides/security-permissions-guidelines-0000000000029886

再看页面结构:

在 resources 下面的:
  • graphic:页面样式效果调配

  • layoput:此 demo 的 java UI 页面布局结构

主页面入口布局代码 ability_main:



<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical"
ohos:background_element="$graphic:background_button">

<Text
ohos:height="match_content"
ohos:width="match_content"
ohos:top_margin="150px"
ohos:layout_alignment="horizontal_center"
ohos:text="你好鸿蒙_你画我猜"
ohos:text_size="38fp"
/>

<Image
ohos:id="$+id:imageComponent"
ohos:height="200vp"
ohos:width="1080"
ohos:top_margin="150px"
ohos:image_src="$media:HM"
/>

<Button
ohos:id="$+id:help_btn"
ohos:height="100vp"
ohos:width="300vp"
ohos:background_element="$graphic:background_button"
ohos:layout_alignment="horizontal_center"
ohos:left_padding="15vp"
ohos:right_padding="15vp"
ohos:text="进入游戏"
ohos:text_size="30vp"
ohos:top_margin="20vp">
Button>


DirectionalLayout>

游戏匹配页面布局 math_game:



<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical">

<Button
ohos:id="$+id:help_btn"
ohos:height="match_content"
ohos:width="500px"
ohos:background_element="$graphic:background_begin"
ohos:layout_alignment="horizontal_center"
ohos:left_padding="15vp"
ohos:right_padding="15vp"
ohos:text="匹配对手"
ohos:text_size="30vp"
ohos:top_margin="200vp">
Button>
<Image
ohos:id="$+id:imageComponent"
ohos:height="200vp"
ohos:width="1080"
ohos:top_margin="100px"
ohos:image_src="$media:NHWC"

/>

DirectionalLayout>

MainAbilitySlice:

packagecom.huawei.codelab.slice;

importstaticohos.security.SystemPermission.DISTRIBUTED_DATASYNC;

importcom.huawei.codelab.ResourceTable;
importcom.huawei.codelab.utils.CommonData;
importcom.huawei.codelab.utils.LogUtil;
importohos.aafwk.ability.AbilitySlice;
importohos.aafwk.content.Intent;
importohos.aafwk.content.Operation;
importohos.agp.components.Component;
importohos.bundle.IBundleManager;



publicclassMainAbilitySliceextendsAbilitySlice{
privatestaticfinalStringTAG=CommonData.TAG+MainAbilitySlice.class.getSimpleName();

privatestaticfinalintPERMISSION_CODE=10000000;

@Override
publicvoidonStart(Intentintent){
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
grantPermission();
initView();
}

voidgrantPermission(){//获取验证权限数据交互允许不同设备间的数据交换。
if(verifySelfPermission(DISTRIBUTED_DATASYNC)!=IBundleManager.PERMISSION_GRANTED){
if(canRequestPermission(DISTRIBUTED_DATASYNC)){
requestPermissionsFromUser(newString[]{DISTRIBUTED_DATASYNC},PERMISSION_CODE);
}
}
}
//
privatevoidinitView(){
findComponentById(ResourceTable.Id_help_btn).setClickedListener(newButtonClick());

}

privatevoidmathGame(){//数学游戏
LogUtil.info(TAG,"ClickResourceTableId_math_game");
IntentmathGameIntent=newIntent();
OperationoperationMath=newIntent.OperationBuilder().withBundleName(getBundleName())
.withAbilityName(CommonData.ABILITY_MAIN)
.withAction(CommonData.MATH_PAGE)
.build();
mathGameIntent.setOperation(operationMath);
startAbility(mathGameIntent);
}

//进入游戏
privateclassButtonClickimplementsComponent.ClickedListener{
@Override
publicvoidonClick(Componentcomponent){
mathGame();
}
}

}

MathGameAbilitySlice:

packagecom.huawei.codelab.slice;
importcom.huawei.codelab.ResourceTable;
importcom.huawei.codelab.devices.SelectDeviceDialog;
importcom.huawei.codelab.utils.CommonData;
importcom.huawei.codelab.utils.LogUtil;
importohos.aafwk.ability.AbilitySlice;
importohos.aafwk.content.Intent;
importohos.aafwk.content.Operation;
importohos.agp.components.Button;
importohos.agp.components.Component;
importohos.data.distributed.common.KvManagerConfig;
importohos.data.distributed.common.KvManagerFactory;
importohos.distributedschedule.interwork.DeviceInfo;
importohos.distributedschedule.interwork.DeviceManager;
importjava.util.ArrayList;
importjava.util.List;


publicclassMathGameAbilitySliceextendsAbilitySlice{
privatestaticfinalStringTAG=CommonData.TAG+MathGameAbilitySlice.class.getSimpleName();

privateButtonhelpBtn;

privateListdevices=newArrayList<>();

@Override
publicvoidonStart(Intentintent){
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_math_game);
initView();

}

privatevoidinitView(){

if(findComponentById(ResourceTable.Id_help_btn)instanceofButton){
helpBtn=(Button)findComponentById(ResourceTable.Id_help_btn);
}
helpBtn.setClickedListener(newButtonClick());
}


privatevoidgetDevices(){
if(devices.size()>0){
devices.clear();
}
ListdeviceInfos=
DeviceManager.getDeviceList(ohos.distributedschedule.interwork.DeviceInfo.FLAG_GET_ONLINE_DEVICE);
LogUtil.info(TAG,"MathGameAbilitySlicedeviceInfossizeis:"+deviceInfos.size());
devices.addAll(deviceInfos);
showDevicesDialog();
}

privatevoidshowDevicesDialog(){
newSelectDeviceDialog(this,devices,deviceInfo->{
startLocalFa(deviceInfo.getDeviceId());
startRemoteFa(deviceInfo.getDeviceId());
}).show();
}

privatevoidstartLocalFa(StringdeviceId){
LogUtil.info(TAG,"startLocalFa......");
Intentintent=newIntent();
intent.setParam(CommonData.KEY_REMOTE_DEVICEID,deviceId);
intent.setParam(CommonData.KEY_IS_LOCAL,true);
Operationoperation=newIntent.OperationBuilder().withBundleName(getBundleName())
.withAbilityName(CommonData.ABILITY_MAIN)
.withAction(CommonData.DRAW_PAGE)
.build();
intent.setOperation(operation);
startAbility(intent);
}

privatevoidstartRemoteFa(StringdeviceId){
LogUtil.info(TAG,"startRemoteFa......");
StringlocalDeviceId=
KvManagerFactory.getInstance().createKvManager(newKvManagerConfig(this)).getLocalDeviceInfo().getId();
Intentintent=newIntent();
intent.setParam(CommonData.KEY_REMOTE_DEVICEID,localDeviceId);
intent.setParam(CommonData.KEY_IS_LOCAL,false);
Operationoperation=newIntent.OperationBuilder().withDeviceId(deviceId)
.withBundleName(getBundleName())
.withAbilityName(CommonData.ABILITY_MAIN)
.withAction(CommonData.DRAW_PAGE)
.withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE)
.build();
intent.setOperation(operation);
startAbility(intent);
}

privateclassButtonClickimplementsComponent.ClickedListener{
@Override
publicvoidonClick(Componentcomponent){
getDevices();//启动机器匹配
}
}
}

责任编辑:haq


声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 鸿蒙系统
    +关注

    关注

    183

    文章

    2642

    浏览量

    68454
  • HarmonyOS
    +关注

    关注

    80

    文章

    2130

    浏览量

    33733

原文标题:鸿蒙版你画我猜,请接招!

文章出处:【微信号:gh_834c4b3d87fe,微信公众号:OpenHarmony技术社区】欢迎添加关注!文章转载请注明出处。

收藏 人收藏
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

    评论

    相关推荐
    热点推荐

    分布式光伏发电监测系统技术方案

    分布式光伏发电监测系统技术方案 柏峰【BF-GFQX】、系统目标 :分布式光伏发电监测系统旨在通过智能化的监测手段,实现对分布式光伏电站的全方位、高精度、实时化管理。该系统能
    的头像 发表于 08-22 10:51 ?138次阅读
    <b class='flag-5'>分布式</b>光伏发电监测系统技术方案

    键部署无损网络:EasyRoCE助力分布式存储效能革命

    分布式存储的性能瓶颈往往在于网络。如何构建高带宽、超低时延、零丢包的无损网络,是释放分布式存储全部潜力、赋能企业关键业务(如实时数据库、AI训练、高性能计算)的关键挑战。
    的头像 发表于 08-04 11:34 ?747次阅读
    <b class='flag-5'>一</b>键部署无损网络:EasyRoCE助力<b class='flag-5'>分布式</b>存储效能革命

    鸿蒙5开发宝藏案例分享---多开发实例(游戏

    十年前藏的现金样惊喜!)这些藏在文档深处的\"武功秘籍\",能帮我们轻松实现分布式游戏、跨端协同这些听起来很酷的功能。快上车,带你解锁鸿蒙开发的正确姿势!
    发表于 06-03 18:22

    使用VirtualLab Fusion中分布式计算的AR波导测试图像模拟

    总计算时间超过31小时。通过使用由8多核PC组成的网络,提供35客户端分布式计算,将模拟时间减少到1小时5分钟。基本模拟任务基本任务
    发表于 04-10 08:48

    分布式云化数据库有哪些类型

    分布式云化数据库有哪些类型?分布式云化数据库主要类型包括:关系型分布式数据库、非关系型分布式数据库、新SQL分布式数据库、以列方式存储数据、
    的头像 发表于 01-15 09:43 ?573次阅读

    AIGC入门及鸿蒙入门

    模型,能够生成与给定文本描述相符的图像。 鸿蒙系统入门 1. 基础知识: 鸿蒙系统(HarmonyOS)是华为推出的分布式操作系统,旨在实现跨设备、跨平台的无缝协同体验。
    发表于 01-13 10:32

    基于ptp的分布式系统设计

    在现代分布式系统中,精确的时间同步对于确保数据致性、系统稳定性和性能至关重要。PTP(Precision Time Protocol)是种网络协议,用于在分布式系统中实现高精度的时
    的头像 发表于 12-29 10:09 ?706次阅读

    HarmonyOS Next 应用元服务开发-分布式数据对象迁移数据权限与基础数据

    ) ?? \'\'); }); } } 在对端UIAbility的onCreate()/onNewWant()中,通过加入与源端致的分布式数据对象组网进行数据恢复。 创建空的分布式数据对象,用于接收
    发表于 12-24 09:40

    FPGA打砖块小游戏设计思路

    ? 交流问题 ? Q :FPGA打砖块小游戏,如何基于FPGA用verilog语言在Vivado平台上写打砖块小游戏,最好能用到PS2与VGA。 A :以下是基于 FPGA? Ve
    的头像 发表于 12-09 16:57 ?1092次阅读

    分布式通信的原理和实现高效分布式通信背后的技术NVLink的演进

    大型模型的大小已经超出了单个 GPU 的范围。所以就需要实现跨多个 GPU 的模型训练,这种训练方式就涉及到了分布式通信和 NVLink。 当谈及分布式通信和 NVLink 时,我们进入了
    的头像 发表于 11-18 09:39 ?1465次阅读
    <b class='flag-5'>分布式</b>通信的原理和实现高效<b class='flag-5'>分布式</b>通信背后的技术NVLink的演进

    分布式光纤测温解决方案

    分布式光纤测温解决方案
    的头像 发表于 11-12 01:02 ?646次阅读
    <b class='flag-5'>分布式</b>光纤测温解决方案

    分布式光纤测温是什么?应用领域是?

    分布式光纤测温是种先进的温度测量技术,它利用光纤的拉曼散射原理进行温度监测。以下是对分布式光纤测温的详细介绍: 、基本原理 分布式光纤测
    的头像 发表于 10-24 15:30 ?1454次阅读
    <b class='flag-5'>分布式</b>光纤测温是什么?应用领域是?

    分布式光纤声波传感技术的工作原理

    分布式光纤声波传感技术(Distributed Acoustic Sensing,DAS)是种利用光纤作为传感元件,实现对沿光纤路径上的环境参数进行连续分布式测量的技术。
    的头像 发表于 10-18 14:50 ?3243次阅读
    <b class='flag-5'>分布式</b>光纤声波传感技术的工作原理

    分布式输电线路故障定位中的分布式是指什么

    所谓分布式指的是产品的部署方式,是相对于集中式而言的。 、部署方式 分散安装:分布式输电线路故障定位系统中的采集装置需要安装在输电线路的多个位置,通常是每隔定距离设置
    的头像 发表于 10-16 11:39 ?769次阅读
    <b class='flag-5'>分布式</b>输电线路故障定位中的<b class='flag-5'>分布式</b>是指什么

    分布式存储费用高吗?大概需要多少钱

    分布式存储的费用是否高,取决于多个因素,包括存储容量、性能要求、服务提供商、计费模式等。因此,无法简单地给出“高”或“不高”的答案。通常分布式存储费用通常包含存储费用、网络费用、增
    的头像 发表于 09-24 10:41 ?661次阅读