Пример #1
0
        public SnakeElement eat()
        {
            snakeLength++;
            Rectangle element = new Rectangle();

            element.Width  = headSize;
            element.Height = headSize;
            SnakeElement snakeElement = new SnakeElement();

            snakeElements.Add(snakeElement);
            return(snakeElement);
        }
Пример #2
0
        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            snake.move();
            for (int i = 0; i < snake.snakeLength; i++)
            {
                Canvas.SetLeft(snake.snakeElements[i].element, snake.snakeElements[i].xCordSnakeElement);
                Canvas.SetTop(snake.snakeElements[i].element, snake.snakeElements[i].yCordSnakeElement);
            }

            if (snake.snakeElements[0].xCordSnakeElement >= gameBoard.Width ||
                snake.snakeElements[0].xCordSnakeElement < 0 ||
                snake.snakeElements[0].yCordSnakeElement >= gameBoard.Height ||
                snake.snakeElements[0].yCordSnakeElement < 0)
            {
                gameOver();
            }


            for (int i = 1; i < snake.snakeLength; i++)
            {
                if (snake.snakeElements[0].xCordSnakeElement == snake.snakeElements[i].xCordSnakeElement &&
                    snake.snakeElements[0].yCordSnakeElement == snake.snakeElements[i].yCordSnakeElement)
                {
                    gameOver();
                }
            }

            if (snake.snakeElements[0].xCordSnakeElement == food.xFood &&
                snake.snakeElements[0].yCordSnakeElement == food.yFood)
            {
                SnakeElement newSnakeElement = snake.eat();
                score++;
                scoreLabel.Content = score;

                Canvas.SetLeft(newSnakeElement.element, newSnakeElement.xCordSnakeElement);
                Canvas.SetTop(newSnakeElement.element, newSnakeElement.yCordSnakeElement);
                gameBoard.Children.Add(newSnakeElement.element);

                gameBoard.Children.Remove(food.rectanglePoint);

                showFood();
                showImage();

                if (timeInterval > 50)
                {
                    timeInterval -= 15;
                }
                dispatcherTimer.Interval = TimeSpan.FromMilliseconds(timeInterval);
            }

            directionChanged = false;
        }
Пример #3
0
        public void move()
        {
            SnakeElement tempElement = new SnakeElement();

            for (int i = 0; i < snakeLength; i++)
            {
                if (i == 0)
                {
                    tempElement.xCordSnakeElement = snakeElements[i].xCordSnakeElement;
                    tempElement.yCordSnakeElement = snakeElements[i].yCordSnakeElement;

                    switch (direction)
                    {
                    case Direction.Up:
                        snakeElements[i].yCordSnakeElement -= 20;
                        break;

                    case Direction.Down:
                        snakeElements[i].yCordSnakeElement += 20;
                        break;

                    case Direction.Left:
                        snakeElements[i].xCordSnakeElement -= 20;
                        break;

                    case Direction.Right:
                        snakeElements[i].xCordSnakeElement += 20;
                        break;
                    }
                }
                else
                {
                    SnakeElement newSnakeElement = new SnakeElement();
                    newSnakeElement.xCordSnakeElement = snakeElements[i].xCordSnakeElement;
                    newSnakeElement.yCordSnakeElement = snakeElements[i].yCordSnakeElement;

                    snakeElements[i].xCordSnakeElement = tempElement.xCordSnakeElement;
                    snakeElements[i].yCordSnakeElement = tempElement.yCordSnakeElement;

                    tempElement.xCordSnakeElement = newSnakeElement.xCordSnakeElement;
                    tempElement.yCordSnakeElement = newSnakeElement.yCordSnakeElement;
                }
            }
        }
Пример #4
0
        public void drawSnake()
        {
            for (int i = 0; i < snakeLength; i++)
            {
                Rectangle element = new Rectangle();
                element.Width  = headSize;
                element.Height = headSize;
                if (i == 0)
                {
                    element.Fill = new SolidColorBrush(Color.FromRgb(240, 25, 25));
                }
                else
                {
                    element.Fill = new SolidColorBrush(Color.FromRgb(58, 228, 81));
                }

                SnakeElement snakeElement = new SnakeElement(xCordSnakeHead - (i * headSize), yCordSnakeHead, element);
                snakeElements.Add(snakeElement);
            }
        }