コード例 #1
0
        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            SnakeDirecion originalSnakeDirection = snakeDirection;

            switch (e.Key)
            {
            case Key.Up:
                if (snakeDirection != SnakeDirecion.Down)
                {
                    snakeDirection = SnakeDirecion.Up;
                }
                break;

            case Key.Down:
                if (snakeDirection != SnakeDirecion.Up)
                {
                    snakeDirection = SnakeDirecion.Down;
                }
                break;

            case Key.Left:
                if (snakeDirection != SnakeDirecion.Right)
                {
                    snakeDirection = SnakeDirecion.Left;
                }
                break;

            case Key.Right:
                if (snakeDirection != SnakeDirecion.Left)
                {
                    snakeDirection = SnakeDirecion.Right;
                }
                break;

            case Key.Space:
                StartNewGame();
                break;
            }
            if (snakeDirection != originalSnakeDirection)
            {
                MoveSnake();
            }
        }
コード例 #2
0
        private void StartNewGame()
        {
            bdrWelcomeMessage.Visibility = Visibility.Collapsed;
            bdrHighscoreList.Visibility  = Visibility.Collapsed;
            bdrEndOfGame.Visibility      = Visibility.Collapsed;
            //stergem posibilele ramasite ale sarpelui si soriceilor
            foreach (SnakePart snakeBodyPart in snakeParts)
            {
                if (snakeBodyPart.UiElement != null)
                {
                    Map.Children.Remove(snakeBodyPart.UiElement);
                }
            }
            snakeParts.Clear();
            if (snakeFood != null)
            {
                Map.Children.Remove(snakeFood);
            }

            //resetam variabilele
            currentScore   = 0;
            snakeLength    = SnakeStartLength;
            snakeDirection = SnakeDirecion.Right;
            snakeParts.Add(new SnakePart()
            {
                Position = new Point(SnakeSquareSize * 5, SnakeSquareSize * 5)
            });
            gameTickTimer.Interval = TimeSpan.FromMilliseconds(SnakeStartSpeed);

            // desenez sarpele
            DrawSnake();
            DrawSnakeFood();

            //updatez statutul jocului
            UpdateGameStatus();
            // start
            gameTickTimer.IsEnabled = true;
        }