Пример #1
0
        public void Move(MovementResult mr, bool grow)
        {
            // save off the old head location, and compute the new one
            mr.OldHead   = Segments[HeadIdx];
            mr.NewHead.X = (byte)(mr.OldHead.X + deltaX);
            mr.NewHead.Y = (byte)(mr.OldHead.Y + deltaY);

            // grow the array if necessary, and remember the last
            // segment of the tail.
            if (grow)
            {
                Array.Resize(ref Segments, Segments.Length + 1);
                HeadIdx = HeadIdx + 1;
                if (HeadIdx < (Segments.Length - 1))
                {
                    Array.Copy(Segments, HeadIdx, Segments, HeadIdx + 1, Segments.Length - HeadIdx - 1);
                }
                mr.MovedTail.X = 255;  // 255 == growing, ignore
                mr.MovedTail.Y = 255;  // 255 == growing, ignore
            }
            else
            {
                HeadIdx = HeadIdx + 1;
                if (HeadIdx == Segments.Length)
                {
                    HeadIdx = 0;
                }
                mr.MovedTail = Segments[HeadIdx];
            }

            // Set the new head location
            Segments[HeadIdx] = mr.NewHead;
        }
Пример #2
0
 private void DrawUpdates(MovementResult mr)
 {
     if (mr.MovedTail.Y != 255)
     {
         // 255 signals that we grew... it's hacky but deliciously so
         Console.SetCursorPosition(mr.MovedTail.X, mr.MovedTail.Y);
         Console.Write(' ');
     }
     Console.SetCursorPosition(mr.NewHead.X, mr.NewHead.Y);
     Console.ForegroundColor = ConsoleColor.Green;
     Console.Write('@');
     Console.ForegroundColor = ConsoleColor.Cyan;
     Console.SetCursorPosition(mr.OldHead.X, mr.OldHead.Y);
     Console.Write('#');
     Console.SetCursorPosition(Console.WindowLeft,
                               Console.WindowTop + Console.WindowHeight - 1);
 }
Пример #3
0
        public void RunGame()
        {
            // write the initial location
            Console.SetCursorPosition(10, 10);
            Console.Write('@');
            DrawApple();

            var mr = new MovementResult();

            while (true)
            {
                System.Threading.Thread.Sleep(Speed + SpFactor);

                ReadUserInput();
                snake.Move(mr, grow);
                grow = false;

                if (OutOfBounds() || snake.SelfCollision())
                {
                    break;
                }

                DrawUpdates(mr);

                if (apples.Eaten(mr.NewHead))
                {
                    grow = true;
                    apples.GrowNew(Console.WindowWidth,
                                   Console.WindowHeight - 1,
                                   snake.Collision);
                    DrawApple();
                    if (Speed > 10)
                    {
                        Speed -= 10;
                    }
                }
            }

            bool OutOfBounds() =>
            (mr.NewHead.X >= Console.WindowWidth) ||
            (mr.NewHead.Y >= Console.WindowHeight - 1);
        }