I am very new to programming and im trying to create a simple stick figure that walks across the screen. I cant seem to figure out how to make him walk across. Any help would be great. THanks guys

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;


namespace exercise
{
    class Program
    {
        static void Main(string[] args)
        {
            string head1 = " 0 ";
            string body1 = "!T!";
            string legs1 = "/  \\";
            string legs2 = " | ";
            string numberOfSpaces = "";
            bool legsShouldPrint = true;

            Console.WriteLine(head1);
            Console.WriteLine(body1);

            for (int i = 0; i < 72; i++)
            {
                Console.Clear();


                Console.WriteLine(" " + head1);
                Console.WriteLine(" " + body1);
                if (legsShouldPrint)
                {
                    Console.WriteLine(legs1);
                }
                else
                {
                    Console.WriteLine(legs2);
                }

                legsShouldPrint = !legsShouldPrint;
                Thread.Sleep(80);

            }
            Console.ReadLine();
        }

Use PadLeft(int, char) to move the character further from the left side on each iteration. E.g.

string pad = head1.padLeft(i, " ");
// repeat for all body parts
console.write(pad);

Thats should increase the padding on each loop so each iteration has your figure 1 space further to the right.

Actually PadLeft only accepts char not string. Also you need to offset i by 3 otherwise the first couple of steps the figure walks in one spot:

string head1 = " 0 ";
string body1 = "!T!";
string legs1 = "/ \\";
string legs2 = " | ";
bool legsShouldPrint = true;
Console.WriteLine(head1);
Console.WriteLine(body1);
for (int i = 0; i < 72; i++)
{
    Console.Clear();
    int offset = i + 3;
    Console.WriteLine(head1.PadLeft(offset,' '));
    Console.WriteLine(body1.PadLeft(offset,' '));
    if (legsShouldPrint)
    {
        Console.WriteLine(legs1.PadLeft(offset, ' '));
    }
    else
    {
        Console.WriteLine(legs2.PadLeft(offset, ' '));
    }
    legsShouldPrint = !legsShouldPrint;
    Thread.Sleep(150);
}
Console.ReadLine();

The Padleft solution is another alternative, I used the string you supplied but didn't use. I suspect it is more efficient using Pad. Your delay seemed really short and that does produce a running man effect. When I added 100 to your delay, it was better but still had a hitch to his walk. I also added aesthtic effects to make the man more reasonably proportioned.

Here is code:

` static void stick()
{
string head1 = " 0 ";
string body1 = " !T!";
string legs0 = " / \";
string legs1 = "/ \";
string legs2 = " | ";
string numberOfSpaces = "";
bool legsShouldPrint = true;

        Console.WriteLine(head1);
        Console.WriteLine(body1);

        for (int i = 0; i < 72; i++)
        {
            Console.Clear();


            Console.WriteLine(numberOfSpaces + head1);
            Console.WriteLine(numberOfSpaces + body1);
            if (legsShouldPrint)
            {
                Console.WriteLine(numberOfSpaces + legs0);
                Console.WriteLine(numberOfSpaces + legs1);
            }
            else
            {
                Console.WriteLine(numberOfSpaces + legs2);
                Console.WriteLine(numberOfSpaces + legs2);
                Thread.Sleep(180);
            }
            numberOfSpaces += " ";
            legsShouldPrint = !legsShouldPrint;
            Thread.Sleep(180);

        }
        Console.Read();
    }

`

Well, I obviously am too inexperienced with Daniweb controls. I tried the Code routine and that "Here is code" was the result. Then I used Inline Code and have had problems with that in the past. Indentation problems there. I use functions in a test Console instead of the main routine so I can keep logic and just execute it when I want that section executed.This time it screwed over the spaces put in the string as well putting code outside the area I had marked as code.

One quick way to format the code, is to copy the code from the IDE. Paste it into the message area, making sure there is an empty line between the first line of code and any text above it. Select all the code and hit tab, this will render it as code instead of just text. You can tell that it works before you hit submit because the recognized code turns green.

Thanks Tinstaffl,
For some reason I wanted to play around with the stick figure more. Do note in the IDE you can change the properties of the Console screen the debugger uses and the debugger keeps those changes for that application. Here is my playcode, it goes down as well as across:

