繁体   English   中英

C#无法在ASCII控制台游戏中移动字符

[英]C# unable to move character in ASCII Console game

我有一个学校的C#项目。 我无法在此ASCII控制台游戏中移动角色。 它必须是OO。 运行代码后,我得到以下信息:

在按左或右之前的结果。

我按向左或向右后

这个。

有人可以帮我吗? 任何帮助表示赞赏。

program.cs:

class Program
{

    static void Main(string[] args)
    {
        SuperConsole.Init(50, 44);

        // create the game
        Game game = new Game(30, 30);

        //create objects
        Entity spaceship = new Entity(1, 15, '@', SuperConsoleColor.Cyan); 



        // start the game loop
        RunGameLoop(game);
    }

    protected static void RunGameLoop(Game game)
    {
        SuperConsole.BackgroundColor = SuperConsoleColor.DarkBlue;
        SuperConsole.ForegroundColor = SuperConsoleColor.Gray;

        int refreshRate = 20;

        SuperConsole.CursorVisible = false;

        SuperConsole.BackgroundColor = SuperConsoleColor.DarkBlue;
        SuperConsole.ForegroundColor = SuperConsoleColor.Gray;
        SuperConsole.Clear();

        Reset(game);

        game.Draw(0.0f);

        SuperConsole.Flush();

        /*
        SuperConsole.SetCursorPosition(0, 0);*/

        while (true)
        {
            while (SuperConsole.KeyAvailable)
            {
                ConsoleKeyInfo key = SuperConsole.ReadKey(true);
                game.OnInput(key.Key);
            }

            System.Threading.Thread.Sleep(1000 / refreshRate);

            Reset(game);
            game.Draw(1.0f / (float)refreshRate);
            SuperConsole.Flush();


        }
    }

    protected static void Reset(Game game)
    {
        SuperConsole.BackgroundColor = SuperConsoleColor.Black;
        SuperConsole.ForegroundColor = SuperConsoleColor.Black;
        SuperConsole.SetCursorPosition(0, 0);
        for (int y = 0; y < game.GetHeight(); ++y)
        {
            for (int x = 0; x < game.GetWidth(); ++x)
            {
                SuperConsole.Write(" ");
            }
            SuperConsole.WriteLine();
        }
        SuperConsole.SetCursorPosition(0, 0);
    }
}

}

game.cs:

class Game : Entity
{
    protected int width, height;

    Entity spaceship = new Entity(1, 15, '@', SuperConsoleColor.Cyan);

    public Game(int newWidth, int newHeight)
    {

        // set the size
        width = newWidth;
        height = newHeight;

        // set the window
        Console.WindowWidth = width+1;
        Console.WindowHeight = height+1;
    }

    public int GetWidth()
    {
        return width;
    }

    public int GetHeight()
    {
        return height;
    }

    public void Draw(float dt) 
    {

        SuperConsole.SetCursorPosition(spaceship.GetX(), spaceship.GetY());
        SuperConsole.ForegroundColor = spaceship.GetColour();
        SuperConsole.Write(spaceship.GetChar());
    }



    public void OnInput(ConsoleKey key)
    {
        int redo = 0;
        ConsoleKey pressedKey = key;

        do
        {    
            Console.Clear();

            switch (pressedKey)
            {
                case ConsoleKey.LeftArrow:
                    SuperConsole.SetCursorPosition(spaceship.GetX() - 1, spaceship.GetY());
                    break;
                case ConsoleKey.UpArrow:

                    break;
                case ConsoleKey.RightArrow:

                    SuperConsole.SetCursorPosition(spaceship.GetX()+1, spaceship.GetY());

                    break;
                case ConsoleKey.DownArrow:

                    break;
            }
        } while (redo == 0);

    }

}

}

entity.cs:

class Entity 
{
    protected int xPos;
    protected int yPos;

    protected char character;

    protected SuperConsoleColor colour;



    public Entity()
    {
    }

    public Entity(int xPosNew, int yPosNew, char newCharacter, SuperConsoleColor newColour) 
    {
        //define position
        xPos = xPosNew;
        yPos = yPosNew;

        //define character
        character = newCharacter;

        //define colour
        colour = newColour;
    }

    public char GetChar()
    {
        return character;
    }

   public int GetX()
    {
        return xPos;
    }




    public int GetY()
    {
        return yPos;
    }

    public SuperConsoleColor GetColour()
    {
        return colour;
    }




}

}

我看到两个问题:

  • RunGameLoop(Game game)就应该更换while (SuperConsole.KeyAvailable)if (SuperConsole.KeyAvailable)
  • Game.OnInput(ConsoleKey)您可以更改光标位置而不是宇宙飞船的位置

还尝试使用断点检查代码是否到达Game.Draw(),并检查飞船位置和光标位置是否正确。

另外,您还应该对C#有所了解。 代替

public char GetChar()
{
    return character;
}
private character;

.Net允许您使用属性:

public char Character
{
    get; private set;
}

要么

public char Character
{
    get { return character; }
}
private character = '@';

希望这可以帮助。

除此之外:没有冒犯,但是这个问题实际上并不是Stackoverflow的目的。 将来,请耐心等待,尝试使用调试技巧,而不要让Stackoverflow为您完成“肮脏的工作”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM