当前位置: 首页 > article >正文

366小游戏HTML5小游戏,使用Matter.js实现的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();

http://www.lryc.cn/news/2419012.html

相关文章:

  • 【React实战技巧】——将数据库返回的时间转换为几分钟前、几小时前、几天前……
  • C语言指针高级部分:void指针和数据指针
  • Android一点 打造全功能屏幕适配AutoView
  • 深度解析JavaScript中常见设计模式及其应用场景,看完去面试,面试官说:就你了
  • 安卓期末大作业——安卓手机网上商城-2号店
  • JavaWeb实现简单的文件上传和下载
  • PerformanceCounter 性能计数器的使用
  • 六亿美金到数千万美元,西雅图行业先驱要私有化!
  • 问题解决:com.lowagie.text.DocumentException: Font 'STSong-Light' with 'UniGB-UCS2-H'
  • 软件开发完整流程整套文档(开发实施运维安全交付)
  • 超宽屏幕比例_显示器屏幕比例与分辨率对照表
  • C# 设计模式(一)
  • JCreator调试全攻略
  • 基于领域特定语言(DSL)的用例驱动开发(UDD)
  • 【YOLO使用】YOLOv5训练目标检测任务入门用法(一)
  • 文件系统格式FAT16、FAT32、NTFS、exFAT的区别
  • 什么是垂直搜索引擎
  • 【软件基础】面向对象分析与设计思想总结
  • freepascal linux 界面,FreePascal中的编译器选项
  • eclipse安装flash builder 4.7插件以及java、flex整合开发
  • MSF使用教程
  • h3c防火墙u200配置命令_绝了!华为、H3C、锐捷交换机配置命令大全
  • sort函数用法与stable_sort函数
  • 世界华人名人录
  • TypedArray的用法和declare-styleable:自定义控件的属性
  • 【IO】读写文件(FileInputStream、FileOutputStream、FileReader、FileWriter、RandomAccessFile)
  • 网络信息安全等级与标准_比较tcsec,cc及我国信息安全等级保护标准
  • 斗罗大陆武魂觉醒礼包码最新兑换码2024
  • 五笔字根表
  • (虚拟机)win10忘记密码重置_copy sethc,熬夜整理华为最新网络安全笔试题