Current Battery Level

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
CalixtheGreat
Regular
Posts: 72
Joined: Thu Jun 08, 2017 12:00 am
Projects: Zephyr Breeze Investigations
itch: calixthegreat
Location: Philippines
Contact:

Current Battery Level

#1 Post by CalixtheGreat »

Good day!
Just want to ask if it's possible to show the current battery level of your PC/phone in the game? I also want to include the current date but I don't know how. I already have the real time clock shown on my game.

Sorry for my English Hahah. Thanks in advance! 😁
Image
FB PAGE:
CALIX THE GREAT

User avatar
IrinaLazareva
Veteran
Posts: 399
Joined: Wed Jun 08, 2016 1:49 pm
Projects: Legacy
Organization: SunShI
Location: St.Petersburg, Russia
Contact:

Re: Current Battery Level

#2 Post by IrinaLazareva »

a)current date:
viewtopic.php?f=8&t=37939&p=410599#p410599

b)current battery:

Code: Select all

init python:
    import pygame_sdl2
    bat = pygame_sdl2.power.get_power_info().percent
    
label start:
    '[bat] %%'

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Current Battery Level

#3 Post by Imperf3kt »

IrinaLazareva wrote: Mon Nov 05, 2018 10:57 am b)current battery:

Code: Select all

init python:
    import pygame_sdl2
    bat = pygame_sdl2.power.get_power_info().percent
    
label start:
    '[bat] %%'
That won't work as expected if placed in a screen, the charge level will never update.

I did something extremely similar not too long ago:

Code: Select all

screen ancilliary():
    python:
        import pygame_sdl2
        battery_level = pygame_sdl2.power.get_power_info().percent
    zorder 1000
    vbox:
        xalign 0.99
        yalign 0.01
        text "{color=#000}{size=18}Battery:[battery_level]%{/size}{/color}"
        
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
IrinaLazareva
Veteran
Posts: 399
Joined: Wed Jun 08, 2016 1:49 pm
Projects: Legacy
Organization: SunShI
Location: St.Petersburg, Russia
Contact:

Re: Current Battery Level

#4 Post by IrinaLazareva »

Well, using a python block (especially, the import $ block) inside a screen is not the really good idea... Don't ask, I can't explain because of language barrier.

https://www.renpy.org/doc/html/screens.html#python
https://www.renpy.org/doc/html/screens.html#default
https://www.renpy.org/doc/html/screen_a ... l#Function

Anyway, I have answered the question posed, no more :wink:

User avatar
PyTom
Ren'Py Creator
Posts: 16096
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: Current Battery Level

#5 Post by PyTom »

A python block is fine, import is bad.

Code: Select all

init python:
        import pygame_sdl2

screen ancilliary():
    python:
        battery_level = pygame_sdl2.power.get_power_info().percent

    zorder 1000

    vbox:
        xalign 0.99
        yalign 0.01
        text "Battery:[battery_level]% color "#000" size 18
Is how I'd do it.
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
CalixtheGreat
Regular
Posts: 72
Joined: Thu Jun 08, 2017 12:00 am
Projects: Zephyr Breeze Investigations
itch: calixthegreat
Location: Philippines
Contact:

Re: Current Battery Level

#6 Post by CalixtheGreat »

Thanks for the response guys.
Is there a way to show the battery in a bar form instead of numbers? Thank you again!
Image
FB PAGE:
CALIX THE GREAT

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Current Battery Level

#7 Post by Imperf3kt »

Appreciate the explanation, Pytom, IrinaLazareva. I'll remember it from now on.

CalixtheGreat, you could use a bar, I forget it's exact name.

I'm working on this too, so I'll share whatever I come up with tonight.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
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
Contact:

Re: Current Battery Level

#8 Post by Remix »

StaticValue would be the bar you want...

On an aside; you could pull pygame_sdl2 from the alias within display.behavior rather than import...

Code: Select all

screen power_status():

    $ pwr = renpy.display.behavior.pygame.power.get_power_info()

    fixed:

        area (0.85, 0.0, 0.15, 0.1)

        fixed:

            pos (0,0)

            bar value StaticValue(value=float(pwr.percent)/100.0, range=1.0)

        if pwr.state == 4: ## you could test the other numbers for 'unplugged', 'low battery' etc

            text "charging" pos (0,0)
            # add "on_charge.png" pos (20,0)

        if pwr.seconds > 0:

            text "[pwr.seconds] seconds until fully charged" pos (0,30)
Frameworks & Scriptlets:

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Current Battery Level

#9 Post by Alex »

This should be in a cookbook...;)

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Current Battery Level

#10 Post by Imperf3kt »

Remix wrote: Wed Nov 07, 2018 7:32 am StaticValue would be the bar you want...

On an aside; you could pull pygame_sdl2 from the alias within display.behavior rather than import...

Code: Select all

screen power_status():

    $ pwr = renpy.display.behavior.pygame.power.get_power_info()

    fixed:

        area (0.85, 0.0, 0.15, 0.1)

        fixed:

            pos (0,0)

            bar value StaticValue(value=float(pwr.percent)/100.0, range=1.0)

        if pwr.state == 4: ## you could test the other numbers for 'unplugged', 'low battery' etc

            text "charging" pos (0,0)
            # add "on_charge.png" pos (20,0)

        if pwr.seconds > 0:

            text "[pwr.seconds] seconds until fully charged" pos (0,30)
Thank you for pointing out StaticValue, it helped a lot.
I came up with something before you posted, but it wasn't working right using Value, do I substituted StaticValue in its place and now it works perfect!

I am curious about your comment in this line though:

Code: Select all

if pwr.state == 4: ## you could test the other numbers for 'unplugged', 'low battery' etc
I am unable to find these numbers you speak of.
I'm tempted to substitute random numbers and see what happens, but it'd be easier with documentation.

Edit:
I think I found it:
https://wiki.libsdl.org/SDL_GetPowerInfo
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

Post Reply

Who is online

Users browsing this forum: Google [Bot], Sugar_and_rice