17 April 2025

How to show different background for day/night cycles in Renpy

 Here is a quick explanation for how I keep track of day/night cycles in my Renpy games, including how to show different background images depending on the time and day.

Adding a DayTracker class

Here is the python code I use which creates a dayTracker class. This class is used to keep track of the day of the week and time of day. There are a few other functions in there too.

class dayTracker:
        _tod = 1
        _dow = 0
        _game_day = 0
        weekdays = (
            'Mon',
            'Tue',
            'Wed',
            'Thu',
            'Fri',
            'Sat',
            'Sun',
            )
        weekdays_long = (
            'Monday',
            'Tuesday',
            'Wednesday',
            'Thursday',
            'Friday',
            'Saturday',
            'Sunday',
            )
       
        def is_morning(self):
            return self._tod == 1
       
        def is_afternoon(self):
            return self._tod == 2
       
        def is_evening(self):
            return self._tod == 3
        def is_night(self):
            return self._tod == 4
        def is_dark(self):
            return self._tod >= 3
        def set_tod(self,tod):
            self._tod = tod
        def set_dow(self,dow):
            self._dow = dow
       
        def image(self, name, layer = "master"):
            import re
            tmp = name
            if not re.search('\{}', name):
                name = name + '{}'
            if  not self.is_morning() and not self.is_afternoon():
                name = name.replace("_day", "")
                if self.is_evening():
                    tmp = name.format("_evening")
                    if renpy.exists("images/"+tmp):
                        return tmp
                else:
                    tmp = name.format("_night")
                    if renpy.exists("images/"+tmp):
                        return tmp
                if renpy.exists("images/"+tmp):
                        return tmp
                return name.format("_night")
            else:
                return name.format("")
       
        def is_weekend(self):
            return self._dow == 5 or self._dow ==6
       
        def dayOfWeek(self,full=False):
            if full:
                return self.weekdays_long[self._dow]
            else:
                return self.weekdays[self._dow]
       
        def gameDay(self):
            return self._game_day
       
        def tick(self,tod=None):
            if tod:
                self._tod = tod
            elif self._tod < 4:
                self._tod += 1

        
        def sleep(self):
            self._tod = 1
            self._dow = (self._dow +1) % 7
            self._game_day += 1
            renpy.force_autosave()


Time of day (tod) is split into 4 day parts (Morning = 1, Afternoon = 2, Evening = 3 , Night= 4)
To move time forward, I use dayTracker.tick(), which increments tod by 1 until it is night.
Once it is night, I use a dayTracker.sleep(), which resets the tod and increments the day of the week.

Setting a different background image for different day parts

First, set up your images correctly. I use the following naming conventions for my backgrounds.
Morning = IMAGENAME.jpg
Afternoon = IMAGENAME.jpg
Evening = IMAGENAME_evening.jpg
Night = IMAGENAME_night.jpg

Then I usage dayTracker.image() to handle updating the background image.
For example, let's say I have a background of a library called campus_library.jpg.
I'd also have campus_library_evening.jpg for the evening scene and campus_library_night.jpg for the night scene.
In Renpy, I'd write:

scene expression dayTracker.image("backgrounds/campus_library{}.jpg")

This will check the time of day, and then replace the {} with either _evening or _night, depending on the tod.

Hope this helps! Leave a comment if you have any issues.

No comments:

Post a Comment

How to show different dialogue boxes for different characters

 Want to show different dialogue boxes depending on the character? You can create as many dialogue boxes (also called 'say screens')...