خرید بک لینک

Vote count: 0

I'm making a game with a maze (in C# XNA). All the maze algorithms I've seen say something along the lines of "selecting a wall". I have a list of walls, and an object called a "turtle". The turtle is the object that moves around the screen and picks walls to remove. Here is the "SelectWall" method:

private int SelectWall(Turtle turtle, Direction dir, List<Wall> walls)
    {
        turtle.Update();
        int row = 0;
        int column = 0;
        Direction2 dir2 = Direction2.LeftRight;

        switch (dir)
        {
            case Direction.Up:
                dir2 = Direction2.LeftRight;
                row = turtle.X / ROW_SIZE;
                column = turtle.Y / ROW_SIZE;
                break;
            case Direction.Down:
                dir2 = Direction2.LeftRight;
                row = turtle.X / ROW_SIZE;
                column = (turtle.Y / ROW_SIZE) + 1;
                break;
            case Direction.Left:
                dir2 = Direction2.UpDown;
                row = turtle.X / ROW_SIZE;
                column = turtle.Y / ROW_SIZE;
                break;
            case Direction.Right:
                dir2 = Direction2.UpDown;
                row = (turtle.X / ROW_SIZE) + 1;
                column = (turtle.Y / ROW_SIZE) + 1;
                break;
        }

        int index = 0;
        for (int i = 0; i < Walls.Count; i++)
        {
            if (Walls[i].Column == column)
            {
                if (Walls[i].Row == row)
                {
                    if (Walls[i].Direction == dir2)
                    {
                        index = i;
                        break;
                    }
                }
            }
        }

        retu index;
    }

Now I'll explain what it does.

The turtle parameter is, of course, the turtle moving around the screen. "Dir" is the direction that the turtle is supposed to pick the wall from (ex. If "dir" was Direction.Up, then the wall we're trying to pick is above the turtle). The List of Walls is the List of all the Walls on screen.

The Wall class has a few properties: Direction (which is of type Direction2. Direction2 specifies whether a wall is side-to-side (LeftRight) or up and down (UpDown)), Row (the row the wall is in), and Column (the column the wall is in). Previously, I tried this using X and Y positions, and used the Turtle's X and Y to calculate the Wall's. This didn't work, so I tried a different approach, but if you want to use this in your answer, go ahead... I'm too desperate at this point to be picky :)

Next, there is a switch statement which uses the turtle's position to calculate the Wall's row and column.

You'll see a constant called "ROW_SIZE". Each wall has a long side and a short side (they're rectangular). The long side is 70 pixels, while the short is 4. ROW_SIZE is the long side plus half of the short side, or 72. This is the length that the turtle takes up while in a cell; it's partially covered by walls.

Anyway, the switch statement calculates the Wall's row, column, and direction. After that is a for loop that finds the Wall in the list with the specified values. It then sets the retu value of index to that number, where the maze generator uses that index to preform an action with the wall.

Here is the problem.

The "row" value in the switch statement is always set to 10 in the begiing, which is understandable. I'm using the binary tree algorithm with a northwest bias, so the turtle always starts in the bottom-right coer. Anyway, the maximum value for the row value is 8, and I've checked everything to make sure that the turtle is in the right position and that it doesn't go off-screen.

Does anyone know how to fix this? If you need to know anything else, just ask, as there's a lot going around in the program. Any solution is welcome, but just keep in mind that I'm a 13-year-old begier, so I might not understand all the terms you use.

Thanks in advance!

asked 8 secs ago

برچسب: نویسنده: استخدام کار تاريخ: شنبه 19 تير 1395 ساعت: 5:30

صفحه بندی