C++实践贪吃蛇
2020-12-21
来源:汇智旅游网
C++项目实践:贪吃蛇游戏 环境:WINDOWS7 VC6.0 程序清单: //库文件MYFILE.H #ifndef _MYFILE_ #define _MYFILE_ #include
#include /*********************************C++常用类******************************/ template class STACK { private: int top; T_STACK stackspace[100]; //栈 public: if(top<-1) { top = -1; cout<<\"STACK EMPTY\"<=100-1) else { } top++; stackspace[top]=x; cout<<\"STACK OVER FLOW\"< class LIST { T_List OUTList() { } void showList() { } int i=0; for(i=point_out+1;i<=point_in;i++) cout< #define xystep (15) #define IMAGE_X (450) #define IMAGE_Y (450) //初始化UI界面 void UI_init() { } //display_rectange(坐标轴x,坐标轴y,要显示的字符串,颜色); void display_str(int x,int y,char* s,int color) { } //使用printf在指定位置输出字符串(WIN32API) void WIN32display(int x,int y,char* c) { //在x,y显示一个字符串 outtextxy(x,y,s); x = (x)*xystep; y = (y)*xystep; switch(color) { case 1:settextcolor(BLACK);break; case 2:settextcolor(LIGHTBLUE);break; case 3:settextcolor(LIGHTGREEN);break; case 4:settextcolor(LIGHTCYAN);break; case 5:settextcolor(LIGHTRED);break; case 6:settextcolor(LIGHTMAGENTA);break; case 7:settextcolor(YELLOW);break; case 8:settextcolor(WHITE);break; case 9:settextcolor(BROWN);break; case 10:settextcolor(LIGHTGRAY);break; case 11:settextcolor(RED);break; default:settextcolor(WHITE); } initgraph(IMAGE_X,IMAGE_Y); settextcolor(LIGHTGREEN); setbkcolor(BLACK); } #endif //源程序 #include #include #include #include #include #include /*****************************函数和类声明******************************/ void UI(); void Creatfood(); class SQUARE; /*****************************一些宏***********************************/ #define BUILD_COLOR 5 #define STEP 20000 //建筑物颜色 //速度 //RECT rect; COORD pos; HANDLE hOutput; pos.X = x; pos.Y = y; hOutput = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hOutput , pos); printf(\"%s\#define TIMR_REPAIR 0 #define SNAKE_COLOR 3 #define FOOD_COLOR 4 //时间量修复(系统键盘输入等待偏差) //蛇的身体颜色 //食物的颜色 /*****************************一些全局变量****************************/ static int key_vaule = 'U'; static int key_vaule_2='D'; static int SCORE=0; static SQUARE *Snake; static SQUARE *Build; static SQUARE *Food; static int END=0; static int time_step=0; /*****************************节点类定义******************************/ class SQUARE //一个小方块类 //key_vaule_2是key_vaule的一个备份 { public: }; /*****************************子函数定义*****************************/ void Display(SQUARE *X) { vIew->NEXT = X; SQUARE *vIew; vIew = new SQUARE(\" \"); //展示链表: Display(头节点) void display() { } void eraser() { } display_str(x_location,y_location,\" \ display_str(x_location,y_location,discha,color); ~SQUARE() { } //注意:链表在销毁时,不能随意对其中的节点删除NEXT指针 //因为NEXT指针指向的,恰好就是下一个节点 //而这个类参与组成链表,所以不能在析构函数里面写delete NEXT; //而其他的地方创建的节点(如vIew),凡是运行完毕跳出循环都NEXT归了NULL; //又或者本来就不能删掉(NextHead,Snake头节点,Build整条链,New,Food) //所以这个析构函数其实不必写 SQUARE(char *s,int x=-1,int y=-1,int color_in=2,int nature=0) { } x_location=x; y_location=y; color=color_in; strcpy(discha,s); NEXT=NULL; int int int x_location; y_location; color; //x坐标【列坐标】 //y坐标 char discha[5]; int nature; SQUARE *NEXT; //0:隐身 1:砖块 2:蛇 3:食物 } void Snake_Move(int direction,SQUARE *food) { int Cut=1; char score[20]={'\\0'}; //蛇的移动[带有判断能力] delete vIew; vIew = NULL; while(vIew->NEXT!=NULL) { } vIew->NEXT->display(); vIew->NEXT = vIew->NEXT->NEXT; //----------------------增加蛇的头---------------------- if(direction==1) { } if(direction==2) { } if(direction==3) { } if(direction==4) { } NextHead->x_location=Snake->x_location+1; NextHead->y_location=Snake->y_location; //右 NextHead->x_location=Snake->x_location-1; NextHead->y_location=Snake->y_location; //左 NextHead->x_location=Snake->x_location; NextHead->y_location=Snake->y_location+1; //下 NextHead->x_location=Snake->x_location; NextHead->y_location=Snake->y_location-1; //上 SQUARE *NextHead; NextHead = new SQUARE(\" \ NextHead->nature=3; strcpy(NextHead->discha,\"●\"); NextHead->color=SNAKE_COLOR; NextHead->NEXT=Snake; strcpy(Snake->discha,\"⊙\"); Snake=NextHead; //---------------------------尝试割掉蛇的尾巴[带判断]----------------- } vIew->NEXT->NEXT->eraser(); delete(vIew->NEXT->NEXT); vIew->NEXT->NEXT=NULL; if(food->x_location==Snake->x_location&&food->y_location==Snake->y_location) { Cut=0; SCORE++;Creatfood(); itoa(SCORE,score,10);display_str(27,10,score,3); } SQUARE *vIew; vIew = new SQUARE(\" \vIew->NEXT = Snake; if(Cut) { while(vIew->NEXT->NEXT->NEXT!=NULL) { } vIew->NEXT=vIew->NEXT->NEXT; //-----------是否失败?--------------------- vIew->NEXT = Build->NEXT; while(vIew->NEXT!=NULL) { if(vIew->NEXT->x_location==Snake->x_location&&vIew->NEXT->y_location==Snake->y_location) {display_str(7,12,\"END THE GAME!!\游戏失败 else vIew->NEXT = vIew->NEXT->NEXT; //咬到建筑物?失败! vIew->NEXT = Snake->NEXT; while(vIew->NEXT!=NULL) { } if(vIew->NEXT->x_location==Snake->x_location&&vIew->NEXT->y_location==Snake->y_location) {display_str(7,12,\"END THE GAME!!\游戏失败 else vIew->NEXT = vIew->NEXT->NEXT; //咬到自己?失败! } Display(Snake); end:delete vIew; } void Creatfood() { } void key_deal(SQUARE *Food) { if(kbhit()) { { key_vaule = getch(); switch(key_vaule) case 'A': //kbhit():检测当前是否有按键按下 非阻塞函数 SQUARE *vIew; vIew = new SQUARE(\" \vIew->NEXT = Snake; while(vIew->NEXT!=NULL) { } Food->display(); delete vIew; vIew = NULL; if(vIew->NEXT->x_location==Food->x_location&&vIew->NEXT->y_location==Food->y_location) Creatfood(); else vIew->NEXT = vIew->NEXT->NEXT; //防止食物出在蛇的身上 delete Food; //先释放,再分配,才能得到新的数据空间 vIew = NULL; Food = new SQUARE(\"★\Food->NEXT= NULL; case 'a':if(key_vaule_2!='D'&&key_vaule_2!='d') {key_vaule_2 = key_vaule;time_step=STEP;} else {key_vaule='U';time_step+=(TIMR_REPAIR);}break; case 'D': case 'd':if(key_vaule_2!='A'&&key_vaule_2!='a') {key_vaule_2 = key_vaule;time_step=STEP;} else {key_vaule='U';time_step+=(TIMR_REPAIR);}break; case 'S': case 's':if(key_vaule_2!='W'&&key_vaule_2!='w') {key_vaule_2 = key_vaule;time_step=STEP;} else {key_vaule='U';time_step+=(TIMR_REPAIR);}break; case 'w': case 'W':if(key_vaule_2!='S'&&key_vaule_2!='s') {key_vaule_2 = key_vaule;time_step=STEP;} else {key_vaule='U';time_step+=(TIMR_REPAIR);}break; } } else { } time_step--; key_vaule='U'; if(time_step<=0) { key_vaule=key_vaule_2; time_step=STEP; } //自动下降 case 'U':break; default:; //U:不做反应 if(key_vaule!='U') { } switch(key_vaule) case 'A': case 'a':Snake_Move(3,Food);break; case 'D': case 'd':Snake_Move(4,Food);break; case 'S': case 's':Snake_Move(2,Food);break; case 'w': case 'W':Snake_Move(1,Food);break; case 'U':break; default:; //U:不做反应 } void UI() { } /*****************************主函数执行******************************/ void main() { SQUARE *New; int i=0; UI(); srand((unsigned)time(NULL)); display_str(24,20,\"W:向上\display_str(24,21,\"S:向下\display_str(24,22,\"A:向左\display_str(24,23,\"D:向右\ //用户问好界面 UI_init(); display_str(23,10,\"SCORE\display_str(27,10,\"0 \display_str(1,0,\"YWQ:贪吃蛇 平台WINDOWS_7 VC6.0 \display_str(1,1,\"欢迎各路大神讨论C/C++的学习 \//----------------游戏前的建筑工事------------------ //扩建链表 for(i=1;i<21;i++) { New = new SQUARE(\"■\//建立链表1:建筑物 SQUARE *Current; Build = new SQUARE(\" \Current = new SQUARE(\" \Current->NEXT Build->NEXT = = Build; NULL; //Current指向最后一个节点 //Build:头结点 New->NEXT = NULL; Current->NEXT->NEXT=New; Current->NEXT=New; } for(i=1;i<21;i++) { New = new SQUARE(\"■\New->NEXT = NULL; Current->NEXT->NEXT=New; Current->NEXT=New; } for(i=0;i<22;i++) { New = new SQUARE(\"■\New->NEXT = NULL; Current->NEXT->NEXT=New; Current->NEXT=New; } for(i=0;i<22;i++) { New = new SQUARE(\"■\New->NEXT = NULL; Current->NEXT->NEXT=New; Current->NEXT=New; } //建立链表2:蛇 START:; Snake = new SQUARE(\"⊙\Snake->NEXT= NULL; //扩建链表 蛇 for(i=1;i<3;i++) { New = new SQUARE(\"⊙\New->NEXT = Snake; Snake = New; } strcpy(Snake->discha,\"●\"); //创建一个食物节点 Creatfood(); Display(Build); Display(Snake); Display(Food); while(1) { if(END==1) { } Sleep(2500); break; } key_deal(Food); //-----------------------重新开始游戏,销毁之前的数据以及二次初始化------------------- key_vaule = 'U'; while(vIew->NEXT!=NULL) { } display_str(Food->x_location,Food->y_location,\" \display_str(7,12,\" \END=0; SCORE=0; display_str(27,10,\"0 \ Temp->NEXT= vIew->NEXT; vIew->NEXT = vIew->NEXT->NEXT; display_str(Temp->NEXT->x_location,Temp->NEXT->y_location,\" \delete (Temp->NEXT); //注意:销毁链表,但保存头节点 SQUARE *vIew,*Temp; Temp = new SQUARE(\" \Temp->NEXT = NULL; vIew = new SQUARE(\" \vIew->NEXT = Snake->NEXT; key_vaule_2= 'D'; } /*****************************此源代码结束******************************/ getch(); getch(); goto START; delete Temp; Temp = NULL; delete vIew; vIew = NULL;