服务器之家

服务器之家 > 正文

C语言链表实现贪吃蛇小游戏

时间:2021-11-08 14:23     来源/作者:暖心淡年华

本文实例为大家分享了C语言链表实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下

项目名称:

贪吃蛇小游戏

运行环境:

Linux

编程语言:

C语言

主要语法:

链表,指针,函数

备注:

游戏中可选不同难度模式,

1.简易——Easy——速度慢,可穿墙,可触碰自己
2.困难——Hard——速度快,不可穿墙,不可触碰自己
3.自动——Auto——外挂模式,自动吃食,直到胜利

代码

贪吃蛇小游戏代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include <curses.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
 
#define UP     1
#define DOWN   -1
#define LEFT   2
#define RIGHT  -2
 
struct Snake    //define the snake
{
 int hang;
 int lie;
 struct Snake *next;
};
 
struct Snake food; //define the food
struct Snake *head= NULL;
struct Snake *tail = NULL;
int key;
int dir;
int set = 49;
int nub = 4;
 
void initFood()  //init food
{
 int x = rand()%20;
 int y = (rand()%19)+1;
 food.hang = x;
 food.lie = y;
}
 
void initNcurse() //init ncurse
{
 initscr();
 keypad(stdscr,1);
 noecho();
}
 
int hasFood(int i,int j) //judge whether shoule show the food
{
 if(food.hang == i && food.lie == j)
 {
  return 1;
 }
 return 0;
}
 
int hasSnakeNode(int i, int j) //judge whether shoule show the snake
{
 struct Snake *p;
 p = head;
 while(p != NULL)
 {
  if(p->hang == i && p->lie == j)
     {
                return 1;
  }
         p = p->next;
 }
 return 0;
}
 
void gamePic()   //show the whole picture
{
 int hang;
 int lie;
 move(0,0);
 for(hang=0;hang<20;hang++)     //20*20 size map
 {
  if(hang == 0)
  {
   for(lie=0;lie<20;lie++)
   {
    printw("--");
   }
   printw("\n");
  }
  if(hang >= 0 && hang <= 19)
  {
                        for(lie=0;lie<=20;lie++)
                        {
                                if(lie == 0 || lie == 20)
                                        printw("|");
                         else if(hasSnakeNode(hang,lie))
    {
     printw("[]");
    }
    else if(hasFood(hang,lie))
    {
     printw("##");
    }
    else
                        printw("  ");
   }
   printw("\n");
  }
  if(hang == 19)
                {
                        for(lie=0;lie<20;lie++)
                        {
                                printw("--");
                        }
   printw("\n");
  }
 }
 printw("by HaoQing, Happy Game!\n");
 switch(dir)
        {
                case UP:
                        printw("Snake UP!   \n");
                        break;
                case DOWN:
   printw("Snake DOWN! \n");
                        break;
                case LEFT:
   printw("Snake LEFT! \n");
                        break;
                case RIGHT:
   printw("Snake RIGHT!\n");
                        break;
        }
 if(nub >= 380)
 {
  printw("------------------------------------\n");
  printw("==============YOU WIN!==============\n");
         printw("------------------------------------\n");
 }
}
 
void addNode()   //add the node of snake
{
 struct Snake *new = (struct Snake *)malloc(sizeof(struct Snake));
 
 new->next = NULL;
 
 switch(dir)
 {
  case UP:
   new->hang = tail->hang-1;
                        new->lie = tail->lie;
                        break;
  case DOWN:
                        new->hang = tail->hang+1;
                        new->lie = tail->lie;
                        break;
  case LEFT:
                        new->hang = tail->hang;
                        new->lie = tail->lie-1;
                        break;
  case RIGHT:
                        new->hang = tail->hang;
                        new->lie = tail->lie+1;
                        break;
 }
 tail->next = new;
 tail = new;
}
 
void initSnake()                //init snake
{
        struct Snake *p;
 
        dir = RIGHT;
 
        while(head != NULL)
        {
                p = head;
                head = head->next;
                free(p);
        }
        initFood();
        head = (struct Snake *)malloc(sizeof(struct Snake));
        head->hang = 1;
        head->lie = 1;
        head->next = NULL;
 
        tail = head;
        addNode();
        addNode();
        addNode();
}
 
void deleNode()   //delete the node of snake  
{
 struct Snake *p;
 p = head;
 head = head->next;
 free(p);
}
 
int ifSnakeDie()  //judge whether the snake is die
{
 struct Snake *p;
 p = head;
 if(tail->hang < 0 || tail->lie == 0 || tail->hang == 20 || tail->lie == 20)
        {
         return 1;    
        }
 while(p->next != NULL)
 {
  if(p->hang == tail->hang && p->lie == tail->lie)
   return 1;
  p=p->next;
 }
 return 0;
}
 
void moveSnake()  //move the snake
{
 addNode();
 if(hasFood(tail->hang,tail->lie))
 { initFood();
  nub++;  //judge whether win
 }
 else
  deleNode();
 if(set != 49 && set != 50 && set != 53)
 {
  if(ifSnakeDie())
   initSnake();
 }
}
 
void refreshJieMian()  //refresh the picture
{
 while(1)
 {
  moveSnake();
  gamePic();
  refresh();
  switch(set)
  {
   case 49:
    usleep(200000);
    break;
   case 50:
                                usleep(100000);
                                break;
   case 51:
                                usleep(100000);
                                break;
   case 52:
                                usleep(80000);
                                break;
   case 53:
    usleep(70000);
    break;
   default:
    usleep(150000);
  }
 }
}
 
void turn(int direction) //prevent reverse
{
 if(set != 49 && set != 50 && set != 53)
 { if(abs(dir) != abs(direction))
  {
   dir = direction;
  }
 }
 else
  dir = direction;
}
 
void changeDir()  //change direction
{
 while(1)
 {
  if(set != 53)
  {
   key = getch();
   switch(key)
   {
    case KEY_DOWN:
     turn(DOWN);   //-1
     break;
    case KEY_UP:
     turn(UP);     //1
     break;
    case KEY_LEFT:
     turn(LEFT);   //2
     break;
    case KEY_RIGHT:
     turn(RIGHT);  //-2
     break;
   }
  }
  else
  {
   if(tail->hang > food.hang)
    turn(UP);
   else if(tail->hang < food.hang)
    turn(DOWN);
   if(tail->lie > food.lie)
    turn(LEFT);
   else if(tail->lie < food.lie)
    turn(RIGHT);
  }
 }
}
 
int main()
{
 initNcurse();
 
 printw("======Welcome To Snake Eating!======\n");
 printw("------------------------------------\n");
 printw("Please select difficulty level:\n");
 printw("\n");
 printw("   Fool,please enter    \"1\"\n");
 printw("   Simple,please enter  \"2\"\n");
 printw("   Hard,please enter    \"3\"\n");
 printw("   Hell,please enter    \"4\"\n");
 printw("   Auto,please enter    \"5\"\n");
 printw("------------------------------------\n");
 printw("You want:");
 
 set = getch();
 
 pthread_t t1;
 pthread_t t2;
 
 initSnake();
 
 gamePic();
 
 pthread_create(&t1,NULL,(void *)refreshJieMian,NULL);
 pthread_create(&t2,NULL,(void *)changeDir,NULL);
 
 while(1);
 
 endwin();
 return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_45346142/article/details/116856597

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部