How to make a day and night cycle

Continuing the discussion from Aariv’s General Topic and announcements topic!:

I felt like this could use its own topic.

This is not the best way to code this, but it should work. If you do make this better, post about how you did it on this topic.

The first thing you’ll notice is that 217 shows up a lot. This is the B value in the RGB for the brightest part of the day. As you can see, if time is less than 217, it will increase time by one, redraw the background, wait a bit, then repeat until it is equal to 217. (Since it starts at 0. I explain more about the default values of variables here.)

Okay. This is the background ability. The check one of checks if time is greater than the G value of the brightest part of the day minus one. Since here it is 90, it checks if it’s greater than 89.
Why? Because the block inside is a draw a trail with a width of 3000. The color is an RGB block. R is set to zero, while G is 90-time. This means that thanks to the check if else, the lowest the g will get is 0. B is set to 217-time. Does that number seem familiar? Every other 217 in this project is based off of that specific one.
In the else area, I have the exact same code, but with one major difference: G is set to 0.

This is the delay ability. What it does is it waits the amount of blocks in the repeat times. For example, this ability takes as long as five flip blocks would take, but does absolutely nothing. This is useful, as we can prolong the day and night this way.

So that’s the code for 12:00 to 0:00. But what about from 0:00 to 12:00? That’s simple.

As you can see, in the else area of the very first check if else, we have almost identical code. There’s only one difference. Can you spot it?
We decrease time instead of increasing it.

And that’s it. Let me know if there’s anything you didn’t understand, and I’ll explain it.


! Իḙǭṳ!Իḙ ⑂✺ṳԻ ᾰṧṧ!ṧтᾰℵḉḙ, ℘ḙ✺℘ℓḙ ♭ḙℓ!ḙṽḙ ωḙ ᾰԻḙ ✺ℵḙ.


Thanks! So it’ll become day and night and keeps going?

1 Like

I might need further help too. When it is night,I need certain sprites to become visible. Would it be
‘Repeat forever
time= or time<
Set invisibility 0’

1 Like

Woah, that is really cool! Thanks for making this tutorial. Everything is very well written and easy to understand.

1 Like

This is a really cool tutorial! It seems really easy to follow!

Just use a when block for when time is in the range you want the objects to appear. [(Greater than) and (less than)]


Hi @Petrichor @Aariv
We can leverage the power of the Sine function and HSB colors to do this quite simply. As angle varies from 0-360, sin(angle) varies from -1 to 1. So this is perfect for a simple day/night algorithm.

  • #night is set to 20 elsewhere. “20” could be used explicitly, but I like using Constants as it’s easier to update if the value is used multiple places
  • The +1 for Rotation controls how quickly time changes
  • The ^0.5 just tweaks the rate/timing of dawn & dusk (a value of 0.5 here is the same as square root, but with the exponent we get the flexibility to further adjust the timing as desired)
  • The last rule isn’t necessary if you want night time to be pitch black

Then you just use the Brightness variable in the HSB color of a Draw a Trail block

That’s all you need for the basics but it can easily extended with clones and a little more math;

(Example using dynamic trails)


For some reason the code isn’t working. Do you have less complicated code?

I thought you were creative with coding.


I like coding, but I’m not MagmaPoP.

1 Like

:thinking: we know.

1 Like