Astar Game Explained Script
Astar Game Explained Script
We define a class called AStarGame, which contains everything — the grid, player,
pathfinding logic, and visuals.
In the init function, we set up the screen, define colors, and prepare the grid.
We create a square grid where 1 means obstacle and 0 means open space.
The start point is near the top-left and the goal is near the bottom-right.
We also add a border of obstacles to make a clean boundary.
The heuristic function estimates the distance from any point to the goal — we use
straight-line (Euclidean) distance.
If the path exists and we're not placing obstacles, we draw the path in yellow
lines connecting each step.
At the bottom, we display a helpful message — based on the current game state.
If the spacebar is pressed, we stop placing obstacles and calculate the path.
Pressing R resets the game.
Arrow keys move the player if the next cell isn’t a wall.
If the player reaches the goal — we win!
The run function ties it all together — looping forever, handling events, updating
visuals, and keeping the game smooth.
And that’s it! You’ve just walked through a full pathfinding game — step by step,
beginner-friendly.