This project implements a classic Snake Game in C++. It solves the problem of creating an interactive, real-time console application using object-oriented design principles. This project matters because it demonstrates fundamental concepts of game development, including the game loop architecture, state management, and dynamic memory handling without relying on heavy game engines.
- Classic Gameplay: Navigate a snake to eat food, grow in length, and increase your score.
- Dynamic Growth: The snake's body grows dynamically as food is consumed, managed by a custom data structure.
- Score Tracking: Real-time score updates displayed on the screen.
- Cross-Platform Support: Utilizes
MacUILibto abstract terminal input/output, allowing the game to run on both Windows and POSIX systems (Mac/Linux). - Collision Detection: Handles logic for collecting food and preventing invalid moves (like reversing direction instantly).
- Language: C++ (Standard 98/11 compatible)
- Build Tool: GNU Make (
makefile) - Libraries:
MacUILib: A custom library for handling console I/O (wrapsconio.h/windows.hon Windows and standard POSIX calls on Mac/Linux).- Standard C++ Libraries (
iostream,cstdlib,time.h).
Tradeoff: Custom objPosArrayList vs. std::vector
- Decision: I chose to implement a custom
objPosArrayListclass to manage the snake's body segments instead of using the standard library'sstd::vector. - Why: This decision was made to explicitly handle memory management and deep copying logic. While
std::vectoris more robust, building a custom list provided finer control over howobjPosobjects are stored and allowed for a deeper understanding of dynamic array resizing and memory deallocation in C++.
-
Build the Project: Open your terminal and run the make command to compile the source code.
make
-
Run the Game: Execute the generated binary.
- On Windows:
./Project.exe
- On Mac/Linux:
./Project
- On Windows:
-
Controls:
- Use WASD keys to control the snake's direction.
- Press the designated exit key (usually defined in the game logic, e.g.,
ESCorSPACE) to quit.
- Game Loop Architecture: Understanding the cycle of Input -> Logic -> Draw and how to manage timing to create smooth gameplay.
- Dynamic Memory Management: Gained concrete skills in allocating and deallocating memory on the heap, specifically for the snake's body segments, to prevent memory leaks.
- Modular Design: Learned how to separate concerns by splitting the game into distinct classes (
GameMechs,Player,ScreenDrawer), making the codebase easier to maintain and extend. - Finite State Machines: Implemented basic state logic for player movement and game states (playing, game over).