Source

hexbattle / scene.py

Full commit
#!/usr/bin/env python2
# encoding: utf-8

"""An example scene.

Goal: Keep as much logic in the scenes themselves as possible. Keep the core lean.

Every scene which can be paused needs 2 functions:

* start(dt=0): start the scene.
* resume(dt=0): resume the scenes events after it was paused.

To pause a scene,

* append its resume function to core.paused_scene_resume_functions and
* stop its events and store them in a way which tells the resume function how to start them again.

When the scene gets reactivated, its resume function gets popped from the paused_scene_resume_functions and then scheduled.

To remove activated events from the event stack, use core.win.remove_handler(handler). See http://pyglet.org/doc/programming_guide/stacking_event_handlers.html and http://pyglet.org/doc/api/pyglet.event.EventDispatcher-class.html#remove_handler

Both functions have to take the optional dt parameter, so they can be properly scheduled with pyglet.clock.schedule_once(event, delay)

If the scene cannot be paused, it only needs start(dt=0).

"""

# shared data
from core import core, run, __copyright__
# event scheduling functions → interactivity
from pyglet.clock import schedule, schedule_once, schedule_interval, unschedule

visible = []
active = True
paused = False
events_to_start = [] #: [(delay, event), …]

def schedule_event(event, delay):
    global active
    global paused
    if paused:
        return events_to_start.append((delay, event))
    if active:
        return schedule_once(event, delay)

def switch_to_intro():
    import intro
    schedule_once(intro.start, 0.01)


## Necessary functions for the simple scene interaction API

def start(dt=0):
    """Start the scene."""
    switch_to_intro()

def resume(dt=0):
    active = True
    paused = False
    for delay, event in events_to_start:
        schedule_event(event, delay)

if __name__ == "__main__": 
    run(__file__)