Yup, schedule_interval is the key.
On Tuesday, November 22, 2016 at 4:34:03 AM UTC-6, Bastien Sevajol wrote:
>
> Oh, a i think i found it:
> # ...
>
> def execute_job(*args, **kwargs):
> label.text = str(datetime.now().strftime('%I:%S'))
>
> pyglet.clock.schedule_interval(execute_job, 1/60.0)
> pyglet.app.run()
>
> :)
>
> Le mardi 22 novembre 2016 09:27:17 UTC+1, Bastien Sevajol a écrit :
>>
>> Hello,
>>
>> I want to execute background jobs in pyglet. Example, in a game,
>> executing AI of characters, evolution of environment ... I found a way
>> with pyglet.clock.schedule.
>>
>> In this example, i update a text each seconds with actual hour and
>> seconds:
>>
>> # -*- coding: utf-8 -*-
>> from datetime import datetime
>> import pyglet
>> import time
>> from pyglet.clock import schedule
>>
>> window = pyglet.window.Window()
>> label = pyglet.text.Label(
>> '...',
>> font_name='Times New Roman',
>> font_size=36,
>> x=window.width//2,
>> y=window.height//2,
>> anchor_x='center',
>> anchor_y='center',
>> )
>>
>> class BackgroundJob(object):
>> def __init__(self, interval: float=1.0):
>> self.interval = interval
>> self.last_execution_time = 0
>>
>> def is_time_to_execute_job(self):
>> return time.time() - self.last_execution_time >= self.interval
>>
>> def execute_job(self):
>> self.last_execution_time = time.time()
>> label.text = str(datetime.now().strftime('%I:%S'))
>>
>> def schedule(self, *args, **kwargs):
>> if self.is_time_to_execute_job():
>> self.execute_job()
>>
>> background_job = BackgroundJob()
>> schedule(background_job.schedule)
>>
>>
>> @window.event
>> def on_draw():
>> window.clear()
>> label.draw()
>>
>> pyglet.app.run()
>>
>> I am doing it right ? Or something else is designed to this usage ?
>> Thank's
>> Bastien.
>>
>
--
You received this message because you are subscribed to the Google Groups
"pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/pyglet-users.
For more options, visit https://groups.google.com/d/optout.