Javascript 随机起点蛇

Javascript 随机起点蛇,javascript,Javascript,我一直在研究本教程: 但由于某种原因,我无法弄清楚如何让蛇每次都在一个随机的地方开始 有什么办法吗?这是在网格中设置起始位置的地方: // The current position of the Snake's head, as xy coordinates this.currentPosition = [50, 50]; 要从随机点开始,请执行以下操作: var randX = Math.floor(Math.random() * x), // x = 50 in the default

我一直在研究本教程:

但由于某种原因,我无法弄清楚如何让蛇每次都在一个随机的地方开始


有什么办法吗?

这是在网格中设置起始位置的地方:

// The current position of the Snake's head, as xy coordinates
this.currentPosition = [50, 50];
要从随机点开始,请执行以下操作:

var randX = Math.floor(Math.random() * x),  // x = 50 in the default grid
    randY = Math.floor(Math.random() * y);  // y = 50 in the default grid

this.currentPosition = [randX, randY];
根据博客的说法,如果你复制了实际的源,它使用的是一个对象而不是一个数组作为坐标,并且计算的结果略有不同。在
start
功能中,您只需要选择一个新的起始单元格

var randX = Math.floor(Math.random() * 40) * this.gridSize,  
    randY = Math.floor(Math.random() * 30) * this.gridSize; 

this.currentPosition = {'x': randX, 'y': randY};

这是在网格中设置起始位置的位置:

// The current position of the Snake's head, as xy coordinates
this.currentPosition = [50, 50];
要从随机点开始,请执行以下操作:

var randX = Math.floor(Math.random() * x),  // x = 50 in the default grid
    randY = Math.floor(Math.random() * y);  // y = 50 in the default grid

this.currentPosition = [randX, randY];
根据博客的说法,如果你复制了实际的源,它使用的是一个对象而不是一个数组作为坐标,并且计算的结果略有不同。在
start
功能中,您只需要选择一个新的起始单元格

var randX = Math.floor(Math.random() * 40) * this.gridSize,  
    randY = Math.floor(Math.random() * 30) * this.gridSize; 

this.currentPosition = {'x': randX, 'y': randY};

天啊。。。很抱歉,这是离题,但感谢您发布该链接!自从我的旧诺基亚brick手机问世以来,我一直在努力寻找snake。在我看来,这仍然是迄今为止最好的手机游戏。@Mike在真正的电脑上,这个游戏通常被称为Nibbles@scrappedcola,现在我只是尝试更改
this.currentPosition={'x':50,'y':50}的编号
以确定该位置是否设置在那里。但是没有运气。。即使如此,我也很确定那应该是选择星星位置的地方。。。很抱歉,这是离题,但感谢您发布该链接!自从我的旧诺基亚brick手机问世以来,我一直在努力寻找snake。在我看来,这仍然是迄今为止最好的手机游戏。@Mike在真正的电脑上,这个游戏通常被称为Nibbles@scrappedcola,现在我只是尝试更改
this.currentPosition={'x':50,'y':50}的编号
以确定该位置是否设置在那里。但是没有运气。。即使是这样,我也很确定那应该是选择明星位置的地方。我只是通过调试实际游戏的javascript来尝试,将
start
函数中的
this.currentPosition
更改为类似
{x:200,y:200}
确实会更改开始位置。我只是通过调试实际游戏的javascript来尝试,将
这个更改为
start
函数中的currentPosition
更改为类似
{x:200,y:200}
的内容,确实会更改开始位置。