【机器人小游戏---html(附源代码)】(2)

}

// ---- update points ----

for (const point of this.points) {

// ---- dragging ----

if (this === dancerDrag && point === pointDrag) {

point.x += (pointer.x - point.x) * 0.1;

point.y += (pointer.y - point.y) * 0.1;

}

// ---- dance ----

if (this !== dancerDrag) {

point.fn && point.fn(16 * Math.sqrt(this.size), this.dir);

}

// ---- verlet integration ----

point.vx = point.x - point.px;

point.vy = point.y - point.py;

point.px = point.x;

point.py = point.y;

point.vx *= 0.995;

point.vy *= 0.995;

point.x += point.vx;

point.y += point.vy + 0.01;

}

// ---- ground ----

for (const link of this.links) {

const p1 = link.p1;

if (p1.y > canvas.height * ground - link.size * 0.5) {

p1.y = canvas.height * ground - link.size * 0.5;

p1.x -= p1.vx;

p1.vx = 0;

p1.vy = 0;

}

}

// ---- center position ----

const delta = (this.x - this.points[0].x) * 0.0002;

this.points[9].x += delta;

this.points[10].x += delta;

}

draw() {

for (const link of this.links) {

if (link.size) {

const dx = link.p1.x - link.p0.x;

const dy = link.p1.y - link.p0.y;

const a = Math.atan2(dy, dx);

const d = Math.sqrt(dx * dx + dy * dy);

// ---- shadow ----

ctx.save();

ctx.translate(link.p0.x + link.size * 0.25, link.p0.y + link.size * 0.25);

ctx.rotate(a);

ctx.drawImage(

link.shadow,

-link.size * 0.5,

-link.size * 0.5,

d + link.size,

link.size

);

ctx.restore();

// ---- stroke ----

ctx.save();

ctx.translate(link.p0.x, link.p0.y);

ctx.rotate(a);

ctx.drawImage(

link.image,

-link.size * 0.5,

-link.size * 0.5,

d + link.size,

link.size

);

ctx.restore();

}

}

}

}

Robot.Link = class Link {

constructor(parent, p0, p1, dist, size, light, force, disk) {

// ---- cache strokes ----

function stroke(color, axis) {

const image = document.createElement(“canvas”);

image.width = dist + size;

image.height = size;

const ict = image.getContext(“2d”);

ict.beginPath();

ict.lineCap = “round”;

ict.lineWidth = size;

ict.strokeStyle = color;

if (disk) {

ict.arc(size * 0.5 + dist, size * 0.5, size * 0.5, 0, 2 * Math.PI);

ict.fillStyle = color;

ict.fill();

} else {

ict.moveTo(size * 0.5, size * 0.5);

ict.lineTo(size * 0.5 + dist, size * 0.5);

ict.stroke();

}

if (axis) {

const s = size / 10;

ict.fillStyle = “#000”;

ict.fillRect(size * 0.5 - s, size * 0.5 - s, s * 2, s * 2);

ict.fillRect(size * 0.5 - s + dist, size * 0.5 - s, s * 2, s * 2);

}

return image;

}

this.p0 = p0;

this.p1 = p1;

this.distance = dist;

this.size = size;

this.light = light || 1.0;

this.force = force || 0.5;

this.image = stroke(

“hsl(” + parent.color + " ,30%, " + parent.light * this.light + “%)”,

true

);

this.shadow = stroke(“rgba(0,0,0,0.5)”);

}

};

Robot.Point = class Point {

constructor(x, y, fn, w) {

this.x = x;

this.y = y;

this.w = w || 0.5;

this.fn = fn || null;

this.px = x;

this.py = y;

this.vx = 0.0;

this.vy = 0.0;

}

};

// ---- set canvas ----

const canvas = {

init() {

this.elem = document.querySelector(“canvas”);

this.resize();

window.addEventListener(“resize”, () => this.resize(), false);

return this.elem.getContext(“2d”);

},

resize() {

this.width = this.elem.width = this.elem.offsetWidth;

this.height = this.elem.height = this.elem.offsetHeight;

ground = this.height > 500 ? 0.85 : 1.0;

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

dancers[i].x = (i + 2) * canvas.width / 9;

}

}

};

