- ✕This summary was generated using AI based on multiple online sources. To view the original source information, use the "Learn more" links.
The Snake Game is a classic arcade game where the player controls a snake to eat food while avoiding collisions with walls or its own body. Below is a simple implementation of the Snake Game in C, designed for console-based gameplay.
Code Implementation
#include <conio.h>#include <stdio.h>#include <stdlib.h>#include <windows.h>#define HEIGHT 20#define WIDTH 40int snakeTailX[100], snakeTailY[100];int snakeTailLen, gameover, key, score;int x, y, fruitx, fruity;void setup() {gameover = 0;x = WIDTH / 2;y = HEIGHT / 2;fruitx = rand() % WIDTH;fruity = rand() % HEIGHT;score = 0;}void draw() {system("cls");for (int i = 0; i < WIDTH + 2; i++) printf("-");printf("\n");for (int i = 0; i < HEIGHT; i++) {for (int j = 0; j <= WIDTH; j++) {if (j == 0 || j == WIDTH) printf("#");else if (i == y && j == x) printf("O");else if (i == fruity && j == fruitx) printf("*");else {int printTail = 0;for (int k = 0; k < snakeTailLen; k++) {if (snakeTailX[k] == j && snakeTailY[k] == i) {printf("o");printTail = 1;}}if (!printTail) printf(" ");}}printf("\n");}for (int i = 0; i < WIDTH + 2; i++) printf("-");printf("\nScore: %d\n", score);}void input() {if (_kbhit()) {switch (_getch()) {case 'a': if (key != 2) key = 1; break;case 'd': if (key != 1) key = 2; break;case 'w': if (key != 4) key = 3; break;case 's': if (key != 3) key = 4; break;case 'x': gameover = 1; break;}}}void logic() {int prevX = snakeTailX[0], prevY = snakeTailY[0];int prev2X, prev2Y;snakeTailX[0] = x;snakeTailY[0] = y;for (int i = 1; i < snakeTailLen; i++) {prev2X = snakeTailX[i];prev2Y = snakeTailY[i];snakeTailX[i] = prevX;snakeTailY[i] = prevY;prevX = prev2X;prevY = prev2Y;}switch (key) {case 1: x--; break;case 2: x++; break;case 3: y--; break;case 4: y++; break;}if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) gameover = 1;for (int i = 0; i < snakeTailLen; i++) {if (snakeTailX[i] == x && snakeTailY[i] == y) gameover = 1;}if (x == fruitx && y == fruity) {fruitx = rand() % WIDTH;fruity = rand() % HEIGHT;score += 10;snakeTailLen++;}}int main() {setup();while (!gameover) {draw();input();logic();Sleep(100);}return 0;}Copied!✕Copy Snake Game in C - GeeksforGeeks
Jul 23, 2025 · The game ends when the snake crashes, with the player's score based on how much food was collected. In this article, we will learn how to create snake game using C …
See results only from geeksforgeeks.orgSign In
The game ends when the snake crashes, with the player's score based on how much food was collected. In this article, we will learn how to create s…
Snake Game Project Using C Language - Studytonight
See more on studytonight.comThis Snake Game Mini Project in C is a basic console program with no graphics. You may play the famous "Snake Game" in this project exactly as you would anywhere else. To move the snake, use the up, down, right, and left arrows. Food is placed at various co-ordinates on the screen for the snake to consume. The snake…GitHub - ClayWarren/snake-in-c: Snake game in C
A simple console-based implementation of the classic Snake game written in pure C. This project is ideal for learning C fundamentals like loops, conditionals, arrays, manual input handling, and …
Searches you might like
Create the Classic Snake Game in C – Learn Programming
Feb 5, 2025 · The goal is to avoid hitting the walls and the snake’s own body. In this tutorial, we will learn how to create the Snake game using the C programming language.
Snake Game Program In C With Source Code
Creating a Snake Game in C - LabEx
Explore the classic snake game with this comprehensive C programming tutorial, featuring terminal-based gameplay, collision detection, and more.
- People also ask
Snake Game in C: Complete Code Tutorial - Blog - Silicon Cloud
Learn how to code the classic Snake game in C language with our complete tutorial. Includes source code and step-by-step implementation guide.
Classic Snake Game In C Programming With Source …
Nov 16, 2024 · To sum it up, this mini project on Snake game in C allows you to record the player’s name and the corresponding score obtained. File …
Code in code::blocks: Part 1: Snake Game in C
Dec 4, 2024 · To move snake in player's given direction, display moving snake and check for collision, we need snake characteristics such as snake's …
How to Create Snake Game in C Programming
Aug 27, 2024 · Learn how to create a classic Snake Game in C programming. Follow this simple step-by-step guide to build and run your own game. …