コード例 #1
0
        private void DrawSnake()
        {
            snakeCanvas.Children.Clear();
            ISnake    snake = model.GetSnake();
            SnakePart prev = null, next = null;
            SnakePart sp    = snake.GetSnakeHead();
            Image     part1 = new Image();

            Canvas.SetLeft(part1, sp.PositionOnX * viewCellSize);
            Canvas.SetTop(part1, sp.PositionOnY * viewCellSize);
            part1.Width  = part1.Height = viewCellSize;
            part1.Source = new BitmapImage(new Uri("/SnakeGame;component/View/Resources/snake/cap2.png", UriKind.RelativeOrAbsolute));

            switch (snake.Direction)
            {
            case SnakeDirection.Up:
                part1.RenderTransform = new RotateTransform(180, viewCellSize / 2, viewCellSize / 2);
                break;

            case SnakeDirection.Left:
                part1.RenderTransform = new RotateTransform(90, viewCellSize / 2, viewCellSize / 2);
                break;

            case SnakeDirection.Right:
                var transformGroup = new TransformGroup();
                transformGroup.Children.Add(new RotateTransform(270, viewCellSize / 2, viewCellSize / 2));
                transformGroup.Children.Add(new ScaleTransform(1, -1, viewCellSize / 2, viewCellSize / 2));
                part1.RenderTransform = transformGroup;

                break;

            case SnakeDirection.Down:
                break;

            default:
                break;
            }


            snakeCanvas.Children.Add(part1);

            do
            {
                string type = "-";

                prev = sp;
                sp   = snake.GetNextPart(sp);
                next = snake.GetNextPart(sp);

                int partType = -1;

                Image part = new Image();
                if (sp != null)
                {
                    // e ultimul
                    if (next == null)
                    {
                        if (coada)
                        {
                            part.Source = new BitmapImage(new Uri("/SnakeGame;component/View/Resources/snake/coada1.png", UriKind.RelativeOrAbsolute));
                        }
                        else
                        {
                            part.Source = new BitmapImage(new Uri("/SnakeGame;component/View/Resources/snake/coada2.png", UriKind.RelativeOrAbsolute));
                        }
                        coada = !coada;

                        switch (GetCoadaDirection(prev, sp))
                        {
                        case SnakeDirection.Up:

                            break;

                        case SnakeDirection.Left:
                            var transformGroup = new TransformGroup();
                            transformGroup.Children.Add(new RotateTransform(90, viewCellSize / 2, viewCellSize / 2));
                            transformGroup.Children.Add(new ScaleTransform(-1, 1, viewCellSize / 2, viewCellSize / 2));
                            part.RenderTransform = transformGroup;
                            break;

                        case SnakeDirection.Right:
                            transformGroup = new TransformGroup();
                            transformGroup.Children.Add(new RotateTransform(270, viewCellSize / 2, viewCellSize / 2));
                            transformGroup.Children.Add(new ScaleTransform(-1, 1, viewCellSize / 2, viewCellSize / 2));
                            part.RenderTransform = transformGroup;
                            break;

                        case SnakeDirection.Down:
                            part.RenderTransform = new RotateTransform(180, viewCellSize / 2, viewCellSize / 2);
                            break;

                        default:
                            break;
                        }
                    }
                    else
                    {
                        partType = GetSnakePartType(prev, sp, next);
                    }

                    Canvas.SetLeft(part, sp.PositionOnX * viewCellSize);
                    Canvas.SetTop(part, sp.PositionOnY * viewCellSize);
                    part.Width = part.Height = viewCellSize;

                    switch (partType)
                    {
                    case 1:
                        part.Source = new BitmapImage(new Uri("/SnakeGame;component/View/Resources/snake/part1.png", UriKind.RelativeOrAbsolute));
                        break;

                    case 2:
                        part.Source = new BitmapImage(new Uri("/SnakeGame;component/View/Resources/snake/part2.png", UriKind.RelativeOrAbsolute));
                        break;

                    case 3:
                        part.Source = new BitmapImage(new Uri("/SnakeGame;component/View/Resources/snake/part3.png", UriKind.RelativeOrAbsolute));
                        break;

                    case 4:
                        part.Source = new BitmapImage(new Uri("/SnakeGame;component/View/Resources/snake/part4.png", UriKind.RelativeOrAbsolute));
                        break;

                    case 5:
                        part.Source = new BitmapImage(new Uri("/SnakeGame;component/View/Resources/snake/vertical2.png", UriKind.RelativeOrAbsolute));
                        break;

                    case 6:
                        part.Source = new BitmapImage(new Uri("/SnakeGame;component/View/Resources/snake/horizontal2.png", UriKind.RelativeOrAbsolute));
                        break;

                    default:
                        break;
                    }


                    snakeCanvas.Children.Add(part);
                }
            } while (sp != null);
        }