A timer that would work and after leaving the project

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
Message
Author
User avatar
Andredron
Miko-Class Veteran
Posts: 714
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

A timer that would work and after leaving the project

#1 Post by Andredron »

Sorry for my terrible English!
Good afternoon. Made a timer that works even if the user does not sign in the project. Тhe information was taken from here https://python-scripts.com/datetime-time-python Here is the code:

Code: Select all

init python:
    import datetime

label splashscreen:
    scene black
    if not persistent.my_time:
        $ persistent.my_time = datetime.datetime.today()
    else:
        $ a = datetime.datetime.today()
        $ delta = a - persistent.my_time
        $ seconds = delta.total_seconds()
        $ hours = seconds // 3600
        centered "It's been [seconds] seconds since the previous pressing."
    return
    
screen test_scr():
    textbutton "Press!" action SetField(persistent, "my_time", datetime.datetime.today()) align(0.5, 0.05)

# The game starts here.
label start:
    show screen test_scr
    "..."
    e "You've created a new Ren'Py game."
    e "Once you add a story, pictures, and music, you can release it to the world!"
    return
  
Here the check is made during startup of the game (in the splashscreen ). It remains only not to forget to store the new value into persistent variables before closing the game...
Last edited by Andredron on Thu Jan 25, 2018 5:01 pm, edited 1 time in total.

User avatar
PyTom
Ren'Py Creator
Posts: 16093
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: A timer that would work and after leaving the project

#2 Post by PyTom »

Here's my version of it.

Code: Select all

init python:
    import time

default persistent.last_time = time.time()
default persistent.money = 10

define MONEY_PER_SECOND = 100 / 3600.0 # 100 per hour.

init python:

    class CashIn(Action):
        def __call__(self):
            now = time.time()

            if now < persistent.last_time:
                return

            change = (now - persistent.last_time) * MONEY_PER_SECOND

            persistent.money += change
            persistent.last_time = now

            renpy.show_screen("got_money", change=change, total=persistent.money )
            renpy.restart_interaction()

screen got_money(change, total):

    modal True

    frame:
        xalign 0.5
        yalign 0.5

        text "You just got [change:.2f] moneys. You have [total:.2f] moneys total."

        null height 20

        textbutton "Cool." action Hide("got_money")


screen cash_button():

    vbox:
        text "Wallet: [persistent.money:.2f] money."
        textbutton "Cash In" action CashIn()


label start:

    screen black

    show screen cash_button

    "You can click the button to cash in now."

    return
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom


User avatar
Andredron
Miko-Class Veteran
Posts: 714
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Timer with online check

#4 Post by Andredron »

author 7dots
https://store.steampowered.com/curator/29064054

https://pastebin.com/Hd0XnvXU

Code: Select all

a timer on renpy that checks time over the internet
 
init python:
    import requests
    import datetime
 
    # get current datetime from site
    def get_worldtime():
        res = None
        try:
            # get data from worldtimeapi.org
            data = requests.get('http://worldtimeapi.org/api/timezone/Etc/UTC').json()["datetime"]
            # convert string to datetime format
            res = datetime.datetime.fromisoformat(data)
        except requests.RequestException as e:
            renpy.notify("Something wrong: " + str(e))
        return res
 
    # using sample
    def check_time():
        world_now = get_worldtime().timestamp()
        now = datetime.utcnow().timestamp()
        # do what you want
        # ***
        dt = now - world_now
        renpy.notify(str(dt))
 
label start:
    # test
    # $ check_time()
    # $ t = get_worldtime()
    # "[t]"
 
    python:
        now = datetime.utcnow().timestamp()
        world_now = get_worldtime().timestamp()
        dt = now - world_now
    $ xxx = str(dt)
    "No pause: [xxx]"
 
    python:
        now = datetime.utcnow().timestamp()
        renpy.pause(2)
        world_now = get_worldtime().timestamp()
        dt = now - world_now
    $ xxx = str(dt)
    "With pause: [xxx]"
 
    return

Post Reply

Who is online

Users browsing this forum: No registered users