This project is a beginner-friendly implementation of the classic Snake Game using C for the game logic and Character User Interface (CUI) for rendering. It integrates SQL to save high scores, allowing players to compete for the best performances.
- Learn basic game logic with C.
- Gain hands-on experience in database integration using SQL.
- Understand how to create interactive console applications.
- Classic Snake Game in the console.
- Snake Movement: Use
W
,A
,S
,D
or arrow keys. - Randomly generated food and real-time score tracking.
- Game ends on collision with walls or the snake itself.
- Save player scores in a MySQL database.
- Display the Top 10 High Scores at the start of the game.
To run the project, you need the following installed on your system:
- C Compiler: GCC (MinGW or TDM-GCC for Windows).
- MySQL Server: To store and manage high scores.
- MySQL Connector for C: To enable communication between your C program and MySQL.
git clone https://github.com/<your-username>/snake-game-c-sql.git
cd snake-game-c-sql
- Download the MySQL Connector for C from MySQL Connector/C Downloads.
- Extract the files to a directory, e.g.,
C:\mysql-connector-c
. - Add the
lib
andinclude
folders to your system's Environment Variables (PATH
).
- Open your MySQL Command Line Client or Workbench.
- Create a database and table:
CREATE DATABASE snake_game; USE snake_game; CREATE TABLE high_scores ( id INT AUTO_INCREMENT PRIMARY KEY, player_name VARCHAR(50), score INT, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP );
Edit the database connection details in main.c
:
mysql_real_connect(conn, "localhost", "your-username", "your-password", "snake_game", 3306, NULL, 0);
Replace:
your-username
with your MySQL username.your-password
with your MySQL password.
Use GCC to compile the program:
gcc -o snake_game main.c -IC:\mysql-connector-c\include -LC:\mysql-connector-c\lib -lmysql
Run the executable:
./snake_game
- Start the Game:
- Run the program, and the Top 10 high scores will be displayed.
- Control the Snake:
- Use the keyboard:
W
: UpA
: LeftS
: DownD
: Right
- Alternatively, use arrow keys.
- Use the keyboard:
- Goal:
- Eat food (
X
) to grow and increase your score. - Avoid colliding with walls (
#
) or the snake itself.
- Eat food (
- Game Over:
- When the game ends, your score is saved in the database.
- Restart to play again.
snake-game-c-sql/
├── main.c # Main game logic and database integration
├── README.md # Project documentation
libmysql.dll not found
:- Ensure the
libmysql.dll
file is in the same directory as your executable or in a directory listed in yourPATH
.
- Ensure the
- Database Connection Issues:
- Verify that the MySQL server is running and the credentials in
main.c
are correct.
- Verify that the MySQL server is running and the credentials in
- Compilation Errors:
- Ensure the paths to
include
andlib
folders are correct in yourgcc
command.
- Ensure the paths to
Run this simple query in main.c
to test the connection:
if (conn) {
printf("Connected to MySQL successfully!\n");
} else {
printf("MySQL connection failed: %s\n", mysql_error(conn));
}
- Add difficulty levels (increase speed as the score increases).
- Introduce obstacles for added challenge.
- Use advanced libraries like ncurses to improve graphics.
- Allow players to resume saved games.
If you have any questions or feedback, feel free to contact me:
- GitHub: Your GitHub Profile
- Email: richajaishwal2006@gmail.com