コード例 #1
0
ファイル: Methods.cs プロジェクト: nzhul/TelerikAcademy
        //Load the last saved game, player and score from a file        
        public static void LoadGame(string filePath, PlayField playField, Player player)
        {
            try
            {
                StreamReader reader = new StreamReader(filePath);
                using (reader)
                {
                    player.Name = reader.ReadLine();
                    player.Score = int.Parse(reader.ReadLine());

                    for (int i = 0; i < playField.GetLength(0); i++)
                    {
                        string line = reader.ReadLine();
                        string[] currentLine = line.Trim().Split(' ');
                        int counter = 0;
                        for (int j = 0; j < currentLine.Length; j += 4)
                        {
                            int x = int.Parse(currentLine[j]);
                            int y = int.Parse(currentLine[j + 1]);
                            char symbol = char.Parse(currentLine[j + 2]);
                            ConsoleColor color = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), currentLine[j + 3]);
                            Box box = new Box(x, y, symbol, color);
                            box.InitBox(symbol);
                            playField[i, counter] = box;
                            counter++;
                        }
                    }
                }
            }
            catch (Exception)
            {
                Console.Clear();
                throw;
            }
        }
コード例 #2
0
ファイル: Methods.cs プロジェクト: nzhul/TelerikAcademy
        //Save current game, player and score in a file
        public static void SaveGame(string filePath, PlayField playField, Player player)
        {
            try
            {
                StreamWriter writer = new StreamWriter(filePath);
                using (writer)
                {
                    writer.WriteLine(player.Name);
                    writer.WriteLine(player.Score);

                    for (int i = 0; i < playField.GetLength(0); i++)
                    {
                        for (int j = 0; j < playField.GetLength(1); j++)
                        {
                            writer.Write(playField[i, j].X + " ");
                            writer.Write(playField[i, j].Y + " ");
                            writer.Write(playField[i, j].Symbol + " ");
                            writer.Write(playField[i, j].Color + " ");
                        }
                        writer.WriteLine();
                    }
                }
            }
            catch (Exception)
            {
                Console.Clear();
                throw;
            }
        }
コード例 #3
0
        //The Main Menu of the Game       
        private static void Menu()
        {
            Methods.Settings(PLAYFIELDWIDTH, PLAYFIELDHIGHT);
            Methods.DrawMenu(player.Name, gameoverFlag);
            bool isCommand = false;

            while (!isCommand)
            {
                ConsoleKeyInfo keyPressed = Console.ReadKey(true);
                switch (keyPressed.Key)
                {
                    //Load a new game
                    case ConsoleKey.N:
                        {
                            gameoverFlag = false;
                            player = new Player(Methods.LoadPlayer(), 0);
                            Console.Clear();
                            Console.SetWindowSize(PLAYFIELDWIDTH + 7, PLAYFIELDHIGHT);
                            Console.SetBufferSize(PLAYFIELDWIDTH + 7, PLAYFIELDHIGHT);
                            playField = new PlayField();
                            playField.InitPlayField(CHARBASE);
                            playField.DrawPlayField();
                            Methods.Settings(PLAYFIELDWIDTH, PLAYFIELDHIGHT);
                            Methods.VisualizeSound(soundflag);
                            FallDownAndGenerateNewJewels();
                            Methods.ScoreField(SCOREFIELDWIDTH, SCOREFIELDHIGHT, player);

                            //Draw a progress bar showing how near is the maximum score                                                        
                            Methods.DrawProgress(player.Score, MAXSCORE, PROGRESSBARWIDTH, PROGRESSBARHIGHT);

                            escapeFlag = 0;
                            Engine();
                            isCommand = true;
                        } break;

                    //Return to the game or go to the Menu
                    case ConsoleKey.Escape:
                        {
                            if ((player.Name != "") && !gameoverFlag)
                            {
                                if (escapeFlag == 0)
                                {
                                    Console.Clear();
                                    Console.SetWindowSize(PLAYFIELDWIDTH + 7, PLAYFIELDHIGHT);
                                    Console.SetBufferSize(PLAYFIELDWIDTH + 7, PLAYFIELDHIGHT);
                                    playField.DrawPlayField();
                                    Methods.Settings(PLAYFIELDWIDTH, PLAYFIELDHIGHT);
                                    Methods.ScoreField(SCOREFIELDWIDTH, SCOREFIELDHIGHT, player);

                                    //Draw a progress bar showing how near is the maximum score                                                        
                                    Methods.DrawProgress(player.Score, MAXSCORE, PROGRESSBARWIDTH, PROGRESSBARHIGHT);

                                    Engine();
                                    escapeFlag = 1;
                                }
                                else
                                {
                                    escapeFlag = 0;
                                    Menu();
                                }
                                isCommand = true;
                            }
                        }; break;

                    //Save the game in a file
                    case ConsoleKey.S:
                        {
                            if ((player.Name != "") && !gameoverFlag)
                            {
                                Methods.SaveHighestScores(HIGHSCOREFILEPATH, player, TOPSCORECOUNT);
                                Methods.SaveGame(GAMEFILEPATH, playField, player);

                                string saveMessage = "The Game has been saved!";
                                Console.SetCursorPosition((Console.WindowWidth - saveMessage.Length) / 2, Console.WindowHeight - 2);
                                Console.ForegroundColor = ConsoleColor.DarkRed;
                                Console.WriteLine(saveMessage);
                            }
                            isCommand = false;
                        }; break;

                    //Load a game from a file
                    case ConsoleKey.L:
                        {
                            gameoverFlag = false;
                            Console.Clear();
                            Console.SetWindowSize(PLAYFIELDWIDTH + 7, PLAYFIELDHIGHT);
                            Console.SetBufferSize(PLAYFIELDWIDTH + 7, PLAYFIELDHIGHT);
                            playField = new PlayField();
                            Methods.LoadGame(GAMEFILEPATH, playField, player);
                            playField.DrawPlayField();
                            Methods.Settings(PLAYFIELDWIDTH, PLAYFIELDHIGHT);
                            Methods.ScoreField(SCOREFIELDWIDTH, SCOREFIELDHIGHT, player);
                            Methods.VisualizeSound(soundflag);

                            //Draw a progress bar showing how near is the maximum score                                                        
                            Methods.DrawProgress(player.Score, MAXSCORE, PROGRESSBARWIDTH, PROGRESSBARHIGHT);

                            escapeFlag = 0;
                            Engine();
                            isCommand = true;
                        }; break;

                    case ConsoleKey.T:
                        {
                            topscoreFlag = true;
                            Methods.ShowTopScores(HIGHSCOREFILEPATH);
                            Engine();
                            //MessageBox.Show(message, "TOP SCORES!!!", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                        }; break;

                    //Quit the game
                    case ConsoleKey.Q:
                        {
                            Environment.Exit(0);
                            isCommand = true;
                        }; break;
                    default:
                        {
                            isCommand = false;
                        };
                        break;
                }
            }
        }