Skip to main content
deleted 72 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Xaml Wpf snake GameSnake game

I'm doing a Gamegame in Xaml WPF, and I didn't really choose the tools and languages. It's to be hidden in a really big app with a lot of codescode.

The whole snake taketakes place in a simple xaml canvas and xaml.cs file here

https://github.com/Jebik/XAMLSnake/blob/master/XAMLSnake/XAMLSnake/XAMLSnakeWindow.xaml.cshere.

But.. Actually, the snake became really laggy after being taller than 20 part. And, and even before the speed is absolutely not constant.

I don't know a lot about game programming but i'mI'm doing software for a while now.

  How can iI improve this game? Make, make speed less dependent on snake size? Make, and make it lag way less with the same computer perf?

First post on code Review feel free to comment if I must change the topic message ..

I think the more important is the paintSnake Methodmethod called every timer Tick.

Xaml Wpf snake Game

I'm doing a Game in Xaml WPF, I didn't really choose the tools and languages. It's to be hidden in a really big app with a lot of codes.

The whole snake take place in a simple xaml canvas and xaml.cs file here

https://github.com/Jebik/XAMLSnake/blob/master/XAMLSnake/XAMLSnake/XAMLSnakeWindow.xaml.cs

But.. Actually the snake became really laggy after being taller than 20 part. And even before the speed is absolutely not constant.

I don't know a lot about game programming but i'm doing software for a while now.

  How can i improve this game? Make speed less dependent on snake size? Make it lag way less with same computer perf?

First post on code Review feel free to comment if I must change the topic message ..

I think the more important is paintSnake Method called every timer Tick

Xaml Wpf Snake game

I'm doing a game in Xaml WPF and I didn't really choose the tools and languages. It's to be hidden in a really big app with a lot of code.

The whole snake takes place in a simple xaml canvas and xaml.cs file here.

Actually, the snake became really laggy after being taller than 20 part, and even before the speed is absolutely not constant.

I don't know a lot about game programming but I'm doing software for a while now. How can I improve this game, make speed less dependent on snake size, and make it lag way less with the same computer perf?

I think the more important is the paintSnake method called every timer Tick.

ImI'm doing a Game in Xaml WPF, I didn't really choose the tools and languages. It's to be hidden in a really big app with a lot of codes.

But.. Actually the snake became really laggy after being taller than 20 part. And even before the speed is absolutlyabsolutely not constant.

I don't know a lot about game programationprogramming but i'm doing software for a while now.

How can i improve this game? Make speed less dependend todependent on snake size? Make it lagginglag way less with same computer perf?

First post on code Review feel free to comment if iI must change the topic message ..

Im doing a Game in Xaml WPF, I didn't really choose the tools and languages. It's to be hidden in a really big app with a lot of codes.

But.. Actually the snake became really laggy after being taller than 20 part. And even before the speed is absolutly not constant.

I don't know a lot about game programation i'm doing software for a while now.

How can i improve this game? Make speed less dependend to snake size? Make it lagging way less with same computer perf?

First post on code Review feel free to comment if i must change the topic message ..

I'm doing a Game in Xaml WPF, I didn't really choose the tools and languages. It's to be hidden in a really big app with a lot of codes.

But.. Actually the snake became really laggy after being taller than 20 part. And even before the speed is absolutely not constant.

I don't know a lot about game programming but i'm doing software for a while now.

How can i improve this game? Make speed less dependent on snake size? Make it lag way less with same computer perf?

First post on code Review feel free to comment if I must change the topic message ..

