Search code examples
pythontimerresetscenepyxel

How to transition from one scene to another using pyxel and python


I'm working on a text-based rpg based on choices. This scene in my rpg has a timer in it. I'm trying to figure out a way to reset the timer itself when the player makes a succesful transition to another scene. To do this the timer would also need to be reset each time. Any suggestions?

I tried to transition from one scene to another by using another function escape() to transition from this original scene to the next. The problem is the timer doesn't reset and it automatically goes to "You did it!".

import time
import pyxel

class App:
    def __init__(self):
        pyxel.init(250, 150, display_scale=3, title='Kingsforth')
        self.timer = True
        self.start = time.time()
        self.time_left = None
        pyxel.run(self.update, self.draw)

    def update(self):
        self.time_left = 5 - int(time.time() - self.start)
        if self.time_left >= 0 and (pyxel.btn(pyxel.KEY_LEFT) or pyxel.btn(pyxel.KEY_RIGHT) or pyxel.btn(pyxel.KEY_UP)):
            self.timer = False

    def draw(self):
        pyxel.cls(pyxel.COLOR_BLACK)
        if self.timer and self.time_left >= 0:
            pyxel.text(x=1, y=1, s='You walk into an opening filled with guards! Where do you go?', col=pyxel.COLOR_WHITE)
            pyxel.text(x=1, y=10, s='Options:', col=pyxel.COLOR_WHITE)
            pyxel.text(x=35, y=10, s='Left, Right, Up', col=pyxel.COLOR_GREEN)
            pyxel.text(x=1, y=20, s='Time left:', col=pyxel.COLOR_WHITE)
            pyxel.text(x=45, y=20, s=str(self.time_left), col=pyxel.COLOR_RED)
        elif self.timer and self.time_left < 0:
            pyxel.text(x=1, y=1, s='The guards catch you. They execute you on the spot.. You die.', col=pyxel.COLOR_WHITE)
        elif not self.timer:
         self.escape()

    def escape(self):
        pyxel.cls(pyxel.COLOR_BLACK)
        if self.timer and self.time_left >= 0:
            pyxel.text(x=1, y=1, s='You run into junk yard filled with debree.. Where do you go?', col=pyxel.COLOR_WHITE)
            pyxel.text(x=1, y=10, s='Options:', col=pyxel.COLOR_WHITE)
            pyxel.text(x=35, y=10, s='Left, Right, Up', col=pyxel.COLOR_GREEN)
            pyxel.text(x=1, y=20, s='Time left:', col=pyxel.COLOR_WHITE)
            pyxel.text(x=45, y=20, s=str(self.time_left), col=pyxel.COLOR_RED)
        elif self.timer and self.time_left < 0:
            pyxel.text(x=1, y=1, s='The guards catch you. They execute you on the spot.. You die.', col=pyxel.COLOR_WHITE)
        elif not self.timer:
            pyxel.text(x=1, y=1, s='You did it!', col=pyxel.COLOR_WHITE)


App()

Solution

  • It's better to devide your game text and the actual code. You can store it in the separate variable.

    import time
    import pyxel
    
    
    SCENES = [{'Description': 'You walk into an opening filled with guards! Where do you go?', 'Left': 1, 'Right': 2, 'Up': 3, 'Dead': 'The guards caught you. They execute you on the spot.. You die.'},
              {'Description': 'You run into junk yard filled with debree.. Where do you go?', 'Left': 2, 'Right': 0, 'Up': 3, 'Dead': 'The junk monster caught you.. You slowly die in his stomach.'},
              {'Description': 'You run into the forest.. Where do you go?', 'Left': 0, 'Right': 1, 'Up': 3, 'Dead': 'The wolf caught you. He tore your flesh apart with his sharp teeth.'},
              {'Description': 'You run into the graveyard.. Where do you go?', 'Left': 0, 'Right': 1, 'Up': 4, 'Dead': 'The undead caught you and took you to its grave.'},
              {'Description': 'You did it!'}]
    
    
    class App:
        def __init__(self):
            pyxel.init(250, 150, display_scale=3, title='Kingsforth')
            self.scene_id = 0
            self.start = time.time()
            self.time_left = None
            pyxel.run(self.update, self.draw)
    
        def update(self):
            self.time_left = 5 - int(time.time() - self.start)
            if self.time_left >= 0 and SCENES[self.scene_id].get('Dead') and (pyxel.btnr(pyxel.KEY_LEFT) or pyxel.btnr(pyxel.KEY_RIGHT) or pyxel.btnr(pyxel.KEY_UP)):
                if pyxel.btnr(pyxel.KEY_LEFT):
                    self.scene_id = SCENES[self.scene_id]['Left']
                elif pyxel.btnr(pyxel.KEY_RIGHT):
                    self.scene_id = SCENES[self.scene_id]['Right']
                elif pyxel.btnr(pyxel.KEY_UP):
                    self.scene_id = SCENES[self.scene_id]['Up']
                self.start = time.time()
                self.time_left = 0
    
        def draw(self):
            pyxel.cls(pyxel.COLOR_BLACK)
            if SCENES[self.scene_id].get('Dead'):
                if self.time_left >= 0:
                    pyxel.text(x=1, y=1, s=SCENES[self.scene_id]['Description'], col=pyxel.COLOR_WHITE)
                    pyxel.text(x=1, y=10, s='Options:', col=pyxel.COLOR_WHITE)
                    pyxel.text(x=35, y=10, s='Left, Right, Up', col=pyxel.COLOR_GREEN)
                    pyxel.text(x=1, y=20, s='Time left:', col=pyxel.COLOR_WHITE)
                    pyxel.text(x=45, y=20, s=str(self.time_left), col=pyxel.COLOR_RED)
                else:
                    pyxel.text(x=1, y=1, s=SCENES[self.scene_id]['Dead'], col=pyxel.COLOR_WHITE)
            else:
                pyxel.text(x=1, y=1, s=SCENES[self.scene_id]['Description'], col=pyxel.COLOR_WHITE)
    
    App()
    

    Output:

    enter image description here