另类的贪吃蛇
- 格式:doc
- 大小:38.50 KB
- 文档页数:7
#include #include #include #include #include #include #define LINE 20 #define ROW 20 using namespace std;
void run(); enum dir {up,down,lef,rit} direct1=rit; class CFrame {
public: void InFrame(); void OutFrame(); public: char point[LINE][ROW];
} f; class CSnake; class CSnakeNode { public: CSnakeNode() { prior=NULL; next=NULL; } friend class CSnake; CSnakeNode *prior; CSnakeNode *next; public:
int x; int y; }; void CFrame::InFrame() { for(int i=0;ifor(int j=0;j{ if(i==0||j==0||i==(LINE-1)||j==(ROW-1)) point[i][j]='*'; else point[i][j]=' '; } } void CFrame::OutFrame() { for(int i=0;i{ for(int j=0;j{ cout<} cout<} } class CMove; class CSnake { public: CSnake() { head=NULL; tail=NULL; } void Clear(); void AddHead(int x,int y); void DelTail(); friend class CMove; friend void Isexit(int x,int y,CSnake snake); public: CSnakeNode *head; CSnakeNode *tail; }; void CSnake::Clear() { CSnakeNode *node; while(head) { node=head; f.point[head->x][head->y]=' '; head=head->next; delete node; } } void CSnake::AddHead(int x,int y) { CSnakeNode *p=new CSnakeNode; p->x=x; p->y=y; p->prior=NULL; p->next=head; if(head!=NULL) { head->prior=p; f.point[head->x][head->y]='*'; } head=p; if(tail==NULL) tail=head; f.point[x][y]='@'; } void CSnake::DelTail() { CSnakeNode *q=tail; f.point[tail->x][tail->y]=' '; if(tail==head) tail= head= NULL; else { tail= tail->prior; tail->next= NULL; } delete q; } int Ifexit(int x,int y,CSnake &snake) { CSnakeNode *p=new CSnakeNode(); p=snake.head; int flag=0; while(p) { if(p->x==x&&p->y==y) { flag=1; break; } else p=p->next; } return flag; }
class CFood { public: void GetFood(CSnake &snake); public: int food_x; int food_y; } food; class CMove { public: void Moving(CSnake &snake); void Change_dir(char); public: dir direct; } ; void CFood::GetFood(CSnake &snake) { srand((unsigned int) time(NULL)); food_x=rand()%(LINE-2)+1; food_y=rand()%(ROW-2)+1; while(Ifexit(food_x,food_y,snake)) { food_x=rand()%(LINE-2)+1; food_y=rand()%(ROW-2)+1; } f.point[food_x][food_y]='#'; } void CMove::Change_dir(char keydown) { switch(keydown) { case 'w': case 72: case 'W':direct=up;break; case 's': case 80: case 'S':direct=down;break; case 'A':case 75: case 'a':direct=lef;break; case 'D':case 77: case 'd':direct=rit;break; }
} void CMove::Moving(CSnake &snake) { int a,b; a=snake.head->x; b=snake.head->y; switch(direct) { case up:--a;break; case down:++a;break; case lef:--b;break; case rit:++b;break; } direct1=direct; if(a==0||a==(LINE-1)||b==0||b==(ROW-1)||Ifexit(a,b,snake)) { char c; cout<<"游戏结束,是否继续: 是:y 否:n"