0

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.

OPMUANRK
  • 13
  • 5

2 Answers2

1

Okay, it seems like Viborita[i] is the position of the ith snake. The problem is you don't keep track of how the snake is curled, or what shape it is in. I recommend that you create a Snake class that keeps track of the position of the snake's head and the positions of it's other characters, as well as any other relevant data.


Also, you are creating a new font and a new brush every time you draw a character. It would be better to put your brush and font in a field of the class this is in.

Paramecium13
  • 345
  • 1
  • 7
  • I didn't know that, I'm new with C#, sorry And if I add that "i+=1" it doesn't work, I think it has to do with X and Y, which are position variables... But I don't know how to make it work... – OPMUANRK Jan 30 '16 at 04:40
0

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.

OPMUANRK
  • 13
  • 5