当前位置:首页 >> 车险

开源鸿蒙 OpenHarmony 3.1画布解析,教你如何完成飞机恶战小游戏

来源:车险   2024年12月08日 12:19

illRect(200, 200, 300, 300); }}

2.1.2 放大与镜子

CanvasRenderingContext2D 取向透过了 scale (x,y) 新方法,值 x 声称居中朝向上放大平方根,y 声称纵轴朝向上放大的平方根,值得节录意的是放大过程里面定位也会被放大。如下图标明,是将上个下例里面的填入方形通过 scale (2,1.5) 放大,并通过 shadowBlur 新方法加上镜子后的精准度。

下例代码如下:

//xxx.jsexport default { onShow() { const el =this.$refs.canvas; const ctx = el.getContext('2d');//另设所画镜子的模糊级别 ctx.shadowBlur = 80; ctx.shadowColor = 'rgb(0,0,0)'; ctx.fillStyle = 'rgb(0,0,0)'; // x Scale to 200%,y Scale to 150% ctx.scale(2, 1.5); ctx.fillRect(200, 200, 300, 300); }}

2.2 表示遗憾结构设计开发计划实例里面 canvas 的所画新方法

表示遗憾结构设计开发计划实例,采用 TS 自然语言并展开表示遗憾结构设计 UI 语法扩展到,从元件、动效和状态监管三个维度透过了 UI 所画并能,目从前之从前透过了 canvas 元件所画并能,但功能仍在齐备里面。下面我们将通过两个下例展出表示遗憾结构设计开发计划实例里面 canvas 元件的基础用于新方法。

2.2.1 截图发散

如下图标明,是三张截图发散的精准度,顶层的截图覆盖了底层的截图。通过依次用于 drawImage (x,y, width, height) 新方法另设截图经纬度及宽度,后面所画的截图系统会覆盖原来的位图,从而远超预期精准度。

扩展到的 TS 自然语言采用不够接近自然形式化的编程方结构设计,让开发计划者可以直观地描述 UI 界面,下例代码如下:

@Entry@Componentstruct IndexCanvas1 { private settings:RenderingContextSettings = new RenderingContextSettings(true);//借助上色取向 private ctx: RenderingContext = new RenderingContext(this.settings);//列显露所要用到的截图 private img:ImageBitmap = new ImageBitmap("common/bg.jpg"); build() { Column() { //创始人canvas Canvas(this.ctx) .width(1500) .height(900) .border({color:"blue",width:1,}) .backgroundColor('#ffff00') //开始所画 .onReady(() => { this.ctx.drawImage( this.img,400,200,540,300); this.ctx.drawImage( this.img,500,300,540,300); this.ctx.drawImage( this.img,600,400,540,300); }) } .width('100%') .height('100%') }}

2.2.2 点击创始人线性渐变

如下图标明,是一个线性渐变精准度。基于 canvas 扩展到了一个 Button 元件,通过点击“Click”按住,触发 onClick () 新方法,并通过codice_ createLinearGradient () 新方法,所画显露了一个线性渐变色。

下例代码如下:

@Entry@Componentstruct GradientExample { private settings: RenderingContextSettings = new RenderingContextSettings(true); private context: RenderingContext = new RenderingContext(this.settings); private gra: CanvasGradient = new CanvasGradient(); build() { Column({ space: 5 }) {//创始人一个画笔 Canvas(this.context) .width(1500) .height(900) .backgroundColor('#ffff00 ') Column() {//另设按住的外观上 Button('Click').width(250).height(100).backgroundColor('#000000') .onClick(() => {//创始人一个线性渐变色 var grad = this.context.createLinearGradient(600, 200, 400, 750) grad.addColorStop(0.0, 'red'); grad.addColorStop(0.5, 'white'); grad.addColorStop(1.0, 'green'); this.context.fillStyle = grad; this.context.fillRect(400, 200, 550, 550); }) }.alignItems(HorizontalAlign.center) } } }架飞机大战该游戏所画实践

如下图标明,是一款”架飞机大战”该游戏,通过控制轰炸机的漂移炸毁德国空军。如何用于 ArkUI 开发计划方透过的 canvas 元件轻松发挥作用这个经典怀旧的该游戏?发挥作用思路及关键代码如下:

1. 首先列显露游戏所用到的截图

private imgList:Array = ["xx.png","xx.png"…];

2. 将截图图像到 canvas 画笔上

let img:ImageBitmap = new ImageBitmap("截图路径(如common/images)/"+this.imgList[数组十六进制]);this.ctx.drawImage( img,150/* x经纬度*/, 150/* y经纬度*/, 600/*宽*/, 600/*高*/)

3. 所画背景截图和轰炸机向下漂移的精准度

this.ctx.drawImage(this.bg, 0, this.bgY);this.ctx.drawImage(this.bg, 0, this.bgY - 480);this.bgY++ == 480 && (this.bgY = 0);

4. 用于 Math.round 函数随机借助德国空军截图并图像到画笔上,并且改变德国空军 y 轴经纬度,使它向下群众运动。

Efight = Math.round(Math.random()*7);//从前七张为德国空军截图。let img:ImageBitmap = new ImageBitmap("common/img"+this.imgList[Efight]);this.ctx.drawImage(img, 0, this.Eheight + 50);//图像德国空军

5. 在配页每隔 120s 消失一排,之从前减小或增大(x,y)轴的经纬度远超射显露精准度。

let i= 0;setInterval(()=>{ this.ctx.drawImage(this.bulImg1,image.x – 10 – (i *10) , image.x + (i *10)) this.ctx.drawImage(this.bulimg2, this. bulImg1,image.x – (i *10) , i image.x + (i *10)) this.ctx.drawImage(this.bulimg3, image.x + 10 + (i *10), image.x + (i *10))i ++;},120)

6. 用于 onTouch 新方法借助轰炸机漂移从前面,借助启动时的经纬度后重新另设轰炸机的截图经纬度,使轰炸机发挥作用启动时精准度。

.onTouch((event)=>{ var offsetX = event.localX ||event.touches[0].localX; var offsetY = event.localY ||event.touches[0].localY; var w = this.heroImg[0].width, h = this.heroImg[0].height; var nx = offsetX - w / 2, ny = offsetY - h / 2; nx (this.windowWidth - w / 2 - 20) ? nx = (this.windowWidth - w / 2 - 20) : 0; ny (this.windowHeight - h / 2) ? ny = (this.windowHeight – h/2) : 0; this.hero.x = nx; this.hero.y = ny; this.hero.count = 2;

节录:本下例引用了大多开源人力,有用的开发计划者可参考此开源人力,结合文里面的发挥作用思路除此以外代码。( xs528 / game)

以上就是本期技术细节,期待广大开发计划者能通过 canvas 元件所画显露精美的图形,不够多 canvas 元件的详细用于新方法,请参考文档展开学习:

筋骨痛
眼睛结膜炎的症状有哪些
孩子积食

长期便秘会有什么危害
胃酸过多怎么调理最快最有效
罗氏和瑞特血糖仪哪个准确
常乐康和亿活哪个好
金笛如何治疗新冠
家用选哪种血糖仪比较好
友情链接