-1

I'm currently learning programming in C# and i don't know a lot. I have just the basic knowledge about this program, so when you're answering i would be very grateful if you explain to me the code in details.

Let's take the letter 'a'. I want to make the letter move to the right 1 time every 2 seconds using the For loop. To make it work 2 seconds i use the code: System.Threading.Thread.Sleep(TimeSpan.FromSeconds(2)); This code will be included inside the loop. My problem is, i don't know to replace the 'a' with a 'space' and that after the space, the letter 'a' will show up. Something like this:

a

After 2 seconds, i want the 'a' to move to the right like this:

 a

I'm searching for a code that could replace the letter with a 'space' and add the letter 'a' after the space. The code should run 10 times in the For loop, making the letter move to the right 10 times. Please notice, i don't want to erase the whole output, only the letter 'a'.

I really don't know how to explain my question better then this. Thanks in advance.

edit : I am using Microsoft Visual C# 2010 Express. Console Application. static void Main(string[] args) (might help)

Denisk1ng
  • 33
  • 1
  • 6
  • 3
    Is this console, winform, wpf? Could you provide more code? – Thomas Ayoub Mar 11 '16 at 12:44
  • 1
    sounds like console and sounds like home work. String.padding i think is what your looking for, – Seabizkit Mar 11 '16 at 12:45
  • I strongly recommend looking out for some simple tutorials to get used to the basics before asking specific questions. – Rob Mar 11 '16 at 12:46
  • 1
    Did you search? I suggest the answer here: http://stackoverflow.com/questions/888533/how-can-i-update-the-current-line-in-a-c-sharp-windows-console-app – PMBjornerud Mar 11 '16 at 12:47

4 Answers4

1

Try this (you can call this method with a given offset and position). You can will be resposible of keeping track of the current non-empty position:

        public void Move(char[] input, int position, int offset, int direction) {
        int newPosition;

        if (direction == 0) {
            //Move Left
            newPosition = position - offset;
        }
        else {
            //Move Right
            newPosition = position + offset;
        }

        if (newPosition < 0 || newPosition > input.Length-1) {
            throw new ArgumentException("Offset is invalid", "offset");
        }
        input[newPosition] = input[position];
        input[position] = ' ';
    }
Tamas Ionut
  • 4,240
  • 5
  • 36
  • 59
0

I don't know why you need this, but here is code:

static void Main(string[] args)
    {
        string a = "a";
        for (int i = 0; i < 100; i++)
        {
            Console.Clear();
            Console.Write(a);
            a = " " + a;
            System.Threading.Thread.Sleep(100);
        }
    }

Hope it helps.

SᴇM
  • 7,024
  • 3
  • 24
  • 41
  • Our teacher asked us to make a game. Snake is one of the options. I need to make the snake look like it is moving. – Denisk1ng Mar 11 '16 at 13:07
  • I have tried and the code does work. About the code `Console.Clear();` , is it clearing the whole output? – Denisk1ng Mar 11 '16 at 13:15
  • I've tried too, and it works perfectly, `Console.Clear();` clearing output window, but before that we adding a space at begging of 'a', then we printing the result to output and we got illusion that the 'a' is moving. – SᴇM Mar 11 '16 at 13:19
  • The problem is, i don't need to clear the whole output windows.. in a game like snake i will need the 'apple' to stay in one place all the time. – Denisk1ng Mar 11 '16 at 13:23
0

You could use a timer like so:

Console.Write("a");
Timer timer = new Timer(200);
timer.Elapsed += (object sender, ElapsedEventArgs e) =>
    {
        Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
        Console.Write(" a");
    };
timer.Start();
TVOHM
  • 2,740
  • 1
  • 19
  • 29
0

Here try this

    string sampleString="a";

    Console.WriteLine(sampleString);


    for(int i=0;i<10;i++)
    {
      Thread.Sleep(2000)// 2 seconds sleep

      Console.SetCursorPosition(0, Console.CursorTop -1);
      sampleString=" "+sampleString;
      Console.WriteLine(sampleString);

   }
Rohit
  • 10,056
  • 7
  • 50
  • 82
  • This one seemed to work the best. The `Console.SetCursorPosition` is the key to my problem. Thanks a lot! `Thread.Sleep` didn't work for me, so i replaced it with `System.Threading.Thread.Sleep(TimeSpan.FromSeconds(2));` – Denisk1ng Mar 11 '16 at 13:28