七、添加食物相关信息

1.食物初始化

1 #include<curses.h>
  2 #include<stdlib.h>
  3 #include<unistd.h>
  4 #include<pthread.h>
  5 
  6 #define UP    1
  7 #define DOWN  -1
  8 #define LEFT  2
  9 #define RIGHT -2
 10 
 11 struct NodeofSnake
 12 {
 13         int hang;
 14         int lie;
 15         struct NodeofSnake *next;
 16 };
 17 
 18 struct NodeofSnake *head=NULL;
 19 struct NodeofSnake *tail=NULL;
 20 int key;
 21 int dir;
 22 struct NodeofSnake food;
 23 
 24 void addNode()
 25 {
26         new=(struct NodeofSnake*)malloc(sizeof(struct NodeofSnake));
 27         new->next=NULL;
 28 
 29         switch(dir){
 30                 case UP:
 31                         new->hang=tail->hang-1;
 32                         new->lie =tail->lie;
 33                         break;
 34                 case DOWN:
 35                         new->hang=tail->hang+1;
 36                         new->lie =tail->lie;
 37                         break;
 38                 case LEFT:
 39                         new->hang=tail->hang;
 40                         new->lie =tail->lie-1;
 41                         break;
 42                 case RIGHT:
 43                         new->hang=tail->hang;
 44                         new->lie =tail->lie+1;
 45                         break;
 46         }
 47 
 48         tail->next=new;
 49         tail=new;
 50 }
51 
 52 void initFood()
 53 {
 54         static int x=3;
 55         static int y=3;
 56         food.hang=x;
 57         food.lie =y;
 58         food.next=NULL;
 59 
 60         x+=2;
 61         y+=2;
 62 
 63 }
 64 
 65 void initSnake()
 66 {
 67         dir=RIGHT;
 68         struct NodeofSnake *point =head;
 69         if(point!=NULL){
 70                 head=head->next;
 71                 free(point);
 72         }
 73         initFood();
 74         head=(struct NodeofSnake*)malloc(sizeof(struct NodeofSnake));
 75         head->hang=2;
76         head->lie =2;
 77         head->next=NULL;
 78 
 79         tail=head;
 80 
 81         addNode();
 82         addNode();
 83         addNode();
 84 }
 85 
 86 int wheatherOfSnakeNode(int hang,int lie)
 87 {
 88 
 89         struct NodeofSnake *point = head;
 90         while(point!=NULL){
 91                 if(hang==point->hang && lie==point->lie ){
 92                         printw("[]");
 93                         return 1;
 94                 }
 95                 point=point->next;
 96         }
 97         return 0;
 98 }
 99 
100 int wheatherOfFoodNode(int hang,int lie)
101 {
102         if(hang==food.hang && lie==food.lie){
103                 printw("##");
104                 return 1;
105         }
106         return 0;
107 }
108 
109 void initMap()
110 {
111         int hang=0;
112         int lie =0;
113         move(0,0);
114 
115         for(hang=0;hang<=20;hang++){
116                 if(hang==0||hang==20){
117                         for(lie=0;lie<20;lie++){
118                                 printw("--");
119                         }
120                         printw("\n");
121                 }
122                 if(hang>0&&hang<=19){
123                         for(lie=0;lie<=20;lie++){
124                                 if(lie==0||lie==20){
125                                         printw("|");
126                                 }else if(wheatherOfSnakeNode(hang,lie)){
127                                 }
128                                 else if(wheatherOfFoodNode(hang,lie)){
129                                 }
130                                 else{
131                                         printw("  ");
132                                 }
133                         }
134                         printw("\n");
135                 }
136         }
137         printw("By:GuoMing,key=%d\n",key);
138         //      printw("key=%d\n",key);
139 }
140 void deleteNode()
141 {
142         struct NodeofSnake *point = head;
143         head=head->next;
144         free(point);
145 }
146 void snakeMove()
147 {
148         addNode();
149         if(tail->hang==food.hang && tail->lie==food.lie){
150                 initFood();
151         }else{
152                 deleteNode();
153         }
154         if(tail->lie==20 ||tail->lie==0 ||tail->hang==0 ||tail->hang==20 ){
155                 initSnake();
156         }
157 }
158 
159 void* refreshJieMian()
160 {
161         while(1){
162                 snakeMove();
163                 initMap();
164 
165                 refresh();
166                 usleep(100000);
167         }
168 
169 }
170 
171 //use abs() to address direction question.
172 
173 void dirWheatherTurn(int direction)
174 {        //   origion       key now dir
175         if(abs(dir) != abs(direction)){
176                 dir=direction;
177         }
178 
179 }
180 
181 void* changeDir()
182 {
183         //      int key;
184         while(1){                 //alway input keyboard.
185                 key=getch();
186                 //              printw("key=%d\n",key);
187 
188                 switch(key){
189                         case KEY_DOWN:
190                                 dirWheatherTurn(DOWN);
191                                 break;
192                         case KEY_UP:
193                                 dirWheatherTurn(UP);
194                                 break;
195                         case KEY_LEFT:
196                                 dirWheatherTurn(LEFT);
197                                 break;
198                         case KEY_RIGHT:
199                                 dirWheatherTurn(RIGHT);
200        201                 }
202         }
203 }
204 
205 int main()
206 {
207         pthread_t t1,t2 ;
208         initscr();
209         keypad(stdscr,1);
210 
211         //      struct NodeofFood food = {3,3};  if write that,food sites will not change.
212 
213         //      dir=RIGHT;             forbid initdir ,cause:only one use.
214         initSnake();
215         initMap();
216         pthread_create(&t1,NULL,refreshJieMian,NULL);
217         pthread_create(&t2,NULL,changeDir,NULL);
218 
219         while(1);
220         getch();
221         endwin();
222 
223         return 0;
224 }
                         break;

 

2.食物随机问题

使用rand()函数,他会随机产生一个随机数,因此对这个随机数进行取余使结果落在地图里面。

 53 void initFood()
 54 {
 55        // static int x=3; //static function is?
 56        // static int y=3;
 57         int x=rand()%20;
 58         int y=rand()%20;
 59 
 60         food.hang=x;
 61         food.lie =y;
 62         food.next=NULL;
 63 }
 64 

可以将食物坐标打印出来

 3.蛇撞自身死亡

148 int deathOfSnake()
149 {
150         if(tail->lie==20 ||tail->lie==0 ||tail->hang==0 ||tail->hang==20 ){
151                 return 1;
152         }
153 
154         struct NodeofSnake *point =head;
155         while(point->next!=NULL){
156                 if(point->hang==tail->hang&&point->lie==tail->lie){
157                         return 1;
158                 }
159                 point=point->next;
160         }
161         return 0;
162 }
163 
164 void snakeMove()
165 {
166         addNode();
167         if(tail->hang==food.hang && tail->lie==food.lie){
168                 initFood();
169         }else{
170                 deleteNode();
171         }
172         if(deathOfSnake()){
173                 initSnake();
174         }
175 }
176 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值