-1

Hi so i'm making this top down shooter using c# in visual studios and for some reason i can't stop the picture box from moving out of the borders, right now what happens is that if i place the picture box on the bottom right hand corner then it works but doesn't work if i place the picture box in the center here's my code:

private void Level_1_of__desolor_KeyDown(object sender, KeyEventArgs e)
    {
        int xpos = CharacterMain.Location.X;
        int ypos = CharacterMain.Location.Y;
        if (e.KeyCode == Keys.A)
        {
            this.CharacterMain.Load(@"../Graphics\Energy L.png");
        }
        else if (e.KeyCode == Keys.D)
        {
            this.CharacterMain.Load(@"../Graphics\Energy R.png");
            CharacterMain.Left += speed;
        }
        else if (e.KeyCode == Keys.W)
        {
            this.CharacterMain.Load(@"../Graphics\Energy F.png");
            CharacterMain.Top -= 10;
        }
        else if (e.KeyCode == Keys.S)
        {
            this.CharacterMain.Load(@"../Graphics\Energy B.png");
            CharacterMain.Top += 10;
        }
        if (xpos > 12 && xpos < 1096 && ypos == 523)
        {
            if (ypos == 523)
            {
                CharacterMain.Top -= 10;
            }
        }
        if (CharacterMain.Location.Y < 523 && CharacterMain.Location.Y > 12)
        {
            if (CharacterMain.Location.X == 12)
            {
                CharacterMain.Left += 10;
            }
        }
        if (CharacterMain.Location.Y < 523 && CharacterMain.Location.Y > 12)
        {
            if (CharacterMain.Location.X == 12)
            {
                CharacterMain.Left += 10;
            }
        }
        if (CharacterMain.Location.X > 12 && CharacterMain.Location.X < 1102)
        {
            if (CharacterMain.Location.Y == 12)
            {
                CharacterMain.Top += 10;
            }
        }
    }

1 Answers1

0

I figured it out my logic came out stupid basically what was happening was i was doing the reverse of everything here's the fixed code.

//Border Control
        #region
        if (xpos < 44)
        {
                CharacterMain.Left += 10;
        }
        if (ypos > 523)
        {
            CharacterMain.Top -= 10;
        }
        if (ypos < 89)
        {
            CharacterMain.Top += 10;
        }
        if (xpos > 1068)
        {
            CharacterMain.Left -= 10;
        }
        #endregion