Esempio n. 1
0
        private void StartGame()
        {
            gameover = false;
            snake.Clear();
            snakeSpeed         = 1.5f;
            snakeLoop.Interval = (int)(1000 / snakeSpeed);
            score = 0;
            SnakePart head = new SnakePart(10, 8);

            snake.Add(head);
            GenerateFood();
        }
Esempio n. 2
0
        public void moveTailParts(SnakePart snakeHead)
        {
            var previousPart = snakeHead;

            foreach (var tailPart in TailParts)
            {
                var tempPart = tailPart.Clone();
                tailPart.X   = previousPart.X;
                tailPart.Y   = previousPart.Y;
                previousPart = (SnakePart)tempPart;
            }
        }
Esempio n. 3
0
        private void MoveSnake()
        {
            //a)	删掉尾巴
            while (snakeParts.Count>=snakeLength)
            {
                GameArea.Children.Remove(snakeParts[0].UiElement);
                snakeParts.RemoveAt(0);
            }

            //b)	蛇头换上蛇身子的颜色
            foreach (var snakePart in snakeParts)
            {
                (snakePart.UiElement as Rectangle).Fill = snakeBodyBrush;
                snakePart.IsHead = false;
            }

            //c)	找个地方,添个新蛇头
            SnakePart snakeHead = snakeParts[snakeParts.Count -1];
            double nextX = snakeHead.Position.X;
            double nextY = snakeHead.Position.Y;

            switch(snakeDirection)
            {
                case SnakeDirection.Left:
                    nextX -= SnakeSquareSize;
                    break;
                case SnakeDirection.Right:
                    nextX += SnakeSquareSize;
                    break;
                case SnakeDirection.Up:
                    nextY -= SnakeSquareSize;
                    break;
                case SnakeDirection.Down:
                    nextY += SnakeSquareSize;
                    break;
                default:
                    break;
            }
            //,添个新蛇头
            snakeParts.Add(new SnakePart
            {
                Position = new Point(nextX, nextY),
                IsHead=true
            }
            ) ;

            //d)	重新画出来
            DrawSnake();

            //判断是否撞墙
            DoCollisionCheck();
        }
Esempio n. 4
0
        private void MoveSnake()
        {
            // Remove the last part of the snake, in preparation of the new part added below
            while (snakeParts.Count >= snakeLength)
            {
                GameArea.Children.Remove(snakeParts[0].UiElement);
                snakeParts.RemoveAt(0);
            }
            // Next up, we'll add a new element to the snake, which will be the (new) head
            // Therefore, we mark all existing parts as non-head (body) elements and then
            // we make sure that they use the body brush
            foreach (SnakePart snakePart in snakeParts)
            {
                (snakePart.UiElement as Rectangle).Fill = snakeBodyBrush;
                snakePart.IsHead = false;
            }

            // Determine in which direction to expand the snake, based on the current direction
            SnakePart snakeHead = snakeParts[snakeParts.Count - 1];
            double    nextX     = snakeHead.Position.X;
            double    nextY     = snakeHead.Position.Y;

            switch (snakeDirection)
            {
            case SnakeDirection.Left:
                nextX -= SnakeSquareSize;
                break;

            case SnakeDirection.Right:
                nextX += SnakeSquareSize;
                break;

            case SnakeDirection.Up:
                nextY -= SnakeSquareSize;
                break;

            case SnakeDirection.Down:
                nextY += SnakeSquareSize;
                break;
            }

            // Now add the new head part to our list of snake parts...
            snakeParts.Add(new SnakePart()
            {
                Position = new Point(nextX, nextY),
                IsHead   = true
            });
            //... and then have it drawn!
            DrawSnake();
            // We'll get to this later...
            DoCollisionCheck();
        }
