Search code examples
c#foreachdrawstring

Superimposed chars in drawstring in C#


I'm working on a game, it's like a snake, but you type something when the game starts and the word that you type becomes the body of the snake.

So... I have almost everything figured out, but when drawing the snake, the letters overlap, and I want them to be separated.

Here's my code:

private void pbArea_Paint(object sender, PaintEventArgs e)
    {
        Graphics area = e.Graphics;

        if(!Config.FinJuego)
        {
            //Set snake color
            Brush colorVib;

            //Snake body colors
            for(int i=0; i< Viborita.Count; i++)
            {
                if (i == 0)
                    colorVib = Brushes.Black; //Draws the head
                else
                    colorVib = Brushes.OrangeRed; //Draws the rest of the body

                //Draws full snake
                foreach (char c in nombre)
                {
                    area.DrawString(c.ToString(), new Font(FontFamily.GenericSansSerif, 15, FontStyle.Regular),
            new SolidBrush(Color.Black), Viborita[i].X * Config.Ancho, Viborita[i].Y * Config.Alto);
                }

                //Draw food
                area.FillEllipse(Brushes.Green,
                    new Rectangle(comida.X * Config.Ancho,
                    comida.Y * Config.Alto, Config.Ancho, Config.Alto));

            }
        }
        else
        {
            string finjuego = "Fin del juego.\nTu puntuación final fue: " + Config.Puntuacion +
                "\n Presiona la tecla Enter para reintentar.";
            if (RecordFinal==0)
            {
                Record = Config.Puntuacion;
                RecordFinal = Record;
            }
            if(Config.Puntuacion>RecordFinal)
            {
                RecordFinal = Config.Puntuacion;
            }
            lbFinJuego.Text = finjuego;
            lbFinJuego.Visible = true;
        }
    }

So, as you can see, I'm using DrawString, "area" its a PictureBox, "Viborita" it's the name of the project, so... Any ideas?

I'm sorry if I'm not being clear, it's because I'm not speaking my mother tongue, if you have any doubts about something in the code let me know, thanks by the way.


Solution

  • So... The problem is resolved. The problem was this:

    area.DrawString(c.ToString(), new Font(FontFamily.GenericSansSerif, 15, FontStyle.Regular),
            new SolidBrush(Color.Black), Viborita[i].X * Config.Ancho, Viborita[i].Y * Config.Alto);
    

    Instead of using "Viborita[i].X * Config.Ancho" and "Viborita[i].Y * Config.Alto" there, I used this:

    Ancho = Viborita[i].X * Config.Ancho;
                    Alto = Viborita[i].Y * Config.Alto;
     foreach (char c in nombre)
                    {
                            area.DrawString(c.ToString(), new Font(FontFamily.GenericSansSerif, 15, FontStyle.Regular),
                new SolidBrush(Color.Black), Ancho, Alto);
                            Ancho += 14;
                    }
    

    And that resolves my problem.