【C语言】贪吃蛇(Linux环境)

这是一个用C语言编写的蛇吃食物小游戏,使用方向键控制蛇移动,食物随机生成。游戏地图由井字墙和蛇身、食物符号表示。目前尚未实现计分系统,游戏包含初始化食物、创建地图、判断碰撞、移动蛇体等功能,采用多线程处理刷新屏幕、用户输入和食物生成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  训练C语言基础和调试能力的小游戏,这里所有物体(蛇身、墙体、食物)均由符号代替未作修饰。

e6916d5f83ee49e4a507a77259e3e568.gif

实现的主要功能:

1.方向键控制蛇的移动方向。

2.食物随机出现。

3.实时计分(还未写计分系统)。

————————————————————————————————————————————

项目代码:

#include<curses.h>
#include<stdio.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 *next;
};

int key;
int dir;
int score = -1;
struct Snake *head = NULL;
struct Snake *tail = NULL;
struct Snake food;

void* initFood()
{
        int x = rand()%20;
        int y = rand()%30;
        while(x==0 || y==0 || x==20 || y==30){
                x = rand()%20;
                y = rand()%30;
        }
        food.hang = x;
        food.lie = y;
        score+=1;
        noecho();
}

void initCury()
{
        initscr();
        keypad(stdscr,1);
        noecho();
}

int hasNode(int x,int y)
{
        struct Snake *p;
        p = head;
        while(p != NULL){
                if(p->hang == x && p->lie == y){
                        return 1;
                }
                p = p->next;
        }
        return 0;
}
int hasFood(int x,int y)
{
        if(food.hang == x && food.lie == y){
                return 1;
        }
        return 0;
}

void mapGame()
{
        int i,j;
        move(0,0);
        for(i=0;i<=19;i++){
                if(i == 0){
                        for(j=0;j<=30;j++){
                                printw("#");
                        }
                        printw("\n");
                }// 0  
                if(i>=0 || i<=19){
                        for(j=0;j<=30;j++){
                                if(j == 0 || j == 30){
                                        printw("#");
                                }
                                else if(hasNode(i,j)){
                                        printw("@");
                                }
                                else if(hasFood(i,j)){
                                        printw("$");
                                }
                                else{
                                        printw(" ");
                                }
                        }
                        printw("\n");
                }
                if(i == 19){
                        for(j=0;j<=30;j++){
                                printw("#");
                        }
                }
        }
        printw("\nScore: %d$\n",score);
        printw("food.x:%d, food.y:%d\n",food.hang,food.lie);
        printw("\n\n<By: Tao Xie, Feb22,2023>\n");
        noecho();
}

void addBody()
{
        struct Snake *newBody = (struct Snake*)malloc(sizeof(struct Snake));
        newBody->hang = tail->hang+1;
        newBody->lie  = tail->lie;
        newBody->next = NULL;
        switch(dir){
                case UP:
                        newBody->hang = tail->hang-1;
                        newBody->lie  = tail->lie;
                        break;
                case DOWN:
                        newBody->hang = tail->hang+1;
                        newBody->lie  = tail->lie;
                        break;
                case LEFT:
                        newBody->hang = tail->hang;
                        newBody->lie  = tail->lie-1;
                        break;
                case RIGHT:
                        newBody->hang = tail->hang;
                        newBody->lie  = tail->lie+1;
                        break;
        }
        tail->next = newBody;
        tail = newBody;
}

void delBody()
{
        struct Snake *p;
        p = head;
        head = head->next;
        free(p);
}

void initSnake()
{
        struct Snake *p;
        dir = DOWN;
        while(head != NULL){
                p = head;
                head = head->next;
                free(p);
        }
        initFood();
        head = (struct Snake*)malloc(sizeof(struct Snake));
        head->hang = 0;
        head->lie  = 15;
        head->next = NULL;
        tail = head;
        addBody();
        addBody();
}

int snakeKilled()
{
        struct Snake *p;
        p = head;
        if(tail->hang<0||tail->lie==0||tail->hang==20||tail->lie==30){
                return 1;
        }
        while(p->next!=NULL){
                if(p->hang==tail->hang && p->lie==tail->lie){
                        return 1;
                }
                p = p->next;
        }
        return 0;
}

void moveBody()
{
        addBody();
        if(hasFood(tail->hang,tail->lie)){
                initFood();
        }
        else{
                delBody();
        }
        if(snakeKilled()){
                score=-1;
                initSnake();
        }
}

void* refreshPage()
{
        while(1){
                moveBody();
                mapGame();
                refresh();
                usleep(150000);
        }
}

void turn(int direction)
{
        if(abs(dir) != abs(direction)){
                dir = direction;
        }
}

void* contKey()
{
        while(1){
                key = getch();
                switch(key){
                        case KEY_UP:
                                printw("key:UP\n");
                                turn(UP);
                                break;
                        case KEY_DOWN:
                                printw("key:DOWN\n");
                                turn(DOWN);
                                break;
                        case KEY_LEFT:
                                printw("key:LEFT\n");
                                turn(LEFT);
                                break;
                        case KEY_RIGHT:
                                printw("key:RIGHT\n");
                                turn(RIGHT);                                     
                                break;
                }
        }
        noecho();
}

void main()
{
        pthread_t t1;
        pthread_t t2;
        pthread_t t3;

        initCury();
        initSnake();
        mapGame();
        pthread_create(&t1,NULL,refreshPage,NULL);
        pthread_create(&t2,NULL,contKey,NULL);
        pthread_create(&t3,NULL,initFood,NULL);
        while(1);

        getch();
        endwin();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值