manbeardgames / monogame-astar-pathfinding Goto Github PK
View Code? Open in Web Editor NEWA quick implementation of A* pathfinding that can be used with a MonoGame project
License: The Unlicense
A quick implementation of A* pathfinding that can be used with a MonoGame project
License: The Unlicense
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);
}
}`
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.