Skip to main content

Project in C Snake Game

 

This Project in C Snake Game is a simple console application without graphics. In this project, you can play the popular “Snake Game” just like you played it elsewhere. You have to use the up, down, right or left arrows to move the snake.

Foods are provided at the several co-ordinates of the screen for the snake to eat. Every time the snake eats the food, its length will by increased by one element along with the score.

 

Steps to create this game:

  • There will be four user-defined functions.
  • Build a boundary within which the game will be played.
  • The fruits are generated randomly.
  • Then increase the score whenever the snake eats a fruit.

The user-defined functions created in this program are given below:

  • Draw(): This function creates the boundary in which the game will be played.
  • Setup(): This function will set the position of the fruit within the boundary.
  • Input(): This function will take the input from the keyboard.
  • Logic(): This function will set the movement of the snake.

Built-in functions used:

  • kbhit(): This function in C is used to determine if a key has been pressed or not. To use this function in a program include the header file conio.h. If a key has been pressed, then it returns a non-zero value otherwise it returns zero.
  • rand(): The rand() function is declared in stdlib.h. It returns a random integer value every time it is called.

// C program to build the complete
// snake game
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int i, j, height = 20, width = 20;
int gameover, score;
int x, y, fruitx, fruity, flag;

// Function to generate the fruit
// within the boundary
void setup()
{
    gameover = 0;

    // Stores height and width
    x = height / 2;
    y = width / 2;
label1:
    fruitx = rand() % 20;
    if (fruitx == 0)
        goto label1;
label2:
    fruity = rand() % 20;
    if (fruity == 0)
        goto label2;
    score = 0;
}

// Function to draw the boundaries
void draw()
{
    system("cls");
    for (i = 0; i < height; i++) {
        for (j = 0; j < width; j++) {
            if (i == 0 || i == width - 1
                || j == 0
                || j == height - 1) {
                printf("#");
            }
            else {
                if (i == x && j == y)
                    printf("0");
                else if (i == fruitx
                        && j == fruity)
                    printf("*");
                else
                    printf(" ");
            }
        }
        printf("\n");
    }

    // Print the score after the
    // game ends
    printf("score = %d", score);
    printf("\n");
    printf("press X to quit the game");
}

// Function to take the input
void input()
{
    if (kbhit()) {
        switch (getch()) {
        case 'a':
            flag = 1;
            break;
        case 's':
            flag = 2;
            break;
        case 'd':
            flag = 3;
            break;
        case 'w':
            flag = 4;
            break;
        case 'x':
            gameover = 1;
            break;
        }
    }
}

// Function for the logic behind
// each movement
void logic()
{
    sleep(0.01);
    switch (flag) {
    case 1:
        y--;
        break;
    case 2:
        x++;
        break;
    case 3:
        y++;
        break;
    case 4:
        x--;
        break;
    default:
        break;
    }

    // If the game is over
    if (x < 0 || x > height
        || y < 0 || y > width)
        gameover = 1;

    // If snake reaches the fruit
    // then update the score
    if (x == fruitx && y == fruity) {
    label3:
        fruitx = rand() % 20;
        if (fruitx == 0)
            goto label3;

    // After eating the above fruit
    // generate new fruit
    label4:
        fruity = rand() % 20;
        if (fruity == 0)
            goto label4;
        score += 10;
    }
}

// Driver Code
void main()
{
    int m, n;

    // Generate boundary
    setup();

    // Until the game is over
    while (!gameover) {

        // Function Call
        draw();
        input();
        logic();
    }
}

Comments

Popular posts from this blog

Web Front-End Development

Web Front-End Development Web pages can be developed using languages  HTML,CSS,JAVASCRIPT,BOOTSTRAP. html( HyperText Markup Language ) HTML is the standard markup language for Web pages HTML  elements  are the building blocks of HTML pages HTML elements are represented by  <> tags HTML adds content to webpage. ex:- paragraphs,tables,images,buttons etc.. CSS(Cascading Style Sheets) CSS describes how HTML elements are to be displayed CSS adds styles to the webpage Ex:- colors,alignments,font-size,background-image etc.. Javascript JavaScript is the  Programming Language  for the Web. JavaScript can update and change both  HTML  and  CSS. JavaScript can  calculate ,  manipulate , and  validate  data. Bootstrap CSS Framework for developing responsive and mobile-first websites. Bootstrap includes HTML and CSS based design templates for typography, forms, buttons, tables, navigation, modals, im...

C Programming Introduction

  hi friends! Before we depth into c program let us understand, what is programming Programming is the process of creating a set of instructions that tell a computer how to perform a task.Programming can be done using a variety of computer programming languages, such as JavaScript, Python, and C++, PHP, react native, etc. ex:- websites are developed using php mobile application can be developed using reactnative etc Depending on application type, we choose different programming languages to develop it. Let us see an example of c programming to print "hello world" on screen program #include <stdio.h> int main() { /* Our first simple C basic program */ printf("Hello World! "); return 0; } output Hello World! run the code on online compiler https://www.onlinegdb.com/online_c_compiler or install c compiler on system and run c programs. C Basic commands Explanation #include <stdio.h> This is a preprocessor command that include...

Frontend Vs Backend Coding

Frontend Vs Backend Coding Let us understand how this development is done.Development is divided into two parts frontend development and backend development. The front end, also called “ client-side ” programming, is what happens in the browser. It’s everything the user sees and interacts with. The back end, also called “ server-side ” programming, happens on the server and the database. It’s the machinery that works behind the scenes. developing a website Front-end Development Part of a website that the user interacts. Everything that users experience directly: text colors and styles, images, graphs and tables, buttons, colors, and navigation menu. HTML, CSS, and Javascript are the languages used for Front End development. HTML ( HyperText Markup Language) adds content on the web page with the help of HTML elements CSS  (Cascading Style Sheets)  adds styles to HTML elements Javascript adds behavior in the webpage   Ex: calculate, manipulate, and validate d...