Esempio n. 1
0
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            Log(e.Key.ToString());
            int CurRow, CurCol;

            CurRow = (int)Player.GetValue(Grid.RowProperty);
            CurCol = (int)Player.GetValue(Grid.ColumnProperty);
            try
            {
                switch (e.Key)
                {
                case Key.Up:
                    if (TheBoard.MoveUp())
                    {
                        Player.SetValue(Grid.RowProperty, (int)(Player.GetValue(Grid.RowProperty)) - 1);
                    }
                    break;

                case Key.Down:
                    if (TheBoard.MoveDown())
                    {
                        Player.SetValue(Grid.RowProperty, (int)(Player.GetValue(Grid.RowProperty)) + 1);
                    }
                    break;

                case Key.Left:
                    if (TheBoard.MoveLeft())
                    {
                        Player.SetValue(Grid.ColumnProperty, (int)(Player.GetValue(Grid.ColumnProperty)) - 1);
                    }
                    break;

                case Key.Right:
                    if (TheBoard.MoveRight())
                    {
                        Player.SetValue(Grid.ColumnProperty, (int)(Player.GetValue(Grid.ColumnProperty)) + 1);
                    }
                    break;
                }
            }catch (ArgumentException ex)
            {
                Log("Cannot move out of bounds");
            }
        }
Esempio n. 2
0
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            Log(e.Key.ToString());
            int CurRow, CurCol;

            CurRow = (int)Player.GetValue(Grid.RowProperty);
            CurCol = (int)Player.GetValue(Grid.ColumnProperty);
            try
            {
                switch (e.Key)
                {
                case Key.Up:
                    Player.Source = pbackward;
                    if (TheBoard.MoveUp())     //Move successful
                    {
                        //disable door im walking into
                        DisableWall("n", CurCol, CurRow);

                        //disable door im coming out of (opposite)
                        DisableWall("s", TheBoard.XPos, TheBoard.YPos);

                        //move player
                        Player.SetValue(Grid.RowProperty, TheBoard.YPos);
                    }
                    else     //Move failed
                    {
                        ReinforceWall("n", CurCol, CurRow);
                        if (CurRow != 0)
                        {
                            ReinforceWall("s", CurCol, CurRow - 1);
                        }
                    }
                    break;

                case Key.Down:
                    Player.Source = pforward;
                    if (TheBoard.MoveDown())
                    {
                        //disable door im walking into
                        DisableWall("s", CurCol, CurRow);

                        //disable door im coming out of (opposite)
                        DisableWall("n", TheBoard.XPos, TheBoard.YPos);

                        //move player
                        Player.SetValue(Grid.RowProperty, TheBoard.YPos);
                        CheckForWinner();
                    }
                    else
                    {
                        ReinforceWall("s", CurCol, CurRow);
                        if (CurRow != 4)
                        {
                            ReinforceWall("n", CurCol, CurRow + 1);
                        }
                    }

                    break;

                case Key.Left:
                    Player.Source = pleft;
                    if (TheBoard.MoveLeft())
                    {
                        //disable door im walking into
                        DisableWall("w", CurCol, CurRow);

                        //disable door im coming out of (opposite)
                        DisableWall("e", TheBoard.XPos, TheBoard.YPos);

                        //move player
                        Player.SetValue(Grid.ColumnProperty, TheBoard.XPos);
                    }
                    else
                    {
                        ReinforceWall("w", CurCol, CurRow);
                        if (CurCol != 0)
                        {
                            ReinforceWall("e", CurCol - 1, CurRow);
                        }
                    }
                    break;

                case Key.Right:
                    Player.Source = pright;
                    if (TheBoard.MoveRight())
                    {
                        //disable door im walking into
                        DisableWall("e", CurCol, CurRow);

                        //disable door im coming out of (opposite)
                        DisableWall("w", TheBoard.XPos, TheBoard.YPos);

                        //move player
                        Player.SetValue(Grid.ColumnProperty, TheBoard.XPos);


                        CheckForWinner();
                    }
                    else
                    {
                        ReinforceWall("e", CurCol, CurRow);
                        if (CurCol != 4)
                        {
                            ReinforceWall("w", CurCol + 1, CurRow);
                        }
                    }
                    break;
                }
                CheckForLoss();
            }
            catch (ArgumentException ex)
            {
                Log("Cannot move out of bounds");
            }
        }