Xela--who is so marvelous!
So, I got the clock working...everything I wanted. Woot! I tried your animated add--but as written it wouldn't trigger the chimes. They key is that the minutes have to increment by 1 so that the clock can notice if it passes the 12 with the minute hand and set off the chimes when it does so. The steps sometimes result in stepping over the 12. It also wasn't working with the new redraw(0). Also, there is a check for the chimes that happens in the render, so the check for the steps has to happen outside the render so it is alway checking it.
So what I did was modify my original animated add to be more like yours. I came up with the idea that I could vary the length of the pauses between 1 minute clicks based on how quickly or slowly you wanted the animation to happen. Something like: Pause = animation/minutes. It results in a faster animation to be sure...but...I don't think the speed is varying based on the animation variable. Do you have any thoughts on how to fix this?
Anyhow, I'm going to post the clock.rpy project here...It isn't completely annotated for new users yet, but I'm hold off doing that until the Clock code is finalized. Once everything is completely updated, I'll rethink about the explanations to new users and repost it to the Cookbook.
Code: Select all
init -1 python:
from datetime import datetime #This is to get the realtime datetime
import math
import time
img = ["00 images/VintageClockBase450x450.png", "00 images/VintageClockHour450x450.png", "00 images/VintageClockMinute450x450.png", "00 images/VintageClockSecond400x400.png"]
renpy.music.register_channel("TockBG", mixer= "sfx", loop=True)
renpy.music.register_channel("ChimeBG", mixer= "sfx", loop=False, tight=True)
#as a UDD this needs to extend the Displayable class)
class Clock(renpy.Displayable):
#The clock class constructor
def __init__(self, h, m, resize=150, sH=True, **kwargs):
super(Clock, self).__init__(**kwargs)
self.width = resize
self.height = resize
self.bI = im.Scale(img[0], resize, resize)
self.hI = im.Scale(img[1], resize, resize)
self.mI = im.Scale(img[2], resize, resize)
self.sI = im.Scale(img[3], resize, resize)
self.minutes = (h*60)+m
self.seconds = 0
self.minutes_target = 0 #Do I need this
self.step = 0 #Do I need this
self.auto_run = False
self.realtime_run = False
self.sec_run = False
self.offset = (resize*2**0.5-resize)/2
self.secondHand = sH
self.sound = False
self.chime_run = False
self.last_sec = None #Used in secondson
self.old_minute = None #Used in autorun
self.last_minute = 0
self.chplay = False
def render(self, width, height, st, at):
# Create transform to rotate the second hand
tM = Transform(child=self.mI, rotate=self.minutes*6, subpixel=True)
tH = Transform(child=self.hI, rotate=self.minutes*0.5, subpixel=True)
# Create a render from the child.
base_render = renpy.render(self.bI, width, height, st, at)
minute_render = renpy.render(tM, width, height, st, at)
hour_render = renpy.render(tH, width, height, st, at)
self.realclock()
self.autoclock(st)
if self.chime_run == True:
if self.last_minute != self.minutes:
self.chplay = False
self.last_minute = self.minutes
self.chimeon()
# Create the render we will return.
render = renpy.Render(self.width, self.height)
# Blit (draw) the child's render to our render.
render.blit(base_render, (0, 0))
render.blit(minute_render, (-self.offset, -self.offset))
render.blit(hour_render, (-self.offset, -self.offset))
if self.secondHand:
tS = Transform(child=self.sI, rotate=self.seconds*6, subpixel=True)
sec_render = renpy.render(tS, width, height, st, at)
render.blit(sec_render, (-self.offset, -self.offset))
self.secondson(st)
#This makes sure our object redraws itself after it makes changes
renpy.redraw(self, 0)
# Return the render.
return render
#Returns a list of all the child displayables for this displayable...but be here
def visit(self):
return [self.bI, self.hI, self.mI, self.sI]
def get_time(self):
h, m = divmod(self.minutes, 60)
if h is 0:
h = 12
elif h > 12:
h = h%12
h = int(h)
m = int(m)
return h, m
#Directly set the time of the watch
def set_time(self, h, m):
self.minutes = (h*60)+m
self.seconds = 0
#Manually add a certain amount of time to the clock
def add_time(self, h, m, animate=0): #This is working, but it isn't really slowing down
if self.realtime_run == False:
num = (h*60)+m
if animate:
p = float(animate/num)
for x in range(num):
self.minutes += 1
renpy.pause(p)
else:
self.minutes += num
def autoclock(self, st):
if self.auto_run:
self.realtime_run = False
if self.old_minute is None:
dt = 0
else:
dt = st//60 - self.old_minute
self.old_minute = st//60
self.minutes += dt
def realclock(self):
if self.realtime_run:
self.auto_run = False
t = datetime.today()
self.minutes = (60 * t.hour) + t.minute
def secondson(self, st):
if self.sec_run:
if self.realtime_run:
t = datetime.today()
self.seconds = t.second
else:
if self.last_sec is None:
dt = 0
else:
dt = st - self.last_sec
self.last_sec = st
self.seconds += dt
def runmode(self, mode, snd=True):
if mode == "none":
self.realtime_run = False
self.auto_run = False
self.sec_run = False
self.sound_on(snd, snd)
else:
self.sec_run = True
self.sound_on(snd, snd)
if mode == "auto":
self.realtime_run = False
self.auto_run = True
elif mode == "real":
self.auto_run = False
self.realtime_run = True
def sound_on(self, sndR, chR):
self.sound = sndR
self.chime_run = chR
if self.sound:
if renpy.music.is_playing(channel='TockBG'):
renpy.music.stop(channel='TockBG', fadeout=0.1)
renpy.music.play("00 sounds/ClockTick1.flac", channel='TockBG')
else:
renpy.music.stop(channel='TockBG')
if self.chime_run:
self.chime_run = True
else:
renpy.music.stop(channel='ChimeBG')
self.chime_run = False
def chime_looper(self):
h, m = self.get_time()
ch = ["00 sounds/ChimePart.ogg",]*(h-1)
ch.append("00 sounds/Chime1.ogg")
return ch
def chimeon(self):
if self.chime_run:
if not self.chplay:
if self.minutes%60 == 0:
self.chplay = True
cf = self.chime_looper()
renpy.music.queue(cf, channel='ChimeBG')