简体   繁体   English

试图理解“西蒙”游戏

[英]trying to understand "Simon" game

I'm Working on Simon game.我正在研究西蒙游戏。 I need help with understanding how to reference an id and creating an action without a click but say with page refresh.我需要帮助来理解如何引用一个 id 并在没有点击的情况下创建一个动作,但要说页面刷新。

1) on screen refresh (the start of the game), I CAN randomize the color id chosen...and save it in a var called randomColorChosen... but I can NOT figure out how to make that var randomColorChosen, make the same actions as if they were a clicked on button like my code at the bottom. 1)在屏幕刷新(游戏开始时),我可以随机化选择的颜色ID......并将其保存在名为randomColorChosen的var中......但我无法弄清楚如何制作该var randomColorChosen,制作相同操作就好像它们是单击按钮一样,就像我底部的代码一样。

2) My btn click and animate color and play sound... work fine, 2)我的 btn 单击并动画颜色和播放声音......工作正常,

I have tried many iterations of my newbie coding... Now I'm just guessing and here is it's last state....我已经尝试了我的新手编码的许多迭代......现在我只是猜测,这是最后的状态......

... random Color Chosen works... its what's next I need help (explanations on) ......随机颜色选择有效......接下来我需要帮助(解释)

... $(document).keydown(function(event) {} works and tested with an alert() ... $(document).keydown(function(event) {}使用 alert() 工作和测试

... but the rest, down until my but clicks, I have changed so many times my head is spinning. ......但其余的,直到我的但点击,我已经改变了很多次我的头在旋转。 Help my understand where I am dumb AF!帮助我了解我在哪里愚蠢 AF! lol哈哈

... user btn click event action and sound... works fine! ... 用户 btn 单击事件动作和声音 ... 工作正常!

Here is my code so far:到目前为止,这是我的代码:

 buttonColors=["btn green","btn red", "btn yellow", "btn blue"]; randomColorChosen=buttonColors[Math.floor(Math.random() * (buttonColors.length))]; $(document).keydown(function(event) { setTimeout(2000); $("#id").attr(function(randomColorChosen){ $(this).fadeOut(100).fadeIn(100); var myPadColor = $(this).attr("#id"); }); }); $(document).ready(function() { $(".btn").click(function(event) { $(this).fadeOut(100).fadeIn(100);//when a "this" button is clicked .fadeOut(100).fadeIn(100); var myPadColor = $(this).attr("class"); ; switch (myPadColor) { case "btn green": var greenPad = new Audio('sounds/green.mp3'); greenPad.play(); break; case "btn red": var redPad = new Audio('sounds/red.mp3'); redPad.play(); break; case "btn yellow": var yellowPad = new Audio('sounds/yellow.mp3'); yellowPad.play(); break; case "btn blue": var bluePad = new Audio('sounds/blue.mp3'); bluePad.play(); break; default: console.log(myPadColor); } }); });

It was a little unclear what you are trying to accomplish.有点不清楚你想要完成什么。 I created an example with some possible improved code.我创建了一个示例,其中包含一些可能的改进代码。

 $(function() { var seq = []; var player = []; var c; var round = 0; var sounds = [ new Audio('sounds/green.mp3'), new Audio('sounds/red.mp3'), new Audio('sounds/yellow.mp3'), new Audio('sounds/blue.mp3') ]; function getRandom() { return Math.floor(Math.random() * 4); } function initSeq(n) { for (var i = 0; i < n; i++) { seq.push(getRandom()); } } function flash(b) { //console.log("Flashing Button " + b); var btn = $("#game-board .btn").eq(b); btn.fadeTo(300, 1.0, function() { btn.fadeTo(300, 0.35); }); sounds[b].play(); } function endGame() { $("#game-board").hide(); $("h1").show(); $("h1").after("<h2>Gome Over. Score: " + player.length + "</h2>"); } function waitUserInput() { c = setInterval(endGame, 10 * 1000); } function playSequence(x) { if (x == 0 || x == undefined) { x = 20; } $.each(seq, function(i, s) { if (i < x) { flash(s); } }); } function nextRound() { playSequence(++round); } function initGame() { initSeq(20); player = []; c = null; round = 0; nextRound(); } $(document).keydown(function(event) { $("h1").hide(); $("#game-board").show(); initGame(); }); $("#game-board .btn").click(function(event) { var i = $(this).index(); flash(i); player.push(i); }); $("#playSeq").click(function() { var p = $(this).next().val(); console.log("TEST: Play Sequence to " + p + " Position."); if (seq.length == 0) { initSeq(20); $("#game-board").show(); } //console.log("TEST", seq); playSequence(p); }); });
 #game-board { width: 410px; white-space: normal; } .btn { width: 200px; height: 200px; display: inline-block; opacity: 0.35; padding: 0; margin: 0; border: 0; } .green { background: green; border-radius: 199px 0 0 0; } .red { border: 0; background: red; border-radius: 0 199px 0 0; } .yellow { border: 0; background: yellow; opacity: 0.65; border-radius: 0 0 0 199px; } .blue { border: 0; background: blue; opacity: 0.65; border-radius: 0 0 199px 0; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="test-bar"> <button id="playSeq">Play To</button> <input type="number" value="1" style="width: 2.5em;" /> </div> <h1>Press Any Key to Begin.</h1> <div id="game-board" style="display: none;"> <div class="btn green"></div> <div class="btn red"></div> <div class="btn yellow"></div> <div class="btn blue"></div> </div>

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

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