Javascript 如何制作基于数组的Simon Js游戏?
我正在写一个简单的js simon游戏,我不知道怎么做 我知道: 我需要创建两个数组和一个level(score)变量Javascript 如何制作基于数组的Simon Js游戏?,javascript,arrays,2d-games,Javascript,Arrays,2d Games,我正在写一个简单的js simon游戏,我不知道怎么做 我知道: 我需要创建两个数组和一个level(score)变量 需要添加从1到4(含)的随机生成的数字 对于第一个数组,当按下四个按钮中的一个时,值 如果第二个数组不是 与第一个数组大小相同或更大。每次添加一个值时 对于第二个数组,检查该值是否等于 如果第一个数组中的位置相同,则清除两个数组,然后 将levelvar设置为1,并警告“gameover”,这意味着如果您得到一个 错了,你不能继续。如果第二个数组的长度 匹配级别变量,向数组1
- 需要添加从1到4(含)的随机生成的数字
对于第一个数组,当按下四个按钮中的一个时,值
如果第二个数组不是
与第一个数组大小相同或更大。每次添加一个值时 对于第二个数组,检查该值是否等于
如果第一个数组中的位置相同,则清除两个数组,然后 将levelvar设置为1,并警告“gameover”,这意味着如果您得到一个
错了,你不能继续。如果第二个数组的长度
匹配级别变量,向数组1添加一个随机数,清除
数组2,增量levelvar
Red
绿色
黄色
蓝色
点击查看您点击的内容
如何使两个数组看到某个值是否相同
假设生成的数组是:[1,2,3,4,1,2,3]
我在位置5,按2,我怎么检查这两个数字是否匹配?
提前感谢一次检查一个阵列的位置
i
是x
是最简单的方法
if (gen_arr[i] == x) {
// matches
} else {
// doesn't match
}
因此,如果你将游戏流程概念化,你会想在每次按下按钮时:
gen\u array.shift()
来获取gen\u 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!
}
}
}
虽然这代表了每个步骤的一般“检查内容”,但不建议使用这种逐字逐句的方式,因为它会很快导致非结构化的游戏
我建议研究一下“游戏状态”图,它基本上是流程图,包含游戏的每个“状态”——在你的例子中,至少包括
if (gen_arr[i] == x) {
// matches
} else {
// doesn't match
}
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!
}
}
}