Exemplo n.º 1
0
        static void Main(string[] args)
        {
            string segChar = "0";

            SnakeSegment[] segments = new SnakeSegment[4]
            {
                new SnakeSegment(3, 7, "#"),
                new SnakeSegment(3, 6, segChar),
                new SnakeSegment(3, 5, segChar),
                new SnakeSegment(3, 4, segChar)
            };

            Random rnd = new Random();

            Snake snake = new Snake(ref segments, segChar);

            Apple apple = new Apple(10, 10, "A", rnd);

            Graphics g = new Graphics(15, 25, snake, apple);

            g.Draw();

            ConsoleKeyInfo key;
            Direction      dir = 0;

            while (snake.CheckSnakeAlive())
            {
                WaitForPressKey(2, ref dir);

                if (dir == Direction.Right)
                {
                    snake.MoveSegments(snake.GetFirstX(), snake.GetFirstY() + 1);
                }
                else if (dir == Direction.Left)
                {
                    snake.MoveSegments(snake.GetFirstX(), snake.GetFirstY() - 1);
                }
                else if (dir == Direction.Up)
                {
                    snake.MoveSegments(snake.GetFirstX() - 1, snake.GetFirstY());
                }
                else if (dir == Direction.Down)
                {
                    snake.MoveSegments(snake.GetFirstX() + 1, snake.GetFirstY());
                }

                if (snake.GetFirstX() == apple.GetX() && snake.GetFirstY() == apple.GetY())
                {
                    snake.AddSegment();
                    apple.Relocate();
                }

                g.Draw();
            }
            Console.WriteLine($"GAME OVER!");
            WaitForExit();
        }
Exemplo n.º 2
0
        public Snake(int width, int height)
        {
            area_width  = width;
            area_height = height;

            Head = new SnakeSegment(new Vector2(width / 2, height / 2), null);


            Console.CursorVisible = false;
            //  Console.SetWindowSize(width, height);

            PrintArea();
        }
Exemplo n.º 3
0
        public void Move()
        {
            //foreach (SnakeSegment a in segments)
            //{
            //    System.Windows.Forms.MessageBox.Show(Board.GetPositionStatus(a.X, a.Y).ToString()  );
            //}


            int snakeLength = segments.Count;

            for (int i = snakeLength - 1; i >= 0; i--) // Move each segment starting at END OF TAIL
            {
                if (!segments[i].IsNew)                // move it if not new
                {
                    if (segments[i].IsHead)
                    {
                        SegmentToDraw = segments[i];

                        switch (Direction)
                        {
                        case Utility.Movements.up:
                            // TODO: boundary check
                            if (segments[i].Y <= 0)
                            {
                                endGame(methodOfDeath.wall);
                            }


                            Board.SetPositionStatus(segments[i].X, segments[i].Y, Board.PositionStates.empty);
                            segments[i].Y -= 1;


                            if (Board.GetPositionStatus(segments[i].X, segments[i].Y) == Board.PositionStates.snake)
                            {
                                endGame(methodOfDeath.snake);
                            }


                            if (Board.GetPositionStatus(segments[i].X, segments[i].Y) == Board.PositionStates.food)
                            {
                                Score++;
                                GrowSegment();
                                Board.PlaceFood(segments);
                            }

                            break;

                        case Utility.Movements.down:
                            // TODO: boundary check
                            if (segments[i].Y >= Board.PlayfieldHeight - 1)
                            {
                                endGame(methodOfDeath.wall);
                            }

                            Board.SetPositionStatus(segments[i].X, segments[i].Y, Board.PositionStates.empty);
                            //segments[i].X = segments[i].X;
                            segments[i].Y += 1;
                            //Board.SetPositionStatus(segments[i].X, segments[i].Y, Board.PositionStates.snake);

                            if (Board.GetPositionStatus(segments[i].X, segments[i].Y) == Board.PositionStates.snake)
                            {
                                endGame(methodOfDeath.snake);
                            }

                            if (Board.GetPositionStatus(segments[i].X, segments[i].Y) == Board.PositionStates.food)
                            {
                                Score++;
                                GrowSegment();
                                Board.PlaceFood(segments);
                            }

                            break;

                        case Utility.Movements.left:
                            // TODO: boundary check
                            if (segments[i].X <= 0)
                            {
                                endGame(methodOfDeath.wall);
                            }

                            Board.SetPositionStatus(segments[i].X, segments[i].Y, Board.PositionStates.empty);
                            segments[i].X -= 1;
                            //segments[i].Y = segments[i].Y;
                            //Board.SetPositionStatus(segments[i].X, segments[i].Y, Board.PositionStates.snake);

                            if (Board.GetPositionStatus(segments[i].X, segments[i].Y) == Board.PositionStates.snake)
                            {
                                endGame(methodOfDeath.snake);
                            }

                            if (Board.GetPositionStatus(segments[i].X, segments[i].Y) == Board.PositionStates.food)
                            {
                                Score++;
                                GrowSegment();
                                Board.PlaceFood(segments);
                            }

                            break;

                        case Utility.Movements.right:
                            // TODO: boundary check
                            if (segments[i].X >= Board.PlayfieldWidth - 1)
                            {
                                endGame(methodOfDeath.wall);
                            }

                            Board.SetPositionStatus(segments[i].X, segments[i].Y, Board.PositionStates.empty);
                            segments[i].X += 1;
                            //segments[i].Y = segments[i].Y;
                            //Board.SetPositionStatus(segments[i].X, segments[i].Y, Board.PositionStates.snake);

                            if (Board.GetPositionStatus(segments[i].X, segments[i].Y) == Board.PositionStates.snake)
                            {
                                endGame(methodOfDeath.snake);
                            }

                            if (Board.GetPositionStatus(segments[i].X, segments[i].Y) == Board.PositionStates.food)
                            {
                                Score++;
                                GrowSegment();
                                Board.PlaceFood(segments);
                            }

                            break;

                        default:
                            break;
                        }
                    }
                    else // not head
                    { // move to same spot as leading segment
                        Board.SetPositionStatus(segments[i].X, segments[i].Y, Board.PositionStates.empty);
                        segments[i].X = segments[i - 1].X;
                        segments[i].Y = segments[i - 1].Y;
                        //Board.SetPositionStatus(segments[i].X, segments[i].Y, Board.PositionStates.snake);

                        // for board drawing
                        if (i == snakeLength - 1) // first loop through -- this is the end of tail piece so erase it
                        {
                            SegmentToErase = segments[i];
                        }
                    }
                }
                else // was new so it didn't move - mark it not new for next time
                {
                    segments[i].IsNew = false;
                    //Board.SetPositionStatus(segments[i].X, segments[i].Y, Board.PositionStates.empty);

                    // Don't mark it to erase since it didn't move
                    SegmentToErase = null;
                }

                foreach (SnakeSegment seg in segments)
                {
                    Board.SetPositionStatus(seg.X, seg.Y, Board.PositionStates.snake);
                }
            }
        }
Exemplo n.º 4
0
        public SnakeSegment(Vector2 _pos, SnakeSegment _next)
        {
            pos = _pos;

            Next = _next;
        }