Discuss Scratch

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
axisjack
Scratcher
100+ posts

How to Make a Day/Night Cycle

Id need to check tbe project
ResExsention
New to Scratch
1000+ posts

How to Make a Day/Night Cycle

shrubmaster wrote:

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

ResExsention wrote:

shrubmaster wrote:

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
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.

Last edited by deck26 (May 22, 2019 08:28:34)

ResExsention
New to Scratch
1000+ posts

How to Make a Day/Night Cycle

deck26 wrote:

ResExsention wrote:

shrubmaster wrote:

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
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.

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

ResExsention wrote:

deck26 wrote:

ResExsention wrote:

shrubmaster wrote:

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
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.

Lol. My logic when I wrote that code was that a timer would always hit every value. Didn't know it was that inaccurate.
It's accurate, not 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

ResExsention wrote:

shrubmaster wrote:

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
it didnt work for me
Feigh10
Scratcher
19 posts

How to Make a Day/Night Cycle

Eisthebestletter wrote:

legendofv wrote:

ResExsention wrote:

shrubmaster wrote:

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
it didnt work for me
Don't Necropost.
It's not necroposting.

forever
if <sasuages are nice> then
say [Sausages are nice]
end
end



























Hi

Powered by DjangoBB