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 pyglet-users+unsubscr...@googlegroups.com.
To post to this group, send email to pyglet-users@googlegroups.com.
Visit this group at https://groups.google.com/group/pyglet-users.
For more options, visit https://groups.google.com/d/optout.

Reply via email to