吃豆子鬼随机移动

时间:2016-04-14 13:54:40

标签: c# c#-4.0 pacman

鬼魂必须在一个罗盘方向(N,S,E或W)移动直到它们 到达一个十字路口,此时他们可以左转或右转;如果他们遇到障碍物/墙壁,他们也会改变方向。

CellType == 'x' is my wall 
int x = rnd.Next(0, 2);
int y = rnd.Next(0, 2);

这是我到目前为止的尝试,但它一直把鬼魂放到墙上

if (x == 0) // this is the random positon 0 which faces the ghost down
{
    if (cells[yposg + 1, xposg].CellType == 'o' || cells[yposg + 1, xposg].CellType == '.' || cells[yposg + 1, xposg].CellType == '!')
    {
        gb.Ghost.yposghost += 20;

        if (cells[yposg, xposg - 1].CellType == 'x')
        {
            y = 1|0;
        }
    }

    if (y == 0) // this is the random positon 3 which faces the ghost right
    {
        if (cells[yposg, xposg ].CellType == 'x' )
        {
            y = 1;
        }

        if (cells[yposg, xposg - 1].CellType == 'o' || cells[yposg, xposg - 1].CellType == '.' || cells[yposg, xposg - 1].CellType == '!')
        {
            gb.Ghost.xposghost -= 20;
        }
    }
     else if (y == 1) // this is the random positon 2 which faces the ghost left
     {
         if (cells[yposg, xposg ].CellType == 'x')
         {
             y = 0;
         }

         if (cells[yposg, xposg + 1].CellType == 'o' || cells[yposg, xposg + 1].CellType == '.' || cells[yposg, xposg + 1].CellType == '!')
         {
             gb.Ghost.xposghost += 20;
         }
     }
}

else if (x ==1) // this is the random positon 1 which faces the ghost up
{
    if (cells[yposg , xposg].CellType == 'o' || cells[yposg - 1, xposg].CellType == '.' || cells[yposg - 1, xposg].CellType == '!')
    {
        gb.Ghost.yposghost -= 20;

        if (cells[yposg, xposg - 1].CellType == 'x')
        {
            y = 1 | 0;
        }
    }

    if (y == 0) // this is the random positon 3 which faces the ghost right
    {
        if (cells[yposg, xposg ].CellType == 'x')
        {
            y = 1;
        }

        if (cells[yposg, xposg - 1].CellType == 'o' || cells[yposg, xposg - 1].CellType == '.' || cells[yposg, xposg - 1].CellType == '!')
        {
            gb.Ghost.xposghost -= 20;
        }
    }
    else if (y == 1) // this is the random positon 2 which faces the ghost left
    {
        if (cells[yposg, xposg ].CellType == 'x')
        {
            y = 0;
        }

        if (cells[yposg, xposg + 1].CellType == 'o' || cells[yposg, xposg + 1].CellType == '.' || cells[yposg, xposg + 1].CellType == '!')
        {
            gb.Ghost.xposghost += 20;
        }
    }
}

0 个答案:

没有答案