Data Structures
Arrays
Arrays are used to store collections of data, typically of the same type. They are commonly used to represent game elements, such as:
Pacman.ts
:pellets
: An array to store the positions of pellets in the game.powerPellets
: An array to store the positions of power pellets.ghosts
: An array to store information about the ghosts.fruits
: An array to store fruit positions and attributes.maze
: An array representing the maze structure (inGame.ts
).
Maps
Maps are used to store key-value pairs, providing a way to access data efficiently using unique keys. They are often used to:
Game.ts
:keyPresses
: A map to track pressed keys (e.g.,leftArrow
-> true).directions
: A map to track which direction each ghost is heading (e.g.,ghostId
->direction
).intervals
: A map to keep track of timers for game events (e.g.,powerPelletTimer
->interval
).
Custom Data Structures
Beyond standard JavaScript structures, there are custom data structures used:
Maze.ts
:Maze
: A class representing the maze structure. It uses an array of arrays (mazeGrid
) to store the map data.
Ghost.ts
:Ghost
: A class representing a ghost. It uses a map (directions
) to store possible movement directions.
Benefits of Data Structures
- Organization: Data structures help organize data, making it easier to manage and access.
- Efficiency: Efficient data storage and retrieval are crucial for smooth game performance.
- Flexibility: Different data structures offer different advantages for specific tasks, allowing for adaptable game logic.
- Code Readability: Using appropriate data structures improves the clarity and maintainability of your code.