            /// <summary>
            ///  Console walking stick figure. (Sideways and down)
            /// </summary>
            /// <param name="cols">Supply the number of columns the figure should walk</param>
            static void stick(int cols)
            {
                if (cols < 1)
                {
                    Console.WriteLine("You entered a column number ({0} less than one. Exiting the stick figure program", cols);
                    return;
                }
                string head1 = "  0 ";
                string body1 = " !T!";
                string body2 = "  X";
                string legs0 = " / \\";
                string legs1 = "/   \\";
                string legs2 = "  | ";
                string numberOfSpaces = "";
                int startLine = 0;
                bool legsShouldPrint = true;

                Console.WriteLine(head1);
                Console.WriteLine(body1);

                for (int i = 0; i < cols; i++)
                {
                    Console.Clear();
                    for (int j = 0; j < startLine; j++) Console.WriteLine();
                    if (i % 4 == 3) startLine++;

                    Console.WriteLine(numberOfSpaces + head1);
                    Console.WriteLine(numberOfSpaces + body1);
                    Console.WriteLine(numberOfSpaces + body2);
                    if (legsShouldPrint)
                    {
                        Console.WriteLine(numberOfSpaces + legs0);
                        Console.WriteLine(numberOfSpaces + legs1);
                    }
                    else
                    {
                        Console.WriteLine(numberOfSpaces + legs2);
                        Console.WriteLine(numberOfSpaces + legs2);
                        Thread.Sleep(180);
                    }
                    numberOfSpaces += " ";
                    legsShouldPrint = !legsShouldPrint;
                    Thread.Sleep(180);
                }
                // Use a recursive call to run it again. Note that this creates a copy of the routine with each recursion.
                // Unlikely but possible to run out of storage.
                Console.Write("Do it again? Current width:{0} Enter new width to continue, non nummeric to stop", cols);
                string _continue = Console.ReadLine();
                if (int.TryParse(_continue, out cols))
                    stick(cols);
            }

Instead of making it recursive, a simple while loop and setting the value of cols each time will need only one function running. Something like this:

 /// <summary>
/// Console walking stick figure. (Sideways and down)
/// </summary>
/// <param name="cols">Supply the number of columns the figure should walk</param>
static void stick(int cols)
{ 
    string head1 = " 0 ";
    string body1 = " !T!";
    string body2 = " X";
    string legs0 = " / \\";
    string legs1 = "/ \\";
    string legs2 = " | ";
    While (cols > 0)
    {               
        string numberOfSpaces = "";
        int startLine = 0;
        bool legsShouldPrint = true;
        Console.WriteLine(head1);
        Console.WriteLine(body1);
        for (int i = 0; i < cols; i++)
        {
            Console.Clear();
            for (int j = 0; j < startLine; j++) Console.WriteLine();
            if (i % 4 == 3) startLine++;
            Console.WriteLine(numberOfSpaces + head1);
            Console.WriteLine(numberOfSpaces + body1);
            Console.WriteLine(numberOfSpaces + body2);
            if (legsShouldPrint)
            {
                Console.WriteLine(numberOfSpaces + legs0);
                Console.WriteLine(numberOfSpaces + legs1);
            }
            else
            {
                Console.WriteLine(numberOfSpaces + legs2);
                Console.WriteLine(numberOfSpaces + legs2);
                Thread.Sleep(180);
            }
            numberOfSpaces += " ";
            legsShouldPrint = !legsShouldPrint;
            Thread.Sleep(180);
        }
        cols = 0;
        Console.Write("Do it again? Current width:{0} Enter new width to continue, non nummeric to stop", cols);
        string _continue = Console.ReadLine();
        int.TryParse(_continue, out cols);
    }
    Console.WriteLine("Exiting the stick figure program");   
}

Agreed, that a while loop in the main routine could be used and therefore have the routine called once multiple times, but this was posted by a begining programmer, so I thought I'd introduce the recursive call in an unusual manner. (Normally the state of the program logic is used to determine if the recursive call continues or ends.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.