C++控制台循环链表实现贪吃蛇
本文实例为大家分享了C++控制台循环链表实现贪吃蛇的具体代码,供大家参考,具体内容如下
-stdafx.h为了简化程序定义一些宏和全局变量
#ifndef__STDAFX_H__ #define__STDAFX_H__ //============上下左右============= constintUP=72; constintDOWN=80; constintLEFT=75; constintRIGHT=77; //==============宽高=============== #defineHEIGHT20 #defineWIDTH39 //==============输出=============== #definecout_foodstd::cout<<"*" #definecout_snakestd::cout<<"■" #definecout_spacestd::cout<<"" #definecout_snake_xy(x,y)SnakeUI::gotoXY(x,y);cout_snake #definecout_food_xy(x,y)SnakeUI::gotoXY(x,y);cout_food #definecout_space_xy(x,y)SnakeUI::gotoXY(x,y);cout_space //=============结束?============== #defineOVERfalse #defineRUNtrue #endif
-SnakeUI.h
主要是初始化UI,初始化蛇,还有生产食物,判断食物和蛇有没有相撞,还有对界面的一些操作
#ifndef__SNAKE_UI_H__ #define__SNAKE_UI_H__ #include#include #include"Snake.h" structFood{ intx; inty; }; classSnakeUI{ public: staticvoidinitUI(); staticvoidinitSnake(); staticvoidgotoXY(intx,inty); staticvoidproductFood(Snake&snake); staticboolmeetWithFood(intx,inty); private: staticFoodfood; }; #endif
-SnakeUI.cpp
#include"stdafx.h" #include"SnakeUI.h" #includeusingnamespacestd; FoodSnakeUI::food={0,0}; //initUI voidSnakeUI::initUI(){ cout<<"┏"; for(inti=1;i 检查身体和尾部 for(SnakeNode*sn=snake.last;sn!=snake.first;sn=sn->prev){ if(sn->x==x&&sn->y==y){ productOK=false; break; } } //不和蛇身碰撞2->检查头部 if(x==snake.first->x&&y==snake.first->y) productOK=false; if(productOK) break; } food.x=x; food.y=y; cout_food_xy(food.x,food.y); } //issnake'sheadmeetwithfood? boolSnakeUI::meetWithFood(intx,inty){ return(food.x==x&&food.y==y); }
-Snake.h
蛇类,蛇的移动和状态
#ifndef__SNAKE_H__ #define__SNAKE_H__ structSnakeNode{ intx; inty; SnakeNode*prev; SnakeNode(intx_t,inty_t){x=x_t;y=y_t;} SnakeNode(){} }; classSnake{ friendclassSnakeUI; public: Snake(); ~Snake(); boolsnakeMove(char&dir); private: voidgetKey(char&dir); private: SnakeNode*first; SnakeNode*last; charstate; }; #endif
-Snake.cpp
#include"stdafx.h" #include"Snake.h" #include"SnakeUI.h" Snake::Snake(){ //状态:向右 state=RIGHT; //创建循环链表 first=newSnakeNode(4,10); last=newSnakeNode(2,10); last->prev=newSnakeNode(3,10); last->prev->prev=first; first->prev=last; //UI SnakeUI::initSnake(); SnakeUI::productFood(*this); } Snake::~Snake(){ SnakeNode*tmp=last; while(last!=last){ last=last->prev; deletetmp; tmp=last; } deletelast; } boolSnake::snakeMove(char&dir){ intx=first->x; inty=first->y; getKey(dir); //撞墙->GameOver switch(state) { caseUP:--y;if(y==0)returnOVER;break; caseDOWN:++y;if(y==HEIGHT)returnOVER;break; caseLEFT:--x;if(x==0)returnOVER;break; caseRIGHT:++x;if(x==WIDTH)returnOVER;break; } //撞到了自己 SnakeNode*tmp=last; for(;tmp!=first;tmp=tmp->prev){ if(first->x==tmp->x&&first->y==tmp->y) returnOVER; } //吃食物 if(SnakeUI::meetWithFood(x,y)){ SnakeNode*newHead=newSnakeNode(x,y); first->prev=newHead; newHead->prev=last; first=newHead; cout_snake_xy(x,y); SnakeUI::productFood(*this); } else{ cout_space_xy(last->x,last->y); last->x=x; last->y=y; first=last; last=last->prev; cout_snake_xy(x,y); } returnRUN; } voidSnake::getKey(char&dir){ switch(dir) { caseUP:if(state==LEFT||state==RIGHT)state=UP;return; caseDOWN:if(state==LEFT||state==RIGHT)state=DOWN;return; caseLEFT:if(state==UP||state==DOWN)state=LEFT;return; caseRIGHT:if(state==UP||state==DOWN)state=RIGHT;return; } }
-main.cpp
#include"stdafx.h" #include"SnakeUI.h" #include"Snake.h" #include#include usingnamespacestd; DWORDWINAPIThreadProc1(LPVOIDlpParameter); chardir=RIGHT; intmain() { SnakeUI::initUI(); Snakesnake; CreateThread(NULL,0,ThreadProc1,NULL,0,NULL); while(snake.snakeMove(dir))Sleep(100); system("pause"); return0; } DWORDWINAPIThreadProc1(LPVOIDlpParameter) { for(;;){ dir=_getch(); } return1; }
关于C++小游戏的更多精彩内容请点击专题:《C++经典小游戏》学习了解
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。