Esempio n. 5
0
        public void MoveSnake(object sender, EventArgs e)
        {
            var length = this.TheSnake.Count;

            // remove tail, the last snake part in list, if the snake has eaten a food, then don't remove this.
            if (this.TheSnake.Count >= this.snakeLength)
            {
                SnakePart snakeTail = this.TheSnake[length - 1];
                this.gameAreaCanvas.Children.Remove(snakeTail.UiElement);
                this.TheSnake.RemoveAt(length - 1);
            }

            // set snake head to part of body
            SnakePart oldHead = this.TheSnake[0];

            oldHead.IsHead = false;

            // compute the position of new head.
            double newX = oldHead.Position.X;
            double newY = oldHead.Position.Y;

            if (this.Direction == Direction.Right)
            {
                newX += 1;
            }
            else if (this.Direction == Direction.Left)
            {
                newX -= 1;
            }
            else if (this.Direction == Direction.Up)
            {
                newY -= 1;
            }
            else if (this.Direction == Direction.Down)
            {
                newY += 1;
            }

            // add new snake head
            SnakePart newHead = new SnakePart(newX, newY, true);

            this.TheSnake.Insert(0, newHead);
            this.gameAreaCanvas.Children.Add(newHead.UiElement);
            Canvas.SetTop(newHead.UiElement, newHead.Position.Y * Constants.SquareSize);
            Canvas.SetLeft(newHead.UiElement, newHead.Position.X * Constants.SquareSize);

            this.CheckCollision();
        }
Esempio n. 6
0
        public Snake(Canvas gameAreaCanvas, DispatcherTimer timer)
        {
            this.TheSnake       = new List <SnakePart>();
            this.Direction      = Direction.Right;
            this.gameAreaCanvas = gameAreaCanvas;
            this.timer          = timer;

            // Initialize the snake with two snake parts. One head and one body
            SnakePart head = new SnakePart(4, 15, true);
            SnakePart body = new SnakePart(3, 15);

            this.TheSnake.Add(head);
            this.TheSnake.Add(body);

            this.DrawSnake();

            timer.Tick    += this.MoveSnake;
            this.snakeFood = new SnakeFood(gameAreaCanvas, this);
        }
Esempio n. 7
0
        private void MoveLast()
        {
            switch (CurrentDirection)
            {
            case Direction.Up:
                if (CurrentDirection != Direction.Down)
                {
                    Tail.Position = Head.Position - new Vector2(0, _snakePartImage.Width + _padding);
                }
                break;

            case Direction.Right:
                if (CurrentDirection != Direction.Left)
                {
                    Tail.Position = Head.Position + new Vector2(_snakePartImage.Width + _padding, 0);
                }
                break;

            case Direction.Down:
                if (CurrentDirection != Direction.Up)
                {
                    Tail.Position = Head.Position + new Vector2(0, _snakePartImage.Width + _padding);
                }
                break;

            case Direction.Left:
                if (CurrentDirection != Direction.Right)
                {
                    Tail.Position = Head.Position - new Vector2(_snakePartImage.Width + _padding, 0);
                }
                break;
            }
            SnakePart newHead = Tail;

            SnakeParts.RemoveLast();
            SnakeParts.AddFirst(newHead);
        }
Esempio n. 8
0
        private void DoCollisionCheck()
        {
            SnakePart snakeHead = snakeParts[snakeParts.Count - 1];

            //蛇吃食物
            if ((snakeHead.Position.X == Canvas.GetLeft(snakeFood)) &&
                (snakeHead.Position.Y == Canvas.GetTop(snakeFood))
                )
            {
                EatSnakeFood();
                return;
            }

            //蛇有没有越界
            if ((snakeHead.Position.Y < 0) ||
                (snakeHead.Position.X < 0) ||
                (snakeHead.Position.Y >= GameArea.ActualHeight) ||
                (snakeHead.Position.X >= GameArea.ActualWidth)
                )
            {
                EndGame();
                return;
            }

            //蛇有没有撞到自己的身体
            foreach (var snakeBodyPart in snakeParts.Take(snakeParts.Count - 1))
            {
                if ((snakeHead.Position.X == snakeBodyPart.Position.X) &&
                    (snakeHead.Position.Y == snakeBodyPart.Position.Y)
                    )
                {
                    EndGame();
                    return;
                }
            }
        }
