服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - Linux下C语言实现贪吃蛇小游戏

Linux下C语言实现贪吃蛇小游戏

2021-10-28 13:51RICKO-_- C/C++

这篇文章主要为大家详细介绍了Linux下C语言实现贪吃蛇小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

此次贪吃蛇小游戏的目的是使得我在Linux底下使用vi进行编写的

心得:

1.自己对linux中如何使用vi更加熟悉

如::wq yy pp dd u 等等

2.对c语言的指针,结构体,链表等更加的牢固

3.借此小项目也运用到多线程作为进入linux的深入学习打下坚实的基础

代码展示

?
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
#include<curses.h>
#include<stdlib.h>
#define UP 1 //1与-1的目的是使用abs()函数防止一上一下
#define DOWN -1
#define LEFT 2
#define RIGHT -2
struct Snake{ //创建一个结构体
 int hang;
 int lie;
 struct Snake *next;
};
struct Snake *head = NULL; //全局定义一个头和尾
struct Snake *tail= NULL;
int key; //定义一个按键的整形变量
int dir;//定义一个方向的整形变量
struct Snake food;
void initFood(){ //定义一个食物## 可以随机生成
 int x = rand()%19;
 int y = rand()%19;
 food.hang = x;
 food.lie = y;
}
void initNcurse(){
 initscr();
 keypad(stdscr,1);
 noecho();
}
int hasSnakeNode(int i, int j){ //显示蛇身体
 struct Snake *p;
 p = head;
 while(p != NULL){
 if(p->hang == i && p ->lie == j){
 return 1;
 }
 p = p->next;
 }
 return 0;
}
int hasFood(int i,int j){ //有食物
 if(food.hang == i && food.lie == j){
 return 1;
 }
 return 0;
}
void gamePic(){ //游戏图形化展示
 int hang;
 int lie;
 move(0,0);
 for(hang=0;hang<20;hang++){
 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 ricko");
 }
 }
}
void addNode(){ //加头并且方向
 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(){ //初始化蛇
 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();
 addNode();
}
void deleNode(){ //删除最后节点
 struct Snake *p;
 p = head;
 head = head->next;
 free(p);
}
int ifSnakeDie(){ //在撞到边界以及自己迟到自己的时候会输出一个1让自己复活
 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(){ //蛇的移动
 addNode();
 if(hasFood(tail->hang,tail->lie)){ //如果吃到食物就不删除最后的节点
 initFood();
 }else{
 deleNode();
 }
 if(ifSnakeDie()){
 initSnake();
 }
}
void refreshJieMian(){ //刷新界面 线程
 while(1){
 moveSnake();
 gamePic();
 refresh();
 usleep(150000); //刷新频率
 }
}
void turn(int direction){ //防止方向键按了上又按下
 if(abs(dir) != abs(direction)){
 dir = direction;
 }
}
void changeDir(){ //改变方向
 while(1){
 key = getch();
 switch(key){
 case KEY_DOWN:
 turn(DOWN);
 break;
 case KEY_UP:
 turn(UP);
 break;
 case KEY_LEFT:
 turn(LEFT);
 break;
 case KEY_RIGHT:
 turn(RIGHT);
 break;
 }
 }
}
int main(){
 pthread_t t1; //定义线程1
 pthread_t t2;
 initNcurse(); //初始化ncurse
 initSnake(); //初始化蛇
 gamePic();  //初始化界面
 pthread_create(&t1,NULL,refreshJieMian,NULL);//启动线程里面的函数
 pthread_create(&t2,NULL,changeDir,NULL);
 while(1);//线程3
 getch();
 endwin();
 return 0;
}

对代码进行编译

?
1
gcc snake.c -lcurses -lpthread

生成a.out运行文件

运行代码

?
1
./a.out

图片展示

Linux下C语言实现贪吃蛇小游戏

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

原文链接:https://blog.csdn.net/weixin_41679960/article/details/114683705

延伸 · 阅读

精彩推荐