#include
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <time.h>
#define SCREEN_WIDTH 40
#define SCREEN_LENGTH 15
#define START_X 16
#define START_Y 2
enum direc{up, down, left, right};
typedef struct snake{
int x;
int y;
struct snake *next;
struct snake *pre;
struct snake *end;
}SNAKE;
typedef struct{
int x;
int y;
}FOOD;
void HideCursor()
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut, &cci);
cci.bVisible = FALSE;
SetConsoleCursorInfo(hOut, &cci);
}
void GotoRood(int x, int y)
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD cod;
cod.X = x;
cod.Y = y;
SetConsoleCursorPosition(hOut, cod);
}
// sleep()代替delay sleep(500)表示延遲500ms
int Random(int n)
{
srand(time(NULL));
return (rand() % n);
}
void BuildSnk(SNAKE *head)
{
SNAKE *p = head;
while (p != NULL)
{
GotoRood(p->x, p->y);
printf("#");
p = p->next;
}
}
void RemoveSnk(SNAKE *head)
{
SNAKE *p = head;
while (p != NULL)
{
GotoRood(p->x, p->y);
printf(" ");
p = p->next;
}
}
void Move(int *d) //移動方向。。。 上下左右 上:72 下:80 左:75 右:77
{
char ch;
while(_kbhit())
{
ch = getch();
if (ch == 72)
{
if ((*d == left || *d == right) && *d != down)
*d = up;
}
else if (ch == 80)
{
if ((*d == left || *d == right) && *d != up)
*d = down;
}
else if (ch == 75)
{
if ((*d == up || *d == down) && *d != right)
*d = left;
}
else if (ch == 77)
{
if ((*d == up || *d == down) && *d != left)
*d = right;
}
}
}
void IniScreen(SNAKE *head)
{
int i,j;
SNAKE *p1, *p2;
HideCursor();
GotoRood(START_X,START_Y);
for (i = 0; i < SCREEN_WIDTH + 2; i++)
printf("-");
for (j = 1; j <= SCREEN_LENGTH; j++)
{
GotoRood(START_X + SCREEN_WIDTH + 1, START_Y + j);
printf("|");
}
GotoRood(START_X, START_Y + SCREEN_LENGTH + 1);
for (i = 0; i < SCREEN_WIDTH + 2; i++)
printf("-");
for (j = 1; j <= SCREEN_LENGTH; j++)
{
GotoRood(START_X,START_Y + j);
printf("|");
}
GotoRood(START_X + 20, START_Y - 1);
printf("[email protected]計科 萬齊飛");
GotoRood(0,START_Y + 2);
printf("遊戲說明:\n\n↑設定等級\n\n按〖回車〗開始\n\n↑↓←→\n控制方向\n");
head->x = START_X + SCREEN_WIDTH / 2;
head->y = START_Y + 8;
head->pre = NULL;
p1 = head;
i = 0;
while (++i < 3)
{
p2 = p1;
p1 = (SNAKE *)malloc(sizeof(SNAKE));
p1->x = START_X + SCREEN_WIDTH / 2;
p1->y = START_Y + 8 + i;
p1->end = NULL;
p2->next = p1;
p1->pre = p2; /
}
p1->next = NULL;
head->end = p1;
BuildBody(head);
}
void CreatFood(FOOD *fd, SNAKE *snk)
{
SNAKE *p = snk;
int clash = 0;
while(1)
{
clash = 0;
fd->x = START_X + 1 + Random(SCREEN_WIDTH);
fd->y = START_Y + 1 + Random(SCREEN_LENGTH);
for (;p != NULL ;p = p->next)
if (fd->x == p->x && fd->y == p->y)
{
clash = 1;
break;
}
if (clash == 0)
break;
}
GotoRood(fd->x, fd->y);
printf("*");
}
void CollisionTest(SNAKE *head)
{
if (head->x == START_X)
head->x = START_X + SCREEN_WIDTH;
else if (head->x == START_X + SCREEN_WIDTH + 1)
head->x = START_X + 1;
else if (head->y == START_Y)
head->y = START_Y + SCREEN_LENGTH;
else if (head->y == START_Y + SCREEN_LENGTH + 1)
head->y = START_Y + 1;
}
int Eated(SNAKE *head, FOOD *fd)
{
if (head->x == fd->x && head->y == fd->y)
return 1;
return 0;
}
int GameOver(SNAKE *head)
{
SNAKE *p;
for(p = head->next; p != NULL; p = p->next)
{
if (head->x == p->x && head->y == p->y)
return 1;
}
return 0;
}
void gameing()
{
int i = 0, eat = 0, rank, scorelimit = 0, score = 0;
int direct = up;
time_t grade = 2;
FOOD *fd;
SNAKE *head, *ptemp, *pnew;
head = (SNAKE *)malloc(sizeof(SNAKE));
fd = (FOOD *)malloc(sizeof(FOOD));
GotoRood(START_X, START_Y - 1);
printf("Grade: %d", grade);
GotoRood(START_X + 10, START_Y - 1);
printf("Score: %-3d", score);
IniScreen(head);
CreatFood(fd, head);
while(rank = getch())
{
if (rank == 72)
{
grade = ++grade % 10;
if (grade == 0)
grade = 1;
GotoRood(START_X, START_Y - 1);
printf("Grade: %d", grade);
}
else if (rank == 13)
break;
}
while (1)
{
RemoveSnk(head);
ptemp = (SNAKE *)malloc(sizeof(SNAKE));
Move(&direct);
switch(direct)
{
case up: ptemp->x = head->x;
ptemp->y = head->y - 1;
break;
case down: ptemp->x = head->x;
ptemp->y = head->y + 1;
break;
case left: ptemp->x = head->x - 1;
ptemp->y = head->y;
break;
case right: ptemp->x = head->x + 1;
ptemp->y = head->y;
break;
}
if (!eat)
{
ptemp->end = head->end->pre;
head->end->pre->next = NULL;
free(head->end);
head->end = NULL;
}
else
{
ptemp->end = head -> end;
}
head->pre = ptemp;
ptemp->next = head;
ptemp->pre = NULL;
head = ptemp;
if (eat = Eated(head, fd))
{
CreatFood(fd,head);
score += grade;
scorelimit += grade;
if (scorelimit / 50 && grade <= 10)
{
GotoRood(START_X, START_Y - 1);
printf("Grade: %d", ++grade);
scorelimit = score % 50;
}
GotoRood(START_X + 10, START_Y - 1);
printf("Score: %-3d", score);
}
if (GameOver(head))
{
BuildSnk(head);
GotoRood(START_X + SCREEN_WIDTH / 2, START_Y + SCREEN_LENGTH / 2);
puts("Game Over!\n");
return;
}
CollisionTest(head);
BuildSnk(head);
Sleep((10 - grade) * 50);
}
}
int main()
{
gameing();
return 0;
}