Discuss Scratch

Dimps76
Scratcher
10 posts

Help with score increasing script

Hi,

I've made a basic game. I've written the script so that after every 50 points, it gets more difficult by increasing the number of things on the screen. However, I've had to write the script for every 50 points, ie after 50 points, after 100 points, after 150 points, etc etc, all the way up to 1000.

I'm trying to find a way to shorten that script in a repeating or forever loop. Something like “when score = score+50 create a clone”. <— that doesn't work, as the score goes up by 10 each time you collect a ball, so it never reaches the 50/100/150 mark.

Is there a way to do this? It has me stumped. I have this repeated over and over with increments of 50 up to 1000.

wait until <[score] = (50)>
switch costume to (pick random (1) to (10))
create clone of [myself v]

Last edited by Dimps76 (May 11, 2017 16:13:20)

drmcw
Scratcher
1000+ posts

Help with score increasing script

if <((score) mod (50))=[0]> then
create clone of [whatever v]
end
Dimps76
Scratcher
10 posts

Help with score increasing script

Thanks for the reply, but I'm not sure that will help. Maybe I've not explained it well enough.

I have a long repeating list of scripts

wait until <[score] = (50)>
switch costume to (pick random (1) to (10))
create clone of [myself v]
wait until <[score] = (100)>
switch costume to (pick random (1) to (10))
create clone of [myself v]
wait until <[score] = (150)>
switch costume to (pick random (1) to (10))
create clone of [myself v]
wait until <[score] = (200)>
switch costume to (pick random (1) to (10))
create clone of [myself v]
and so on all the way to 1000, in 50 point increments. Is there a way to shorten this?
drmcw
Scratcher
1000+ posts

Help with score increasing script

Put my script in a loop….much smaller isn't it? Ideally it should be

when I receive [update score v]
change [score v] by (10)
... // my script

Last edited by drmcw (May 11, 2017 17:52:40)

Dimps76
Scratcher
10 posts

Help with score increasing script

Thanks again for replying, but I'm messing it up even more now.

I've shared the project in case you want to look inside and help me with where I am going wrong. It's called Dodge(foot)ball v2
drmcw
Scratcher
1000+ posts

Help with score increasing script

Dimps76
Scratcher
10 posts

Help with score increasing script

Thank you so much! I'm not sure what exactly you've done, but all that horrible lengthy script has gone! I'm not entirely sure what (mod) does either, is it like a divisor? if score is divisible by 50, then…?!?! I'm a little confused, but it works. Massive thanks again.
rpglurker
Scratcher
100+ posts

Help with score increasing script

mod is a function that divides by the number and returns the remainder….

so 10 mod 3 would be 10/3 = 3 remainder 1…so mod returns 1
10 mod 4 = 2
10 mod 5 = 0

again, mod returns the remainder of a division problem… so score mod 50 = 0 happens every 50 points at 0, 50, 100, 150, 200, etc…


Happy scratching!!!
Dimps76
Scratcher
10 posts

Help with score increasing script

Thanks rpglurker
Birdlegs
Scratcher
1000+ posts

Help with score increasing script

Mod divides the first number by the second and then gives you the remainder

In other words, if you had a variable increasing by one every frame, and a second variable giving the mod 32 of the first variable every frame, you'd see the “mod” variable repeatedly count up to 32 and then roll back to 0.

Edit: Ah! Someone got to it before me

Last edited by Birdlegs (May 11, 2017 19:16:07)

rpglurker
Scratcher
100+ posts

Help with score increasing script

you can do some cool things with mod…for example…

let's say you had the number of days since the beginning of the year….you could use mod 7 to figure out which day of the week it is….

lets say it is 2:00…what time will it be 2500 hours from now:

2 + (2500 mod 24) = 6… it will be 6:00…

division will tell you how many days: 2500 / 24 = 104.16666666666 so 104 days

mod is a nice way of doing things periodically like every 50 points: when score mod 50 == 0… because the remainder = 0 when the score is 0, 50, 100, etc… but you have to be careful not to miss the score if you are adding points by more than 1…if the score jumps from 49 to 52 because you earned 3 points, the mod will not be 0 and it will get skipped…to avoid this, you can create a level variable and then do things when score / 50 > level….then you just have to remember to add one to the level so it will wait another 50 points before it runs again.

mod is a very useful math function for programmers…
Birdlegs
Scratcher
1000+ posts

Help with score increasing script

Come to think of it, an alternative would be:

(([floor v] of ((Score) / (50))) * (50))

This will give you the nearest multiple of 50, rounding down (So, you know, 0, 50, 100, 150). The neat thing about this is that it only changes when you get to the next multiple of fifty, so you could detect change in that to determine when to create a clone. That solves the problem of potentially skipping over mod0
Dimps76
Scratcher
10 posts

Help with score increasing script

Thanks all

JoeyTheChicken wrote:

Come to think of it, an alternative would be:

(([floor v] of ((Score) / (50))) * (50))

This will give you the nearest multiple of 50, rounding down (So, you know, 0, 50, 100, 150). The neat thing about this is that it only changes when you get to the next multiple of fifty, so you could detect change in that to determine when to create a clone. That solves the problem of potentially skipping over mod0

Would this work for levels? So I'm using backdrops for levels, but by using
if <(((score)) mod (50)) = [0]> then
switch backdrop to [next backdrop v]
end
it changes the backdrop when the score is 0. If I used floor, would it remove that problem? If so, how do I use floor? I can't seem to get it into the correct block?

