Snake game program
Snake game program
PROGRAM:
// C program to build the complete
// snake game
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void setup()
game_over = 0;
X = Length / 2;
Y = width / 2;
state1:
goto state1;
state2:
if (food_y == 0)
goto state2;
Score = 0;
void Boundary()
system("cls");
if (k == 0 || k == width - 1
|| j == 0
|| j == Length - 1) {
printf("*");
else {
else if (k == foodx
&& j == food_y)
printf("#");
else
printf(" ");
printf("\n");
printf("\n");
void input()
if (kbhit()) {
switch (getch()) {
case 'h':
flag = 1;
break;
case 'j':
flag = 2;
break;
case 'k':
flag = 3;
break;
case 'u':
flag = 4;
break;
case 'q':
game_over = 1;
break;
void Algorithm()
sleep(0.01);
switch (flag) {
case 1:
Y--;
break;
case 2:
X++;
break;
case 3:
Y++;
break;
case 4:
X--;
break;
default:
break;
game_over = 1;
state3:
if (foodx == 0)
goto state3;
state4:
if (food_y == 0)
goto state4;
Score += 10;
}
// Driver Code
void main()
// Generate boundary
setup();
// Until the game is over
while (!game_over) {
// Function Call
Boundary();
input();
Algorithm();