Beispiel #1
0
        // This method handles saving the game event
        private void SaveBtn_Click(object sender, EventArgs e)
        {
            // Open up the save file dialog
            saveFileDialog1.ShowDialog();

            // Get the file name from the user
            string fileName = saveFileDialog1.FileName;

            // If the file name has not been entered, show the warning message and open the save file dialog again
            if (fileName.Trim().Length < 1)
            {
                MessageBox.Show("Please enter FileName");
                saveFileDialog1.ShowDialog();
            }

            GameData gameData = new GameData()
            {
                User     = this.user,
                Player   = this.Game.Player,
                Score    = this.Game.Score,
                Level    = this.Game.Level,
                Terrains = this.Game.Terrains
            };

            // Using custom class 'GameJsonLoaderSaver', save the file into a text file that contains JSON string
            GameJsonLoaderSaver.SaveGame(gameData, fileName);

            this.Game.State = Game.GAMESTATE_RUNNING;
            SetPause();
        }
Beispiel #2
0
        // This method is handling open file dialog event to load saved games
        private void LoadBtn_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            string fileName = openFileDialog1.FileName;

            try
            {
                // Load the saved data and put the information into GameData object
                GameData gamedata = GameJsonLoaderSaver.LoadGame(fileName);
                // Initialize Game object
                Game game = new Game()
                {
                    // Extracts essential data from GameData object puts it back into Game object
                    Level    = gamedata.Level,
                    Player   = gamedata.Player,
                    Score    = gamedata.Score,
                    Terrains = gamedata.Terrains
                };

                new Form1(game, new Util.KeyHandler(game), gamedata.User).Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot open file");
            }
        }