Help on Project - Design a Game

My student is trying to make a “pong” game and for every 30 frames/seconds he want to get the score to increase by one. Is there an easier way that he can do this with out having to copy and paste the following over and over again?

if (World.frameCount > 30) {
textSize(25);
fill(“white”);
text(“1”, 200, 100);
}

1 Like

@langilla1,

Yes! There is a function called “modulo” that allows you to make things happen on a specified interval. It basically does a division problem and looks at the remainder of the problem, so every time through the draw loop, it divides the WorldSeconds by 30 and looks at only the remainder. If the remainder is 0 (or any other number really), you can have a variable update…

In this case, the student needs to create the variable (such as var score = 0;) at the top of the code. Then, I would suggest moving the score blocks right below the black background code in the main draw loop. Then, instead of hard coding the score as 1, 2, 3, 4, etc, use the name of the score variable.

Finally, I’ll help you with the modulo as it is a little out of scope for CSD, but it’s pretty simple code… You can see that the timer function will now only do one thing … look for when the modulus is 0 and change the score variable. Then, the main program displays the score when it updates the background.

Screen Shot 2021-11-17 at 9.52.24 AM

Hope this helps!

Mike

1 Like

Thank you for your help on this. We still can’t get it yo work. This is what he has done.

https://studio.code.org/projects/gamelab/jYY4jlh_JH_xWHlV5yZJoODPQCTGT2ucXHOJd_rRazs

@langilla1,

That looks great! Only one small fix is needed. Inside the timer function, change the worldFrameRate to worldFrameCount. frameRate uses decimals and frameCount uses whole numbers, so the remainder when dividing a number with decimals won’t ever be 0, so the counter isn’t working, but with worldFrameCount, it will work.

Let us know!

Mike

It worked - thank you!

1 Like