简体   繁体   English

西蒙游戏如何进行?

[英]Simon Game how to proceed?

So I am trying to build a Simon Game. 所以我试图建立一个西蒙游戏。 First of all I created a random array with 20 values that varies between 1 and 4 (That would identify each color of the game). 首先,我创建了一个随机数组,其中包含20个值,该值在1-4之间变化(这将标识游戏的每种颜色)。 So 1 would be red, 2 would be blue, 3 would be green and 4 would be yellow. 所以1将是红色,2将是蓝色,3将是绿色,4将是黄色。 So I got the array which is something like this: 所以我得到了这样的数组:

var arrayRandomColors = [1, 3, 1, 2, 4, 3, 2, 1, 3, 2, 1, 2, 4, 1, 2, 3, 4, 1, 3, 2]

Now this is de order of sounds and colors of my Simon Game. 现在,这是我的西蒙游戏的声音和颜色的混乱。 However, I now need to go over there array like this: 但是,我现在需要像这样遍历数组:

Level 1 - [1] -> Level 2 - [1, 3] -> Level 3 - [1, 3, 1] ->  Level 4 - [1, 3, 1, 2]... etc. And then each time it loops over these arrays it plays a sequence o sounds that corresponds to the current level.

So that's what I did: 这就是我所做的:

var j=2;
    var arrayPlay = [];
    for (var j=2; j<22; j++){
        for(var i=1; i<j; i++){
            arrayPlay.push(arrayRandomColors[i-1]);
        }
        console.log(arrayPlay); //[1] -> [1,3] -> [1,3,1]...etc
        arrayPlay = [];
    }

So far, I managed to get all the arrays I want. 到目前为止,我设法获得了所有想要的阵列。 However, I got stuck about how to proceed, because I need to go over this numbers slowly, showing the colors being selected and playing a sound while it loops, and the for loop is just too fast. 但是,我对如何继续工作感到困惑,因为我需要慢慢遍历这些数字,显示选定的颜色并在循环时播放声音,而for循环太快了。 I thought about doing something like this: 我考虑过做这样的事情:

for (var j=2; j<22; j++){
        for(var i=1; i<j; i++){
            arrayPlay.push(arrayRandomColors[i-1]);
        }
        //console.log(arrayPlay);
        for (var k=0; k<arrayPlay.length; k++){
            var id = setInterval(function(){
                console.log("entrou");
            if (arrayPlay[k] == 1){
                $red.css("background-color", "#ffc1c1");
                $red.delay(500);
                $red.css("background-color", "red");
            }
            else if (arrayPlay[k] == 2){
                $blue.css("background-color", "#a5b0f7");
                $blue.delay(500);
                $blue.css("background-color", "blue");
            }
            else if (arrayPlay[k] == 3){
                $green.css("background-color", "#aff7a5");
                $green.delay(500)
                $green.css("background-color", "green");
            }
            else if (arrayPlay[k] == 4){
                $yellow.css("background-color", "#fffb91");
                $yellow.delay(500);
                $yellow.css("background-color", "yellow");
            }

            }, 500);
        }
        arrayPlay = [];
        console.log("\n");
    }

But it doesn't work. 但这是行不通的。 Can anyone help me out? 谁能帮我吗?

The loop like you said runs too fast. 您说的循环运行得太快。 Your idea to use interval is a good step, but there is a gotcha. 您使用间隔的想法是一个好的步骤,但是有一个陷阱。 You used a fixed interval of 500. Now try to imagine it, the loop runs fast and creates a bunch of intervals all 500, so they all will run almost at the same time after 500. We didn't solve the issue, we just delayed the execution by 500. 您使用了500的固定间隔。现在试想一下,循环运行很快,并创建了一堆全部500的间隔,因此它们都将在500之后几乎同时运行。我们没有解决问题,我们只是延迟执行500次。

The solution is to use an incrementing interval. 解决方案是使用递增间隔。 Multiply it by the index (remember to add one because it is zero based). 将它乘以索引(记住要加一,因为它是基于零的)。 Something like: 就像是:

setInterval(..., 500 * (k+1));

You probably need to use the index j or a combination of k and j . 您可能需要使用索引jkj的组合。 I will leave that to you to explore, but this answer will get you started. 我将让您继续探索,但是这个答案将帮助您入门。

You could use setTimeout() , as described here . 你可以使用setTimeout()如所描述这里 That says: 说的是:

The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds. setTimeout()方法将在指定的毫秒数后调用一个函数或对一个表达式求值。 setTimeout(function(){ alert("Hello"); }, 3000);


.(w3Schools references are not the best ones and not good seen here, but it is not a more elaborate thing, so this link should help.) (w3Schools参考文献不是最好的参考文献,在这里看不见,但这不是更复杂的事情,因此此链接应该有所帮助。)

I myself am trying to build the Simon game(I am just starting in front end development). 我本人正在尝试构建Simon游戏(我只是在前端开发中开始)。 Here are some of the pointers that I made before starting out. 这是我开始之前所做的一些指示。

I need to start with one coloring tile to bleep once and followed by two coloring tiles to bleep once each and so on for number of levels you decide. 我需要先从一个着色图块开始一次着色,然后再从两个着色图块开始一次着色,依此类推,直到您决定的级别数为止。 So, my approach is to have 4 colored tiles already defined say 1 for red,2 for yellow,3 for blue,4 for green tiles. 因此,我的方法是已经定义了4个彩色图块,其中1表示红色,2表示黄色,3表示蓝色,4表示绿色。 Now for each level I can generate random numbers(1 through 4) via a function. 现在,对于每个级别,我可以通过一个函数生成随机数(1到4)。 So, if I am using a loop that runs equal to number of times what level the game is on, I'll have one random number generated for level 1, 2 random numbers generated for level 2 and so on. 因此,如果我使用的循环运行次数等于游戏级别,则将为级别1生成一个随机数,为级别2生成2个随机数,依此类推。

For each random number, you could bleep the respective colored tile. 对于每个随机数,您可以使相应的彩色拼贴变暗。

Now for the time gap between the bleeps of colored tiles, you could make use of setInterval function to run after certain amount of milliseconds, which is only cleared only when required number of tiles have bleeped. 现在,对于有色瓷砖之间的时间间隔,您可以使用setInterval函数在一定的毫秒数后运行,只有在所需数量的瓷砖出血后才清除。 Eg On level 4, the setInterval should run for 4 times with a time gap of 1000 ms. 例如,在级别4上,setInterval应该以1000 ms的时间间隔运行4次。

Now, as advancing through levels you could decrease the time gap to make the game faster and more challenging. 现在,随着级别的提高,您可以减少时间间隔,从而使游戏更快,更具挑战性。

Also, note down how you're going to let user know, that it's their turn to play now and also, once user is done with their level, how do you tell the application it should not wait for more user input and analyse the result of this level and proceed or not for the next level. 另外,记下您要如何让用户知道,现在该轮到他们玩了,并且,一旦用户完成了级别,您如何告诉应用程序它不应该等待更多用户输入并分析结果级别,然后继续进行下一个阶段。

I know, this is all words, but believe me, if you have all this planned, your effort will be less and more fruitful. 我知道,这全是话,但请相信我,如果您计划好了所有这些,那么您的努力就会越来越有成果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM