Problem with boolean changing to false randomly

So I’ve made another code using if else instead of a switch, but more problems arise. For some stupid reason, whenever I press “1” on my keyboard, it sets game to false?

As you can see in the code below, I didn’t make it so that it sets game to false whenever I click 1, only when I press “p”. Same goes for the other 2 parts which I haven’t included here that change xSpeed to something different, they also set game to false, but the statements work fine (xSpeed works correctly).

If you saw my previous post, you would have noticed that I changed origxSpeed to orig cause it was too long lol. I won’t include the variables up top but please let me know if you need them, I will be happy to add them.
I feel nervous to post this as it seems so impossible for game to be set to false unless I accidentally typed it in the “if key==1 statement”. That would be very embarrassing. So embarrassing I would delete the post (that is, if it doesn’t get deleted by someone else first lol).

Oh, and I assure you, resetGame() does not set game to false, I double, triple, quadruple checked lol.

void keyPressed() {
  if ((key == 'p' || key == 'P') && game==false) {
    game=true;
    noCursor();
    xSpeed *= 0.5;
    ySpeed *= 0.8;
    diff="Easy";
  } else {
    if ((key == 'p' || key == 'P') && game==true) {
      if (abs(xSpeed)==0.75*orig) {
        xSpeed *= 4/3.0; 
        ySpeed *= 5.0/6;
      } else {
        if (abs(xSpeed)==0.5*orig) {
          xSpeed *= 2;
          ySpeed *= 1.25;
        } else {
          if (abs(xSpeed)==1.6*orig) {
            xSpeed *= 1;
            ySpeed *= 0.625;
          }
        }
      }
    }
    game=false;
    score=0;
    setup();
    cursor();
  }

  if ((key == '1') && game==true) { 
    if (abs(xSpeed)==0.75*orig) {
      xSpeed *= 2.0/3; 
      ySpeed *= 2.0/3;
    } else {
      if (abs(xSpeed)==0.5*orig) {
        xSpeed *= 1;
        ySpeed *= 1;
      } else {
        if (abs(xSpeed)==1.6*orig) {
          xSpeed *= 0.5;
          ySpeed *= 0.5;
        }
      }
    }
    score=0;
    resetGame();
    diff="Easy";
  }
1 Like

Maybe this line is inside the else??

(and outside the if inside the else section)

3 Likes

? I‘m 95% sure that game will be set to false if you press any key other than ‚p‘…

Your Code goes like this :

Any Key is pressed ->
Check if the key is P ->
If not, set Game to false, Score to 0, setup() and cursor(). 
2 Likes

Lol I feel so embarrassed. You guys were right, the game=false was outside the if. If you look at the blue thing on the curly bracket you will see that it is one line before the game=false.
Capture

4 Likes