开发者

Random movement of star on the screen

i want to write a ' * ' to center of the screen and than it can move random to rigth,left,up or down every 100 milisecond.if ' * ' is on the end of row it must be at start of that row and if it is on the end of column it must be at start of that column(and also their reverse scenerio is valid).I wrote thats code but the problem is that i can't delete the old movement(so there are ' * ' for every movement)i solved it to put ' _ ' to its old movement.is there any other solution to make just one ' * ' on screen ? And also i can't delete the old movement if ' * ' is at the end of the row.Here is the source code :

private static void MovementOfStar()
    {
        int row = 40, col = 12;
        Console.CursorVisible = false;
    开发者_JAVA技巧    Console.SetCursorPosition(row, col);

        int direction = 0;
        Random r = new Random();

        for (int i = 0; i < 1000; i++)   // count of movement
        {
            Console.Write("*");
            System.Threading.Thread.Sleep(100);
            Console.Write('\b');
            Console.Write("_");



            direction = r.Next(5);
            while (direction== 0)
                direction = r.Next(5);

            switch (direction)
            {
                case 1:
                     if (row + 1 >= 80)
                         row = 0;
                    Console.SetCursorPosition(row++, col);
                    break;
                case 2:
                    if (row - 1 <= 0)
                        row=79;
                    Console.SetCursorPosition(row--, col);
                    break;
                case 3:
                    if (col + 1 >= 25)
                        col = 0;
                    Console.SetCursorPosition(row, col++);
                    break;
                case 4:
                    if (col - 1 <= 0)
                        col = 24;
                    Console.SetCursorPosition(row, col--);
                    break;
            }


        }


Rather than using backspace and overwriting with a _, why not just reposition the cursor:

Console.SetCursorPosition(col, row);
Console.Write("*");

Thread.Sleep(100);

// Wipe out the previous star
Console.SetCursorPosition(col, row);
Console.Write(" ");

(Note that I've transposed col and row here - you appear to have columns and rows the wrong way round.)

I would also suggest that you don't use the compound assignment operators (row++ etc) in your method calls - it makes it harder to work out what's going on. I would change your code to use things like:

row--;
if (row < 0)
{
   row = 24;
}

in your case statement - and then the single call to Console.SetCursorPosition just before you print the *.


Before updating row and col, set the cursor position to the old row and col and write a space.

By the way, rather than generating a random number from 0 to 4 and then retrying on 0, why not generate a random number from 0 to 3 and make those the four values you check for? You can say "case 0" -- zero is as good a number as 1. Or if you're really addicted to 1 to 4, change the random number generation to "next(4)+1".


You can call Console.Clear() to clear the (whole) console screen.


Instead of:

Console.Write('\b');
Console.Write("_");

Why not have:

Console.SetCursorPosition(row, col);
Console.Write("_");

I.e. you already knew how to position the cursor directly for where you wanted to write a character.


Write a space - " " instead of a "_"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