Skip to content

Advance Techniques

Syndelis edited this page Sep 8, 2020 · 1 revision

In your first program section, you will learn how to use your system resources more efficiently and write better code for your game overall.

The load decorator

In the previous section you were presented to the draw decorator, that looks like this

@window.draw
def drawMyGame(eel):
    ...

The draw decorator will have the decorated functions run every frame and is a must for your game's logic and drawing. However, there are some times when you need to have functions be executed only once, and for that reason we have the load decorator.

@window.load
def initializePlayer(eel):
    # This will be executed only once
    global player
    player = Knight()

You might think this is not that useful, but there's a good reason for its existance. Some assets like figure.Font, figure.Sprite and shader.Shader need to be initialized only after a window has been opened. If you are interested in the why, that's because they need an OpenGL context to exist, and that only happens after the window is open.

That makes the load decorator perfect for this kind of situation, since it will run only once and exactly after the window has ben opened.

We will see extensive usage of this feature in the next section.

Clone this wiki locally