Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How to Make a Day/Night Cycle
- shrubmaster
- Scratcher
5 posts
How to Make a Day/Night Cycle
I'm in the middle of making a real-time farming game set on local time, and I've got my backgrounds set up to change every 15 minutes. I was trying to set up a moon/sun that scrolls from the left of the screen to the right to simulate the sun/moon rise/set, and I'm trying to figure out if there's a better way to set up the movement rather than 96 if-statements or altering my backgrounds to include the moon/sun.
Bippity Boppity Boop
- ResExsention
- New to Scratch
1000+ posts
How to Make a Day/Night Cycle
I'm in the middle of making a real-time farming game set on local time, and I've got my backgrounds set up to change every 15 minutes. I was trying to set up a moon/sun that scrolls from the left of the screen to the right to simulate the sun/moon rise/set, and I'm trying to figure out if there's a better way to set up the movement rather than 96 if-statements or altering my backgrounds to include the moon/sun.
Again, you could use a timer, and go like:
...
forever
if <(timer) = [900]> then // or however timers measure
broadcast [night v] // or day
switch backdrop to [night v] // or day
end
if <(timer) = [1800]> then // or however timers measure
broadcast [day v] // or night
switch backdrop to [day v] // or night
reset timer
end
end
Last edited by ResExsention (May 22, 2019 04:27:29)
Infrequently active.
It feels weird to see how far we've come. I hope you're well, wherever you are!
- deck26
- Scratcher
1000+ posts
How to Make a Day/Night Cycle
NEVER use the equals wirth the built-in timer as you're very unlikely to hit the exact value - use inequality like greater than or less than. For example, the timer may go from 6.99 seconds to 7.02 seconds between one check and the next so (apparently) never be equal to 7.I'm in the middle of making a real-time farming game set on local time, and I've got my backgrounds set up to change every 15 minutes. I was trying to set up a moon/sun that scrolls from the left of the screen to the right to simulate the sun/moon rise/set, and I'm trying to figure out if there's a better way to set up the movement rather than 96 if-statements or altering my backgrounds to include the moon/sun.
Again, you could use a timer, and go like:...
forever
if <(timer) = [900]> then // or however timers measure
broadcast [night v] // or day
switch backdrop to [night v] // or day
end
if <(timer) = [1800]> then // or however timers measure
broadcast [day v] // or night
switch backdrop to [day v] // or night
reset timer
end
end
Last edited by deck26 (May 22, 2019 08:28:34)
- ResExsention
- New to Scratch
1000+ posts
How to Make a Day/Night Cycle
NEVER use the equals wirth the built-in timer as you're very unlikely to hit the exact value - use inequality like greater than or less than. For example, the timer may go from 6.99 seconds to 7.02 seconds between one check and the next so (apparently) never be equal to 7.I'm in the middle of making a real-time farming game set on local time, and I've got my backgrounds set up to change every 15 minutes. I was trying to set up a moon/sun that scrolls from the left of the screen to the right to simulate the sun/moon rise/set, and I'm trying to figure out if there's a better way to set up the movement rather than 96 if-statements or altering my backgrounds to include the moon/sun.
Again, you could use a timer, and go like:...
forever
if <(timer) = [900]> then // or however timers measure
broadcast [night v] // or day
switch backdrop to [night v] // or day
end
if <(timer) = [1800]> then // or however timers measure
broadcast [day v] // or night
switch backdrop to [day v] // or night
reset timer
end
end
Lol. My logic when I wrote that code was that a timer would always hit every value. Didn't know it was that inaccurate.
Infrequently active.
It feels weird to see how far we've come. I hope you're well, wherever you are!
- deck26
- Scratcher
1000+ posts
How to Make a Day/Night Cycle
It's accurate, not inaccurate.NEVER use the equals wirth the built-in timer as you're very unlikely to hit the exact value - use inequality like greater than or less than. For example, the timer may go from 6.99 seconds to 7.02 seconds between one check and the next so (apparently) never be equal to 7.I'm in the middle of making a real-time farming game set on local time, and I've got my backgrounds set up to change every 15 minutes. I was trying to set up a moon/sun that scrolls from the left of the screen to the right to simulate the sun/moon rise/set, and I'm trying to figure out if there's a better way to set up the movement rather than 96 if-statements or altering my backgrounds to include the moon/sun.
Again, you could use a timer, and go like:...
forever
if <(timer) = [900]> then // or however timers measure
broadcast [night v] // or day
switch backdrop to [night v] // or day
end
if <(timer) = [1800]> then // or however timers measure
broadcast [day v] // or night
switch backdrop to [day v] // or night
reset timer
end
end
Lol. My logic when I wrote that code was that a timer would always hit every value. Didn't know it was that inaccurate.
Scratch will generally loop approx 30 times a second so roughly 0.033 seconds pass between one check of your if statement and the next. So the chances of you checking the time exactly on a whole second number are very slim.
- shrubmaster
- Scratcher
5 posts
How to Make a Day/Night Cycle
I think I figured it out.
when green flag clicked
set [Time Dilation] to [1]
if <<(current [hour]) < [7]> or <(current [hour]) > [18]>> then
switch costume to [Moon]
if <(current [hour]) > [12]> then
set x to ((-920) + ((((current [hour]) * (4)) + ((current [minute]) / (15))) * (10)))
else
set x to ((-240) + ((((current [hour]) * (4)) + ((current [minute]) / (15))) * (10)))
end
else
switch costume to [Sun]
set x to ((-240) + ((((current [hour]) * (4)) + ((current [minute]) / (15))) * (10)))
end
forever
glide ((900) * (Time Dilation)) secs to x: ((x position) + (10)) y: ((y position) + (pick random (-0.99999) to (0.99999)))
if <(x position) > [240]> then
set x to (-240)
next costume
end
end
Last edited by shrubmaster (May 24, 2019 01:26:42)
Bippity Boppity Boop
- legendofv
- Scratcher
1 post
How to Make a Day/Night Cycle
it didnt work for meI'm in the middle of making a real-time farming game set on local time, and I've got my backgrounds set up to change every 15 minutes. I was trying to set up a moon/sun that scrolls from the left of the screen to the right to simulate the sun/moon rise/set, and I'm trying to figure out if there's a better way to set up the movement rather than 96 if-statements or altering my backgrounds to include the moon/sun.
Again, you could use a timer, and go like:...
forever
if <(timer) = [900]> then // or however timers measure
broadcast [night v] // or day
switch backdrop to [night v] // or day
end
if <(timer) = [1800]> then // or however timers measure
broadcast [day v] // or night
switch backdrop to [day v] // or night
reset timer
end
end
- Feigh10
- Scratcher
19 posts
How to Make a Day/Night Cycle
It's not necroposting.Don't Necropost.it didnt work for meI'm in the middle of making a real-time farming game set on local time, and I've got my backgrounds set up to change every 15 minutes. I was trying to set up a moon/sun that scrolls from the left of the screen to the right to simulate the sun/moon rise/set, and I'm trying to figure out if there's a better way to set up the movement rather than 96 if-statements or altering my backgrounds to include the moon/sun.
Again, you could use a timer, and go like:...
forever
if <(timer) = [900]> then // or however timers measure
broadcast [night v] // or day
switch backdrop to [night v] // or day
end
if <(timer) = [1800]> then // or however timers measure
broadcast [day v] // or night
switch backdrop to [day v] // or night
reset timer
end
end
forever
if <sasuages are nice> then
say [Sausages are nice]
end
end
Hi
- Discussion Forums
- » Help with Scripts
- » How to Make a Day/Night Cycle