当前位置: 移动技术网 > IT编程>开发语言>C/C++ > c++贪吃蛇

c++贪吃蛇

2019年06月13日  | 移动技术网IT编程  | 我要评论

精英乒乓网,抚顺网,猥琐委员长小游戏

显示方式:清屏打印二位数组,数组即游戏地图,包括墙面(用‘w’表示),蛇(‘h’表蛇头,‘b’表身体)和食物(用‘f’表示)。

 

const int maxmap = 20;
char map[maxmap][maxmap];

边缘为墙面:

    for (int i = 0; i < maxmap; i++){
        map[0][i] = 'w';
        map[maxmap - 1][i] = 'w';
    }
    for (int i = 1; i < maxmap-1; i++){
        map[i][0] = 'w';
        map[i][maxmap - 1] = 'w';
    }

蛇的身体用结构体连接:

struct snake{
    int x;
    int y;
    struct snake *next;
}head,body1,body2,tail;

初始蛇长为4,head表示蛇头,其next指向null;tail表示蛇尾,进食后增加的身体加到tail之前。

    int centerpoint = maxmap / 2;
    head = { centerpoint, centerpoint, null };
    body1 = { centerpoint, centerpoint-1, &head };
    body2 = { centerpoint, centerpoint - 2, &body1 };
    tail = { centerpoint, centerpoint - 3, &body2 };

    struct snake *p = &tail;
    while (p != null){
        map[p->x][p->y] = 'b';
        p = p->next;
    }
    map[head.x][head.y] = 'h';

引入一个随机种子,生成食物:

void food(){
    int x = 0, y = 0;
    srand(time(0));
    while (map[x][y]!=' '){
        x = rand() % (maxmap - 1) + 1;
        y = rand() % (maxmap - 1) + 1;
    }
    map[x][y] = 'f';
    return;
}

蛇的移动:

首先定义蛇的默认移动方向,这里默认为右(wsad)。

char direction = 'd';

接收键盘输入的移动方向,并将其替换为默认方向;如果键盘未输入,则为默认方向。

(蛇不能向身体方向移动,此方向无效。)

(nextx,nexty表示蛇头下一个位置与此时的相对位置。)

计算蛇头应出现的下一个位置。蛇的身体跟随移动,清除原蛇尾。

    char letter = direction;
    struct snake *p = &tail;
    int nextx = 0, nexty = 0, tailx = p->x, taily = p->y;
    if (_kbhit()){
        letter = _getch();
        if (letter == 'w' || letter == 's' || letter == 'a' || letter == 'd'){
            if (0 != (direction - letter)){
                if (!((letter == 'w' && direction == 's') ||
                    (letter == 's' && direction == 'w') ||
                    (letter == 'a' && direction == 'd') ||
                    (letter == 'd' && direction == 'a')))
                    direction = letter;
            }
        }
    }
    switch (direction){
    case 'w':
        nextx = -1;
        nexty = 0;
        break;
    case 's':
        nextx = 1;
        nexty = 0;
        break;
    case 'a':
        nextx = 0;
        nexty = -1;
        break;
    case 'd':
        nextx = 0;
        nexty = 1;
        break;
    default:
        break;
    }
    map[p->x][p->y] = ' ';

蛇身的移动,蛇头位置的计算

    while (p->next != null){
        
        p->x = p->next->x;
        p->y = p->next->y;
        map[p->x][p->y] = 'b';
        p = p->next;
    }
    
    map[head.x][head.y] = 'b';
    head.x += nextx;
    head.y += nexty;

 

吃食和游戏结束判定:

蛇头出现的下一个位置如果为墙面或自己的身体,游戏结束。

bool gameover(int x,int y){
    if (map[x][y] == 'w' || map[x][y] == 'b')
        return 1;
    else
        return 0;
}

蛇头出现的下一个位置如果有食物,蛇长加1。

bool eat(int x,int y){
    if (map[x][y] == 'f'){
        food();
        return 1;
    }
    else{
        return 0;
    }
}

进食后,new一个新身体:

if (eat(head.x, head.y)){
        snake *newbody = new snake;
        newbody->x = tail.x; newbody->y = tail.y; newbody->next = tail.next;
        tail.x = tailx, tail.y = taily;
        tail.next = newbody;
        map[tailx][taily] = 'b';
        snakelength++;
    }
    map[head.x][head.y] = 'h';

 

显示:

循环执行蛇的移动过程,并清屏打印地图。

 

完整代码:

环境:win64,vs2010

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网