[英]How to make a Simon Js game based on arrays?
Im writing a js simple simon game and im clueless on how to do it. 我正在编写一个js简单的simon游戏,对如何做到这一点一无所知。
I know that : 我知道 :
I need to create two arrays, and a level(score) variable 我需要创建两个数组和一个level(score)变量
But, I am clueless in aspect to the code. 但是,我对代码一无所知。
My Jsfiddle : http://jsfiddle.net/jbWcG/2/ 我的Jsfiddle: http : //jsfiddle.net/jbWcG/2/
JS: JS:
var x = []
var y = []
var levelvar = 1
document.getElementById("test").onclick= function() {
document.getElementById("test").innerHTML=x
};
document.getElementById("button1").onclick= function() {
x.push("Red")
};
document.getElementById("button2").onclick= function() {
x.push("Green")
};
document.getElementById("button3").onclick= function() {
x.push("Yellow")
};
document.getElementById("button4").onclick= function() {
x.push("Blue")
};
HTML: HTML:
<button id="button1">Red</button><br />
<button id="button2">Green</button><br />
<button id="button3">Yellow</button><br />
<button id="button4">Blue</button><br />
<p id="test">Click To see What you have clicked</p>
How would I make a two arrays see if a certain value is the same? 我如何使两个数组查看某个值是否相同?
Lets say, that the generated array is : [1,2,3,4,1,2,3] and i am at position 5 and i press 2, how would i check that the two numbers match? 可以说,生成的数组是:[1,2,3,4,1,2,3],我在位置5并按2,如何检查两个数字是否匹配? Thanks in advance
提前致谢
The easiest way to check one at a time that position i
of your array is x
is 一次检查一个数组的位置
i
为x
的最简单方法是
if (gen_arr[i] == x) {
// matches
} else {
// doesn't match
}
So if you conceptualize the flow of your game, you're going to want to, at each button press: 因此,如果您将游戏流程概念化,则需要按一下每个按钮:
gen_arr[i] == x
(and displays game over if it doesn't). gen_arr[i] == x
(如果没有,则显示游戏结束)。 Alternatively, instead of keeping track of which index, you can call gen_array.shift()
to get the first item in gen_array
AND delete it from the array, in a flow kind of like this: 另外,除了跟踪哪个索引之外,还可以调用
gen_array.shift()
来获取gen_array
的第一项gen_array
其从数组中删除,流程如下:
var gen_array = [1,2,3,4,1];
function press_button(button_pressed) {
var supposed_to_be = gen_array.shift();
// at this point, on the first call,
// supposed_to_be = 1, and gen_array = [2,3,4,1]
if (supposed_to_be != button_pressed) {
// game over!
} else {
// you survive for now!
if (gen_array.length() == 0) {
// gen_array is empty, they made it through the entire array
// game is won!
}
}
}
While that represents the general "what to check" at every step, using this verbatim is not recommended as it quickly leads to an unstructured game. 尽管这代表了每个步骤的一般“检查内容”,但不建议使用这种逐字记录,因为它会很快导致游戏结构混乱。
I recommend looking into things called "game state" diagrams, which are basically flow charts which have every "state" of the game -- which in your case, includes at least 我建议您研究一下称为“游戏状态”图的内容,它们基本上是流程图,包含游戏的每个“状态”-在您的情况下,至少包括
And from each state, draw arrows on "how" to transition from one state to the next. 然后从每个状态绘制箭头,说明“如何”从一种状态过渡到另一种状态。 You can do a google search to see examples.
您可以进行Google搜索以查看示例。
Once you have a good game state diagram/flow chart, it's easier to break down your program into specific chunks and organize it better ... and you can usually then see exactly what you need to code and what is missing/what is not missing. 一旦有了良好的游戏状态图/流程图,就可以将程序分解为特定的块并进行更好的组织...更容易了,通常您可以准确地看到需要编写什么代码以及缺少/不缺少的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.