0

我是一名业余程序员,我正在 XNA 游戏工作室开发一个简单的 Noughts 和 Crosss 游戏。现在我要做一个左键点击触发移动,它正在工作。但是当我使用部分类命令将代码传输到两个文件时,我不小心更改了代码,现在右上角的方块不起作用。这是“更新”代码,用于检查:

    private void MouseProcess()
    {

        MouseState mstate = Mouse.GetState();
        Mouse_pos = new Vector2(mstate.X, mstate.Y);
        float xpos = mstate.X;
        float ypos = mstate.Y;

        if (mstate.LeftButton.Equals(ButtonState.Pressed))
        {
            if (xpos >= Gamevar[0].Position.X && xpos <= Gamevar[0].Position.X + 72)
            {
                C = 1;
            }

            if (xpos >= Gamevar[1].Position.X && xpos <= Gamevar[1].Position.X + 72)
            {
                C = 2;
            }

            if (xpos >= Gamevar[2].Position.X && xpos <= Gamevar[2].Position.X + 72)
            {
                C = 3;
            }

            if (ypos >= Gamevar[0].Position.Y && xpos <= Gamevar[0].Position.Y + 75)
            {
                R = 1;
            }

            if (ypos >= Gamevar[3].Position.Y && xpos <= Gamevar[3].Position.Y + 75)
            {
                R = 2;
            }

            if (ypos >= Gamevar[6].Position.Y && xpos <= Gamevar[6].Position.Y + 75)
            {
                R = 3;
            }
        }


    }

    private void Turncode()
    {
        switch (turn)
        {
            case true:
                switch (C)
                {
                    case 1:
                        switch (R)
                        {
                            case 1:
                                Gamevar[0].owner = 1;
                                turn = false;
                                C = 0;
                                R = 0;
                                break;
                            case 2:
                                Gamevar[3].owner = 1;
                                turn = false;
                                C = 0;
                                R = 0;
                                break;
                            case 3:
                                Gamevar[6].owner = 1;
                                turn = false;
                                C = 0;
                                R = 0;
                                break;
                        }
                        break;

                    case 2:
                        switch (R)
                        {
                            case 1:
                                Gamevar[1].owner = 1;
                                turn = false;
                                C = 0;
                                R = 0;
                                break;
                            case 2:
                                Gamevar[4].owner = 1;
                                turn = false;
                                C = 0;
                                R = 0;
                                break;
                            case 3:
                                Gamevar[7].owner = 1;
                                turn = false;
                                C = 0;
                                R = 0;
                                break;
                        }
                        break;

                    case 3:
                        switch (R)
                        {
                            case 1:
                                Gamevar[2].owner = 1;
                                turn = false;
                                C = 0;
                                R = 0;
                                break;
                            case 2:
                                Gamevar[5].owner = 1;
                                turn = false;
                                C = 0;
                                R = 0;
                                break;
                            case 3:
                                Gamevar[8].owner = 1;
                                turn = false;
                                C = 0;
                                R = 0;
                                break;
                        }
                        break;
                }

                break;
            case false:
                switch (C)
                {
                    case 1:
                        switch (R)
                        {
                            case 1:
                                Gamevar[0].owner = 2;
                                turn = true;
                                C = 0;
                                R = 0;
                                break;
                            case 2:
                                Gamevar[3].owner = 2;
                                turn = true;
                                C = 0;
                                R = 0;
                                break;
                            case 3:
                                Gamevar[6].owner = 2;
                                turn = true;
                                C = 0;
                                R = 0;
                                break;
                        }
                        break;

                    case 2:
                        switch (R)
                        {
                            case 1:
                                Gamevar[1].owner = 2;
                                turn = true;
                                C = 0;
                                R = 0;
                                break;
                            case 2:
                                Gamevar[4].owner = 2;
                                turn = true;
                                C = 0;
                                R = 0;
                                break;
                            case 3:
                                Gamevar[7].owner = 2;
                                turn = true;
                                C = 0;
                                R = 0;
                                break;
                        }
                        break;

                    case 3:
                        switch (R)
                        {
                            case 1:
                                Gamevar[2].owner = 2;
                                turn = true;
                                C = 0;
                                R = 0;
                                break;
                            case 2:
                                Gamevar[5].owner = 2;
                                turn = true;
                                C = 0;
                                R = 0;
                                break;
                            case 3:
                                Gamevar[8].owner = 2;
                                turn = true;
                                C = 0;
                                R = 0;
                                break;
                        }
                        break;
                }

                break;

        }
    }

它不是很复杂,但 Gamevar[1]、[2] 和 [5] 不起作用。

4

2 回答 2

0

您可以使用 for 循环遍历正方形以获取光标的位置。(请注意您的值,C并且R将比原始代码中的值小一。)

然后,您可以使用方程式获取一维数组中单元格的索引(R*3)+C

private void MouseProcess()
{
    MouseState mstate = Mouse.GetState();
    Mouse_pos = new Vector2(mstate.X, mstate.Y);
    float xpos = mstate.X;
    float ypos = mstate.Y;

    if (mstate.LeftButton.Equals(ButtonState.Pressed))
    {
        for (int i=0; i<3; i++)
        {
            if (xpos >= Gamevar[i].Position.X && xpos <= Gamevar[i].Position.X + 72)
            {
                C = i;
            }
            if (ypos >= Gamevar[i*3].Position.Y && ypos <= Gamevar[i*3].Position.Y + 75)
            {
                R = i;
            }
        }
    }
}

private void Turncode()
{
    if (turn==true)
    {
        Gamevar[(R*3)+C].owner=1;
    }
    else
    {
        Gamevar[(R*3)+C].owner=2;
    }
    turn=!turn; //toggle to the other person's turn
    C=0;
    R=0;
}

如果您的代码变得重复,请尝试使用循环或找到等式来为您完成工作。

于 2013-06-09T07:12:19.710 回答
0

在内部ProcessMouse,您的if语句检查ypos实际上是xpos在第二个操作数中查看&&

例如

if (ypos >= Gamevar[0].Position.Y && xpos <= Gamevar[0].Position.Y + 75)
{
    R = 1;
}

应该:

if (ypos >= Gamevar[0].Position.Y && ypos <= Gamevar[0].Position.Y + 75)
{
    R = 1;
}

if对于以下两个语句,依此类推。

除此之外,您可能会从重构代码中受益,以使用一种方法来计算单击的正方形的索引,而不是将列和行存储在单独的变量中并使用嵌套的 switch 语句。

于 2013-06-08T06:18:07.493 回答