#include <stdio.h>
#include <windows.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
#define WALLX 60
#define WALLY 20
//¶¨ÒåÉßµÄÉíÌå
typedef struct snake{
int x;
int y;
struct snake *next;
}body, *Pbody;
void show_wall(int x, int y);
void show_target(int *x, int *y,Pbody *head);
void gotoxy(int x, int y);
void move_snake(Pbody *head, char temp,char temp1);
void add_snake(Pbody *N, char temp);
void error_judge(Pbody *N, char temp);
int x, y;
int main(void)
{
//¸Õ¿ªÊ¼´´½¨2¸öÉíÌåµÄÉß,²¢°ÑËûÃǵÄ×ø±êÉèÖõ½×óÉϽÇ,²¢ÏÔʾËûÃÇ
body *head = (body*)malloc(sizeof(body)); //Í·Ö¸Õë
body *new1 = (body*)malloc(sizeof(body));
new1->next = NULL;
head->next = new1;
head->x = 2, head->y = new1->y = 1, new1->x = 1;
gotoxy(new1->x, new1->y);
putchar('@');
gotoxy(head->x, head->y);
putchar('@');
show_target(&x, &y,&head);
char temp = 'd',temp1='d';
show_wall(WALLX, WALLY);
gotoxy(0,22);
printf("W S A D ·Ö±ð¿ØÖÆÉÏ¡¢Ï¡¢×ó¡¢ÓÒ,°´ÈÎÒâ¼ü¿ªÊ¼,¹ý³ÌÖÐÈÎÒâ¼üÔÝÍ£");
gotoxy(0,23);
printf("̰³ÔÉßBeta 1.0 BUG½Ï¶à£¬ÍæÍ滹ÐС£");
getch();
while (1)
{
if (kbhit()) //kbhitº¯ÊýÅжÏÊÇ·ñ´æÔÚ¼üÅÌÊäÈë,ÈôûÓÐÔòÔ˶¯Éß
{
temp1=temp;
temp = getch();
}
else
{
move_snake(&head, temp,temp1);
error_judge(&head, temp);
if (head->x == x&&head->y == y)
{
show_target(&x, &y,&head);
body *new2 = (body*)malloc(sizeof(body)); //³ÔÁËÒ»¸öÖ®ºóÉßÉí±ä³¤£¬È»ºóÔÚmovº¯ÊýÀ¾Í¿ÉÒÔÔö³¤ÉßÉí
new1 = head;
while (new1->next != NULL)
new1 = new1->next;
new1->next = new2;
new2->next = NULL;
}
}
}
}
void gotoxy(int x, int y)
{
COORD coordinate; //COORDÊÇwinapiÖж¨ÒåµÄÒ»ÖֽṹÌ壬±íʾһ¸ö×Ö·ûÔÚ¿ØÖÆÌ¨ÆÁÄ»ÉϵÄ×ø±ê
coordinate.X = x; //°Ñ»ñµÃµÄ×ø±ê´«Èë¸øCOORD½á¹¹Àï
coordinate.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coordinate);
//GetStdHandle(STD_OUTPUT_HANDLE)»ñµÃµÄÊÇÕâ¸ö¿ØÖÆÌ¨µÄ¾ä±ú
}
/***********************************/
/*º¯Êý¹¦ÄÜ£ºÏÔʾǽ±Ú
/*½éÉÜ£ºÓÃÑ»·´òÓ¡
/***********************************/
void show_wall(int x, int y)
{
int i, j;
for (i = 0; i<x; i++)
{
gotoxy(i, 0);
putchar('+');
gotoxy(i, y);
putchar('+');
}
for (i = 0; i <= y; i++)
{
gotoxy(0, i);
putchar('+');
gotoxy(x, i);
putchar('+');
}
}
/***********************************/
/*º¯Êý¹¦ÄÜ£ºÏÔʾʳÎï
/*½éÉÜ£ºÊ¹ÓÃËæ»úÊýrand
/***********************************/
void show_target(int *x, int *y,Pbody *head)
{
body *current = (*head);
*x = rand() % (WALLX - 1) + 1; //Ëæ»úÉú³ÉʳÎï×ø±ê
*y = rand() % (WALLY - 1) + 1;
//È¥³ýʳÎïºÍÉßÉíÖØºÏµÄµØ·½
while(current->next!=NULL)
{
if((*x)==current->x&&(*y)==current->y)
{
*x = rand() % (WALLX - 1) + 1;
*y = rand() % (WALLY - 1) + 1;
}
current=current->next;
}
gotoxy(*x, *y);
putchar('*');
}
/***********************************/
/*º¯Êý¹¦ÄÜ£ºÒƶ¯Éß
/*½éÉÜ£ºÍ¨¹ýÌí¼ÓÉßͷɾ³ýÉßβÒÔʵÏÖÉßµÄÒÆ¶¯
/***********************************/
void move_snake(Pbody *head, char temp,char temp1)
{
Sleep(60);
body *current = (Pbody)malloc(sizeof(body)); //Öмä±äÁ¿£¬ÓÃÀ´±éÀú´òÓ¡
system("cls");
add_snake(head, temp);
current = (*head);
while (current != NULL)
{
gotoxy(current->x, current->y);
current = current->next;
putchar('@');
}
gotoxy(0,22);
printf("W S A D ·Ö±ð¿ØÖÆÉÏ¡¢Ï¡¢×ó¡¢ÓÒ,°´ÈÎÒâ¼ü¿ªÊ¼,¹ý³ÌÖÐÈÎÒâ¼üÔÝÍ£");
gotoxy(0,23);
printf("̰³ÔÉßBeta 1.0 BUG½Ï¶à£¬ÍæÍ滹ÐС£");
show_wall(WALLX, WALLY);
gotoxy(x, y);
putchar('*');
}
void add_snake(Pbody *head, char temp)
{
body *new1 = (Pbody)malloc(sizeof(body));
switch (temp)
{
case 'd':
new1->x = (*head)->x + 1;
new1->y = (*head)->y;
new1->next = (*head);
(*head) = new1;
while (new1->next->next != NULL)
new1 = new1->next;
new1->next = NULL;
break;
case 'w':
new1->x = (*head)->x;
new1->y = (*head)->y - 1;
new1->next = (*head);
(*head) = new1;
while (new1->next->next != NULL)
new1 = new1->next;
new1->next = NULL;
break;
case 's':
new1->x = (*head)->x;
new1->y = (*head)->y + 1;
new1->next = (*head);
(*head) = new1;
while (new1->next->next != NULL)
new1 = new1->next;
new1->next = NULL;
break;
case 'a':
new1->x = (*head)->x - 1;
new1->y = (*head)->y;
new1->next = (*head);
(*head) = new1;
while (new1->next->next != NULL)
new1 = new1->next;
new1->next = NULL;
break;
}
}
void error_judge(Pbody *N, char temp)
{
body *current = (*N);
int x,y;
x = current->x;
y = current->y;
if((*N)->x>=WALLX||(*N)->y>=WALLY||(*N)->x<=0||(*N)->y<=0)
{
system("cls");
gotoxy(15,15);
printf("ͬѧÄãÊäÁË");
Sleep(500000);
}
else
{
while(current->next!=NULL)
{
current=current->next;
if(current->x==x&¤t->y==y)
{
system("cls");
gotoxy(15,15);
printf("ͬѧÄãÊäÁË");
Sleep(500000);
}
}
if(current->x==x&¤t->y==y)
{
system("cls");
gotoxy(15,15);
printf("ͬѧÄãÊäÁË");
Sleep(500000);
}
}
}