Forum Archive

How to restart a scene?

borrax

I wrote a simple solitaire game, everything works, but I would like to include a reset button. My first attempt failed, simply calling run() again did not work. So how to reset a scene?

cvp

@borrax Perhaps you could remove all children tree, then call self.setup()

from scene import *

class MyScene (Scene):
    def setup(self):
        self.background_color = 'midnightblue'
        self.ship = SpriteNode('spc:PlayerShip1Orange')
        self.ship.position = self.size / 2
        self.add_child(self.ship)

    def touch_began(self, touch):
        # touch top of screen to reset
        if touch.location[1] > (ui.get_screen_size()[1]-50):
            for child in self.children:
               child.remove_from_parent()
            self.setup()
            return
        x, y = touch.location
        move_action = Action.move_to(x, y, 0.7, TIMING_SINODIAL)
        self.ship.run_action(move_action)

run(MyScene())
borrax

@cvp, thanks, that worked.