Example #1
0
    def spriteCollide(self, gameobject, collider):
        """Saves if the timer has already run down."""
        if self.save_timer < 0:
            # Deactivate old EMT
            old_spawn = statevars.variables["map"].get("spawn")
            if old_spawn is not None:
                self.obj_mgr.get(old_spawn).call("deactivate")

            # Make this glow and make a sound
            self.glow.setVisibility(True)
            self.sound.play()

            # Prevent saving for five seconds
            self.save_timer = self.save_delay

            # Set the new map spawn point and save the state variables
            statevars.variables["map"]["spawn"] = self.name
            statevars.save()
    def start(self):
        """Initialization for the play state"""
        # The player's energy bar
        self.energy_bar = energybar.EnergyBar(None, 4, 4)

        # Get the map state variable
        map = statevars.variables.get("map")
        if map is None:
            # Since there is no map variable (like when playing a new game), we'll create one and start at the start.
            map_file = "maps/start.tmx"
            map = {}
            statevars.variables["map"] = map
            map["filename"] = map_file
            map["spawn"] = None
            self.coins = 0
            statevars.save()
        else:
            # Get which map file should be loaded and the current collected coins count
            map_file = map.get("filename")
            self.coins = len(map.get("coins", []))
            if map_file is None:
                # If the map filename is not present, start at the start
                map_file = "maps/start.tmx"

        if self.scene is not None:
            # Get rid of old scene
            self.scene.destroy()

        # Create the scene by loading the specified map file
        self.scene = scene.Scene(self, map_file)

        # Get the spawn point for the player.  It would be None if the player has not saved in this map yet.
        spawn = map.get("spawn")
        if spawn is not None:
            # Spawn the player at the specified spawn point
            obj = self.scene.object_mgr.get(spawn)
            obj.call("spawnPlayer")

        # Get how many coins are in this map
        self.max_coins = int(self.scene.properties.get("coins", 0))
        self.updateCoins()

        # The map that should be loaded after the player completes this one
        self.next_map = self.scene.properties.get("next_map")
 def btnNewGame(self):
     """Called when the player selects New Game.  Clear state variables, save them, then starts playing."""
     statevars.variables = {}
     statevars.save("saves/save_1.json")
     statemgr.switch("play")
 def nextLevel(self):
     """Go to the next level"""
     statevars.variables["map"] = {"filename":self.next_map}
     statevars.save()
     self.start()