C#WPF游戏键盘机芯

时间:2017-02-04 05:42:25

标签: c# wpf wpf-controls keyboard-events

我正在尝试使用wpf在c#中制作一个小游戏。我有我的矩形移动,我试图修复的问题是如果你按下原来的键按下另一个键,然后释放其中一个它将朝着被保持的方向。我的代码有效,但是如果一个键被释放就会运行一段延迟,那么矩形将继续移动。

    enum Direction { left, right, up, down, none};
    Direction _direction = Direction.none;
    bool _directionIsPressed = false;
    bool _directionIsReleased = false;
    double x = 0;
    double y = 0;

    public MainWindow()
    {
        InitializeComponent();



        DispatcherTimer timer = new DispatcherTimer();
        timer.Tick += new EventHandler(MovePlayer);
        timer.Start();

        this.KeyDown += new KeyEventHandler(OnButtonKeyDown);
        this.KeyUp += new KeyEventHandler(OnButtonKeyRelease);

    }

    private void MovePlayer(object sender, EventArgs e)
    {
        switch (_direction)
        {
            case Direction.down:
                y += .05;
                Canvas.SetTop(Player, y);
                break;
            case Direction.up:
                y -= .05;
                Canvas.SetTop(Player, y);
                break;
            case Direction.left:
                x -= .05;
                Canvas.SetLeft(Player, x);
                break;
            case Direction.right:
                x += .05;
                Canvas.SetLeft(Player, x);
                break;
        }
    }

    private void OnButtonKeyDown(object sender, KeyEventArgs e)
    {
        switch (e.Key)
        {
            case Key.Down:
                _direction = Direction.down;
                _directionIsPressed = true;
                break;
            case Key.Up:
                _direction = Direction.up;
                _directionIsPressed = true;
                break;
            case Key.Left:
                _direction = Direction.left;
                _directionIsPressed = true;
                break;
            case Key.Right:
                _direction = Direction.right;
                _directionIsPressed = true;
                break;
            default:
                _direction = Direction.none;
                break;
        }
    }

    //When a key is released it will set the direction to none then 
    //reset the direction but there is about a half second delay in 
    //setting the direction again
    private void OnButtonKeyRelease(Object sender, KeyEventArgs e) 
    {
        _direction = Direction.none;
        this.KeyDown += new KeyEventHandler(OnButtonKeyDown);
    }

修改: 如果有人想在C#中使用wpf进行流畅的键盘移动,这就是我修复它的方法。谢谢Stefan的帮助。

        public MainWindow()
    {
        InitializeComponent();
        DispatcherTimer timer = new DispatcherTimer();
        timer.Tick += new EventHandler(MovePlayer);
        timer.Start();
    }

    private void MovePlayer(object sender, EventArgs e)
    {
        if(Keyboard.IsKeyDown(Key.Left))
        {
            x -= .05;
            Canvas.SetLeft(Player, x);
        }
        if(Keyboard.IsKeyDown(Key.Up))
        {
            y -= .05;
            Canvas.SetTop(Player, y);
        }
        if(Keyboard.IsKeyDown(Key.Down))
        {
            y += .05;
            Canvas.SetTop(Player, y);
        }
        if(Keyboard.IsKeyDown(Key.Right))
        {
            x += .05;
            Canvas.SetLeft(Player, x);
        }
    }

2 个答案:

答案 0 :(得分:1)

不是在不同的函数中使用两个switch语句,而是尝试将它们合并为一个。延迟的原因可能是从一个函数跳转到另一个函数所需的开销。

不是单独调用MovePlayer(),而是在按下键时尝试移动播放器。

private void OnButtonKeyDown(object sender, KeyEventArgs e)
{
    switch (e.Key)
    {
        case Key.Down:
            y += .05;
            Canvas.SetTop(Player, y);
            break;
        case Key.Up:
            y -= .05;
            Canvas.SetTop(Player, y);
            break;
        case Key.Left:
            x -= .05;
            Canvas.SetLeft(Player, x);
            break;
        case Key.Right:
            x += .05;
            Canvas.SetLeft(Player, x);
            break;
        default:
            _direction = Direction.none;
            break;
    }
}

答案 1 :(得分:1)

如果您想允许组合移动,例如向上+左移,那么我只能使用计时器和Keyboard.IsKeyDown(键)验证

private void MovePlayer(object sender, EventArgs e)
{
    if (Keyboard.IsKeyDown(Key.Down))
    {
        y += .05;
        Canvas.SetTop(Player, y);
    }
    if(Keyboard.IsKeyDown(Key.Up))
    {
        y -= .05;
        Canvas.SetTop(Player, y);
    }
    if (Keyboard.IsKeyDown(Key.Left))
    {
        x -= .05;
        Canvas.SetLeft(Player, x);
    }
    if (Keyboard.IsKeyDown(Key.Right))
    {
        x += .05;
        Canvas.SetLeft(Player, x);
    }
}

其他情况下,如果此时只能在一个简单的方向上进行移动,则可以检查KeyReleased例程中的语句。

private void OnButtonKeyRelease(Object sender, KeyEventArgs e) 
{
    if (Keyboard.IsKeyDown(Key.Down))
        _direction = Direction.down;
    else if(Keyboard.IsKeyDown(Key.Up))
        _direction = Direction.up;
    else if (Keyboard.IsKeyDown(Key.Left))
        _direction = Direction.left;
    else if (Keyboard.IsKeyDown(Key.Right))
        _direction = Direction.right;
    else
        _direction = Direction.none;
}

但是如果你想同时按下2个以上的移动按钮,你需要将它保存在某个数组中,然后检查女巫是否被按下,并设置动作取决于此按钮。