Code:
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#ifdef linux
#define clr() system("clear");
#elif _WIN32
#define clr() system("cls");
#endif
#define n 20
#define m 40
#define ESC 27
#define UP 72
#define LEFT 75
#define RIGHT 77
#define DOWN 80
#define PAUSE 112
#define ENTER 13
#define FOOD '$'
#define TRUE 1
#define FALSE 0
int hi, hj, ti, tj, direction, score=0, speed=6, exist, temp, x, flag, p, snd=1, len=3;
void gotoxy(int y, int x)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void sound()
{
Beep (2000,10);
}
int HIDDEN_READONLY (const char *fname)
{
DWORD attr;
attr = GetFileAttributes(fname);
if (attr == INVALID_FILE_ATTRIBUTES)
return 0;
attr ^= FILE_ATTRIBUTE_HIDDEN;
attr ^= FILE_ATTRIBUTE_READONLY;
if (!SetFileAttributes(fname, attr))
return 0;
return 1;
}
void read()
{
FILE *in;
char c, tmp[9], i;
clr();
HIDDEN_READONLY("c:\\Highscore.txt");
in = fopen ("c:\\Highscore.txt","r+");
if (in == NULL)
{
printf("No Highscores");
getchar();
clr();
main();
}
gotoxy(10,30);
printf("Name: ");
for (c = fgetc(in);(c > 64 && c!= EOF);c = fgetc(in))
printf("%c",c);
if (c == 48)
printf("NONE");
gotoxy(11,30);
printf("Score: ");
tmp[i=0]=i;
for (c = fgetc(in);c != EOF;c = fgetc(in))
{
tmp[i] = c;
i++;
}
if (tmp[0] != 0)
{
tmp[i] = PAUSE;
for (i=0; tmp[i] != PAUSE ;i++)
printf("%c",tmp[i]);
}
else printf("NONE");
HIDDEN_READONLY("c:\\Highscore.txt");
}
int read_write()
{
FILE *in, *out;
char str[9], name[10];
int max;
HIDDEN_READONLY("c:\\Highscore.txt");
in = fopen ("c:\\Highscore.txt","r+");
if (in == NULL)
{
out = fopen("c:\\Highscore.txt","w+");
fputc ('0',out);
fclose (out);
in = fopen ("c:\\Highscore.txt","r+");
}
fgets (name,10,in);
fgets (str,9,in);
max = strtol(str, NULL, 10);
if (score > max)
{
gotoxy(10,30);
printf ("New Highscore!");
rewind (in);
gotoxy(11,30);
printf("Enter your name: ");
gets(name);
fprintf(in,"%s\n",name);
fprintf (in,"%d",score);
}
fclose(in);
HIDDEN_READONLY("c:\\Highscore.txt");
}
void print()
{
gotoxy(10,45);
printf("Your Score: %d",score);
}
void draw (char board[n][m])
{
int i,j;
//clr();
gotoxy(1,1);
for (i=0;i<n;i++)
{
for (j=0;j<m;j++)
{
if (board[i][j] == UP || board[i][j] == DOWN || board[i][j] == LEFT || board[i][j] == RIGHT)
printf ("o");
else if (board[i][j] == 0)
printf (" ");
else printf("%c",board[i][j]);
}
gotoxy(i+1,m+1);
printf ("#\n#");
}
}
void cpu(char board[n][m])
{
int i,j;
for (i=0;i<n;i++)
{
for (j=0;j<m;j++)
if (board[i][j] == FOOD)
break;
if (board[i][j] == FOOD)
break;
}
if (hi > i && direction != DOWN && (board[hi-1][hj] == 0 || board[hi-1][hj] == FOOD))
x = UP;
else if (hj > j && direction != RIGHT && (board[hi][hj-1] == 0 || board[hi][hj-1] == FOOD))
x = LEFT;
else if (hi < i && direction != UP && (board[hi+1][hj] == 0 || board[hi+1][hj] == FOOD))
x = DOWN;
else if (hj < j && direction != LEFT && (board[hi][hj+1] == 0 || board[hi][hj+1] == FOOD))
x = RIGHT;
else
{
if (direction != DOWN)
x = UP;
else if (direction != LEFT)
x = RIGHT;
else if (direction != UP)
x = DOWN;
else if (direction != RIGHT)
x = LEFT;
}
gotoxy(15,60);
switch (x)
{
case UP: printf("%c",24);
break;
case DOWN: printf("%c",25);
break;
case LEFT: printf("%c",27);
break;
case RIGHT: printf("%c",26);
break;
}
}
void create_food (char board[n][m])
{
int i, j;
srand ((unsigned)time(NULL));
TRY:
i = rand() % (n-3) + 2;
j = rand() % (m-3) + 2;
if (board[i][j] != 0)
goto TRY;
board[i][j] = FOOD;
}
int crash (char board[n][m], int direction, int hi, int hj)
{
switch (direction)
{
case UP:
{
if (hi == 0 || board[hi-1][hj] == UP || board[hi-1][hj] == DOWN || board[hi-1][hj] == LEFT || board[hi-1][hj] == RIGHT)
return 1;
}
break;
case DOWN:
{
if (hi == n-1 || board[hi+1][hj] == UP || board[hi+1][hj] == DOWN || board[hi+1][hj] == LEFT || board[hi+1][hj] == RIGHT)
return 1;
}
break;
case LEFT:
{
if (hj == 0 || board[hi][hj-1] == UP || board[hi][hj-1] == DOWN || board[hi][hj-1] == LEFT || board[hi][hj-1] == RIGHT)
return 1;
}
break;
case RIGHT:
{
if (hj == m-1 || board[hi][hj+1] == UP || board[hi][hj+1] == DOWN || board[hi][hj+1] == LEFT || board[hi][hj+1] == RIGHT)
return 1;
}
break;
}
return 0;
}
void input()
{
x = getch();
if (x == 224)
x = temp = getch();
else if (x == PAUSE)
{
gotoxy(11,45);
system("pause");
gotoxy(11,45);
printf("Press P to Pause ");
x = temp;
}
}
void move(char board[n][m], int x)
{
board[hi][hj] = direction;
CHEAT:
switch (x)
{
case UP:
{
if (direction == DOWN)
{
x = DOWN;
goto CHEAT;
}
if (hi > 0)
{
if (board[hi-1][hj] == FOOD)
exist = FALSE;
else if (board[hi-1][hj] == 0)
direction = UP;
hi--;
}
}
break;
case DOWN:
{
if (direction == UP)
{
x = UP;
goto CHEAT;
}
if (hi < n)
{
if (board[hi+1][hj] == FOOD)
exist = FALSE;
else if (board[hi+1][hj] == 0)
direction = DOWN;
hi++;
}
}
break;
case LEFT:
{
if (direction == RIGHT)
{
x = RIGHT;
goto CHEAT;
}
if (hj > 0)
{
if (board[hi][hj-1] == FOOD)
exist = FALSE;
else if (board[hi][hj-1] == 0)
direction = LEFT;
hj--;
}
}
break;
case RIGHT:
{
if (direction == LEFT)
{
x = LEFT;
goto CHEAT;
}
if (hj < m)
{
if (board[hi][hj+1] == FOOD)
exist = FALSE;
else if (board[hi][hj+1] == 0)
direction = RIGHT;
hj++;
}
}
break;
case ESC:
{
clr();
score = 0;
len = 3;
main();
}
}
board[hi][hj] = '@';
if (exist)
{
board[ti][tj] = 0;
if (board[ti+1][tj] == DOWN && ti < n)
ti++;
else if (board[ti-1][tj] == UP && ti > 0)
ti--;
else if (board[ti][tj+1] == RIGHT && tj < m)
tj++;
else if (board[ti][tj-1] == LEFT && tj > 0)
tj--;
}
else
{
if (snd)
sound();
len++;
score += len*speed;
print();
create_food(board);
exist = TRUE;
}
}
void init (char board[n][m])
{
int i,j;
for (i=0;i<n;i++)
for (j=0;j<m;j++)
board[i][j] = 0;
board[hi = n/2][hj = m/2] = '@';
board[n/2][m/2+1] = LEFT;
board[ti = n/2][tj = m/2+2] = LEFT;
create_food (board);
exist = TRUE;
direction = LEFT;
x = LEFT;
gotoxy(0,0);
for (i=0;i<=n;i++)
printf("##");
printf ("\n#");
for (i=0;i<n;i++)
{
for (j=0;j<m;j++)
{
if (board[i][j] == UP || board[i][j] == DOWN || board[i][j] == LEFT || board[i][j] == RIGHT)
printf ("o");
else if (board[i][j] == 0)
printf (" ");
else printf("%c",board[i][j]);
}
gotoxy(i+1,m+1);
printf ("#\n#");
}
gotoxy(n+1,0);
for (i=0;i<=n;i++)
printf("##");
print();
gotoxy(11,45);
printf("Press P to Pause");
gotoxy(12,45);
printf("Press ESC to Return to Menu");
}
void play(int player)
{
char board[n][m], crs = 0;
init(board);
while (!crash(board,direction,hi,hj))
{
if (player == 0)
cpu(board);
if (kbhit())
if (player == 0)
{
player = 1;
clr();
main();
}
else input();
move (board,x);
draw(board);
sleep (200 - ((speed-1) * 25));
}
clr();
if (player == TRUE)
{
gotoxy(10,30);
printf("You have died!");
sleep(1000);
read_write();
sleep(1000);
clr();
}
score = 0;
len = 3;
main();
}
void demo()
{
int dm = 0;
while (!kbhit())
{
sleep(10);
dm++;
if (dm > 700)
play(0);
}
}
int draw_menu()
{
if (flag)
{
gotoxy(9,30);
printf("Start Game");
gotoxy(10,30);
printf("Options ");
gotoxy(11,30);
printf("Credits ");
gotoxy(12,30);
printf("Exit");
}
else
{
gotoxy(9,30);
if (snd)
printf("Sound: ON ");
else printf("Sound: OFF ");
gotoxy(10,30);
printf("Speed: %d ", speed);
gotoxy(11,30);
printf("Highscore ");
gotoxy(12,30);
printf("Back");
}
return;
}
void menu()
{
demo();
input();
while (x != ENTER)
{
CHOOSE:
switch (x)
{
case UP:
{
gotoxy(p,29);
printf(" ");
if (p > 9)
p--;
gotoxy(p,29);
printf("%c",26);
}
break;
case DOWN:
{
gotoxy(p,29);
printf(" ");
if (p < 12)
p++;
gotoxy(p,29);
printf("%c",26);
}
break;
default:
{
input();
goto CHOOSE;
}
}
demo();
input();
if (snd)
sound();
}
switch (p)
{
case 9:
{
if (flag)
play(1);
if (snd)
snd = 0;
else snd = 1;
draw_menu();
menu();
}
break;
case 10:
{
if (flag)
{
flag = FALSE;
gotoxy(9,29);
printf(" ");
draw_menu();
x=0;
menu();
}
input();
while (x != ENTER)
{
switch (x)
{
case LEFT:
{
if (speed > 1)
speed--;
}
break;
case RIGHT:
{
if (speed < 9)
speed++;
}
break;
}
gotoxy(10,30);
printf("Speed: %d ", speed);
input();
if (snd)
sound();
}
menu();
}
break;
case 11:
{
clr();
if (flag)
{
gotoxy(11,20);
printf("All Rights Reserved To Gavra");
getch();
gotoxy(11,20);
printf(" ");
gotoxy(11,29);
printf("%c",26);
draw_menu();
menu();
}
else
{
x = 0;
read();
gotoxy(12,30);
printf("Reset Highscore [ENTER]");
RESET:
if (kbhit())
x = getch();
else demo();
if (x == ENTER)
{
HIDDEN_READONLY("c:\\Highscore.txt");
FILE *out;
out = fopen("c:\\Highscore.txt","w");
fputc ('0',out);
fclose (out);
HIDDEN_READONLY("c:\\Highscore.txt");
read();
goto RESET;
}
else if (x == ESC)
{clr();}
if (x != ESC && x != ENTER)
goto RESET;
clr();
draw_menu();
gotoxy(11,29);
printf("%c",26);
menu();
}
}
break;
case 12:
{
if (flag)
exit(0);
else
{
flag = TRUE;
draw_menu();
menu();
}
}
break;
}
}
int main()
{
flag = TRUE;
p = 9;
gotoxy(9,29);
printf("%c",26);
draw_menu();
demo();
menu();
getchar();
return 0;
}