Ejemplo n.º 1
0
        // Update the snake's position
        private void UpdateSnake()
        {
            // End game if snake is dead
            if (dead)
            {
                started = false;
            }

            var direction = (SnakeDirection)dir;
            var action    = snakeCharacter.GetNextDirection(direction);


            if ((action == SnakeDirection.Down && direction == SnakeDirection.Up) ||
                (action == SnakeDirection.Up && direction == SnakeDirection.Down) ||
                (action == SnakeDirection.Left && direction == SnakeDirection.Right) ||
                (action == SnakeDirection.Right && direction == SnakeDirection.Left))
            {
                //can't go to opposite direction
            }
            else
            {
                dir = (int)action;
            }

            //// Turn right
            //if (GetKey(Key.Right).Pressed)
            //{
            //    dir++;
            //    if (dir == 4)
            //        dir = 0;
            //}

            //// Turn left
            //if (GetKey(Key.Left).Pressed)
            //{
            //    dir--;
            //    if (dir == -1)
            //        dir = 3;
            //}



            if (started)
            {
                // Move in the direction
                switch (dir)
                {
                case 0:     // UP
                    snake.Insert(0, new SnakeSegment(snake[0].X, snake[0].Y - 1));
                    break;

                case 1:     // RIGHT
                    snake.Insert(0, new SnakeSegment(snake[0].X + 1, snake[0].Y));
                    break;

                case 2:     // DOWN
                    snake.Insert(0, new SnakeSegment(snake[0].X, snake[0].Y + 1));
                    break;

                case 3:     // LEFT
                    snake.Insert(0, new SnakeSegment(snake[0].X - 1, snake[0].Y));
                    break;
                }

                // Pop the tail
                snake.RemoveAt(snake.Count - 1);

                CheckCollision();
            }
        }
Ejemplo n.º 2
0
        private void UpdateSnake()
        {
            if (dead)
            {
                started = false;
            }
            var direction = (SnakeDirection)dir;
            var action    = snakeCharacter.GetNextDirection(direction);

            if ((action == SnakeDirection.Down && direction == SnakeDirection.Up) || (action == SnakeDirection.Up && direction == SnakeDirection.Down) || (action == SnakeDirection.Left && direction == SnakeDirection.Right) || (action == SnakeDirection.Right && direction == SnakeDirection.Left))
            {
            }
            else
            {
                dir = (int)action;
            }
            if (snake[0].X == foodX && snake[0].Y != (foodY))
            {
                if (foodY - snake[0].Y > 0)
                {
                    dir = 2;
                }
                else
                {
                    dir = 0;
                }
            }
            else if (snake[0].Y == foodY && snake[0].X != (foodX))
            {
                if (foodX - snake[0].X > 0)
                {
                    dir = 3;
                }
                else
                {
                    dir = 1;
                }
            }

            if (snake[0].Y == 2 || snake[0].X == 2)
            {
                dir = 2;
            }
            else if (snake[0].Y == 47 || snake[0].X == 47)
            {
                dir = 1;
            }

            if (started)
            {
                switch (dir)
                {
                case 0:
                    snake.Insert(0, new SnakeSegment(snake[0].X, snake[0].Y - 1));
                    break;

                case 1:
                    snake.Insert(0, new SnakeSegment(snake[0].X + 1, snake[0].Y));
                    break;

                case 2:
                    snake.Insert(0, new SnakeSegment(snake[0].X, snake[0].Y + 1));
                    break;

                case 3:
                    snake.Insert(0, new SnakeSegment(snake[0].X - 1, snake[0].Y));
                    break;
                }

                snake.RemoveAt(snake.Count - 1);

                CheckCollision();
            }
        }