We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Right now my code is something like this, and I was wondering if there's a way to shorten this code
if (score >10)speed += 5;
if (score >20)speed += 5;
if (score >30)speed += 5;
Answers
why?
it's perfectly clear as it is.
score is 11, score / 10 = 1 so add 5 * 1
score is 24, score / 10 = 2 so add 5 * 2...
that has no bounds checking on it so 60 will add 30 to the original speed etc
if (score < 40)speed += 5 * int((score-1)/10);
For 0 to 10, inclusive, adds nothing
For 11 to 20, inclusive, adds 5
For 21 to 30, inclusive, adds 10
For 31 to 40, inclusive, adds 15
I assume scores is an integer. Note compact code sacrifices clarity which will make it really difficult for maintenance.
Kf
if(score%10==0) speed+=5;
initially 3
if
-lines won't work as expected btwmine needs to be
(score - 1) / 10
rather thanscore / 10
kfrajer noticed the conditions were strictly greater than, mine is actually >=
once the score is 10, it rapidly starts increasing for all of your codes, how do i only make it increase once every 10 pts
If you look at my code with %
Don't call it in draw()
Call it only once every time the score gets increased
Personally, I'd say don't try to make the speed code as compact as possible. Instead, make it as easy to read and change as possible. To me that means isolating the
if
statements withelse
so that each sets its own speed based on the current score.Nice 1 @TfGuy44! :-bd Here's mine as well: >:)
speed += 5 * min(3, score/10);
P.S.: Oops! Just looked the 1st replies and my attempt is almost the same as theirs. 8-X