在winform板中移动按钮

时间:2015-04-23 18:56:39

标签: c# .net winforms

我试图为2048年的简单游戏编写代码。我已经设法实现了在列中移动按钮的功能,并在它们相等的情况下对它们求和,但不知何故,只是在X比率。他们为什么不在Y做同样的事情?

以下是代码:

bool checkIfMoved; 

private void Form1_KeyDown(object sender, KeyEventArgs e)
{           
        if (e.KeyCode == Keys.Left)
        {
            checkIfMoved = false;

            for (int i = 0; i <= 3; i++)
            {
                for (int j = 0; j <= 3; j++)
                {
                    if ((j != 0) && (board[i, j].Visible == true) && (board[i, j - 1].Visible == false))
                    {
                        moveButton(i, j, e);
                    }
                    else if ((j != 0) && (board[i, j].Visible == true) && (board[i, j - 1].Visible == true))
                    {
                        sumButtons(i, j, e);
                    }
                }
            }

            if (checkIfMoved == true)
            {
                GenerateField();
            }
        }
        else if (e.KeyCode == Keys.Right)
        {
            checkIfMoved = false;

            for (int i = 3; i >= 0; i--)
            {
                for (int j = 3; j >= 0; j--)
                {
                    if ((j != 3) && (board[i, j].Visible == true) && (board[i, j + 1].Visible == false))
                    {
                        moveButton(i, j, e);
                    }
                    else if ((j != 3) && (board[i, j].Visible == true) && (board[i, j + 1].Visible == true))
                    {
                        sumButtons(i, j, e);
                    }
                }
            }

            if (checkIfMoved == true)
            {
                GenerateField();
            }
            else if (e.KeyCode == Keys.Up)
            {
                checkIfMoved = false;

                for (int i = 0; i <= 3; i++)
                {
                    for (int j = 3; j >= 0; j--)
                    {
                        if ((i != 0) && (board[i, j].Visible == true) && (board[i - 1, j].Visible == false))
                        {
                            moveButton(i, j, e);
                        }
                        else if ((i != 0) && (board[i, j].Visible == true) && (board[i - 1, j].Visible == true))
                        {
                            sumButtons(i, j, e);
                        }
                    }

                    if (checkIfMoved == true)
                    {
                        GenerateField();
                    }
                }
            }
            else if (e.KeyCode == Keys.Down)
            {
                checkIfMoved = false;

                for (int i = 3; i >= 0; i--)
                {
                    for (int j = 0; j <= 3; j++)
                    {
                        if ((i != 3) && (board[i, j].Visible == true) && (board[i + 1, j].Visible == false))
                        {
                            moveButton(i, j, e);                                                
                        }
                        else if ((i != 3) && (board[i, j].Visible == true) && (board[i + 1, j].Visible == true))
                        {
                            sumButtons(i, j, e);
                        }
                    }
                }

                if (checkIfMoved == true)
                {
                    GenerateField();
                }
            }
        }
}

现在移动和求和功能:

    private void moveButton(int i, int j, KeyEventArgs e)
    {           
        SwitchKey Switch = new SwitchKey(i, j, e);                           
            try
            {

                while (board[Switch.line, Switch.column].Text == "0")
                {
                    board[Switch.line, Switch.column].Text = board[i, j].Text;
                    board[Switch.line, Switch.column].Visible = true;
                    board[i, j].Visible = false;
                    board[i, j].Text = "0";
                    sumButtons(Switch.line, Switch.column, e);
                    checkIfMoved = true;
                    switch(e.KeyCode)
                    {
                        case(Keys.Left):
                            j--;
                            break;
                        case(Keys.Right):
                            j++;
                            break;
                        case(Keys.Up):
                            i--;
                            break;
                        case(Keys.Down):
                            i++;
                            break;
                    }
                    Switch = new SwitchKey(i, j, e);
                }
            }
            catch { }
    }
    private void sumButtons(int i, int j, KeyEventArgs e)
    {           
        SwitchKey Switch = new SwitchKey(i, j, e);
        while ((board[i, j].Text == board[Switch.line,Switch.column].Text))
        {
            int x;
            int y;
            Int32.TryParse(board[i, j].Text, out x);
            Int32.TryParse(board[Switch.line, Switch.column].Text, out y);
            int z = x + y;
            string a = z.ToString();
            board[Switch.line, Switch.column].Text = a;
            board[Switch.line, Switch.column].Visible = true;
            board[i, j].Visible = false;
            board[i, j].Text = "0";

        }
    }

