#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#include<time.h>

#define WIDTH 20
#define HEIGHT 20

typedef struct{
    int x,y;
}point;

point snake[WIDTH * HEIGHT];
int snake_length;
point food;
char direction;
int game_over;

void initialize_game(){
    snake_length =1;
    snake[0].x=WIDTH/2;
    snake[0].y=HEIGHT/2;
    direction = ' ';
    game_over =0;

    food.x =rand() % WIDTH;
    food.y =rand() % HEIGHT;
}
void gotoXY(int x,int y){
    COORD coord;
    coord.X =x;
    coord.Y =y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}

void draw(){
    gotoXY(0,0);

    for(int i =0;i<WIDTH+2;i++)
        printf("#");
     printf("\n");
   for (int y=0;y<HEIGHT;y++){
       for(int x=0;x<WIDTH;x++){
           if(x==0)
               printf("#");
           int is_snake =0;
           for(int k=0;k<snake_length;k++){
               if(snake[k].x==x && snake[k].y==y){
                   printf("o");
                   is_snake=1;
                   break;
               }
           }
           if(x==food.x && y==food.y){
               printf("F");
           }else if (!is_snake){
               printf(" ");
           }
           if(x == WIDTH -1)
           printf("#");
       	}
           printf("\n");
           }
       for(int i=0;i<WIDTH+2;i++)
           printf("#");
       printf("\n");
       }
    void input(){
        if(_kbhit()){
            switch (_getch()){
                case 'w':
                if(direction !='s')direction ='w';
                break;
                case 'a':
                if(direction!='d')direction ='a';
                break;
                case 's':
                if(direction!='w')direction='s';
                break;
                case 'd':
                if(direction!='a')direction ='d';
            }
        }
    }
    void logic(){
        point prev =snake[0];
        point prev2;

        if(direction==' '){
            return;
        }
        switch(direction){
            case 'w':snake[0].y--;break;
            case 'a':snake[0].x--;break;
            case 's':snake[0].y++;break;
            case 'd':snake[0].x++;break;
        }
        for(int i=1;i<snake_length;i++){
            prev2=snake[i];
            snake[i]=prev;
            prev=prev2;
        }
            if(snake[0].x<0||snake[0].x>=WIDTH || snake[0].y<0 || snake[0].y>=HEIGHT){
                game_over=1;
            }
        for(int i=1;i<snake_length;i++){
            if(snake[i].x==snake[0].x && snake[i].y==snake[0].y){
                game_over=1;
            }
        }
        if(snake[0].x==food.x && snake[0].y==food.y){
            snake_length++;
            food.x=rand() % WIDTH;
            food.y=rand() % HEIGHT;
        }
    }
    int main(){
        srand(time(0));
        initialize_game();
        const int FPS =10;
        const int frameDelay =1000/FPS;

        while (!game_over){
            DWORD frameStart = GetTickCount();
            draw();
            input();
            logic();

            DWORD frameTime =GetTickCount()-frameStart;

            if(frameDelay>frameTime){
                Sleep(frameDelay-frameTime);
            }
        }

    printf("Game Over! Score: %d\n",snake_length-1);
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: