参考:基于C语言Ncurse库和链表的简单贪吃蛇小游戏
作者:三速何时sub20
发布时间:2020-09-29 10:23:51
网址:https://blog.csdn.net/weixin_44234294/article/details/108829135?spm=1001.2014.3001.5501
为何要用到它?传统的C语言输入:getchar() scanf() gets()等都需要输入敲回车,这样对游戏的快速响应效果就大打折扣。因此必须要摆脱敲回车的操作。
其实我们对ncurses本身并不陌生,以下几款大名鼎鼎的软件都用到过ncurses:
vim
emacs
lynx
screen
作为嵌入式驱动开发工程师,Linux内核的配置也离不开ncurses库的使用。
安装与使用
Linux ubuntu下
sudo apt-get install libncurses5-dev
- 1
游戏设计大体思路
首先规划一个地图,蛇在设个范围内移动,蛇身体用动态链表来创建,蛇移动就是在链表尾巴添加新的节点在删掉头节点。如图(最左边使头,这里动态创建链表用的是尾插法)
果子实在规划地图中随机出现,当蛇头和果子重合时,蛇身体加一格,就是链表新加节点不删节点(具体看代码)
贪吃蛇代码
1. **首先我们写个函数初始化nucurse(做相应的配置)可以参考这个网页大部分函数都在这里
https://blog.csdn.net/ztq_12345/article/details/100560314
void initNcurses()
{
initscr(); //打开curses模式 进入你的终端
//完成initscr后,输入模式为预处理模式,(1)所有处理是基于行的,就是说,只有按下回车,输入数据才被传给程序;(2)键盘特殊字符启用,按下合适组合键会产生信号
//keypad(WINDOW *, bool);
keypad(stdscr,1);//指定窗口 激活功能键 上下左右 F1 F2等
//Stdscr数据结构对应的是“标准屏幕”,它的工作方式与stdout相似,是curses程序中的默认输出窗口
//Stdscr:指curses窗口,它与显示器的物理屏幕的尺寸完全一样。//标准屏幕
noecho(); //关闭回显 让键盘输入字符 不显示在终端上
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
2. 规划地图,我这里打算做个40*40的,大小可以根据代码进行调试
void gamePicture()
{
int hang;
int lie;
for(hang = 0;hang < 41;hang ++){
if(hang == 0){
for(lie = 0;lie < 40;lie++){//设计40*40正方形 上边界
printw("--");
}
printw("\n");
}
if(hang >= 0 && hang <= 39){//设计正方形左右两遍的边界
for(lie = 0;lie < 41;lie++){
if(lie == 0 | lie == 40){
printw("|");
}else{
printw(" ");
}
}
printw("\n");
}
if(hang == 40){
for(lie = 0;lie < 40;lie++){//设计40*40正方形 上边界
printw("--");
}
printw("\n");
}
}
printw("By mj\n");
}
3.首先尝试在地图显示蛇的身子
蛇的身子时基于链表的我们要创建一个结构体
struct Snake
{
int hang;
int lie;
struct Snake *next;
};
- 1
- 2
- 3
- 4
- 5
- 6
接着我们初始化蛇的身子,我们设定坐标为(2,2)
void initSnake()
{
head = (struct Snake *)malloc(sizeof(struct Snake));//分配内存空间
head->hang = 2;//初始化蛇的位置
head->lie = 2;
head->next = NULL;
tail = head;
addNode();//增加蛇的长度
addNode();//我们让蛇一开始长度3
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
根据上面的我们做一个addNode函数
void addNode()
{
struct Snake *new =NULL;
new = (struct Snake *)malloc(sizeof(struct Snake));//分配内存空间
new->hang = tail->hang;//因为用的时尾插法,这里用tail而不用head更方便一些
new->lie = tail ->lie+1;//身子加长
new->next = NULL;
tail->next = new;//尾插法
tail = new;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
此时我们可以大概看一下效果
- 我们现在尝试把这条蛇开始移动,我们设定初始方向向右。写个函数来刷新界面
void refreshPicture()
{
while(1){
moveSbake();
gamePicture();
refresh();
usleep(100000);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
动态现在没时间来显示
5.现在要做的是用上下左右四个键控制蛇身体的移动
#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4
- 1
- 2
- 3
- 4
此时我们需要用到线程的知识,因为在蛇不断移动地图刷新的循环同时我们也要需要一个循环来等待我们键盘的输入(上下左右键)。
线程创建
#include <pthread.h>
int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
// 返回:若成功返回0,否则返回错误编号
当pthread_create成功返回时,由tidp指向的内存单元被设置为新创建线程的线程ID。attr参数用于定制各种不同的线程属性,暂可以把它设置为NULL,以创建默认属性的线程。
新创建的线程从start_rtn函数的地址开始运行,该函数只有一个无类型指针参数arg。如果需要向start_rtn函数传递的参数不止一个,那么需要把这些参数放到一个结构中,然后把这个结构的地址作为arg参数传入。
两个线程的创建
pthread_t t1;
pthread_t t2;
pthread_create(&t1,NULL,refreshPicture,NULL);
pthread_create(&t2,NULL,changeDirection,NULL);
- 1
- 2
- 3
- 4
两个函数
void *changeDirection()
{
while(1){
key = getch();//获取按下的值
switch(key){
case KEY_UP://上键
dir = UP;
break;
case KEY_DOWN://下键
dir = DOWN;
break;
case KEY_RIGHT://右键
dir = RIGHT;
break;
case KEY_LEFT://左键
dir = LEFT;
break;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
void *refreshPicture()
{
while(1){
moveSbake();
gamePicture();
refresh();//每次在屏幕绘制之后,需要调用refresh()刷新屏幕
usleep(100000);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
此时根据我们输入上下左右键,我们蛇移动那边的函数也需要做修改
void addNode()
{
struct Snake *new =NULL;
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 RIGHT:
new->hang = tail->hang;
new->lie = tail->lie + 1;
break;
case LEFT:
new->hang = tail->hang;
new->lie = tail->lie - 1;
break;
}
tail->next = new;//尾插法
tail = new;
}
- 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
这边改动过后,此时蛇已经能通过我们的上下左右键自由移动。
6.接着试着在整个地图中随机出现果子,当蛇头和果子重合的时候,蛇的身子加长一个,果子再随机地点出现一个……
首先初始化果子
void initFood()
{
int x = rand()%40;///在0-40之间随机出现一个数
int y = rand()%40;
food.hang = x;
food.lie = y;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
int printFood(int x,int y)
{
if(food.hang == x && food.lie == y){
return 1;
}
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
void gamePicture()
{
int hang;
int lie;
move(0,0);
for(hang = 0;hang < 41;hang ++){
if(hang == 0){
void gamePicture()
{
int hang;
int lie;
move(0,0);
for(hang = 0;hang < 41;hang ++){
if(hang == 0){
for(lie = 0;lie < 40;lie++){//设计40*40正方形 上边界
printw("--");
}
printw("\n");
}
if(hang >= 0 && hang <= 39){//设计正方形左右两遍的边界
for(lie = 0;lie < 41;lie++){
if(lie == 0 | lie == 40){
printw("|");
}else if(printSnakeBody(hang ,lie)){//把打印地图是此时的坐标传过去判断是否时舍得坐标
printw("[]");
}else if(printFood(hang,lie)){
printw("$$");
}else{
printw(" ");
}
}
printw("\n");
}
if(hang == 40){
for(lie = 0;lie < 40;lie++){//设计40*40正方形 上边界
printw("--");
}
printw("\n");
}
}
printw("By mj\n");
}
此时要实现蛇吃果子身子加长
void moveSbake()
{
addNode();
if(tail->hang == food.hang && tail->lie == food.lie){//判断蛇头是否和果子的坐标重合
addNode();
initFood();
}
deleNode();
}
现在蛇能吃果子并且身体加长
7.上面的代码已经可以实现蛇吃果子自由移动,但是一碰到边界之外就结束了,现在的目的是当蛇头碰到边界或者舌头碰到身体则重新刷新蛇
ifSnakeDie函数
int ifSnakeDie()
{
struct Snake *p = NULL;
p = head;
if(tail->hang == 0 | tail->hang == 40 | tail->lie == 0 | tail->lie == 40){//判断是否碰装到边界
return 1;
}
while(p->next != NULL){
if(p->hang == tail->hang && p->lie == tail->lie){//判断是否头和身子相碰撞
return 1;
}
p = p->next;
}
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
moveSbake函数
void moveSbake()
{
addNode();
if(tail->hang == food.hang && tail->lie == food.lie){//判断蛇头是否和果子的坐标重合
addNode();
initFood();
}
deleNode();
if(ifSnakeDie()){
initSnake();
}
}
到这里差不多就结束了,功能都能实现,但是还存在一些问题,就是蛇在右移的过程中如果按左键就直接蛇尾便舌头,蛇头边蛇尾,为了杜绝这种问题,我们用到abs()函数
首先宏定义这边重新设定
#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2
- 1
- 2
- 3
- 4
- 5
绝对值判断的函数absTurn
void absTurn(int x)
{
/*就是蛇在右移的过程中不能左移只能上下,反之一样,在上移的时候不能下移只能左右,反之一样*/
if(abs(dir) !=abs(x)){//判断此时蛇移动的方向值的绝对值是否等于你按下键的值的绝对值
dir = x;
}
}
void *changeDirection()
{
while(1){
key = getch();//获取按下的值
switch(key){
case KEY_UP://上键
absTurn(UP);
break;
case KEY_DOWN://下键
absTurn(DOWN);
break;
case KEY_RIGHT://右键
absTurn(RIGHT);
break;
case KEY_LEFT://左键
absTurn(LEFT);
break;
}
}
}
- 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
整体项目代码
编译方法:gcc snale.c -lcurses -lpthread
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <curses.h>
#include <pthread.h>
#define UP 1
#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;
struct Snake food;
int dir;
int key;
int printSnakeBody(int x,int y)
{
struct Snake *p =NULL;
p = head;
while(p != NULL){
if(p->hang == x && p->lie == y){//确定蛇的坐标
return 1;
}
p = p->next;
}
return 0;
}
void initNcurses()
{
initscr(); //打开curses模式 进入你的终端
//完成initscr后,输入模式为预处理模式,(1)所有处理是基于行的,就是说,只有按下回车,输入数据才被传给程序;(2)键盘特殊字符启用,按下合适组合键会产生信号
//keypad(WINDOW *, bool);
keypad(stdscr,1);//指定窗口 激活功能键 上下左右 F1 F2等
//Stdscr数据结构对应的是“标准屏幕”,它的工作方式与stdout相似,是curses程序中的默认输出窗口
//Stdscr:指curses窗口,它与显示器的物理屏幕的尺寸完全一样。//标准屏幕
noecho(); //关闭回显 让键盘输入字符 不显示在终端上
}
int printFood(int x,int y)
{
if(food.hang == x && food.lie == y){
return 1;
}
return 0;
}
void gamePicture()
{
int hang;
int lie;
move(0,0);
for(hang = 0;hang < 41;hang ++){
if(hang == 0){
for(lie = 0;lie < 40;lie++){//设计40*40正方形 上边界
printw("--");
}
printw("\n");
}
if(hang >= 0 && hang <= 39){//设计正方形左右两遍的边界
for(lie = 0;lie < 41;lie++){
if(lie == 0 | lie == 40){
printw("|");
}else if(printSnakeBody(hang ,lie)){//把打印地图是此时的坐标传过去判断是否时舍得坐标
printw("[]");
}else if(printFood(hang,lie)){
printw("$$");
}else{
printw(" ");
}
}
printw("\n");
}
if(hang == 40){
for(lie = 0;lie < 40;lie++){//设计40*40正方形 上边界
printw("--");
}
printw("\n");
}
}
printw("By mj\n");
}
void addNode()
{
struct Snake *new =NULL;
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 RIGHT:
new->hang = tail->hang;
new->lie = tail->lie + 1;
break;
case LEFT:
new->hang = tail->hang;
new->lie = tail->lie - 1;
break;
}
tail->next = new;//尾插法
tail = new;
}
void deleNode()
{
struct Snake *p = head;
head = head->next;
free(p);
}
void initSnake()
{
dir = RIGHT;
while(head != NULL){//防止内存被占用
struct Snake *p = head;
head = head->next;
free(p);
}
head = (struct Snake *)malloc(sizeof(struct Snake));//分配内存空间
head->hang = 2;//初始化蛇的位置
head->lie = 2;
head->next = NULL;
tail = head;
addNode();//增加蛇的长度
addNode();//我们让蛇一开始长度3
}
void initFood()
{
int x = rand()%40;///在0-40之间随机出现一个数
int y = rand()%40;
food.hang = x;
food.lie = y;
}
int ifSnakeDie()
{
struct Snake *p = NULL;
p = head;
if(tail->hang < 0 | tail->hang == 40 | tail->lie == 0 | tail->lie == 40){//判断是否碰装到边界
return 1;
}
while(p->next != NULL){
if(p->hang == tail->hang && p->lie == tail->lie){//判断是否头和身子相碰撞
return 1;
}
p = p->next;
}
return 0;
}
void moveSbake()
{
addNode();
if(tail->hang == food.hang && tail->lie == food.lie){//判断蛇头是否和果子的坐标重合
addNode();
initFood();
}
deleNode();
if(ifSnakeDie()){
initSnake();
}
}
void *refreshPicture1()
{
while(1){
moveSbake();
gamePicture();
refresh();//每次在屏幕绘制之后,需要调用refresh()刷新屏幕
usleep(70000);//蛇移动的速度
}
}
void *refreshPicture3()
{
while(1){
moveSbake();
gamePicture();
refresh();//每次在屏幕绘制之后,需要调用refresh()刷新屏幕
usleep(100000);//蛇移动的速度
}
}
void absTurn(int x)
{
/*就是蛇在右移的过程中不能左移只能上下,反之一样,在上移的时候不能下移只能左右,反之一样*/
if(abs(dir) !=abs(x)){//判断此时蛇移动的方向值的绝对值是否等于你按下键的值的绝对值
dir = x;
}
}
void *changeDirection()
{
while(1){
key = getch();//获取按下的值
switch(key){
case KEY_UP://上键
absTurn(UP);
break;
case KEY_DOWN://下键
absTurn(DOWN);
break;
case KEY_RIGHT://右键
absTurn(RIGHT);
break;
case KEY_LEFT://左键
absTurn(LEFT);
break;
}
}
}
int main(int argc, char const *argv[])
{
int data;
pthread_t t1;
pthread_t t2;
pthread_t t3;
pthread_t t4;
printf("选择蛇速度 困难(1) 简单(2)\n");
printf("请输入:");
scanf("%d",&data);
initNcurses();
initSnake();
gamePicture();
initFood();
//int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
if(data == 1){
pthread_create(&t1,NULL,refreshPicture1,NULL);
}
pthread_create(&t1,NULL,refreshPicture3,NULL);
pthread_create(&t2,NULL,changeDirection,NULL);
while(1);
getch();//获取键盘输入单个字符
endwin(); //关闭窗口stdscr
return 0;
}
- 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
总结
这里大部分还是考察链表的知识,其中也运用到了线程的相关知识点。