Birdlegs
Scratcher
1000+ posts

Help with score increasing script

Dimps76 wrote:

Thanks all

JoeyTheChicken wrote:

Come to think of it, an alternative would be:

(([floor v] of ((Score) / (50))) * (50))

This will give you the nearest multiple of 50, rounding down (So, you know, 0, 50, 100, 150). The neat thing about this is that it only changes when you get to the next multiple of fifty, so you could detect change in that to determine when to create a clone. That solves the problem of potentially skipping over mod0

Would this work for levels? So I'm using backdrops for levels, but by using
if <(((score)) mod (50)) = [0]> then
switch backdrop to [next backdrop v]
end
it changes the backdrop when the score is 0. If I used floor, would it remove that problem? If so, how do I use floor? I can't seem to get it into the correct block?

Using floor would remove that problem, yeah, but it depends on the way you use it.

The way I'd recommend is to just set a second variable (I'll call it “PreviousScoreNearest50”) to the score's nearest multiple of fifty right before you change the score, then check that value against the new score (Again, nearest multiple of fifty) and, if Score and PreviousScoreNearest50 are not the same, then you know it passed a multiple of fifty and you can then do whatever scripts you plan for changing levels

I'll explain the “floor” trick a little better, though.

Basically, floor is the same as rounding to the nearest whole number, but it always rounds down. You can manipulate that and get the nearest fifty (Rounding down) by dividing a number by fifty (Or whatever number you want the nearest multiple of), taking the floor of that number, and multiplying it back up by fifty, so anything from 0-49 will become 0, anything from 50-99 will become 50, anything from 100-149 will become 100, and so on.

Here's some scripts to get you started:

when I receive [Points Scored v]
set [PreviousScoreNearest50 v] to (([floor v] of ((Score) / (50))) * (50))
change [Score v] by (10)
if <not <(([floor v] of ((Score) / (50))) * (50)) = (PreviousScoreNearest50)>> then
broadcast [Next Level v]
end
rpglurker
Scratcher
100+ posts

Help with score increasing script

Here is a great way to do this with levels

when green flag clicked
hide
set [level v] to [1]
create clone of [myself v]
forever
wait until <((score) / (50)) > (level)>
create clone of [myself v]
change [level v] by [1]
end
Dimps76
Scratcher
10 posts

Help with score increasing script

I haven't tried any of these suggestions yet, but will in a bit, thanks guys.

I have a messy way of doing it, and it works for now.

wait until <(score) = [50]>
switch backdrop to [level 2 v]
go to x: (0) y: (0)
show
say (backdrop name) for (2) secs
hide
wait until <(score) = [100]>
switch backdrop to [level 3 v]
go to x: (0) y: (0)
show
say (backdrop name) for (2) secs
hide

The hide/show is to show a sprite that announces the start of the next level.

However, I'd now like everything to “pause” between levels. I tried a broadcast, but couldn't get that to work, and then tried setting pause to 0 for ‘running’ game, and set pause to 1 for when I want it paused, but couldn't get it to ‘run’ again after the pause. I had -

set [pause v] to [0]
wait until <(pause) = [1]>
stop [other scripts in this sprite v]

I tried to add

wait (3) secs
set [pause v] to [0]

to ‘restart’ it, but that didn't work either. (The stop did have a connecting block on it..can't work out how to get it to show on here)

Off to try your suggestions now.

Last edited by Dimps76 (May 19, 2017 21:11:38)

Dimps76
Scratcher
10 posts

Help with score increasing script

Just thinking about broadcasts and pauses again….If I did 2 separate ones….



broadcast [level 1 start v]
and then the script for changing levels/switching backgrounds. Then on another sprite…

when I receive [broadcast level 1 start v] 

and then the rest of the ‘playing’ script. Then at the 50 point mark..

broadcast [level 1 stop v] 
stop [all v]

Then back to the first sprite and…

broadcast [level 2 start v]

and so forth…Question is, would the broadcast message ‘override’ the stop all? Or once stop all has been activated, that's it? There's no way to re-run the script?
Birdlegs
Scratcher
1000+ posts

Help with score increasing script

Technically, you can override the “stop all” by doing

when green flag clicked
forever
reset timer
end

when [timer v] > (.1)
What you want to happen after "stop all"

But I'd recommend using the “wait” thing you had before with a pause variable. It gives you more control

If you share the project, we can help you debug it! It's hard to say what's not working just by looking at once part of the code
Dimps76
Scratcher
10 posts

Help with score increasing script

JoeyTheChicken wrote:

Technically, you can override the “stop all” by doing

when green flag clicked
forever
reset timer
end

when [timer v] > (.1)
What you want to happen after "stop all"

But I'd recommend using the “wait” thing you had before with a pause variable. It gives you more control

If you share the project, we can help you debug it! It's hard to say what's not working just by looking at once part of the code


Thanks.Can you tell that I like to try and debug myself It's shared now. This is for a uni assignment so I'm trying to have as little ‘help’ as possible, but this is only about my 3rd/4th week of working with Scratch, so I'm still learning. Don't worry, will reference (and credit) everyone's help/input
Birdlegs
Scratcher
1000+ posts

Help with score increasing script

Ah, I relate to that! It's most fun that way!

(And don't worry about crediting me–It's fun to help!)

Anyhow, I dug into the project, and adding a “pause” function worked fine for me so long as every forever loop started with “wait until (pause) = 0”

Of course, that relies on pause being set properly, cause never setting it back to zero will obviously pause the whole thing forever.

Powered by DjangoBB