c语言之贪吃蛇(ncurse库)

1.关于ncurse库
(1)如果没有ncurse这个库先安装ncurse库

sudo apt install libncurses5-dev

(2)在编译时后面加上 -lcurses
(3)测试代码

#include <curses.h>

int main()
{
        char c;

        initscr();//ncurse界面的初始化函数
        printw("this is curses window.\n");//在ncurse模式下的printf
        c = getch();//等待用户输入
        printw("you input:%c\n",c);//用户输入一个字符
        getch();
        endwin();//程序退出,调用该函数来恢复shell终端的显示,如果没有这句话,shell终端乱码

        return 0;
}

(4)在用ncurse获取上下左右键(在/usr/include/curses.h下用vi打开/KEY_UP找到上下左右键的定义,切用这些功能键时要用keypad函数调用)

#include <curses.h>

int main()
{
        int key;//这里用char型显示字节数不够

        initscr();//ncurse界面的初始化函数
        keypad(stdscr,1);//参数1:从标准的stdscr接收功能键,参数2:代表是否接收

        while(1)
        {
                key = getch();//等待用户输入
                switch(key)
                {
                        case KEY_DOWN:
                                printw("DOWN\n");
                                break;
                        case KEY_UP:
                                printw("UP\n");
                                break;
                        case KEY_LEFT:
                                printw("LEFT\n");
                                break;
                        case KEY_RIGHT:
                                printw("RIGHT\n");
                                break;
                }
        }
        endwin();//程序退出,调用该函数来恢复shell终端的显示,如果没有这句话,shell终端乱码

        return 0;
}

(5)贪吃蛇完整代码

#include <curses.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

#define UP    1
#define DOWN  -1
#define LEFT  2
#define RIGHT -2

struct snake
{
        int hang;
        int lie;
        struct snake *pnext;
};
//struct snake node1 = {2,2,NULL};//创建贪吃蛇节点
//struct snake node2 = {2,3,NULL};//创建贪吃蛇节点
//struct snake node3 = {2,4,NULL};//创建贪吃蛇节点
struct snake *head = NULL;
struct snake *tail = NULL;
int key;
int dir;

struct snake food;

void initFood()
{
        int x = rand()%20;
        int y = rand()%20;
        food.hang = x;
        food.lie = y;
}

//ncurse初始化函数
void initNcurse()
{
        initscr();
        keypad(stdscr,1);
        noecho();//不要打印无关信息
}

//判断是否为贪吃蛇身子
int isSnakeNode(int i,int j)
{
        struct snake *p;
        p = head;

        while(NULL != p)
        {
                if(p->hang == i && p->lie == j)
                {
                        return 1;
                }

                p = p->pnext;
        }
        return 0;
}

//判断是否为失误
int isFood(int i,int j)
{
                if(food.hang == i && food.lie == j)
                {
                        return 1;
                }
        return 0;

}

//显示外围框架函数
void gamePic()
{
        int hang;
        int lie;

        move(0,0);//移动ncurse的光标
        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(isSnakeNode(hang,lie))
                                {
                                        printw("[]");
                                }
                                else if(isFood(hang,lie))
                                {
                                        printw("##");
                                }
                                else
                                {
                                        printw("  ");
                                }
                        }
                        printw("\n");
                }
                if(hang == 19)
                {
                        for(lie=0;lie<20;lie++)
                        {
                                printw("--");
                        }
                        printw("\n");
                        printw("by zzh,key = %d,food.hang = %d,food.lie = %d\n",key,food.hang,food.lie);
                }
        }
}

void addNode()
{
        struct snake *new = (struct snake *)malloc(sizeof(struct snake));
        new->pnext = 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->pnext = new;
        tail = new;
}

void deleteNode()
{
        struct snake *p;
        p = head;
        head = head->pnext;
        free(p);

}

void initSnake()
{
        struct snake *p;

        dir = RIGHT;


        while(head != NULL)
        {
                p = head;
                head = head->pnext;
                free(p);
        }
        initFood();
        head = (struct snake *)malloc(sizeof(struct snake));
        head->hang = 1;
        head->lie = 1;
        head->pnext = NULL;
        tail = head;
        addNode();
        addNode();
        addNode();
        addNode();
}

