电子说
基于TS扩展的声明式开发范式编程语言,以及OpenHarmony的分布式能力实现的一个手柄游戏。
完成本篇Codelab需要两台开发板,一台开发板作为游戏端,一台开发板作为手柄端,实现如下功能:
最终效果图如下:
完成本篇Codelab我们首先要完成开发环境的搭建,本示例以RK3568开发板为例,参照以下步骤进行:
qr23.cn/AKFP8k
]鸿蒙next文档籽料+mau123789直接去v拿取
本章节以系统自带的音乐播放器为例(具体以实际的应用为准),介绍如何完成两台设备的分布式组网。
硬件准备:准备两台烧录相同的版本系统的RK3568开发板A、B。
开发板A、B连接同一个WiFi网络。
打开设置-->WLAN-->点击右侧WiFi开关-->点击目标WiFi并输入密码。
将设备A,B设置为互相信任的设备。
配网完毕。
本篇Codelab只对核心代码进行讲解,对于完整代码,我们会在参考章节中提供下载方式,首先介绍一下整个工程的代码结构:
└── HandleGameApplication
│── GameEtsOpenHarmony
│
└── HandleEtsOpenHarmony
其中HandleEtsOpenHarmony为手柄端工程代码,GameEtsOpenHarmony为游戏端工程代码。
@Entry
@Component
struct Index {
...
build() {
Stack() {
...
Text('score:' + this.score)
...
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
Stack() {
Image('/common/images/bigcircle.png')
.width(300)
.height(300)
Image('/common/images/smallcircle.png')
.width(140)
.height(140)
.position({ x: this.smallPosX, y: this.smallPosY }) // 30+75-35
}
...
Row() {
Image('/common/images/a.png')
.width(160)
.height(160)
.margin({ right: 20, bottom: 80 })
Image('/common/images/b.png')
.width(200)
.height(200)
}.alignItems(VerticalAlign.Bottom)
...
}
}
}
onTouchEvent(event: TouchEvent) {
switch (event.type) {
case TouchType.Down:
this.startX = event.touches[0].screenX;
this.startY = event.touches[0].screenY;
break;
case TouchType.Move:
this.curX = event.touches[0].screenX;
this.curY = event.touches[0].screenY;
this.getSmallCurrentPos(this.curX - this.smallR - 60, this.curY - this.smallR - 60)
angle = Math.round(this.calculateAngle());
break;
default:
break;
}
}
calculateAngle() {
var angle = 0
var degree = Math.atan(this.getDisAbsY() / this.getDisAbsX()) * 180 / Math.PI
var quadrant = this.quadrant();
switch (quadrant) {
case this.QUADRANT_1:
// 向右上移动
angle = degree;
break;
case this.QUADRANT_2:
// 向左上移动
angle = 180 - degree;
break;
case this.QUADRANT_3:
// 向左下移动
angle = -180 + degree;
break;
case this.QUADRANT_4:
// 向右下移动
angle = -degree;
break;
default:
angle = 0;
break;
}
return angle;
}
aboutToAppear() {
// 当被拉起时,通过want传递的参数同步对端界面UI
await featureAbility.getWant((error, want) = > {
// 远端被拉起后,连接游戏端的service
if (want.parameters.deviceId) {
let remoteDeviceId = want.parameters.deviceId
connectRemoteService(remoteDeviceId)
}
});
}
async function connectRemoteService(deviceId) {
...
await featureAbility.connectAbility(
{
'deviceId': deviceId,
'bundleName': "com.huawei.cookbook",
'abilityName': "com.huawei.cookbook.ServiceAbility",
},
{
onConnect: onConnectCallback,
onDisconnect: onDisconnectCallback,
onFailed: onFailedCallback,
},
);
}
async function sendMessageToRemoteService() {
...
let option = new rpc.MessageOption();
let data = new rpc.MessageParcel();
let reply = new rpc.MessageParcel();
data.writeInt(actionType);
data.writeInt(angle);
await mRemote.sendRequest(1, data, reply, option);
}
@Entry
@Component
struct Index {
build() {
Stack() {
...
ForEach(this.bullets, item = > {
Image(item.imgSrc)
.width(item.imgWidth)
.height(item.imgHeight)
.position({ x: item.positionX, y: item.positionY })
}, item = > item.timestamp.toString())
ForEach(this.enemyPlanes, item = > {
Image(item.imgSrc)
.width(item.imgWidth)
.height(item.imgHeight)
.position({ x: item.positionX, y: item.positionY })
}, item = > item.timestamp.toString())
Image('/common/images/planeOne.png')
.width(this.planeSize)
.height(this.planeSize)
.position({ x: this.planePosX, y: this.planePosY })
.onTouch((event: TouchEvent) = > {
this.onTouchEvent(event)
})
Image('/common/images/props.png')
.width(this.propsSize)
.height(this.propsSize)
.position({ x: this.propsPosX, y: this.propsPosY })
...
}
.height('100%')
.width('100%')
}
}
startGame() {
var that = this
setInterval(function () {
// 每60*16ms创建一个敌机
if (that.num % 60 == 0) {
that.createEnemyPlane()
}
// 移动子弹
var bulletsTemp: GameElement[] = []
for (var i = 0; i < that.bullets.length; i++) {
var bullet = that.bullets[i]
bullet.positionY -= 8
// 当子弹移除屏幕外的时候,释放掉
if (bullet.positionY > 0) {
bulletsTemp.push(bullet)
}
}
that.bullets = bulletsTemp
// 移动飞机
var enemyPlanesTemp: GameElement[] = []
for (var j = 0; j < that.enemyPlanes.length; j++) {
var enemyPlane = that.enemyPlanes[j]
enemyPlane.positionY += 6
// 当飞机移除屏幕外的时候,释放掉
if (enemyPlane.positionY < that.screenHeight) {
enemyPlanesTemp.push(enemyPlane)
}
}
that.enemyPlanes = enemyPlanesTemp
// 每隔 500*16ms显示降落伞
if (that.num % 500 == 0) {
that.getPropsFlag = true
that.propsPosY = -that.propsSize
that.propsPosX = Math.round((Math.random() * (that.screenWidth - that.propsSize)))
}
// 刷新道具位置
if (that.propsPosY < that.screenHeight) {
that.propsPosY += 6
}
that.checkCollision()
}, 16);
}
checkCollision() {
...
for (var i = 0; i < this.enemyPlanes.length; i++) {
var enemy = this.enemyPlanes[i];
for (var j = 0; j < this.bullets.length; j++) {
var bullet = this.bullets[j];
var inside = this.isInside(bullet, enemy);
// 发生碰撞
if (inside) {
enemy.imgSrc = '/common/images/boom.png'
if (enemy.flag == 1) {
this.score += 50
sendMessageToRemoteService(that.score)
} else if (enemy.flag == 2) {
this.score += 100
sendMessageToRemoteService(that.score)
}
// 清除子弹
this.enemyPlanes.splice(i, 1);
i--;
enemy.flag = 3
// 清除被子弹打中敌机
that.bullets.splice(j, 1);
j--;
}
}
}
// 飞机和降落伞是否发生碰撞
var isGetProps = this.isInside(myPlane, props);
if (isGetProps && this.getPropsFlag) {
this.getPropsFlag = false
this.bombNum++
this.propsPosY = 2000
}
}
@CustomDialog
export struct DeviceListDialog {
controller: CustomDialogController
build() {
Column() {
Text("选择设备")
.fontWeight(FontWeight.Bold)
.fontSize(20)
.margin({ top: 20, bottom: 10 })
List() {
ForEach(deviceList, item = > {
ListItem() {
Stack() {
Text(item)
.fontSize(12)
.margin({ top: 10 })
}
.onClick(() = > {
startRemoteAbility(item)
this.controller.close();
})
.padding({ left: 30, right: 30 })
}
}, item = > item.toString())
}
.height("30%")
.align(Alignment.TopStart)
...
}
}
}
function startRemoteAbility(deviceId) {
var params = {
deviceId: localDeviceId
}
var wantValue = {
bundleName: 'com.huawei.cookbook',
abilityName: 'com.huawei.cookbook.MainAbility',
deviceId: deviceId,
parameters: params
};
featureAbility.startAbility({
want: wantValue
}).then((data) = > {
console.info('[game] featureAbility.startAbility finished, localDeviceId=' + localDeviceId + '----deviceId:' + deviceId);
// 拉起远端后,连接远端service
connectRemoteService(deviceId)
});
}
async function connectRemoteService(deviceId) {
// 连接成功的回调
async function onConnectCallback(element, remote) {
mRemote = remote;
}
...
if (remoteDeviceModel.deviceList.length === 0) {
return;
}
await featureAbility.connectAbility(
{
'deviceId': deviceId,
'bundleName': "com.huawei.cookbook",
'abilityName': "com.huawei.cookbook.ServiceAbility",
},
{
onConnect: onConnectCallback,
onDisconnect: onDisconnectCallback,
onFailed: onFailedCallback,
},
);
}
async function sendMessageToRemoteService(score) {
console.log('[game]connectRemoteService sendMessageToRemoteService:')
if (mRemote == null) {
return;
}
let option = new rpc.MessageOption();
let data = new rpc.MessageParcel();
let reply = new rpc.MessageParcel();
data.writeInt(score);
await mRemote.sendRequest(1, data, reply, option);
}
class GameServiceAbilityStub extends rpc.RemoteObject {
...
onRemoteRequest(code, data, reply, option) {
console.log('[game]Service onRemoteRequest');
var publishCallBack;
if (code === 1) {
// 读取手柄端发送的数据
let actionType = data.readInt();
let angle = data.readInt();
reply.writeInt(100);
var params = {
actionType: actionType,
angle: angle,
}
var options = {
code: 1,
data: 'init data',
isOrdered: true,
bundleName: 'com.huawei.cookbook',
parameters: params
}
publishCallBack = function () {}
// 发布公共事件
commonEvent.publish("publish_action", options, publishCallBack);
}
return true;
}
}
subscribeEvent() {
...
// 订阅公共事件回调
function SubscribeCallBack(err, data) {
let msgData = data.data;
let code = data.code;
...
// 处理接收到的数据data
that.actionType = data.parameters.actionType;
that.angle = data.parameters.angle;
if (that.actionType == 1) {
that.createBullet()
}
if (that.actionType == 2) {
if (that.bombNum > 0) {
that.bombNum--
that.destroyAllEnemy()
}
}
if (that.angle != 0) {
that.movePlaneByHandle()
}
}
//创建订阅者回调
function CreateSubscriberCallBack(err, data) {
subscriber = data;
//订阅公共事件
commonEvent.subscribe(subscriber, SubscribeCallBack);
}
//创建订阅者
commonEvent.createSubscriber(subscribeInfo, CreateSubscriberCallBack);
}
审核编辑 黄宇
全部0条评论
快来发表一下你的评论吧 !