C#无法在ASCII控制台游戏中移动角色

问题描述 投票:0回答:1

我有一个学校的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;
    }




}

}

c# oop console character ascii
1个回答
0
投票

我看到两个问题:

  • 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为您做“肮脏的工作”。

© www.soinside.com 2019 - 2024. All rights reserved.