c++ 贪吃蛇

贪吃蛇的程序,主框架是吉亮大神的,做了点修改,支持改变速度,改变地图大小;

  1 /*NAME:SNAKE DATE:2014/1/11*/
  2 #include <iostream.h>
  3 #include <windows.h>
  4 #include <stdlib.h>
  5 #include <conio.h>
  6 #include <time.h>  //使用当前时间做种子;
  7 enum dir{up,down,left,right};  //枚举类型enum dir;
  8 
  9 int map_lenth,map_wide;//地图的长和宽(即i和j)
 10 
 11 //围墙;
 12 class Fence
 13 {
 14 public:
 15     void InitFence();
 16     void OutputF();
 17 public:
 18     char game[40][40];
 19 }f; //定义对象;
 20 //画框框;
 21 void Fence::InitFence()
 22 {
 23     for(int i=0; i<map_lenth; i++)
 24         for(int j=0; j<map_wide; j++)
 25         {
 26             if(i==0 || i==map_lenth-1 || j==0 || j==map_wide-1)
 27                 game[i][j]= '*';
 28             else game[i][j]= ' ';
 29         }
 30 }
 31 //显示框框;
 32 void Fence::OutputF()
 33 {
 34     for(int i=0; i<map_lenth; i++)
 35     {
 36         for(int j=0; j<map_wide; j++)
 37             cout<<game[i][j]<<' ';
 38         cout<<endl;
 39     }
 40 }
 41 
 42 //蛇结点;
 43 class SnakeNode
 44 {
 45 private:
 46     int x,y;
 47     SnakeNode *prior,*next;
 48 public:
 49     void add_head(int x,int y);
 50     int get_x();
 51     int get_y();
 52     void delete_tail();
 53 }*head=NULL,*tail=NULL;
 54 //插入头结点;
 55 void SnakeNode::add_head(int x,int y)
 56 {
 57     SnakeNode *q=new SnakeNode;
 58     q->x =x; q->y =y;
 59     q->next =head;
 60     q->prior =NULL;
 61     if(head) head->prior =q;
 62     head =q;
 63     if(!tail) tail =head;
 64     f.game[x][y]= '~';  //f对象可以在定义Fence类时定义; 且Fence类在SnakeNode类前定义;
 65 }
 66 int SnakeNode::get_x()
 67 {
 68     return x;
 69 }
 70 int SnakeNode::get_y()
 71 {
 72     return y;
 73 }
 74 //删除尾结点;
 75 void SnakeNode::delete_tail()
 76 {
 77     SnakeNode *p=tail;
 78     f.game[tail->get_x()][tail->get_y()]= ' ';//把尾结点的坐标表示的'*'置为空格;
 79     if(tail==head)
 80         tail= head= NULL;
 81     else
 82     {
 83         tail= tail->prior;
 84         tail->next= NULL;
 85     }
 86     delete p;
 87 }
 88 
 89 //move移动;
 90 class move
 91 {
 92 public:
 93     dir point;    //枚举变量point: 控制方向;
 94     int food_x;
 95     int food_y;
 96 public:
 97     void moving();
 98     void change_point(char);  //改变方向;
 99     void get_food();
100 };
101 
102 void move::moving()
103 {
104     int a,b;
105     a=head->get_x();  //取得头结点横坐标
106     b=head->get_y();  //取得头结点纵坐标
107     switch(point)
108     {
109         case up: --a; break;
110         case down: ++a; break;
111         case left: --b; break;
112         case right: ++b; break;
113     }
114     if(a==map_lenth-1 || b==map_wide-1 || a==0 || b==0)
115     {                //判断是否撞墙;                        
116         cout<<"game over!!!"<<endl;
117         exit(0);
118     }
119     if(a==food_x && b==food_y)
120     {                    //吃food;
121         head->add_head(a,b);
122         get_food();
123     }
124     else{
125         head->add_head(a,b); //插入头结点;
126         head->delete_tail(); //删除尾结点;
127     }
128 }
129 
130 void move::change_point(char keydown)
131 {
132     switch(keydown)
133     {
134         case 'w': 
135         {
136             if (point!=down)
137             {
138                 point=up; break;
139             }
140             else break;
141         }
142         case 's': 
143         {
144             if (point!=up)
145             {
146                 point=down; break;
147             }
148             else break;
149         }
150         case 'a': 
151         {
152             if (point!=right)
153             {
154                 point=left; break;
155             }
156             else
157                 break;
158         }
159         case 'd': 
160         {
161             if (point!=left)
162             {
163                 point=right; break;
164             }
165             else break;
166         }
167     }
168 }
169 void move::get_food()
170 {
171     srand((unsigned int) time(NULL)); //做种子(程序运行时间); 
172     food_x= rand()%(map_lenth-2)+1; 
173     food_y= rand()%(map_wide-2)+1;
174     f.game[food_x][food_y]='x';
175 }
176 
177 //main();
178 int main()
179 {
180     int speed;
181     cout<<"Instruction: The snake's original length is 3.\nUsing 'w,s,a,d' to start the game and control the direction.\nGOOD LUCK!\n";
182     cout<<"\nPlease put in 'lenth' & 'wide' to control the map and press enter to confirm.(limits:4~39)"<<endl;
183     cout<<"Length=";
184     cin>>map_lenth;
185     cout<<"Wide=";
186     cin>>map_wide;
187     cout<<endl<<"Warning:the bigger num means the lower speed.Limits:1~500!\nSpeed=";
188     cin>>speed;
189     //画框框和小蛇;
190     move m;
191     f.InitFence();
192     head->add_head(4,3);
193     head->add_head(4,4);
194     head->add_head(4,5);
195     m.get_food();
196     f.OutputF();
197     while (true)
198     {
199         char keydown=getch(); //getch()返回键盘上读取的字符;包含头文件<conio.h>
200         m.change_point(keydown);
201         while(!kbhit())
202         {                     //判断有没有按键落下;
203             system("cls");  //清屏函数;
204             m.moving();
205             f.OutputF();
206             Sleep(speed);
207         }
208     }
209     return 0;
210 }
SNAKE

 

posted @ 2012-12-09 17:30  Easy sir  阅读(1411)  评论(6编辑  收藏  举报