// boundary using draw()
#include <stdio.h>
#include <stdlib.h>
int i, j, height = 17;
int width = 20, gameover, score;
// Function to draw a boundary
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 {
printf(" ");
}
}
printf("\n");
}
}
// Driver Code
int main() {
// Function Call
draw();
return 0;
}
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;
}.
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;
}
}
}
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;
}
}
void main() {
int m, n;
// Generate boundary
setup();
// Until the game is over
while (!gameover) {
// Function Call
draw();
input();
logic();
}
}