Giter VIP home page Giter VIP logo

monogame-astar-pathfinding's People

Contributors

manbeardgames avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

monogame-astar-pathfinding's Issues

KeyNotFoundException

The line "location = CameFrom[location];" In the CalculatePath(start, goal) function consistently throws KeyNotFountExceptions. My grid is a 40 x 23, 1280 pixels in the x and 736 y. My start and goal locations are valid points and not on any wall locations. For now my band-aid fix is a try/catch. Here's the game class I'm testing with

`public class Game4 : Game {
private GraphicsDeviceManager graphics;
private SpriteBatch spriteBatch;

	SquareGrid grid;
	AstarSearch pathfinder;

	public Game4() {
		graphics = new GraphicsDeviceManager(this);
		Content.RootDirectory = "Content";
		IsMouseVisible = true;
	}

	protected override void Initialize() {
		int width = 1280;
		int height = 736;
		graphics.PreferredBackBufferHeight = height;
		graphics.PreferredBackBufferWidth = width;
		graphics.ApplyChanges();

		Random rand = new Random();
		grid = new SquareGrid(23, 40);
		for (int x = 0; x < 40; x++) {
			for (int y = 0; y < 23; y++) {
				if (rand.NextDouble() < 0.1) {
					Location wall = new Location(x, y);
					grid.walls.Add(wall);

				}
			}
		}
		pathfinder = new AstarSearch(grid);

		base.Initialize();
	}

	protected override void LoadContent() {
		spriteBatch = new SpriteBatch(GraphicsDevice);
	}

	protected override void Update(GameTime gameTime) {
		float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
		Window.Title = (1 / dt).ToString();

		Location start = GetRandomLocation();
		Location goal = GetRandomLocation();

		if (start != goal) {
			if (!grid.walls.Contains(start) &&
				!grid.walls.Contains(goal)) {
				pathfinder = new AstarSearch(grid);
				pathfinder.CalculatePath(start, goal);
			}
		}

		base.Update(gameTime);
	}

	protected override void Draw(GameTime gameTime) {
		GraphicsDevice.Clear(Color.CornflowerBlue);

		spriteBatch.Begin();

		for (int x = 0; x < 40; x++) {
			for (int y = 0; y < 23; y++) {
				Location cell = new Location(x, y);
				Rectangle rect = new Rectangle(cell.X, cell.Y, 32, 32);
				if (cell == pathfinder.Start) {
					spriteBatch.FillRectangle(rect.X * 32, rect.Y * 32, 32, 32, Color.Red);
				}
				else if (cell == pathfinder.Goal) {
					spriteBatch.FillRectangle(rect.X * 32, rect.Y * 32, 32, 32, Color.Green);
				}
				else if (grid.walls.Contains(cell)) {
					spriteBatch.FillRectangle(rect.X * 32, rect.Y * 32, 32, 32, Color.White);
				}
				else if (pathfinder.Path.Contains(cell)) {
					spriteBatch.FillRectangle(rect.X * 32, rect.Y * 32, 32, 32, Color.Blue);
				}
			}
		}

		spriteBatch.End();

		base.Draw(gameTime);
	}

	Location GetRandomLocation() {
		Random rand = new Random();
		int x = rand.Next(0, 40);
		int y = rand.Next(0, 23);
		return new Location(x, y);
	}
}`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.