Scarily simple game calendar... with day parts

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
User avatar
Eileen-Class Veteran
Posts: 1478
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game

Scarily simple game calendar... with day parts

#1 Post by Remix » Fri Aug 31, 2018 7:11 pm

So, you want a calendar in your game?
Something to tell your players whether it is Tuesday or Wednesday?
Whether it is Breakfast or Elevensies? (yes, fellow programmers, some people wake up before 11 and have a mini meal before that time... the world is full of weird)

Python nicely has a module that we can integrate with that does all of this quite simply (read: simple as long as you do not dig deep).
On an extra plus point, Ren'Py ships with that module, so we do not even need to download it.

To the code >>>
Umm, not that way, try just further down...

Code: Select all

init python:

    import datetime

    class GameTime(object):

        def __init__(self, dt="01 Jan 2018"):
            self._dt = datetime.datetime.strptime( dt, "%d %b %Y" )

        def alter(self, **kwargs):
            self._dt += datetime.timedelta( **kwargs )

        def __repr__(self):
            return _strftime("%d %b %Y %H:%M", self._dt.timetuple())

        def tod(self):
            return [ k[-1] for k in (
                (0,1,2,3,4,5,6, "Night Time"),
                (7,8,9,10,11, "Morning"), 
                (12, "Noon"), 
                (13,14,15,16,17, "Afternoon"),
                (18,19,20, "Evening"), 
                (21,22,23, "Late Evening") ) if self._dt.hour in k ][0]

default gt = GameTime("01 Jun 2008")

label start:

    "[gt] ( [gt.tod] )"

    $ gt.alter( hours = 5, minutes = 30 )

    "[gt] ( [gt.tod] )"

    $ gt.alter( minutes = 90 )

    "[gt] ( [gt.tod] )"

Liberally emblazoned with a copious amount of code comments to help people understand :)

Addendum: (for those who do not have the [show hidden code comments] tickbox checked in their profile settings)

GameTime is a class derived from a simple python object.

Code: Select all

default gt = GameTime("01 Jun 2008")
We instantiate it (yeah, that is what programmers call the thing when you first create an instance of it and connect it to a reference or variable) by using Ren'Py's default keyword...
You may certainly change the variable name "gt" to whatever you want...
... If you are really fussy, you can even change the start date... or both

Code: Select all

default a_different_var_name = GameTime("02 Jun 2008")
If we did that though, we'd have to reference the date and time string using that variable name... eileen "It is [a_different_var_name] now"

The __repr__ method returns our _dt in a format we like, so if we reference the instance variable in dialogue or screen we get the string
Once again, if you are fussy you could change the output...
Use These letters to adjust it. Though note that python locale might not reflect Ren'Py translation, so you might have to resort to if:else'ing the method format if you want different output for different languages.

The method uses Ren'Py's inbuilt _strftime which patches into translation :)
So if you translate your game into piglatin (and let's face it, who hasn't), you only need translate the weekdays and months once (hint: January is Anuaryjay... as if you didn't know)

Moving swiftly on (umm, back) to the alter method...
We call this to move time in our game:

Code: Select all

$ gt.alter( years=1, months=7, days=3, hours=9, minutes=45 )
Simple[z] huh?
For those dabbling with future tech or magic or H.G. Wells, you can even smuggle in a negative number and go back to before the lottery was drawn... cool huh?

Almost finally ... the method tod (which in my mind stands for Time Of Day)

Notice the @property before the method?
This tells python that when we address that attribute of the class we call that method, so saying "[gt.tod]" runs that method to get its return value.
(you could add self.tod to the __repr__ output to slim your code even more)

You can, whether fussy or not, adjust the little mapping in there to reflect your game... Not everyone wants "Noon" to be at 12 midday after all (and some programmers might think the day starts at 3pm...). If the game time includes the keyed hour, the Text bit is returned, so, bizarrely "Noon" in the above is really 12:00:00 until 12:59:59 ... which basically gives me an hour extra in bed tomorrow, unless I use the H.G. Wells trick...

The example start label just shows a few time advances though mostly you would just be using one per label or put them in button actions.
If a label makes time go forward 6 hours, just do that in the label... So simplez even a meerkat could do it.

Any questions, email directly,
Frameworks & Scriptlets:

Post Reply

Who is online

Users browsing this forum: No registered users