丢雪球的游戏_h5小游戏怎么制作「建议收藏」

丢雪球的游戏_h5小游戏怎么制作「建议收藏」JavaScript语言:JaveScriptBabelCoffeeScript确定//https://github.com/liabru/matter-js/wiki/Tutorials//https://www.y

丢雪球的游戏_h5小游戏怎么制作「建议收藏」

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

// https://github.com/liabru/matter-js/wiki/Tutorials

// https://www.youtube.com/watch?v=jN-sW-SxNzk

let Engine = Matter.Engine,

World = Matter.World,

Bodies = Matter.Bodies;

var engine = Engine.create();

let world = engine.world;

class Particle {

constructor(x, y, r, option) {

this.defaults = {

isStatic: true,

restitution: 0,

friction: 1,

density: .1

};

this.options = Object.assign({}, this.defaults, option);

this.body = Bodies.circle(x, y, r, this.options);

this.r = r;

World.add(world, this.body);

}

show() {

let pos = this.body.position;

ctx.save();

ctx.fillStyle = this.options.fill || “#456”;

ctx.translate(pos.x, pos.y);

ctx.beginPath();

ctx.arc(0, 0, this.r, 0, Math.PI * 2);

ctx.fill();

ctx.restore();

}

}

class Boundary {

constructor(x, y, w, h) {

this.options = {

isStatic: true,

};

this.body = Bodies.rectangle(x, y, w, h, this.options);

this.w = w;

this.h = h;

World.add(world, this.body);

}

show() {

let pos = this.body.position;

ctx.save();

ctx.fillStyle = this.options.fill || “#111”;

ctx.translate(pos.x, pos.y);

ctx.beginPath();

ctx.fillRect(-this.w / 2, -this.h / 2, this.w, this.h);

ctx.restore();

}

}

let canvas = document.getElementById(“canvas”),

ctx = canvas.getContext(“2d”),

width = (canvas.width = window.innerWidth),

height = (canvas.height = window.innerHeight),

cX = width / 2,

cY = height / 2;

let currentFrame = 0;

let particles = [];

let plinkos = [];

let bounds = [];

let spacing = 14;

//let cols = width/spacing;

let cols = 0.7 * height / spacing;

let rows = 0.7 * height / spacing;

let center = (width – height * .7) / 2;

let offset = 0;

if ((~~rows) % 2 === 1) {

offset = .4 * spacing;

}

for (let i = 0; i < cols + 1; i++) {

for (let j = 0; j < rows; j++) {

let x = center + (.4 * spacing * (j % 2)) + i * spacing;

let y = 0.1 * height + j * spacing;

let p = new Particle(x, y, 2);

plinkos.push(p);

}

bounds.push(new Boundary(center + offset + i * spacing, height – 0.1 * height, 4, 0.2 * height));

}

let b = new Boundary(width / 2, height + 10, width, 20);

bounds.push(b);

Engine.run(engine);

function update() {

Engine.update(engine);

if (currentFrame % 60 === 0) {

let p = new Particle(-3.5 + cX + (Math.random() – .5), 0, 5, {

isStatic: false,

fill: “#fff”

});

particles.push(p);

}

currentFrame++;

}

function draw() {

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

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

particles[i].show();

}

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

plinkos[i].show();

}

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

bounds[i].show();

}

}

function loop() {

update();

draw();

window.requestAnimationFrame(loop);

}

loop();

今天的文章丢雪球的游戏_h5小游戏怎么制作「建议收藏」分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/88513.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注