About 34,300,000 results
Open links in new tab
  1. 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 40

    int 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!
    Feedback
  2. Snake Game Project Using C Language - Studytonight

    This 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…
    See more on studytonight.com
  3. 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 …

  4. 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.

  5. Snake Game Program In C With Source Code

    Oct 11, 2022 · In this snake game in c without graphics also includes a downloadable simple snake game in c for free, just find the downloadable source code below and click to start …

    • Reviews: 3
    • Total Time: 5 mins
  6. 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.

  7. People also ask
  8. 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.

  9. 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 …

  10. 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 …

  11. 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. …