I'm making a paddle ball game on Scratch (just for fun), and I'm running into a problem with my scoring. If you want to look at the code I already wrote, the link to the game is https://scratch.mit.edu/projects/66541388/ . For some reason, when the game is played the score variable does not actually always change by one. It changes by a different number every time I test it. Any ideas on what the problem is or how to fix it?
Here's the core of the code:
when green flag clicked
set [Score v] to [0]
set x to (0)
set y to (0)
point in direction (pick random (-90) to (90))
forever
if <(y position) < [-146]> then
broadcast [gameOver v]
stop [all v]
end
if <touching [Paddle v]?> then
change [color v] effect by (pick random (1) to (1000))
change [Score v] by (1)
point in direction (pick random (-90) to (90))
end
move (10) steps
if on edge, bounce
end
Your problem is that you are dealing with a race condition. When you test for collision between your paddle and penguin the penguin does not leave the paddle as quick as the detection is getting called again causing it to add more than one to the score. You can add some timing code so that it can only increment score by 1 if a timer is greater than lets say one second. Then reset the timer after every "legal" hit.
There are other ways to deal with these conditions, but you have to be creative.