Forum Archive

How to use Scene.delay()?

foresight

Hey Everyone,

I'm making a game of snake (will post code later), and would like to slow down the drawing process. I've looked up scene.delay (which I don't know how to use properly) and also time.sleep. what is the best method and how do I implement it? Thanks!

Sebastian

The delay function doesn't actually slow down the drawing process. It calls a given function after a certain amount of time. It kind of tells a function to wait for n seconds before it is called.

Here's an example of the delay function:

from scene import *

class MyScene(Scene):
    def setup(self):
        self.color = (1,0,0)

    def change_color(self):
        self.color = (0,1,0)

    def draw(self):
        background(*self.color)
        self.delay(3, self.change_color)

run(MyScene())

This draws a red screen, and the delay function will change the color to green after 3 dt have passed.

omz

Another option to slow things down would be to pass the optional frame_interval parameter to the run function, like so:

run(MyScene(), frame_interval=3)
This would basically run the scene at 30 frames per second instead of 60 (passing 3 would be 20fps etc.).