和SwitchKey类:

    class SwitchKey
{
    public int line;
    public int column;

    public SwitchKey(int i, int j, System.Windows.Forms.KeyEventArgs e)
    {


        #region Keycode switch
        switch (e.KeyCode)
        {
            case (System.Windows.Forms.Keys.Left):
                {
                    column = j-1;
                    line = i;
                    break;
                }
            case (System.Windows.Forms.Keys.Right):
                {
                    column = j + 1;
                    line = i;
                    break;
                }
            case (System.Windows.Forms.Keys.Up):
                {
                    column = j;
                    line = i-1;
                    break;
                }
            case (System.Windows.Forms.Keys.Down):
                {
                    column = j;
                    line = i+1;
                    break;
                }
        }
        #endregion
    }
}

当我使用左右键时游戏效果很好但在上下使用时没有做任何事情。我做错了什么?

2 个答案:

答案 0 :(得分:1)

看来你需要一个近距离支撑

    ....
    else if (e.KeyCode == Keys.Right)
    {
        checkIfMoved = false;
        .... a lot of code
        if (checkIfMoved == true)
        {
            GenerateField();
        }  
    }// <-- here
    else if (e.KeyCode == Keys.Up)
    {
       ...

答案 1 :(得分:0)

你错位了}

你的

 else if (e.KeyCode == Keys.Up)
        {

嵌套在

    else if (e.KeyCode == Keys.Right)
    {

试试这个:

bool checkIfMoved; 
private void Form1_KeyDown(object sender, KeyEventArgs e)
{           
    if (e.KeyCode == Keys.Left)
    {

        checkIfMoved = false;
        for (int i = 0; i <= 3; i++)
        {
            for (int j = 0; j <= 3; j++)
            {
                if ((j != 0) && (board[i, j].Visible == true) && (board[i, j - 1].Visible == false))
                {
                    moveButton(i, j, e);
                }
                else if ((j != 0) && (board[i, j].Visible == true) && (board[i, j - 1].Visible == true))
                {
                    sumButtons(i, j, e);
                }
            }
        }
        if (checkIfMoved == true)
        {
            GenerateField();
        }

    }
    else if (e.KeyCode == Keys.Right)
    {
        checkIfMoved = false;
        for (int i = 3; i >= 0; i--)
        {
            for (int j = 3; j >= 0; j--)
            {
                if ((j != 3) && (board[i, j].Visible == true) && (board[i, j + 1].Visible == false))
                {
                    moveButton(i, j, e);
                }
                else if ((j != 3) && (board[i, j].Visible == true) && (board[i, j + 1].Visible == true))
                {
                    sumButtons(i, j, e);
                }
            }
        }
        if (checkIfMoved == true)
        {
            GenerateField();
        }
    }
    else if (e.KeyCode == Keys.Up)
    {
        checkIfMoved = false;
        for (int i = 0; i <= 3; i++)
        {
            for (int j = 3; j >= 0; j--)
            {
                if ((i != 0) && (board[i, j].Visible == true) && (board[i - 1, j].Visible == false))
                {
                    moveButton(i, j, e);
                }
                else if ((i != 0) && (board[i, j].Visible == true) && (board[i - 1, j].Visible == true))
                {
                    sumButtons(i, j, e);
                }
            }
            if (checkIfMoved == true)
            {
                GenerateField();
            }
        }
    }
    else if (e.KeyCode == Keys.Down)
    {
        checkIfMoved = false;

        for (int i = 3; i >= 0; i--)
        {
            for (int j = 0; j <= 3; j++)
            {
                if ((i != 3) && (board[i, j].Visible == true) && (board[i + 1, j].Visible == false))
                {
                    moveButton(i, j, e);                                                
                }
                else if ((i != 3) && (board[i, j].Visible == true) && (board[i + 1, j].Visible == true))
                {
                    sumButtons(i, j, e);
                }
            }
        }
        if (checkIfMoved == true)
        {
            GenerateField();
        }
    }


}