- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
几天来我一直在克隆 agar.io。我已经完成了分割功能。但我的分割机制又旧又无聊。我希望它像 agar.io 一样(以更快的速度加速并减慢速度)。打开agar.io,玩一玩,你就会知道我期望改进什么。
下面是我的代码。
var canvas,
ctx,
width = innerWidth,
height = innerHeight,
mouseX = 0,
mouseY = 0;
var camera = {
x: 0,
y: 0,
// camera
update: function(obj) {
this.x = (obj.blobsExtent.minx + obj.blobsExtent.maxx) / 2;
this.y = (obj.blobsExtent.miny + obj.blobsExtent.maxy) / 2;
this.x -= width / 2;
this.y -= height / 2;
}
},
player = {
defaultMass: 54,
x: 0,
y: 0,
blobs: [],
blobsExtent: {
minx: 0,
miny: 0,
maxx: 0,
maxy: 0,
},
update: function() {
var be = this.blobsExtent;
for (var i = 0; i < this.blobs.length; i++) {
var x = mouseX + camera.x - this.blobs[i].x;
var y = mouseY + camera.y - this.blobs[i].y;
var length = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
var speed = 54 / this.blobs[i].mass;
this.blobs[i].velX = x / length * speed * Math.min(1, Math.pow(x / this.blobs[i].mass, 2));
this.blobs[i].velY = y / length * speed * Math.min(1, Math.pow(y / this.blobs[i].mass, 2));
this.blobs[i].x += this.blobs[i].velX;
this.blobs[i].y += this.blobs[i].velY;
for (var j = 0; j < this.blobs.length; j++) {
if (j != i && this.blobs[i] !== undefined) {
var blob1 = this.blobs[i];
var blob2 = this.blobs[j];
var x = blob2.x - blob1.x;
var y = blob2.y - blob1.y;
var dist = Math.sqrt(x * x + y * y);
if (dist < blob1.mass + blob2.mass) {
x /= dist;
y /= dist;
blob1.x = blob2.x - x * (blob1.mass + blob2.mass);
blob1.y = blob2.y - y * (blob1.mass + blob2.mass);
}
}
}
if (i === 0) {
be.maxx = be.minx = blob1.x;
be.maxy = be.miny = blob1.y;
} else {
be.maxx = Math.max(be.maxx, blob1.x);
be.maxy = Math.max(be.maxy, blob1.y);
be.minx = Math.min(be.minx, blob1.x);
be.miny = Math.min(be.miny, blob1.y);
}
}
this.x += (mouseX - width / 2) / (width / 2) * 1;
this.y += (mouseY - height / 2) / (height / 2) * 1
},
split: function(cell) {
if (cell.mass >= this.defaultMass) {
cell.mass /= 2;
this.blobs.push({
x: cell.x,
y: cell.y,
mass: cell.mass
});
}
},
draw: function() {
for (var i = 0; i < this.blobs.length; i++) {
ctx.fillStyle = "red";
ctx.beginPath();
ctx.arc(-camera.x + this.blobs[i].x, -camera.y + this.blobs[i].y, this.blobs[i].mass, 0, Math.PI * 2);
ctx.fill();
ctx.closePath();
}
}
};
function handleMouseMove(e) {
mouseX = e.clientX;
mouseY = e.clientY;
}
function handleKeydown(e) {
if (e.keyCode == 32) {
var currentLength = player.blobs.length;
for (var i = 0; i < currentLength; i++) {
player.split(player.blobs[i]);
}
}
}
function setup() {
canvas = document.getElementById("game");
ctx = canvas.getContext("2d");
canvas.width = width;
canvas.height = height;
addEventListener("mousemove", handleMouseMove);
addEventListener("keydown", handleKeydown);
player.blobs.push({
x: 0,
y: 0,
mass: player.defaultMass
});
player.blobs.push({
x: 100,
y: 100,
mass: player.defaultMass / 2
});
player.blobs.push({
x: 100,
y: 100,
mass: player.defaultMass / 2
});
player.blobs.push({
x: 100,
y: 100,
mass: player.defaultMass / 2
});
player.blobs.push({
x: 100,
y: 100,
mass: player.defaultMass * 2
});
var loop = function() {
update();
draw();
requestAnimationFrame(loop);
}
requestAnimationFrame(loop);
}
function update() {
camera.update(player);
player.update();
}
function draw() {
ctx.fillStyle = "#fff";
ctx.fillRect(0, 0, width, height);
ctx.fillStyle = "green";
ctx.fillRect(-camera.x + 0, -camera.y + 0, 20, 20);
player.draw();
}
setup();
body {
margin: 0;
padding: 0;
}
<title>Play Agario Clone</title>
<canvas id="game">Kindly update your browser.</canvas>
空格键进行分割。
我希望 split 的 Blob 首先以较高的速度前进,然后减慢速度。所有 blob 更新均由player.update 完成
最佳答案
有点困惑,因为我没有那么多时间。添加了一个快速背景网格来显示运动。
将速度属性重新添加到 Blob 中,并使用它来控制所有 Blob 的速度,而不仅仅是分割 Blob 的速度。当一个 Blob split 时,我给两个 split 的 Blob 一个新的速度(8,但由你决定)
在计算每个 Blob 的速度后,如果速度 > 1(在分割 Blob 的情况下),我通过乘以非常接近 1 (0.995) 的数字来降低速度,这会在许多帧上缓慢降低速度。该数字越接近 1,加速持续的时间就越长。该数字必须低于 1,否则 Blob 会变得越来越快。
// need something to see relative movement so this is to add a background grid
var gPattern;
function createGridPattern() {
const grid = document.createElement("canvas");
grid.width = 128;
grid.height = 128;
grid.ctx = grid.getContext("2d");
grid.ctx.fillStyle = "white";
grid.ctx.fillRect(0, 0, 128, 128);
const alphas = [1, 0.2, 0.4, 0.2];
grid.ctx.fillStyle = "black";
for (var x = 0; x < 128; x += 16) {
grid.ctx.globalAlpha = alphas[(x / 16) % 4];
grid.ctx.fillRect(x, 0, 1, 128);
grid.ctx.fillRect(0, x, 128, 1);
}
return ctx.createPattern(grid,"repeat");
}
var
canvas,
ctx,
width = innerWidth,
height = innerHeight,
mouseX = 0,
mouseY = 0;
var
camera = {
x: 0,
y: 0,
// camera
update: function(obj) {
this.x = (obj.blobsExtent.minx + obj.blobsExtent.maxx) / 2;
this.y = (obj.blobsExtent.miny + obj.blobsExtent.maxy) / 2;
this.x -= width / 2;
this.y -= height / 2;
}
},
player = {
defaultMass: 54,
x: 0,
y: 0,
blobs: [],
blobsExtent: {
minx: 0,
miny: 0,
maxx: 0,
maxy: 0,
},
update: function() {
var be = this.blobsExtent;
for (var i = 0; i < this.blobs.length; i++) {
var x = mouseX + camera.x - this.blobs[i].x;
var y = mouseY + camera.y - this.blobs[i].y;
var length = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
var speed = 54 / this.blobs[i].mass;
this.blobs[i].velX = x / length * this.blobs[i].speed * Math.min(1, Math.pow(x / this.blobs[i].mass, 2));
this.blobs[i].velY = y / length * this.blobs[i].speed * Math.min(1, Math.pow(y / this.blobs[i].mass, 2));
if(this.blobs[i].speed > 1){
this.blobs[i].speed *= 0.995; // make speed a little slow each frame
// make the 0.995 closer to 1 to make the speedup last longer
// eg 0.999 will keep speed longer
// this number must be less than 1
}else{
this.blobs[i].speed = 1;
}
this.blobs[i].x += this.blobs[i].velX;
this.blobs[i].y += this.blobs[i].velY;
for (var j = 0; j < this.blobs.length; j++) {
if (j != i && this.blobs[i] !== undefined) {
var blob1 = this.blobs[i];
var blob2 = this.blobs[j];
var x = blob2.x - blob1.x;
var y = blob2.y - blob1.y;
var dist = Math.sqrt(x * x + y * y);
if (dist < blob1.mass + blob2.mass) {
x /= dist;
y /= dist;
blob1.x = blob2.x - x * (blob1.mass + blob2.mass);
blob1.y = blob2.y - y * (blob1.mass + blob2.mass);
}
}
}
if (i === 0) {
be.maxx = be.minx = blob1.x;
be.maxy = be.miny = blob1.y;
} else {
be.maxx = Math.max(be.maxx, blob1.x);
be.maxy = Math.max(be.maxy, blob1.y);
be.minx = Math.min(be.minx, blob1.x);
be.miny = Math.min(be.miny, blob1.y);
}
}
this.x += (mouseX - width / 2) / (width / 2) * 1;
this.y += (mouseY - height / 2) / (height / 2) * 1
},
split: function(cell) {
if (cell.mass >= this.defaultMass) {
cell.mass /= 2;
cell.speed = 8; // this is the amount of extra speed when split
this.blobs.push({
x: cell.x,
y: cell.y,
mass: cell.mass,
speed : 8, // this is the amount of extra speed when split
});
}
},
draw: function() {
for (var i = 0; i < this.blobs.length; i++) {
ctx.fillStyle = "red";
ctx.beginPath();
ctx.arc(-camera.x + this.blobs[i].x, -camera.y + this.blobs[i].y, this.blobs[i].mass, 0, Math.PI * 2);
ctx.fill();
ctx.closePath();
}
}
};
function handleMouseMove(e) {
mouseX = e.clientX;
mouseY = e.clientY;
}
function handleKeydown(e) {
if (e.keyCode == 32) {
var currentLength = player.blobs.length;
for (var i = 0; i < currentLength; i++) {
player.split(player.blobs[i]);
}
}
}
function setup() {
canvas = document.getElementById("game");
ctx = canvas.getContext("2d");
canvas.width = width;
canvas.height = height;
addEventListener("mousemove", handleMouseMove);
addEventListener("keydown", handleKeydown);
player.blobs.push({
x: 0,
y: 0,
speed : 1,
mass: player.defaultMass
});
player.blobs.push({
x: 100,
y: 100,
speed : 1,
mass: player.defaultMass / 2
});
player.blobs.push({
x: 100,
y: 100,
speed : 1,
mass: player.defaultMass / 2
});
player.blobs.push({
x: 100,
y: 100,
speed : 1,
mass: player.defaultMass / 2
});
player.blobs.push({
x: 100,
y: 100,
speed : 1,
mass: player.defaultMass * 2
});
var loop = function() {
update();
draw();
requestAnimationFrame(loop);
}
gPattern = createGridPattern();
loop();
}
function update() {
camera.update(player);
player.update();
}
function draw() {
ctx.fillStyle = gPattern;
ctx.setTransform(1,0,0,1,(-camera.x % 128) - 128, (-camera.y % 128) - 128);
ctx.fillRect(0,0, width+256, height+256);
ctx.setTransform(1,0,0,1,0,0);
ctx.fillStyle = "green";
ctx.fillRect(-camera.x + 0, -camera.y + 0, 20, 20);
player.draw();
}
setup();
canvas {
border: 2px solid black;
padding : 0px;
margin : 0px;
}
<canvas id="game">
</canvas>
关于javascript - 改进我的分割机制,使其像agar.io,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43980016/
广播的原则 如果两个数组的后缘维度(从末尾开始算起的维度)的轴长度相符或其中一方的长度为1,则认为它们是广播兼容的。广播会在缺失维度和(或)轴长度为1的维度上进行。 在上面的对arr每一列减去列
之前在讲 MySQL 事务隔离性提到过,对于写操作给读操作的影响这种情形下发生的脏读、不可重复读、虚读问题。是通过MVCC 机制来进行解决的,那么MVCC到底是如何实现的,其内部原理是怎样的呢?我们要
我创建了一个 JavaScript 对象来保存用户在 ColorBox 中检查复选框时设置的值。 . 我对 jQuery 和“以正确的方式”编程 JavaScript 比较陌生,希望确保以下用于捕获用
我为了回答aquestion posted here on SO而玩示例,发现很难理解python的import *破坏作用域的机制。 首先是一点上下文:这个问题不涉及实际问题;我很清楚from fo
我想让我的类具有标识此类的参数 ID。例如我想要这样的东西: class Car { public static virtual string ID{get{return "car";}} }
更新:我使用的是 Java 1.6.34,没有机会升级到 Java 7。 我有一个场景,我每分钟只能调用一个方法 80 次。它实际上是由第 3 方编写的服务 API,如果您多次调用它,它会“关闭”(忽
希望这对于那些使用 Javascript 的人来说是一个简单的答案...... 我有一个日志文件,该文件正在被一个脚本监视,该脚本将注销中的新行提供给任何连接的浏览器。一些人评论说,他们希望看到的更多
我们正在开发针对 5.2 开发的 PHP 应用程序,但我们最近迁移到了 PHP 5.3。我们没有时间去解决所有迁移到 PHP 5.3 的问题。具体来说,我们有很多消息: Declaration of
简介 在实现定时调度功能的时候,我们往往会借助于第三方类库来完成,比如: quartz 、 spring schedule 等等。jdk从1.3版本开始,就提供了基于 timer 的定时调度功能。
Java中,一切都是对象,在分布式环境中经常需要将Object从这一端网络或设备传递到另一端。这就需要有一种可以在两端传输数据的协议。Java序列化机制就是为了解决这个问题而
我将编写自己的自定义控件,它与 UIButton 有很大不同。由于差异太大,我决定从头开始编写。所以我所有的子类都是 UIControl。 当我的控件在内部被触摸时,我想以目标操作的方式触发一条消息。
在我的代码中,在创建 TIdIMAP4 连接之前,我设置了一大堆 SASL 机制,希望按照规定的“最好到最差”顺序,如下所示: IMAP.SASLMechanisms.Add.SASL := mIdS
在 Kubernetes 中,假设我们有 3 个 pod,它们物理上托管在节点 X、Y 和 Z 上。当我使用“kubectl expose”将它们公开为服务时,它们都是集群中的节点(除了 X、Y 和
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
我知道进程间通信 (ipc) 有几种方法,例如: 文件 信号 socket 消息队列 管道 命名管道 信号量 共享内存 消息传递 内存映射文件 但是我无法找到将这些机制相互比较并指出它们在不同环境中的
当我尝试连接到 teradata 时,出现了TD2 机制不支持单点登录 错误。 在 C# 中,我遇到了类似的问题,我通过添加 connectionStringBuilder.Authetication
我有一个带有 JSON API 的简单 Javascript 应用程序。目前它在客户端运行,但我想将它从客户端移动到服务器。我习惯于学习新平台,但在这种情况下,我的时间非常有限 - 所以我需要找到绝对
我想了解事件绑定(bind)/解除绑定(bind)在浏览器中是如何工作的。具体来说,如果我删除一个已经绑定(bind)了事件的元素,例如使用 jQuery:$("#anElement").remove
我不是在寻找具体答案,只是一个想法或提示。我有以下问题: Android 应用程序是 Web 服务的客户端。它有一个线程,通过 http 协议(protocol)发送事件(带有请求 ID 的 XML
我正在研究 FreeBSD TCP/IP 栈。似乎有 2 种 syn flood 机制,syncookies 和 syncache。我的问题是关于 syncookies,它是从头开始还是在 SYN 队
我是一名优秀的程序员,十分优秀!