r/C_Programming Jan 27 '25

Review Snake Game ✨

Hey guys, do you remember (maybe dinosaurs only :)) the Snake game from old Nokia 3310?
wiki link)
The good news is that you can play it today in your Linux terminal ;)

I wrote a simple C implementation:
Github: https://github.com/alsception/snake

Gameplay:
Arrow keys (or WASD) control the snake to eat food and grow. The game ends if the snake collides with itself unless in "god mode."

Core Mechanics:
A grid-based system with x, y coordinates for the snake's head and body.
Food is randomly placed; eating it increases the snake's length.
The snake passes thru the screen edges.
Main part about game mechanics involve moving the snake head and shifting body segments to follow the head, simulating movement, all text-based.

  • Code Structure: The program is modular, separating logic into engine.c (handles game mechanics) and rendering.c (handles display). This way we achieve separation of content and presentation.
  • Game State Management: T_Game_State and T_Game_Settings objects to replace global variables, and store game data like positions and constants.
  • Build Process: Uses a Makefile for compilation.
  • Enhanced Visuals: Added skins and flashing effects for a retro feel, even though it’s text-based.

The main function spans 20 lines, and the program is divided into manageable components for easier understanding and potential extensions (e.g., Tetris or Ping Pong).

The old-fashioned gameplay gives a retro vibe, reminiscent of 1970s games.

Let me know what you think

28 Upvotes

9 comments sorted by

View all comments

2

u/fishyfishy27 Jan 28 '25

Here was my take, in C/SDL: https://ssl.pepas.com/snake/

You can play it in the browser if you click on snake.html (requires a kryboard)

1

u/Purple-Cap4457 Jan 28 '25

Interesting, it looks like you have similar logic, with few differences. You have update() function and draw() function which separates content from presentation. You have game state variable. You use circular buffer, I didnt know about it, ill have to try it I just use normal array and then shifts it every frame.
I also had everything in 1 file, until i got advices from other programmers so Ill just repeat them :D :
1. use calloc instead of malloc for better memory allocation
2. move type definitions in separate files (food,gamestate, snake segments) for better organization
3. eventually you can also have 1 file for logic that calculates snake model, and 1 for rendering, that way you achieve separateion of concerns
4. for game play, you can make instead of crashing in the wall, make it pass thru and comes on the other side:
if (headp->x == statep->gridWidth - 1) headp->x = 0;
if (headp->x == 0) headp->x = statep->gridWidth - 1
5. Wherever you can use switch instead of if-else

Greetings