1

I'm coding a game and I am struggling in deleting my stickman before I draw another one.

I could just use Console.Clear() but it clears the whole console and I only need to delete the previous stickman.

I'm trying to use:

    private static string path =  @"c:..\..\characters\";
    private string file;
    private int xpast = 0;
    private int ypast = 0;
    private int LinhasSeparacao;

    public Graphics()
    {
    }

    public Graphics(string file)
    {
        this.file = file;
        StreamReader sr = new StreamReader(path + file);
        LinhasSeparacao = 0;
        do
        {
            LinhasSeparacao++;
        }
        while (sr.ReadLine() != "separar");
        sr.Close();            
    }

    public void Draw(int x, int y,bool forma = true)
    {
        string[] persona = File.ReadAllLines(path + file);
        LimparAnterior(forma,persona,x,y);
        //Console.Clear();
        if (forma)
        {
            for (int i = 0; i < LinhasSeparacao - 1; i++)
            {
                Console.SetCursorPosition(x, y + i);
                Console.Write(persona[i]);
            }
        }
        else
        {
            int j= 0;
            for (int i = LinhasSeparacao; i <persona.Length-1; i++)
            {
                Console.SetCursorPosition(x, y + j);
                Console.Write(persona[i]);
                j++;
            }
        }

        xpast = x;
        ypast = y;
    }

    private  void LimparAnterior(bool forma,string[] persona, int xlive, int ylive)
    {
        int i = 0;

        for (i = 0 ; i < LinhasSeparacao; i++)
        {
            Console.SetCursorPosition(xpast > 0? xpast -1:xpast,ypast  + i);
            Console.Write(" ",persona[i].Length);  
        }
    }

This is my class to draw the new character, it needs x and y coordinates. I use a file to the drawing and put all the line into a array called persona. I'm drawing after someone press the arrows to make the little guy move.

If you need more information, say something. Here is the link to github: https://github.com/digaso/Wizardoft

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
Diogo Alves
  • 23
  • 1
  • 5
  • Move to the position you want to clear and `Console.Write(" ");` (i.e., write a space), or, if you want to clear an entire line, move to the position you want and `Console.WriteLine();` Be aware that the latter call will position your cursor at the start of the next line. – Flydog57 Feb 21 '19 at 22:51
  • thanks for the answer, but i have multiple lines to delete but not the entire line, just the exact number of characters. Please take a look to the link to git hub and look to the file "hero.txt", thats what i want to delete. Feel free to edit – Diogo Alves Feb 21 '19 at 23:01
  • Those are really your choices (as far as I know). If you write a "0" at position (x, y) and a "|" at position (x, z), remember where you wrote them. Then, when you want to delete them, go back to where your wrote them and overwrite them with a space. If you want to delete 4 characters in a row (horizontally), you can write 4 spaces. Someone else might know of a better way. If so, I'd be interested in seeing the suggestion. – Flydog57 Feb 21 '19 at 23:08
  • But the thing is, I used that way. I have variables to save x and y position. After that a set them, i write " " to clear, but doesn't clear all the characters. Please if you wanna help, download the project see for yourself after compiling. – Diogo Alves Feb 21 '19 at 23:14
  • 1
    If you're writing a lot of console games you should check this out: [Advanced console I/O](https://stackoverflow.com/questions/199321/advanced-console-io-in-net) – John Wu Feb 21 '19 at 23:18
  • Oh, and you probably want to care about "insert mode" and "overwrite mode". If you write a space in "insert mode" (i.e., normal mode), everything to the right of it will move one character to the right. You might also be able to write a backspace followed by a space. But, your best bet might be to follow @JohnWu's suggestion. I want to help, but not that much. – Flydog57 Feb 21 '19 at 23:31

1 Answers1

0

The code isn't necessarily your problem, if you look at the hero.txt file you'll notice that there is no space to the right of the hero's sword...

_A_
 0 
/|\/
/ \
separar
 _A_
  0 
\/|\X 
 / \
separar

See where I put the 'X' character? When you hero is facing right there is a sword there. But when you move him left there is no space to overwrite the cell where the sword was.

You either need to alter your hero.txt file so that it has spaces in strategic locations or update your drawing code so that it erases the area where your stickman was, then update his position and draw in the new position.

Since this seems to be a casual learning experience this is fine but if you do decide to get more adventurous you'll want to use a library specifically designed for this kind of thing or redesign your code so that it "composes" the scene by drawing each character to a buffer and then taking the finished buffer to the physical screen. Have fun!

dazedandconfused
  • 3,131
  • 1
  • 18
  • 29
  • Hi, thank you very much, it helped me to figured out other things. But in my other class , “Spawner.cs” that spawns a boss, it doesn’t clears the boss I wrote before. If tou press enter you will see the spawning happening . – Diogo Alves Feb 22 '19 at 17:51
  • From your original question I thought you wanted the characters to be added, not replace the previous character. You will either need to clear the entire background or add a Clear method to your Graphics class. Before spawning a new character, call clear on the current one. The Clear method would go through each line, writing "n" spaces instead of the actual character. Then spawn and redraw. – dazedandconfused Feb 23 '19 at 17:37