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

  训练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();
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的贪吃蛇游戏的C语言代码,希望能帮到你: ```c #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> #include <time.h> #define WIDTH 80 // 游戏区域宽度 #define HEIGHT 24 // 游戏区域高度 #define INIT_LENGTH 5 // 贪吃蛇初始长度 int score; // 得分 int speed; // 速度 int gameOver; // 游戏是否结束 int foodX, foodY; // 食物的位置 // 贪吃蛇的结构体 struct snake { int x, y; // 贪吃蛇的位置 struct snake *next; // 指向下一个节点的指针 }; struct snake *head, *tail; // 贪吃蛇的头和尾指针 // 初始化游戏 void init() { int i; struct snake *p; // 初始化贪吃蛇 head = tail = (struct snake*)malloc(sizeof(struct snake)); head->x = WIDTH / 2; head->y = HEIGHT / 2; head->next = NULL; for (i = 1; i < INIT_LENGTH; i++) { p = (struct snake*)malloc(sizeof(struct snake)); p->x = head->x - i; p->y = head->y; p->next = head; head = p; } // 初始化得分、速度和游戏结束标志 score = 0; speed = 100; gameOver = 0; // 随机生成食物的位置 srand(time(NULL)); foodX = rand() % (WIDTH - 2) + 1; foodY = rand() % (HEIGHT - 2) + 1; } // 显示游戏画面 void display() { int i, j; struct snake *p; // 清屏 system("cls"); // 绘制游戏区域 for (i = 0; i <= HEIGHT; i++) { for (j = 0; j <= WIDTH; j++) { if (i == 0 || i == HEIGHT || j == 0 || j == WIDTH) { printf("#"); } else if (i == foodY && j == foodX) { printf("*"); } else { p = head; while (p) { if (p->x == j && p->y == i) { printf("O"); break; } p = p->next; } if (!p) { printf(" "); } } } printf("\n"); } // 显示得分和速度 printf("Score: %d Speed: %d\n", score, speed); } // 更新贪吃蛇的位置 void update() { int dx = 0, dy = 0; struct snake *p; // 获取键盘输入 if (_kbhit()) { switch (_getch()) { case 'w': // 上 dx = 0; dy = -1; break; case 's': // 下 dx = 0; dy = 1; break; case 'a': // 左 dx = -1; dy = 0; break; case 'd': // 右 dx = 1; dy = 0; break; } } // 计算贪吃蛇的新位置 p = (struct snake*)malloc(sizeof(struct snake)); p->x = head->x + dx; p->y = head->y + dy; p->next = head; // 判断是否吃到了食物 if (p->x == foodX && p->y == foodY) { score++; speed -= 5; if (speed < 20) { speed = 20; } foodX = rand() % (WIDTH - 2) + 1; foodY = rand() % (HEIGHT - 2) + 1; } else { tail = tail->next; free(tail->next); } // 判断是否碰到了边界或自己 if (p->x <= 0 || p->x >= WIDTH || p->y <= 0 || p->y >= HEIGHT) { gameOver = 1; } else { struct snake *q = head->next; while (q) { if (p->x == q->x && p->y == q->y) { gameOver = 1; break; } q = q->next; } } // 更新贪吃蛇的位置 if (!gameOver) { head = p; } } // 游戏主循环 void gameLoop() { while (!gameOver) { display(); update(); Sleep(speed); } } // 结束游戏 void gameOverFunc() { printf("Game Over!\n"); printf("Your score is: %d\n", score); system("pause"); } int main() { init(); gameLoop(); gameOverFunc(); return 0; } ``` 这是一个比较简单的贪吃蛇游戏代码,你可以根据自己的需求进行修改和补充。希望对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值