mercredi 1 avril 2020

Button response issues for a Simon Says game

Ive been developing a Simon says game recently that adds to the score if you click the correct button which there are 3, 1) green 1 2) red 2 3) trick

I've noticed that when I run the game and click the appropriate buttons only one will add to the score whilst the other two subtract from it (Regardless of what the statement says). Im unsure why this is the case and was wondering if anyone had any insights). My thought is that the if functions don't seem to correlate to the new statement that is generated.

Any suggestions are appreciated,

Javascript:

var answers = [
    "Simon says click red !", 
    "Simon says click green !",
    "Simon says click 1 !", 
    "Simon says click 2 !", 
    "Click green",
    "Click red", 
    "Click 1", 
    "Click 2!"
];

var score = 0;
var total = document.getElementById("score");
var statement  = document.getElementById("instruct");
var randomStatement = answers[Math.floor(Math.random() * answers.length)];



function refresh(){
    var randomStatement = answers[Math.floor(Math.random() * answers.length)];
    statement.innerHTML = randomStatement;
}

function pressTrick(){
if(randomStatement === "Click green" || randomStatement === "Click red" || randomStatement === "Click 1" || randomStatement === "Click2!"){
    score++;
    total.style.color = "green";
    total.innerHTML = "Total score: " + score;
    setTimeout(refresh,900);
} else {
    total.style.color = "red";
    score--;
    total.innerHTML = "Total score: " + score;
    setTimeout(refresh,900);

}
}

function pressRed(){
    if(randomStatement === "Simon says click red !" || randomStatement === "Simon says click 2 !"){
    score++;
    total.style.color = "green";
    total.innerHTML = "Total score: " + score;
    setTimeout(refresh,900);    
}  else {
   total.style.color = "red";
   score--;
   total.innerHTML = "Total score: " + score;
   setTimeout(refresh,900);


}}


function pressGreen(){
    if(randomStatement === "Simon says click 1 !" || randomStatement === "Simon says click green !"){
    score++;
    total.style.color = "green";
    total.innerHTML = "Total score: " + score;
    setTimeout(refresh,900);
}   else {
    total.style.color = "red";
    score--;
    total.innerHTML = "Total score: " + score;
    setTimeout(refresh,900);

}}

function  start(){
    var i = 60;
    var count = setInterval(timer,1000);
    refresh();



function timer() {
    var display = document.getElementById("timer");
    var finished = document.getElementById("heading");


    if(i < 1){
   clearInterval(count);
   finished.innerHTML = "Game Over! Your final Score is : " + score;
   display.style.color = "red";
   display.style.fontWeight = "bold";
   document.body.style.backgroundColor = "black";

} else if(i <= 10) {
    i--;
   display.style.color = " red";
   display.style.fontWeight = "bold";
   display.innerHTML =  i + " Seconds Left";
} else  {
    i--;
    display.style.color = "green";
    display.style.fontWeight = "bold";
    display.innerHTML =  i + " Seconds Left";  
}}}

HTML:

<html>
    <head>
        <title> Simon Says! </title>
        <link rel = "stylesheet" href = "simonsays.css"/>
    </head>
    <body>

    </br>
        <h1> Test Your Reflexes! </h1>
        <div id = "heading"; class = "h2"> Click on Simon to Begin! </div>
    </br> 
        <img src = "boy.jpg" onclick = "start()"; onclick = "timer()"; onclick = "returnStatement";/>
    </br>
    <div id = "instruct" class="statement"></div>
</br>
        <div class = "align">
        <input type = "button" class = "button2" id = "button2" value = "1" onclick = "pressGreen()"; ></input> 
        <input type = "button" class = "button" id = "button" value = "2" onclick = "pressRed()"; ></input>
        <input type = "button" class = "button3" id = "button3 " value = "Trick" onclick = "pressTrick()";></input>
</div>
</br>
        <div id = "score" class = "score"><b> Score: </b></div>
        <div id = "timer" class = "timer"><b> Time left: </b></div>
        <script src = "simonsays.js"></script>
    </body>


</html>

Thank you!

Aucun commentaire:

Enregistrer un commentaire