fixed copy/paste indentation
Source Link
t3chb0t
  • 44.3k
  • 9
  • 84
  • 190
 private void paintSnake(Point currentposition)
        {
            cleanSnake();
            paintSnakeHead(currentposition);
            paintSnakeBody(currentposition);
        }
        private void cleanSnake()
        {
            var count = paintCanvas.Children.Count;
            var toDel = count - (NbBonus + 1);
            paintCanvas.Children.RemoveRange(NbBonus + 1, toDel);
        }
        private void paintSnakeHead(Point currentposition)
        {
            var rot = GetRotation();
            RotateTransform aRotateTransform = new RotateTransform();
            aRotateTransform.CenterX = 0.5;
            aRotateTransform.CenterY = 0.5;
            aRotateTransform.Angle = rot;
            _headBrush.RelativeTransform = aRotateTransform;
            Rectangle headRect = new Rectangle();
            headRect.Fill = _headBrush;
            headRect.Height = headSize;
            headRect.Width = headSize;
            Canvas.SetTop(headRect, currentposition.Y);
            Canvas.SetLeft(headRect, currentposition.X);
            //On decale tout les point du snake
            snakePoints.Insert(0, currentposition);
            paintCanvas.Children.Add(headRect);
            int count = snakePoints.Count;
            // Restrict the tail of the snake
            if (count > length)
            {
                snakePoints.RemoveAt(length);
            }
        }
        private int GetRotation()
        {
            switch (direction)
            {
                case (int)MOVINGDIRECTION.DOWNWARDS:
                    return 180;
                case (int)MOVINGDIRECTION.UPWARDS:
                    return 0;
                case (int)MOVINGDIRECTION.TOLEFT:
                    return 270;
                case (int)MOVINGDIRECTION.TORIGHT:
                    return 90;
                default:
                    return 0;
            }
        }
        private void paintSnakeBody(Point currentposition)
        {
            for (int i = 1; i < snakePoints.Count; i++)
            {
                //On ne dessinne un cercle que tout les headsize position
                if (i % headSize == 0)
                {
                    Rectangle bodyRect = new Rectangle();
                    bodyRect.Fill = _bodyBrush;
                    bodyRect.Height = headSize;
                    bodyRect.Width = headSize;
                    Canvas.SetTop(bodyRect, snakePoints[i].Y);
                    Canvas.SetLeft(bodyRect, snakePoints[i].X);
                    paintCanvas.Children.Add(bodyRect);
                }
            }
        }
 private void paintSnake(Point currentposition)
        {
            cleanSnake();
            paintSnakeHead(currentposition);
            paintSnakeBody(currentposition);
        }
        private void cleanSnake()
        {
            var count = paintCanvas.Children.Count;
            var toDel = count - (NbBonus + 1);
            paintCanvas.Children.RemoveRange(NbBonus + 1, toDel);
        }
        private void paintSnakeHead(Point currentposition)
        {
            var rot = GetRotation();
            RotateTransform aRotateTransform = new RotateTransform();
            aRotateTransform.CenterX = 0.5;
            aRotateTransform.CenterY = 0.5;
            aRotateTransform.Angle = rot;
            _headBrush.RelativeTransform = aRotateTransform;
            Rectangle headRect = new Rectangle();
            headRect.Fill = _headBrush;
            headRect.Height = headSize;
            headRect.Width = headSize;
            Canvas.SetTop(headRect, currentposition.Y);
            Canvas.SetLeft(headRect, currentposition.X);
            //On decale tout les point du snake
            snakePoints.Insert(0, currentposition);
            paintCanvas.Children.Add(headRect);
            int count = snakePoints.Count;
            // Restrict the tail of the snake
            if (count > length)
            {
                snakePoints.RemoveAt(length);
            }
        }
        private int GetRotation()
        {
            switch (direction)
            {
                case (int)MOVINGDIRECTION.DOWNWARDS:
                    return 180;
                case (int)MOVINGDIRECTION.UPWARDS:
                    return 0;
                case (int)MOVINGDIRECTION.TOLEFT:
                    return 270;
                case (int)MOVINGDIRECTION.TORIGHT:
                    return 90;
                default:
                    return 0;
            }
        }
        private void paintSnakeBody(Point currentposition)
        {
            for (int i = 1; i < snakePoints.Count; i++)
            {
                //On ne dessinne un cercle que tout les headsize position
                if (i % headSize == 0)
                {
                    Rectangle bodyRect = new Rectangle();
                    bodyRect.Fill = _bodyBrush;
                    bodyRect.Height = headSize;
                    bodyRect.Width = headSize;
                    Canvas.SetTop(bodyRect, snakePoints[i].Y);
                    Canvas.SetLeft(bodyRect, snakePoints[i].X);
                    paintCanvas.Children.Add(bodyRect);
                }
            }
        }
private void paintSnake(Point currentposition)
{
    cleanSnake();
    paintSnakeHead(currentposition);
    paintSnakeBody(currentposition);
}
private void cleanSnake()
{
    var count = paintCanvas.Children.Count;
    var toDel = count - (NbBonus + 1);
    paintCanvas.Children.RemoveRange(NbBonus + 1, toDel);
}
private void paintSnakeHead(Point currentposition)
{
    var rot = GetRotation();
    RotateTransform aRotateTransform = new RotateTransform();
    aRotateTransform.CenterX = 0.5;
    aRotateTransform.CenterY = 0.5;
    aRotateTransform.Angle = rot;
    _headBrush.RelativeTransform = aRotateTransform;
    Rectangle headRect = new Rectangle();
    headRect.Fill = _headBrush;
    headRect.Height = headSize;
    headRect.Width = headSize;
    Canvas.SetTop(headRect, currentposition.Y);
    Canvas.SetLeft(headRect, currentposition.X);
    //On decale tout les point du snake
    snakePoints.Insert(0, currentposition);
    paintCanvas.Children.Add(headRect);
    int count = snakePoints.Count;
    // Restrict the tail of the snake
    if (count > length)
    {
        snakePoints.RemoveAt(length);
    }
}
private int GetRotation()
{
    switch (direction)
    {
        case (int)MOVINGDIRECTION.DOWNWARDS:
            return 180;
        case (int)MOVINGDIRECTION.UPWARDS:
            return 0;
        case (int)MOVINGDIRECTION.TOLEFT:
            return 270;
        case (int)MOVINGDIRECTION.TORIGHT:
            return 90;
        default:
            return 0;
    }
}
private void paintSnakeBody(Point currentposition)
{
    for (int i = 1; i < snakePoints.Count; i++)
    {
        //On ne dessinne un cercle que tout les headsize position
        if (i % headSize == 0)
        {
            Rectangle bodyRect = new Rectangle();
            bodyRect.Fill = _bodyBrush;
            bodyRect.Height = headSize;
            bodyRect.Width = headSize;
            Canvas.SetTop(bodyRect, snakePoints[i].Y);
            Canvas.SetLeft(bodyRect, snakePoints[i].X);
            paintCanvas.Children.Add(bodyRect);
        }
    }
}
added 3025 characters in body
Source Link
Jebik
  • 113
  • 7
Loading
Source Link
Jebik
  • 113
  • 7
Loading