C++贪吃蛇

大一写的,有许多冗余的或不当的地方,仅供参考。

代码涉及数据结构链表知识和部分windows句柄函数。

代码
#include 
#include
#include
#include
#include
#define N 24
#define S 49
using namespace std;

void color(int a)
{

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);//通过句柄设置文本样式

}

void gotoxy(int x,int y)
{
    COORD pos;
    pos.X=x;
    pos.Y=y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);//句柄设置光标位置
}

typedef struct snake_line
{
    int x;
	int y;
	struct snake_line *next;	
}line;

int show(line *head,int a)
{
	int n;
	line *p=head->next;
	color(12);
	gotoxy(head->x,head->y);
	if(a==1)
	cout<<'^';
	else if(a==2)
	cout<<'v';
	else if(a==3)
	cout<<'<';
	else if(a==4)
	cout<<'>';
	while(p!=NULL)
	{
		if(p->x==head->x&&p->y==head->y||head->x==50||head->x==0||head->y==25||head->y==0)
		{
			system("cls");
			n=1;
		}
		color(13);
		gotoxy(p->x,p->y);
		cout<<'*';
		p=p->next;
	}
	Sleep(200);
	return n;
}

int main()
{
	while(1)
	{
	
	int n=0;
	color(154);
	int i;
	//围栏初始化
	for(i=0;i<=25;i++)
	{
		gotoxy(50,i);
		cout<<'|';
		gotoxy(0,i);
		cout<<'|';
	}
	for(i=0;i<=50;i++)
	{
		gotoxy(i,25);
		cout<<'_';
		gotoxy(i,0);
		cout<<'_';
	}
	srand((unsigned)time(NULL));//配合rand
	//初始化食物
    int food[2];
	food[0]=rand()%S+1;
    food[1]=rand()%N+1;
    color(42);
    gotoxy(food[0],food[1]);
    cout<<'O';
    //初始化蛇头
	line *head=(line *)malloc(sizeof(line));
	head->x=10;
	head->y=10;
	head->next=NULL;
	gotoxy(head->x,head->y);
	color(12);
    cout<<'^';
    
    gotoxy(52,4);
    cout<<"use numbers 1 2 3 5 to play!\t";
    cout<<"number input the game start!\n";
  	char ch;
	ch=getch();
	while(1)
	{
	int j=0;
	color(42);
	gotoxy(food[0],food[1]);
    cout<<'O';
    color(154);
	int i;
	for(i=0;i<=25;i++)
	{
		gotoxy(50,i);
		cout<<'|';
		gotoxy(0,i);
		cout<<'|';
	}
	for(i=0;i<=50;i++)
	{
		gotoxy(i,25);
		cout<<'_';
		gotoxy(i,0);
		cout<<'_';
	}
	line *p,*q;
	p=(line *)malloc(sizeof(line));
	p->x=head->x;
	p->y=head->y;
	if(kbhit())
	ch=getch();
	int a=0;
	switch(ch)
    {
    case '5':a=1;head->y--;p->next=head->next;head->next=p;break;
    case '2':a=2;head->y++;p->next=head->next;head->next=p;break;
    case '1':a=3;head->x--;p->next=head->next;head->next=p;break;
    case '3':a=4;head->x++;p->next=head->next;head->next=p;break;
    default: break;
    }q=head;
    if(head->x==food[0]&&head->y==food[1])
	{
		food[0]=rand()%S+1;
        food[1]=rand()%N+1;
        color(42);
        gotoxy(food[0],food[1]);
        cout<<'O';
        j=show(head,a);
        n+=1;
	} 
    	
    else
	{
		
		while(q->next->next!=NULL)
		{
			q=q->next;
		}
		color(0);
		gotoxy(q->next->x,q->next->y);
		cout<<' ';
		delete(q->next);
		q->next=NULL;
		j=show(head,a);
		if(j==1)
        {
        	system("cls");
        	break;
		}
	}
	color(12);
	gotoxy(52,4);
	cout<<"ok"<";
char c[3];
cin>>c;
int a=strlen(c);
while(c[0]!='y'&&c[0]!='n'||a!=1)
{
	cout<<"to input y or n charter!";
	cin>>c;
	a=strlen(c); 
 } 
 if(c[0]=='y')
 {
 	continue;
 }
 else if(c[0]=='n')
 {
 	system("cls");
 	break;
 }
}}
实现效果

C++贪吃蛇_第1张图片

你可能感兴趣的:(C++,c++,链表,开发语言)