I know that it is not permissible to watch them, but I would like to know how to prevent oneself from watching it especially when alone or when going to sleep?

Do wudhu before you sleep
And read 3 times ayatul Kursi and think about death and day of judgment. And Try to be in the company of pious people