Pacman in Godot

I decided to implement Pacman in the Godot engine as an exercise to explore the basic building blocks of video games, as well as to understand how these could be implemented in Godot.

Initially, I attempted to handle player movement using the CharacterBody2D move_and_slide function, combined with the TileMap to define solid regions where the player couldn’t move. However, the character became locked if there was a precise fit between the size of the player’s collision shape and the width of the corridors.

I then changed my approach and started to define nodes and edges where the player could move, instead of relying on physics simulation. This information would later be utilized for the path-finding of the enemies.

The tiles that visually defined the corridors of the maze provided me with an initial set of grid positions and their connections. I simplified this data to only include nodes where there was an intersection or a change in direction on the path, as well as the connections between these nodes.

When the player presses a direction to move for the first time, I identify the edge the player is on. Using that edge and the player’s direction, I can discern the node towards which the player is moving and the node from which they are coming. If the player presses a direction that is not along the edge they are on, that direction is saved, and I try to apply it when the player arrives at a node - but only if that node connects to another node along the saved direction.

The data on the nodes and their connections was used to set up the Godot AStar2D class, which served as the foundation for calculating the shortest path between the enemies and the player. Since Godot’s AStar implementation calculates the best path between two nodes and not between two positions along the edges of the nodes, I had to extend it to accommodate this particular use case.

Published 2023.08.02

Want to know when I release a new game?