简体   繁体   English

如何在屏幕上移动对象

[英]How to move object on the screen

how I can move any Object on screen in WinForm ? 如何在WinForm中在屏幕上移动任何对象?

When I press right -> the object will move right until i'll press any other arrow key 当我按向右->该对象将向右移动,直到我按任意其他箭头键

and when I press left <- the object will move left until i'll press any other arrow key 当我按向左<-时,对象将向左移动,直到按任意其他箭头键

The object all the time will be in motion (like in the Snake game) 该对象将始终处于运动状态(例如在Snake游戏中)

thank's in advance 提前致谢

You probably want to inherit from Form and override the OnPaint method. 您可能想从Form继承并重写OnPaint方法。 You'll probably also need to Invalidate() every time to force repaint. 您可能每次也需要Invalidate()来强制重绘。 This will set up a simple game loop and allow you to update the screen pretty quickly. 这将设置一个简单的游戏循环,并允许您快速更新屏幕。

In your OnPaint method you'll update the position of an object based on how much time has elapsed since the last time your OnPaint method was called and which key was pressed last. 在OnPaint方法中,将基于自上次调用OnPaint方法和最后一次按下哪个键以来经过的时间来更新对象的位置。 You'll probably do this by using a velocity as follows: 您可能会使用如下速度来做到这一点:

newPosition = oldPosition + (elapsedTime * velocity)

The value of velocity will change based on which key you press (ie negative for left, positive for right). 速度值将根据您按下的键而改变(即,负号表示左,正号表示右)。 You'll also need a variable and some code to keep track of whether it's moving horizontally or vertically. 您还需要一个变量和一些代码来跟踪它是水平移动还是垂直移动。

This is a pretty low performance way to accomplish this (ie it's a hack). 这是一种相当低性能的方法来完成此操作(即,是一种hack)。 If you want to stick with Windows but get better performance without too much work, you might look into XNA . 如果您想坚持使用Windows但又不需要太多工作就能获得更好的性能,则可以考虑使用XNA If you want much better performance and are willing to do significantly more work, look into Interop with DirectX and the Win32 API or just switch over all together. 如果您想获得更好的性能并愿意做更多的工作,请查看具有DirectX和Win32 API的Interop或一起切换。

  enum position
    {
        Left,Right,up,down
    }
    private int _x;
    private int _y;
    private position _objposition;
    public Form1()
    {
        InitializeComponent();
        _x = 50;
        _y = 50;
        _objposition = position.down;
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.FillRectangle(Brushes.DarkSlateBlue,_x,_y,100,100);
    }

    private void timertick_Tick(object sender, EventArgs e)
    {
        if (_objposition == position.Right)
        {
            _x += 10;
        }
        else if (_objposition == position.Left)
        {
            _x -= 10;
        }
        else if (_objposition == position.up)
        {
            _y -= 10;
        }
        else if (_objposition == position.down)
        {
            _y += 10;
        }
        Invalidate();
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Left)
        {
            _objposition =position.Left;
        }
        else if (e.KeyCode == Keys.Right)
        {
            _objposition = position.Right;
        }
        else if (e.KeyCode == Keys.Up)
        {
            _objposition = position.up;
        }
        else if (e.KeyCode == Keys.Down)
        {
            _objposition = position.down;
        }
    }
}

This will create a rectangle of 50X50 size. 这将创建一个50X50大小的矩形。 By default i will set the position of that box downward.But when you press any key for eg:- if you press left key arrow then key event will fire and it will set the position of the box towards left and the box will start moving in left until another key is pressed 默认情况下,我将框的位置向下设置。但是当您按任意键时,例如:-如果您按向左箭头键,则将触发键事件,它将将框的位置向左设置,框将开始移动在左边直到按下另一个键

您需要一个游戏循环来继续按一下按钮即可开始的运动方向。

捕获键,调整对象坐标。

Assuming you're doing this in 2D, you could setup an x and y change factor that you setup properly with each arrow key press. 假设您正在2D模式下进行操作,则可以设置每次按箭头键即可正确设置的x和y变化因子。 Then setup a timer to update position at the rate you want. 然后设置一个计时器以所需的速率更新位置。 And when the timer ticks, adjust the position of your object by the x/y change factors. 当计时器计时时,通过x / y变化因子调整对象的位置。

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

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