// ---- set pointer ----

const pointer = {

init(canvas) {

this.x = 0;

this.y = 0;

window.addEventListener(“mousemove”, e => this.move(e), false);

canvas.elem.addEventListener(“touchmove”, e => this.move(e), false);

window.addEventListener(“mousedown”, e => this.down(e), false);

window.addEventListener(“touchstart”, e => this.down(e), false);

window.addEventListener(“mouseup”, e => this.up(e), false);

window.addEventListener(“touchend”, e => this.up(e), false);

},

down(e) {

this.move(e);

for (const dancer of dancers) {

for (const point of dancer.points) {

const dx = pointer.x - point.x;

const dy = pointer.y - point.y;

const d = Math.sqrt(dx * dx + dy * dy);

if (d < 60) {

dancerDrag = dancer;

pointDrag = point;

dancer.frame = 0;

}

}

}

},

up(e) {

dancerDrag = null;

},

move(e) {

let touchMode = e.targetTouches,

pointer;

if (touchMode) {

e.preventDefault();

pointer = touchMode[0];

} else pointer = e;

this.x = pointer.clientX;

this.y = pointer.clientY;

}

};

// ---- init ----

const dancers = [];

let ground = 1.0;

const ctx = canvas.init();

pointer.init(canvas);

let dancerDrag = null;

let pointDrag = null;

// ---- main loop ----

const run = () => {

requestAnimationFrame(run);

ctx.clearRect(0, 0, canvas.width, canvas.height);

ctx.fillStyle = “#222”;

ctx.fillRect(0, 0, canvas.width, canvas.height * 0.15);

ctx.fillRect(0, canvas.height * 0.85, canvas.width, canvas.height * 0.15);

for (const dancer of dancers) {

dancer.update();

dancer.draw();

}

};

// ---- robot structure ----

const struct = {

points: [

{

x: 0,

y: -4,

f(s, d) {

this.y -= 0.01 * s;

}

},

{

x: 0,

y: -16,

f(s, d) {

this.y -= 0.02 * s * d;

}

},

{

x: 0,

y: 12,

f(s, d) {

this.y += 0.02 * s * d;

}

},

{ x: -12, y: 0 },

{ x: 12, y: 0 },

{

x: -3,

y: 34,

f(s, d) {

if (d > 0) {

this.x += 0.01 * s;

this.y -= 0.015 * s;

} else {

this.y += 0.02 * s;

}

}

},

{

x: 3,

y: 34,

f(s, d) {

if (d > 0) {

this.y += 0.02 * s;

} else {

this.x -= 0.01 * s;

this.y -= 0.015 * s;

}

}

},

{

x: -28,

y: 0,

f(s, d) {

this.x += this.vx * 0.035;

this.y -= 0.001 * s;

}

},

{

x: 28,

y: 0,

f(s, d) {

this.x += this.vx * 0.035;

this.y -= 0.001 * s;

}

},

{

x: -3,

y: 64,

f(s, d) {

this.y += 0.015 * s;

if (d > 0) {

this.y -= 0.01 * s;

} else {

this.y += 0.05 * s;

}

}

},

{

x: 3,

y: 64,

f(s, d) {

this.y += 0.015 * s;

if (d > 0) {

this.y += 0.05 * s;

} else {

this.y -= 0.01 * s;

}

}

}

],

links: [

{ p0: 3, p1: 7, size: 12, lum: 0.5 },

{ p0: 1, p1: 3, size: 24, lum: 0.5 },

{ p0: 1, p1: 0, size: 60, lum: 0.5, disk: 1 },

{ p0: 5, p1: 9, size: 16, lum: 0.5 },

{ p0: 2, p1: 5, size: 32, lum: 0.5 },

{ p0: 1, p1: 2, size: 50, lum: 1 },

{ p0: 6, p1: 10, size: 16, lum: 1.5 },

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-EiPs2j34-1715793101968)]

[外链图片转存中…(img-zjy10lTy-1715793101969)]

[外链图片转存中…(img-4pgrYiRp-1715793101969)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值