C# console application WASD controls don't work properly

时间:2017-08-13 13:59:45

标签: c#

So i'm new to C#, but i have experience in C++, python and JavaScript.

i'm trying to recreate space invaders in a console application. I've managed to render the border that the characters will be in and have rendered a character with basic controls.

** - The Question **

public static void Update()
    {
        ConsoleKeyInfo keypressed = Console.ReadKey();
        //Console.
        up = keypressed.Key == ConsoleKey.W;
        down = keypressed.Key == ConsoleKey.S;
        left = keypressed.Key == ConsoleKey.A;
        right = keypressed.Key == ConsoleKey.D;

        checkUp = (playerPosY > height - 7);
        checkDown = (playerPosY < height - 2);
        checkLeft = (playerPosX > 1);
        checkRight = (playerPosX < width - 2);


        shoot = keypressed.Key == ConsoleKey.Spacebar;

        if (up && checkUp) playerPosY--;
        if (down && checkDown) playerPosY++;
        if (left && checkLeft) playerPosX--;
        if (right && checkRight) playerPosX++;

        if (shoot) { }

    }

This is how the controls work at the moment. But I know this has a few faults already. e.g.

  • pressing both of the opposite directions will make the character twerk on the spot.
  • can't really go diagonally.
  • if i press and hold a direction the character will move once, pause for a second, then move quite fast all of a sudden.

Does anyone have any suggestions as to how I can improve these controls?

p.s. The method is called in the main() within a while loop, I have 2 other functions... Initialize() (outside of the main loop) and Render() which runs just after the Update method.

p.p.s. I'm using a nested for loop to render the screen using Console.Write(" ");

0 个答案:

没有答案