I have set an default, well actually 3 (phoneincharge, phoneinchargehalf, phoneinchargefull) half and full are the amounts of the battery level. I have also put the first default when the phone gets charged and it works perfectly.
I only having problems that after 6 hrs of charging the screen of the display should change into half full battery level and after another 6 hours the phone is charged.
To expand on Anne's solution, based on your example...
Python:
default battery_level = 100
label world_time:
if world_time_minute >=60:
while world_time_minute >= 60:
$ world_time_minute = world_time_minute -60
$ world_time_hour = world_time_hour + 1
if phoneincharge:
call recharge_cellphone
else:
call discharge_cellphone
if world_time_hour >=8 and world_time_hour <=20:
$ world_day_night = "Day"
else:
$ world_day_night = "Night"
if world_time_hour >=24:
$ world_time_hour = world_time_hour -24
$ world_daycount = world_daycount + 1
if world_day <=6:
$ world_day = world_day + 1
else:
$ world_day = 1
return
label recharge_cellphone:
$ battery_level += 9 # full charge battery from 0 to 100 in 12 hours.
if battery_level >= 100:
$ battery_level = 100
$ phonefullcharge = True
$ phonehalfcharge = False
elif battery_level >= 50:
$ phonehalfcharge = True
$ phonefullcharge = False
else:
$ phonehalfcharge = False
$ phonefullcharge = False
return
label discharge_cellphone:
# assuming no other battery use mechanic, like increased battery drain while watching video, etc.
$ battery_level -= 4 # full discharge of battery from 100 to 0 in a little over 24 hours.
if battery_level <= 0:
$ battery_level = 0
$ phonehalfcharge = False
$ phonefullcharge = False
elif battery_level <= 50
$ phonehalfcharge = True
$ phonefullcharge = False
else:
$ phonefullcharge = True
$ phonehalfcharge = False
return
This code holds the battery level as a percentage in the variable
battery_level
. It starts at full charge.
If the phone is on charge, it is increased by 9%. While charging the battery level will show as empty until it reaches 50% and won't show full until it reaches 100%.
If the phone is not on charge, I'm assuming a nominal discharge rate. I've picked 4%, since that will drain the battery from 100% to 0% in a little over 24 hours. You may or not want to use this mechanic. However I'm treating the battery level indicators different during discharge - showing full battery from 100% to 51% and half battery until the battery reaches 0%. I figure this is better than showing the "half battery" indicator when the battery is at 96%... though obviously you may view that differently. It will look odd though if the phone is put on charge (e.g. battery at 80% on charge will show half battery, but will show full while not on charge).
A better way would be to not use the
batteryhalfcharge
or
batteryfullcharge
values to pick static images, and instead use a
You must be registered to see the links
as part of your
You must be registered to see the links
to show the actual value of
battery_level
.
Also, I removed a duplicated bit of code in your example. You advance the hour by 1 when the minutes exceed 60 and then enter a loop to see if the hour needs increasing again. You only need the loop, since it's doing that already. I assume you're adding minutes in 15, 30, 60, 120, 180, etc blocks - allowing time to advance by any number of hours up to 24.