Need help with a students game

I cant figure out how to help my student with his game. Your help is much appreciated.

On function “Pblue” he wants to make the carrots variable to go up by 2 every second when the score is over 200 and he presses the b button.

How can he accomplish this. Thank you in advance!

When I click on this link, I am getting a message “This project is not available for sharing”. Will you try sharing the game again so we can look at the code?

Sorry, didn’t realize the link didn’t work.

Here is a working link:

We worked on it a bit more after we posted the question. After getting some information from W3 Schools we added:

setInterval(function(){ (carrots = carrots + 4) }, 1000);

to line 81 and it worked.

Is there a better way to accomplish making the score increase on its own after pressing the “b” button when the score is > 200?

This is beyond my knowledge and just want to make sure I’m helping my student correctly.

Again, thank you in advance!

Mr Sanchez,

Playing the game I’m still confused as to what is actually supposed to be happening. In general, which your student has correctly, you need two conditions to be true - 'b' pressed and score > 200, and then the code inside works. Does your student want the score to increase without pressing it? setInterval completes a function (which your student have adding to the carrots) every so many milliseconds (which your student has set to 1000 or 1 second).

In other programming languages they have sleep() (Swift) or wait() (Python) to accomplish this. Any more details on what the student is trying to accomplish OR is that just it, when the score is greater than 200 and B button pressed the score will increase by 4 every second - either way, let me know!

Brad

The setInterval thing may seem weird at first but the only other way that I can think of starting something and then only doing it every 1sec or after a time interval is a little more involved. And the awesome thing is that y’all had a problem, no one knew how to solve it, and you used your resources to make it work. I think that is awesome and totally what is great about the class and computer science in general.

  1. I would make a variable that would be the amount to add to the carrots. (carrotsToAdd)
  2. I would make a boolean variable that would control whether or not to start adding to the carrots. (startAdding)
  3. I would make a frame count, to count a certain number of frames before adding to the carrots. (this is the 1000 in the setinterval) . (frameCount)

In the if statements

if (keyWentDown("g")&&carrots >= 1200) {
    carrots = carrots - 1200;
     setInterval(function(){ (carrots = carrots + 8) }, 1000);
    
  }

In the if statement, it would make the boolean true, set the amount of carrots to add every time, and then have another if statement outside of the above if statement to check whether to add carrots and actually add the carrots based on whether so many frames had gone by.

if ( startAdding)
{
    frameCount++ ;
    //60 being 60fps for 60 frames is one second. 
    if ( frameCount == 60)
    {
         frameCount = 0 ; 
         carrots = carrots + carrotsToAdd ;
    }
}

Hopefully this makes some sense.

Hello Brad,

I just checked with the student and that was all he wanted to do. Thanks for the help!

Just showed the example you provided to my student. Thank you for the help!