Esempio n. 9
0
        public static void Main(string[] a)
        {
            SnakePart snake = new SnakePart();
            bool gameOver = false;
            bool pause = false;
            bool inuse = false;
            string newKey = "right"; // 0 = up, 1 = right, 2 = down, 3 = left
            string prevKey = newKey;
            int boardWidth = Console.WindowWidth; //
            int boardHeight = Console.WindowHeight; //

            var rng = new Random();
            var point = new Point();
            var snakeParts = new List<Point>();

            Console.CursorVisible = false;
            for (int i = 0; i < 4; i++)
            {
                snakeParts.Add(new Point(10, 10));
            }

            Console.ForegroundColor = ConsoleColor.Green;
            Console.SetCursorPosition(10, 10);
            Console.Write("@");

               Food food = new Food();

            food.DrawFood();

            /*            while (true)
            {
                Point.X = rng.Next(0, boardWidth);
                Point.Y = rng.Next(0, boardHeight);
                bool spot = true;
                foreach (Point p in snakeParts)
                {
                    if (p.X == Point.X && p.Y == Point.Y)
                    {
                        spot = false;
                        break;
                    }
                }
                if (spot)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.SetCursorPosition(Point.X, Point.Y);
                    Console.Write("$");
                    break;
                }
            }*/
            var time = new Stopwatch();
            time.Start();
            while (!gameOver)
            {

            /*                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo controls = Console.ReadKey(true); // 0 = up, 1 = right, 2 = down, 3 = left
                    if (controls.Key == ConsoleKey.Escape)
                    {
                        gameOver = true;
                    }
                    else if (controls.Key == ConsoleKey.Spacebar)
                    {
                        pause = !pause;
                    }
                    else if (controls.Key == ConsoleKey.UpArrow && prevKey != "down")
                    {
                        newKey = "up";
                    }
                    else if (controls.Key == ConsoleKey.RightArrow && prevKey != "left")
                    {
                        newKey = "right";
                    }
                    else if (controls.Key == ConsoleKey.DownArrow && prevKey != "up")
                    {
                        newKey = "down";
                    }
                    else if (controls.Key == ConsoleKey.LeftArrow && prevKey != "right")
                    {
                        newKey = "left";
                    }
                }*/
                if (!pause)
                {
                    if (time.ElapsedMilliseconds < 100)
                    {
                        continue;
                    }
                    time.Restart();

                    Input input = new Input();
                    input.CheckInput();
            /*                    var tail = new Point(snakeParts.First());
                    var head = new Point(snakeParts.Last());
                    var newHead = new Point(head);*/
            /*                    switch (newKey)
                    {
                        case "up":
                            newHead.Y -= 1;
                            break;
                        case "right":
                            newHead.X += 1;
                            break;
                        case "down":
                            newHead.Y += 1;
                            break;
                        default:
                            newHead.X -= 1;
                            break;
                    }*/
                    if (snake.newHead.X < 0 || snake.newHead.X >= boardWidth)
                    {
                        gameOver = true;
                    }
                    else if (snake.newHead.Y < 0 || snake.newHead.Y >= boardHeight)
                    {
                        gameOver = true;
                    }
                    if (snake.newHead.X == point.X && snake.newHead.Y == point.Y)
                    {
                        if (snakeParts.Count + 1 >= boardWidth * boardHeight)
                        {
                            // No more room to place apples -- game over.
                            gameOver = true;
                        }
                        else
                        {
                            while (true)
                            {
                                point.X = rng.Next(0, boardWidth);
                                point.Y = rng.Next(0, boardHeight);
                                bool found = true;
                                foreach (Point i in snakeParts)
                                {
                                    if (i.X == point.X && i.Y == point.Y)
                                    {
                                        found = false;
                                        break;
                                    }
                                }
                                if (found)
                                {
                                    inuse = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (!inuse)
                    {
                        snake.snakeParts.RemoveAt(0);
                        foreach (Point x in snakeParts)
                        {
                            if (x.X == snake.newHead.X && x.Y == snake.newHead.Y)
                            {
                                // Death by accidental self-cannibalism.
                                gameOver = true;
                                break;
                            }
                        }
                    }
                    if (!gameOver)
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.SetCursorPosition(snake.head.X, snake.head.Y);
                        Console.Write("O");
                        if (!inuse)
                        {
                            Console.SetCursorPosition(snake.tail.X, snake.tail.Y);
                            Console.Write(" ");
                        }
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.SetCursorPosition(point.X, point.Y);
                            Console.Write("$");
                            inuse = false;
                        }
                        snake.snakeParts.Add(snake.newHead);
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.SetCursorPosition(snake.newHead.X, snake.newHead.Y);
                        Console.Write("@");
                        prevKey = newKey;
                    }
                }
            }
        }
Esempio n. 10
0
        private void GenerateFood()
        {
            Random random = new Random();

            food = new SnakePart(random.Next(0, 20), random.Next(0, 15));
        }
Esempio n. 11
0
        private void UpdateSnake(object sender, EventArgs e)
        {
            if (!gameover)
            {
                for (int i = snake.Count - 1; i >= 0; i--)
                {
                    if (i == 0)
                    {
                        switch (direction)
                        {
                        case 0:
                            snake[0].Y++;
                            break;

                        case 1:
                            snake[0].X--;
                            break;

                        case 2:
                            snake[0].X++;
                            break;

                        case 3:
                            snake[0].Y--;
                            break;
                        }
                        //falnak mentünk-e?
                        SnakePart head = snake[0];
                        if (head.X >= 20 || head.X < 0 || head.Y >= 15 || head.Y < 0)
                        {
                            GameOver();
                        }
                        //magunknak mentünk-e?
                        for (int j = 1; j < snake.Count; j++)
                        {
                            if (head.X == snake[j].X && head.Y == snake[j].Y)
                            {
                                GameOver();
                            }
                        }
                        //kajalas
                        if (head.X == food.X && head.Y == food.Y)
                        {
                            SnakePart part = new SnakePart(snake[snake.Count - 1].X, snake[snake.Count - 1].Y);
                            snake.Add(part);
                            GenerateFood();
                            score++;
                            if (snakeSpeed < 30)
                            {
                                snakeSpeed         = snakeSpeed + 0.5f;
                                snakeLoop.Interval = (int)(1000 / snakeSpeed);
                            }
                        }
                    }
                    else
                    {
                        snake[i].X = snake[i - 1].X;
                        snake[i].Y = snake[i - 1].Y;
                    }
                }
            }
        }
Esempio n. 12
0
        //Start up code for creating the snake parts
        private void StartUp()
        {
            _oldState = Keyboard.GetState();
            _dead = false;

            EndPause();
            this.SnakeLength = this.StartLength;

            for (int i = 0; i <= this.SnakeLength - 1; i++)
            {
                SnakePart part = new SnakePart(new Vector2(120 - (10 * i), 120), 3);
                _Snake.Add(part);
            }

            _paused = false;
            _pauseKeyDown = false;
            _pausedForGuide = false;
        }
Esempio n. 13
0
 /// <summary>
 /// Makes the snake grow bigger!
 /// </summary>
 /// <returns></returns>
 private bool GrowSnake()
 {
     SnakePart part = new SnakePart(_Snake[this.SnakeLength - 1].Position, _Snake[this.SnakeLength - 1].Facing);
     _Snake.Add(part);
     this.SnakeLength += 1;
     return true;
 }
 public void AddSnakePart(SnakePart snakePart) => Parts.Add(snakePart);