HarmonyOS鸿蒙最新鸿蒙应用开发游戏(四)---大鱼吃小鱼(互吃升级),HarmonyOS鸿蒙高级工程师面试题-字节跳动

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!


img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以戳这里获取

该篇对NPC进行了升级,这里可以投入多个NPC,且互不影响,npc之间不会触发eat,只和玩家触发eat,且每个NPC有自己的属性,他们的等级在他们的头顶

1、放置多个NPC

我们放置多个NPC就要把NPC属性抽离出来,这里很复杂,又牵扯到了知识点《状态管理》,在鸿蒙中分为了多种状态管理

这里感兴趣的话,可以去看看官方文档,我是觉得咋一看挺复杂的,用起来确实不简单。这里我就简单说一下我用到的,就是鸿蒙开发都是按照组件来组建的,一个titleview,一个小鱼等都可以封装成一个组件

@Component

用这个修饰。在主组件中定义的变量我们如果传递到子组件,可以让他只读也可以让他修改,这就用到了状态管理,子组件中如果改变了变量,在主组件中做出了相应改变就说明是父子双向同步。

下面开始写npc属性

@Observed
export default class NpcInfo {
//NPC
public npcSpeed: number = 3
public npcFishX: number = 200
public npcFishY: number = 200
public npcAngle: number = 0
public npcSin: number = 1
public npcCos: number = 1
public npcLevel: number = 2

constructor(npcSpeed: number
, npcFishX: number
, npcFishY: number
, npcAngle: number
, npcLevel: number
, npcCos: number
, npcSin: number) {
this.npcSpeed = npcSpeed
this.npcFishX = npcFishX
this.npcFishY = npcFishY
this.npcAngle = npcAngle
this.npcLevel = npcLevel
this.npcCos = npcCos
this.npcSin = npcSin
}
}

单独使用@Observed是没有任何作用的,需要搭配@ObjectLink或者@Prop使用。

