コード例 #1
0
        private void StartNewGame()
        {
            Console.Clear();
            ReadAllWords();

            SpaceshipObject spaceship = new SpaceshipObject(new Point(12, 39));

            spaceship.Draw();

            Thread thread = new Thread(() => this.DrawWords());

            thread.Start();

            while (true)
            {
                ConsoleKeyInfo key = Console.ReadKey(true);

                // termination durring the game
                if (key.Key == ConsoleKey.Escape)
                {
                    this.LivesCount = 0;
                }

                if (!Char.IsLetter(key.KeyChar))
                {
                    continue;
                }

                if (this.IsWordSelected)
                {
                    if (this.SelectedWord.Element.Text[this.CurrentCharIndex] == key.KeyChar)
                    {
                        char[] text = this.SelectedWord.Element.Text.ToCharArray();
                        text[this.CurrentCharIndex]    = ' ';
                        this.SelectedWord.Element.Text = new string(text);
                        this.CurrentCharIndex++;

                        if (this.CheckWordIsCompleted(this.SelectedWord.Element.Text))
                        {
                            this.SelectedWord.Draw();
                            this.IsWordSelected           = false;
                            this.SelectedWord.IsSelected  = false;
                            this.SelectedWord.IsVisible   = false;
                            this.SelectedWord.IsDestroyed = true;
                            this.Score           += this.Coefficient * 10;
                            this.Coefficient      = 0;
                            this.CurrentCharIndex = 0;
                        }
                    }
                }
                else
                {
                    this.SelectedWord = visibleWords.Where(x => x.Element.Text[0] == key.KeyChar).FirstOrDefault();

                    if (this.SelectedWord != null)
                    {
                        this.Coefficient             = this.SelectedWord.Element.Text.Length;
                        this.IsWordSelected          = true;
                        this.SelectedWord.IsSelected = true;
                        char[] text = this.SelectedWord.Element.Text.ToCharArray();
                        text[this.CurrentCharIndex]    = ' ';
                        this.SelectedWord.Element.Text = new string(text);
                        this.CurrentCharIndex++;
                    }
                }
            }
        }
コード例 #2
0
        private void DrawWords()
        {
            //separation line
            for (int i = 0; i < 45; i++)
            {
                PrintStringOnPosition(30, i, "|", ConsoleColor.Green);
            }

            Stopwatch newGameStopwatch = new Stopwatch();

            newGameStopwatch.Start();

            while (true)
            {
                if (newGameStopwatch.ElapsedMilliseconds % 3000 < 500)
                {
                    char       randomLetter = this.GetRandomLetter();
                    WordObject currentWord  = wordsDictionary[randomLetter]
                                              .Where(x => x.IsVisible == false && x.IsMissed == false && x.IsDestroyed == false)
                                              .FirstOrDefault();

                    if (currentWord != null)
                    {
                        currentWord.IsVisible = true;
                    }
                }

                foreach (var currentWordCollection in wordsDictionary.ToArray())
                {
                    foreach (var word in currentWordCollection.Value)
                    {
                        if (word.IsVisible)
                        {
                            Point currentPoint = word.Element.CoordinatePoint;
                            currentPoint.Y += 1;
                            if (currentPoint.Y > 0 && currentPoint.Y < 2)
                            {
                                currentPoint.X = randomGenerator.Next(0, 25);
                            }
                            word.Element.CoordinatePoint = currentPoint;
                            if (currentPoint.Y == 45)
                            {
                                this.LivesCount--;
                                //here clears words fallen
                                Console.SetCursorPosition(0, 44);
                                Console.Write(new string(' ', 29));
                                Console.SetCursorPosition(0, 0);
                                SpaceshipObject spaceship = new SpaceshipObject(new Point(12, 39));
                                spaceship.Draw();
                            }
                        }
                    }
                }


                //Console.Clear();

                foreach (var currentWordCollection in wordsDictionary.ToArray())
                {
                    foreach (var word in currentWordCollection.Value)
                    {
                        if (word.IsVisible)
                        {
                            word.Draw();
                            this.visibleWords.Add(word);
                        }
                    }
                }



                PrintStringOnPosition(32, 10, "Lives: " + this.LivesCount, ConsoleColor.Green); //fix here 30->32
                PrintStringOnPosition(32, 11, "Points: " + this.Score, ConsoleColor.Green);     //fix here 30->32
                if (this.LivesCount == 0)
                {
                    Console.Clear();
                    CheckHighScore(this.Score); //add to HallOfFame

                    PrintStringOnPosition(10, 10, "Game over", ConsoleColor.Red);
                    PrintStringOnPosition(10, 11, "Press ENTER to exit", ConsoleColor.Red);
                    PrintStringOnPosition(10, 12, "Points: " + this.Score, ConsoleColor.Red);
                    Console.WriteLine();

                    Environment.Exit(0);
                }

                Thread.Sleep(350);
                this.visibleWords.Clear();
            }
        }