0

I am trying to learn c# by making simple game. I have a picture box that I am controlling with the keyboard and another picture box. How to make the one I am controlling move over the other picture and how to chose which picture-boxes is on top of the other picture-boxes?

        public Form1()
    {
        InitializeComponent();
    }
    int speed = 20;
    Point xy = new Point();

    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        xy = pic1.Location;
        xy = Methods.MoveXY(xy, e,speed);
        pic1.Location = xy;

    }

 public static Point MoveXY(Point xy, KeyPressEventArgs e,int speed)
    {

        switch (e.KeyChar)
        {

            case 'd':
                xy.X += speed;
                break;
            case 'a':
                xy.X -= speed;
                break;
            case 'w':

                xy.Y -= speed;
                break;
            case 's':

                xy.Y += speed;
                break;


        }

The two picture boxes are created by drag and drop in the form1 designer.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Fi Jeleva
  • 63
  • 1
  • 9

1 Answers1

0

Simply put both on the same parent (maybe the form) and call the method BringToFront() on the picturebox you want to have "over" the other.

Alternatively, you can put one picturebox into another. This way, the inner one will always be on top of the other one (it's parent).

Waescher
  • 5,361
  • 3
  • 34
  • 51