`
maosuhan
  • 浏览: 109861 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

用纯命令行编写贪吃蛇

    博客分类:
  • cc++
阅读更多

开发环境为linux+gcc+netbeans

本来我喜欢gvim但是实在不会调试c语言。于是转战netbeans,能很方便地调试,就是爽。

 

首先要说一下思路。就是多线程,一个线程负责逻辑和画图,一个线程监听按键。看一下线程创建的代码。

 

void * waitForKey(void *para) {
    while (1) {
        input = getch();
    }
}

pthread_t id; //声明一个linux线程,按键等待线程

 int ret;
    ret = pthread_create(&id, NULL, waitForKey, NULL);//创建线程
    if (ret != 0) {
        exit(1);
    }
 

我还用到了一个库,就是curses,这个好象是专门用来绘图ui用的。但是在/usr/include里面是没有的,要到网上去下

sudo apt-get install libncurses-dev

使用时的格式应该是这样

 

initscr();

do_some_drawing();

refresh();

endwin();

其中我用到的函数有move(x,y)是把光标定位在某行某列上。还有addStr(s)和addch(c)。是在光标处写字符串和写字符。

还有getch()等待用户按键。还有refresh(),将缓冲的addch阿还有addStr等等的绘图操作都输出到屏幕上。

 

此外还用到了usleep(int )函数这里的参数是 int型的,表示的是微秒数,1秒等于1000000微秒。这里的时间间隔是蛇每次移动时的间隔时间。

 

代码如下

#include<stdio.h>
#include <pthread.h>
#include <sys/time.h>
#include <curses.h>
#include <stdlib.h>

#define MAX_X 70   //场地宽
#define MAX_Y 20  //场地长
#define CORNER_X 4  //左上角x坐标
#define CORNER_Y 2   //左上角y坐标

struct point { 
    int x;
    int y;
};
struct point SnakeBody[50]; 
struct point food;

int Length = 4;  //初始蛇长
int life = 1;   //是否还活着
int input = 0;  //记录键盘按键的ascii
pthread_t id; //声明一个linux线程,按键等待线程


void FoodCheck();
void FoodProduce();
void Initializition();
void SnakeHeadMovement();
void DeathCheck();
void Paint();
void * waitForKey(void *);
void drawDot(int x, int y,char s);
void clearDot(int x, int y);
void end();


//主函数

int main(int argc, char** argv) {

    Initializition();
    while (life) {
        Paint();
        usleep(200000);
        SnakeHeadMovement();
        DeathCheck();
    }
    end();
    return 0;
}

void * waitForKey(void *para) {
    while (1) {
        input = getch();
    }
}

void end() {

    move(1, 0);
    addstr("Press any key to quit!");
    refresh();
    getch();
   endwin();
}


//食物的随机产生

void FoodProduce() {
    int superposition = 0;
    int i;
    srand(time(NULL));
    do {
        food.x = (rand() % ((MAX_X-2) / 2))*2+2;  //2 to MAX_X-2  and is 偶数
        food.y = rand() % (MAX_Y-1)+1;   //1 to MAX_Y-1
        for (i = 0; i < Length; i++) {
            if (food.x == SnakeBody[i].x && food.y == SnakeBody[i].y)
                superposition = 1;
        }
    } while (superposition); /*直到没有重合*/
}

//蛇身和食物的初始化 初始化的蛇身为4节长度

void Initializition() {
    initscr();//curses初始化

    int i;
    for (i = 3; i <= 6; i++) {//初始化蛇
        SnakeBody[6 - i].x = 4;
        SnakeBody[6 - i].y = i;
    }
    FoodProduce();
    int ret;
    ret = pthread_create(&id, NULL, waitForKey, NULL);//创建线程
    if (ret != 0) {
        exit(1);
    }

    for ( i = 0; i <= MAX_X; i+=2) {  //画围墙
        drawDot(i, 0,'*');
        drawDot(i, MAX_Y,'*');
    }

    for (i = 0; i <= MAX_Y; i++) {
        drawDot(0, i,'*');
        drawDot(MAX_X, i,'*');
    }

}

//蛇移动,依次从尾巴到头赋值
void SnakeBodyMovement() {
    int i;
    for (i = Length - 1; i > 0; i--) {
        SnakeBody[i].x = SnakeBody[i - 1].x;
        SnakeBody[i].y = SnakeBody[i - 1].y;
    }

}


void SnakeHeadMovement() {

    clearDot(SnakeBody[Length - 1].x, SnakeBody[Length - 1].y);
    int directionX, directionY; /*定义原本蛇前进的方向,可通过蛇头坐标减去蛇的第二部分*/
    int newX, newY;
    newX = SnakeBody[0].x;
    newY = SnakeBody[0].y;

    directionX = SnakeBody[0].x - SnakeBody[1].x;
    directionY = SnakeBody[0].y - SnakeBody[1].y;
    

   

    if (input == 'w' && directionY<=0) //不走回头路
        newY--;
    else if (input == 's' && directionY>=0 )
        newY++;
    else if (input == 'a' && directionX<=0)
        newX -= 2; /*因为字符高是宽的两倍*/
    else if (input == 'd' && directionX>=0)
        newX += 2;
    else {
        newX += directionX;
        newY += directionY;
    }
    FoodCheck(); 
    SnakeBodyMovement();
    SnakeBody[0].x = newX;
    SnakeBody[0].y = newY;

}
//判断是否吃到食物,以及吃到后长度变长还有产生新的食物

void FoodCheck() {
    if (food.x == SnakeBody[0].x && food.y == SnakeBody[0].y) {
        Length = Length + 1;
        FoodProduce();
    }
}
//判断是否死亡

void DeathCheck() {
    int i;
    if (SnakeBody[0].x <=1 || SnakeBody[0].x >= MAX_X  || SnakeBody[0].y <= 0 || SnakeBody[0].y >=MAX_Y)
        life = 0;
    for (i = 4; i < Length; i++)
        if (SnakeBody[0].x == SnakeBody[i].x && SnakeBody[0].y == SnakeBody[i].y)
            life = 0;
}

//排序和打印

void Paint() {
    int i = 0;

    drawDot(SnakeBody[i].x, SnakeBody[i].y,'@');
    for (i=1; i < Length; i++) {
        drawDot(SnakeBody[i].x, SnakeBody[i].y,'*');
    }
    drawDot(food.x, food.y,'$');
    clearDot(0, 0);
    refresh();//刷新画布
}

void drawDot(int x, int y,char s) {
    move(y+CORNER_Y, x+CORNER_X);
    addch(s);
}

void clearDot(int x, int y) {
    move(y+CORNER_Y, x+CORNER_X);
    addch(' ');
}

 

编译的时候的命令如下 gcc main.c -o main -lpthread -lcurses

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics