Gold
Gold

Reputation: 62424

How to move object on the screen

how I can move any Object on screen in 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)

thank's in advance

Upvotes: 0

Views: 9123

Answers (5)

palak
palak

Reputation: 1

  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. 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

Upvotes: 0

Waylon Flinn
Waylon Flinn

Reputation: 20247

You probably want to inherit from Form and override the OnPaint method. You'll probably also need to Invalidate() every time to force repaint. 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. 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 (i.e. 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 (i.e. it's a hack). If you want to stick with Windows but get better performance without too much work, you might look into 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.

Upvotes: 1

Timothy Carter
Timothy Carter

Reputation: 15785

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. 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.

Upvotes: 0

anonymous
anonymous

Reputation:

capture keys, adjust object coordinates.

Upvotes: 0

Chris Morley
Chris Morley

Reputation: 2426

You need a game loop to continue the direction of the movement you originally initiated with a single press of a button.

Upvotes: 0

Related Questions