A pacman game made entirely in rust to run inside the terminal. I built this project to learn more about how rust works, more specifically how ownership, mutability, borrowing and references work and behave inside a running program. In addition this project features basic usage of threads.
Please Note: If you are working under Windows, switch to the Windows console host. Although the game is technically fully functional in the normal Windows terminal, it can run worse than in the Windows console host, especially when it comes to rendering the individual frames, which can lead to a flickering effect in the normal terminal.
- Rust
Here's how you can play this game:
- Press W or Up to move upwards
- Press S or Down to move downwards
- Press A or Left to move to the left
- Press D or Right to move to the right
- The yellow '@' is your character (pacman)
- The colored 'o' circles are the enemies (ghots)
- The blue '#' are the walls
This project follows the exact same rules as the first pacman, collect all the points on the map and don't get hit by the ghosts.
The player has 3 hearts in total, which means ghosts can hit him 3 times until he looses the game.
Before the game begins, the program parses the map template defined in the code and precomputes the coordinates of all walls and coins. Storing these positions in advance makes it efficient to determine whether a given coordinate contains an object, wall, or walkable path during gameplay.
Each game entity is initialized with a starting position. For example, the player begins at (x: 24, y: 18).
Once the game loop starts, every iteration checks for user input (W/A/S/D or arrow keys). These inputs update a global direction queue that controls the player’s movement logic (see What is a direction queue? ).
After processing input, the program recalculates the current positions of the player and ghosts and then renders the next frame.
To maintain gameplay stability and prevent the game from running too fast, rendering is rate-limited to one frame every 120 milliseconds, which is approximately 8.3 frames per second.
The ghosts use a simple algorithm that only allows them to change direction when they hit an obstacle, such as a wall, or reach an intersection.
When multiple movement options are available, the program randomly selects a new direction, which may also be the same as the current one.
This helps make ghost movement feel more natural, but can sometimes result in repetitive behaviour.
The direction queue is an important feature for making the game feel smoother and more natural to play.
Without it, players would need to perfectly time their input to turn at an intersection, which can feel cluncky. With the direction queue, inputs made before reaching an intersection are remembered and applied when the player can turn, making the overall experience much better.
There is always a lot of room for improvements, but here are my personal pain points of this project:
- The game can flicker when running in some terminals (it runs smoothly in most IDE terminals or in terminals on different operating systems)
- When the player moves towards a ghost and the ghost moves towards the player, the entities actually never hit each other because they just swap places.
- Powerpills would be a great addition to the game and the infrastructure is already there.