//头撞到尾巴死掉函数
int ifSnakeDie()
{
        struct snake *p;
        p = head;

        if(tail->hang < 0 || tail->lie == 0 || tail->hang == 20 || tail->lie == 20)
        {
                return 1;
        }
        while(NULL != p->pnext)
        {
                if(p->hang == tail->hang && p->lie == tail->lie)
                {
                        return 1;
                }
                p = p->pnext;
        }
        return 0;
}

void moveSnake()
{
        //struct snake *p;

        //p = head;
        addNode();
        if(isFood(tail->hang,tail->lie))
        {
                initFood();
        }
        else
        {
                deleteNode();
        }

        if(ifSnakeDie())
        {
                initSnake();
        }

}

//刷新界面函数
void *refreshJieMian()
{
        while(1)
        {
                        moveSnake();
                        gamePic();//刷新地图

                        refresh();//每隔100000微秒刷新界面
                        usleep(100000);
        }
}

//转弯函数
void turn(int direction)
{
        if(abs(dir) != abs(direction))//abs求绝对值
        {
                dir = direction;
        }
}

//改变方向函数
void *changeDir()
{
        sleep(1);//让另一个线程先执行,否则出现地图乱码
        while(1)
        {
                key = getch();//等待用户输入
                switch(key)
                {
                        case KEY_DOWN:
                                printw("DOWN\n");
                                turn(DOWN);
                                break;
                        case KEY_UP:
                                printw("UP\n");
                                turn(UP);
                                break;
                        case KEY_LEFT:
                                printw("LEFT\n");
                                turn(LEFT);
                                break;
                        case KEY_RIGHT:
                                printw("RIGHT\n");
                                turn(RIGHT);
                                break;
                }
        }
}

int main()
{
        pthread_t t1;
        pthread_t t2;

        initNcurse();
        initSnake();
        gamePic();

        pthread_create(&t1,NULL,refreshJieMian,NULL);
        pthread_create(&t2,NULL,changeDir,NULL);

        while(1);
        getch();
        endwin();
        return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中,编写一个贪吃蛇游戏通常会涉及到图形界面,而直接使用图片可能并不是C语言的标准做法,因为C语言本身并不直接支持图形操作。然而,如果你想要在控制台上实现一个简单的贪吃蛇游戏,可以使用文本模式的输出,例如ASCII字符来表示蛇和食物。 以下是一个简单的贪吃蛇游戏的基本结构,使用字符数组来表示屏幕(这里没有包含图片): ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #define SNAKE_SPEED 10 #define SNAKE_LENGTH 3 typedef struct { int x, y; } SnakePart; SnakePart snake[SNAKE_LENGTH] = { {1, 1}, {1, 0}, {1, -1} }; int food_x = 5, food_y = 5; int score = 0; void draw_board(char board[30][30]) { // 清空屏幕 system("cls"); // 对于Windows system("clear"); // 对于Unix-like系统 for (int i = 0; i < 30; i++) { printf("%s\n", board[i]); } } void move_snake(char board[30][30]) { // 更新蛇的位置 snake.x = snake.x; snake.y = snake.y; if (snake.x == food_x && snake.y == food_y) { score++; food_x = rand() % 30 + 1; food_y = rand() % 30 + 1; } else { snake[snake_length - 1].x = snake[snake_length - 2].x; snake[snake_length - 1].y = snake[snake_length - 2].y; } // 判断边界并移动 if (snake.x > 29 || snake.x < 0 || snake.y > 29 || snake.y < 0) { // 失败处理 } } int main() { srand(time(NULL)); char board[30][30]; // 初始化游戏板和蛇 for (int i = 0; i < 30; i++) { for (int j = 0; j < 30; j++) { board[i][j] = ' '; } board[i][food_x - 1] = '#'; } draw_board(board); while (1) { move_snake(board); // 检查碰撞 for (int i = 1; i < SNAKE_LENGTH; i++) { if (snake[0].x == snake[i].x && snake.y == snake[i].y) { // 失败处理 break; } } draw_board(board); printf("Score: %d\n", score); sleep(SNAKE_SPEED); // 暂停一段时间 } return 0; } ``` 如果你想在图形界面下实现,C语言可能会结合使用像SDL、SFML这样的游戏开发,但这超出了纯C语言的范畴。如果你对如何使用这些感兴趣,可以告诉我,我可以提供一些指导。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值