@Component
struct npcView {
@ObjectLink item: NpcInfo;

build() {
Column() {
Text(this.item.npcLevel + “”)
.fontColor(‘#f11’)
.fontSize(12)
Image($r(“app.media.icon_npc_2”))
.objectFit(ImageFit.ScaleDown)
.width(40)
.height(40)
}.position({
x: this.item.npcFishX - 40,
y: this.item.npcFishY - 40
})
.rotate({ angle: this.item.npcAngle, centerX: ‘50%’, centerY: ‘50%’ })

}
}

@ObjectLink 同样也不能在@Entry修饰的组件中使用。这里面有几个需要注意的点就是npcInfo需要用@ObjectLink修饰,而且也需要单独写一个npcView组件,这样npcList数据发生改变,组件才会监控到,和咱们平常安卓是不是不一样,我刚开始的写法是直接在主组件中写,如下

ForEach(
this.npcList,
(item: NpcInfo, index) => {
//错误写法
Column() {
Text(item.npcLevel + “”)
.fontColor(‘#f11’)
.fontSize(12)
Image($r(“app.media.icon_npc_2”))
.objectFit(ImageFit.ScaleDown)
.width(40)
.height(40)
}.position({
x: item.npcFishX - 40,
y: item.npcFishY - 40
})
.rotate({ angle: item.npcAngle, centerX: ‘50%’, centerY: ‘50%’ })
}
)

这样写也可以创建出来npc,但是小鱼不会动,我看日志npc(x,y)坐标是改变的,但就是view不动,后来查资料发现需要双向数据同步,不然就是单向数据问题。正确写法是

@Component
struct npcView {
@ObjectLink item: NpcInfo;

build() {
Column() {
Text(this.item.npcLevel + “”)
.fontColor(‘#f11’)
.fontSize(12)
Image($r(“app.media.icon_npc_2”))
.objectFit(ImageFit.ScaleDown)
.width(40)
.height(40)
}.position({
x: this.item.npcFishX - 40,
y: this.item.npcFishY - 40
})
.rotate({ angle: this.item.npcAngle, centerX: ‘50%’, centerY: ‘50%’ })

}
}

主组件中

ForEach(
this.npcList,
(item1: NpcInfo, index) => {
npcView({ item: item1 })
}
)

这样写就可以了,那多条小鱼在处理坐标问题时应该如何操作呢,我这里是用数组的形式,用for循环动态设置所有npc的小鱼坐标。如果有其他的方式请给我留言。

if (this.isBegin == false) {
Button(‘开始游戏’)
.backgroundColor(‘#36d’)
.onClick(() => {
this.isBegin = true
clearInterval(this.intervalIdNPC_1)
this.intervalIdNPC_1 = setInterval(() => {

for (let i = 0; i < this.npcList.length; i++) {

//6、设置小鱼的移动位置,
this.npcList[i].npcFishX += this.npcList[i].npcSpeed * this.npcList[i].npcCos
this.npcList[i].npcFishY += this.npcList[i].npcSpeed * this.npcList[i].npcSin

this.npcList[i].npcFishX = this.getNPCBorderX(i, this.npcList[i].npcFishX)
this.npcList[i].npcFishY = this.getNPCBorderY(i, this.npcList[i].npcFishY)

console.log(“小鱼走了吗” + i + " " + this.npcList[i].npcFishX)
}

}, 40)

})
}

这里需要注意的是,多个npc之间是没有关联的,只有当npc碰到屏幕边缘或者某个点的时候掉头,其他npc不受影响。这里对getNPCBorderX,Y()方法做了修改,。

getNPCBorderX(i: number, x: number) {
if (x <= this.fishRadius) {
x = this.fishRadius + 10
this.getRandom(i)
}
if (x > this.screenWidth - this.fishRadius) {
x = this.screenWidth - this.fishRadius - 15
this.getRandom(i)
}
return x
}

getNPCBorderY(i: number, y: number) {
if (y <= this.fishRadius) {
y = this.fishRadius + 10
this.getRandom(i)
}
if (y > this.screenHeight - this.fishRadius) {
y = this.screenHeight - this.fishRadius - 10
this.getRandom(i)
}
return y
}

/随机获取一个角度/
getRandom(i: number) {
this.npcList[i].npcAngle = this.selectFrom(0, 359)
// let angle = Math.random()+Math.random()+Math.random()
// this.npcAngle = angle * 180 / Math.PI
//这是是求弧度,弧度 = 角度 * π / 180

this.npcList[i].npcSin = Math.sin(this.npcList[i].npcAngle * Math.PI / 180)
this.npcList[i].npcCos = Math.cos(this.npcList[i].npcAngle * Math.PI / 180)

}

好了,到这多个NPC就已经放置完成了,并且已经动起来了。哦,对了还需要初始化

onPageShow() {
// 速度,x,y 角度/等级/cos/sin
this.npcList.push(new NpcInfo(3, 300, 200, 0, 2, 1, 1))
this.npcList.push(new NpcInfo(3, 300, 100, 0, 3, 1, 1))
this.npcList.push(new NpcInfo(3, 200, 200, 0, 2, 1, 1))
this.npcList.push(new NpcInfo(1, 200, 200, 0, 1, 1, 1))
}

2、互吃逻辑

竟然要pk,就要有血量或者等级,这里暂时写等级

//等级
@State level: number = 3

只要玩家碰到NPC了就要判断level是否相等,如果大就要吃掉NPC,npcList就要减去一个,如果小于NPC等级就是被吃,游戏结束。

//互吃逻辑
eatFish(){
for (let i = 0; i < this.npcList.length; i++) {

let vx = this.xFish - this.npcList[i].npcFishX;
let vy = this.yFish - this.npcList[i].npcFishY;

let distance = Math.sqrt(vx * vx + vy * vy)
if(distance >0 && distance<20){
if(this.level >= this.npcList[i].npcLevel){
this.level+=1;
this.npcList.splice(i,1)
}else{
//游戏结束
}
}
}

}

使用地方

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

)]
[外链图片转存中…(img-IpOy3DDW-1715184773859)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 8
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大鱼小鱼》是一款简单而有趣的游戏,适合初学者学习Java游戏开发。下面我将手把手教你如何用Java开发这款游戏,并提供相应的源码。 步骤一:创建游戏窗口和游戏主类 首先,我们需要创建游戏的窗口,可以使用Java提供的Swing框架来实现。创建一个GameWindow类,继承JFrame类,并在构造方法中设置窗口的基本属性。 步骤二:添加游戏画布 在GameWindow类中,创建一个GameCanvas类,继承JPanel类,并重写paintComponent()方法,在此方法中实现游戏场景的绘制,包括大鱼小鱼和其他游戏元素。 步骤三:添加游戏逻辑 在GameWindow类中,添加游戏逻辑的处理方法,包括大鱼的移动、小鱼的生成和碰撞检测等。 步骤四:添加游戏控制 在GameWindow类中,添加游戏控制的方法,包括键盘事件的处理和游戏状态的切换等。 步骤五:运行游戏 在GameWindow类中,添加一个main()方法,创建游戏窗口对象,并启动游戏循环。 以上是《大鱼小鱼游戏的基本开发步骤,下面提供相应的源码供参考: ```java import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class GameWindow extends JFrame implements KeyListener{ private GameCanvas gameCanvas; private boolean isRunning; public GameWindow(){ super("大鱼小鱼"); setSize(800, 600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); addKeyListener(this); gameCanvas = new GameCanvas(); add(gameCanvas, BorderLayout.CENTER); isRunning = true; } public void start(){ while(isRunning){ update(); gameCanvas.repaint(); try{ Thread.sleep(10); }catch(InterruptedException e){ e.printStackTrace(); } } } public void update(){ // 游戏逻辑更新 } public void keyPressed(KeyEvent e){ // 键盘按下事件处理 } public void keyReleased(KeyEvent e){ // 键盘释放事件处理 } public void keyTyped(KeyEvent e){ // 键盘输入事件处理 } public static void main(String[] args){ GameWindow gameWindow = new GameWindow(); gameWindow.setVisible(true); gameWindow.start(); } } class GameCanvas extends JPanel{ protected void paintComponent(Graphics g){ super.paintComponent(g); // 游戏场景绘制 } } ``` 通过以上源码和步骤,我们可以实现《大鱼小鱼游戏的基本开发,通过添加更多的游戏逻辑和个性化设计,可以进一步完善这款游戏。希望对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值