Got A Question With My Snake

I got a question for you im trying to learn Javascript and im using processing and i Make A game calls Snake I try to add a few things like a pause button. It works almost the only thing is that, when I pause the length of mine snake is reseted to 1. I Created a var pause = false; It wil be set to true if you press the pause button

    this.death = function() {
        if (!pause) {
          for (var i = 0; i < this.tail.length; i++) {
            var pos = this.tail[i];
            var d = dist(this.x, this.y, pos.x, pos.y);
            if (d < 1) {
              console.log('starting over');
              this.total = 0;
              this.tail = [];
              hit.play()
          }
        }
       }
    }

The link of my Own Project Is here : https://github.com/jolkedejonge/Code-Js

Answers

  • Im Sorry But This Does Not work when i use it in my loop like this it won't work function keyPressed() { if (pause == true) { noLoop(); } if (!pause) { if (keyCode === UP_ARROW) { s.dir(0, -1); } else if (keyCode === DOWN_ARROW) { s.dir(0, 1); } if (keyCode === LEFT_ARROW) { s.dir(-1, 0); } else if (keyCode === RIGHT_ARROW) { s.dir(1, 0); } } }

  •  function keyPressed() {
      if (pause == true) {
        noLoop();
      }
         if (!pause) {
           if (keyCode === UP_ARROW) {
            s.dir(0, -1);
        } else if (keyCode === DOWN_ARROW) {
            s.dir(0, 1);
        }
          if (keyCode === LEFT_ARROW) {
            s.dir(-1, 0);
        } else if (keyCode === RIGHT_ARROW) {
            s.dir(1, 0);
          }
        }
      }
    
  • Im so sorry I type it in the wrong section oops

  • But it Still not Works

  •   this.death = function() {
         if (pause == true) {
           noLoop();
         } 
       for (var i = 0; i < this.tail.length; i++) {
            var pos = this.tail[i];
            var d = dist(this.x, this.y, pos.x, pos.y);
            if (d < 1) {
              console.log('starting over');
              this.total = 0;
              this.tail = [];
              hit.play()
          }
        }
       }
    
  • So, with this last comment, are you sharing your solution or it is not working yet?

    Kf

  • its not Working Yet

  • let paused;
    
    function keyPressed() {
      if (key == 'P')  (paused ^= true)? noLoop() : loop();
    }
    
  • What Do You main with The 'P'

  • You need to choose some key(s) to toggle the paused state.
    In my own sample above I've picked 'P' for that action. ;;)

  • P as in your keyboard, after you hit the letter P. What Goto is doing there is to assign a value to pause and then, based on this value, he calls noLoop() or loop().

    Kf

  • Oh Thank you ill try

  • I might Now What the problems is if (d < 1) { d = dist and when my pause is the direction will be set dir(0, 0) but i guess that the game thing that the dis = 0 if somebody knows how to fix i tryed much

Sign In or Register to comment.