Home > Software design >  C# Drawling lines in Forms App go of the screen
C# Drawling lines in Forms App go of the screen

Time:11-07

I'm learning how to draw things on screen using Forms Application. I want to make a Snake Game using grid.

I can currently draw a grid using this code. Problem is that my squares are drawn a bit of the screen. Once on the right of the screen are not a full box. I would also like so the lines of the edge are visible to the eye and not hidden like the bottom on in the picture.

Thanks a lot in advance.

private void Form1_Paint(object sender, PaintEventArgs pe)
{
    var numCells = (float)this.Height;
    var cellSize = 20.0F;

    Graphics g = pe.Graphics;
    Pen blackPen = new Pen(Color.Black, 1);

    for (int i = 0; i < numCells; i  )
    {
        //Vertical Lines
        pe.Graphics.DrawLine(blackPen, i * cellSize, 0, i * cellSize, numCells * cellSize);

        //Horizontal Lines
        pe.Graphics.DrawLine(blackPen, 0, i * cellSize, numCells * cellSize, i * cellSize);
    }
}

enter image description here

CodePudding user response:

I think you want

var cellSize = 20.0F;
var numCells = Math.Floor((float)this.Height / cellSize);

since right now you're drawing lines waaaay off the screen too.

  • Related