Forum Archive

Chaining scene.Scenes

ccc

I have written many times in this Forum that you can not call scene.run() multiple times but now I have proof that this is NOT true.

Multiple scene.Scenes can be chained one after another as shown in the code below which runs 6 scene.Scenes one after the other WITHOUT using MultiScene.

'''
scene_chain.py -- multiple scene.Scenes can be chained one after another.

lessons learned:
    scene.setup() is only called AFTER the main program has ended.

    If one scene creates another scene and scene.run() is called 
    on that new scene then the older scene stops (without its
    stop() method being called) and the new scene starts.

    The last scene will end only when the user taps the X in the
    upper right corner of the scene.  At this time, the stop()
    method will be called for the first time.
'''

import scene

colorRed   = scene.Color(1, 0, 0)
colorGreen = scene.Color(0, 1, 0)
colorBlue  = scene.Color(0, 0, 1)
greyLight  = scene.Color(.7, .7, .7)
greyMedium = scene.Color(.5, .5, .5)
greyDark   = scene.Color(.3, .3, .3)
theColors  = [colorRed, colorGreen, colorBlue, greyLight, greyMedium, greyDark]

fmt = '{:<20} {}'

class MyScene(scene.Scene):
    def __init__(self, inColorList):
        #print(inColorList)
        self.colorList  = inColorList
        self.bgColor    = self.colorList.pop()
        self.frameCount = 0
        scene.run(self) # a self running scene

    def setup(self):
        self.center = self.bounds.center()
        print(fmt.format('Scene setup:', self.bgColor))

    def draw(self):
        self.frameCount += 1
        if not self.frameCount % 100:
            print(fmt.format('Frame Count: {}'.format(self.frameCount), self.bgColor))
        if self.frameCount == 100: # 500:
            if self.colorList:
                MyScene(self.colorList)
            return
        scene.background(self.bgColor[0], self.bgColor[1], self.bgColor[2])
        scene.text('Frame count: {}\n{}'.format(self.frameCount, self.bgColor),
             font_size=40, x=self.center.x, y=self.center.y)

    def stop(self):
        print('stop() =====')
        #if self.colorList:
        #    MyScene(self, self.colorList)

print('=' * 12)
print('Main starts.')
theScene = MyScene(list(reversed(theColors)))
print('Main ends.')
mirko

Thank you, but how would you properly loop a scene?
I mean properly deallocate whatever was allocated and "reboot" it?

Sebastian

I keep getting this error:

AttributeError: 'MyScene' object has no attribute 't'