ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

贪吃蛇|C语言|终端输出操作

2021-01-22 17:59:18  阅读:168  来源: 互联网

标签:蛇头 globalMap int C语言 headerIndex 贪吃蛇 终端 snakeMap


输出贪吃蛇背景地图

贪吃蛇背景地图的最终效果如下图所示:


钻红色空心方框表示边框,绿色实心方框表示贪吃蛇的活动区域。

#include <stdio.h>
#include <conio.h>
#include <windows.h>
int main(){
    int width = 30, height = width;  //宽度和高度
    int x, y;  //x、y分别表示当前行和列
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  
    //设置窗口大小
    system("mode con: cols=64 lines=32");

    //打印背景,按行输出
    for(x=0; x<width; x++){
        for(y=0; y<height; y++){
            if(y==0 || y==width-1 || x==0 || x==height-1){  //输出边框
                SetConsoleTextAttribute(hConsole, 4 );
                printf("□");
            }else{  //贪吃蛇活动区域
                SetConsoleTextAttribute(hConsole, 2 );
                printf("■");
            }
        }
        printf("\n");
    }

    //暂停
    getch();
    return 0;
}

程序的关键是两层嵌套的循环。x=0 时,内层循环执行30次,输出第0行;x=1 时,内层循环又执行30次,输出1行。以此类推,直到 x=30,外层循环不再执行(内存循环当然也就没机会执行),输出结束。

注意,□和■虽然都是单个字符,但它们不在ASCII码范围内,是宽字符,占用两个字节,用 putchar 等输出ASCII码(一个字节)的函数输出时可能会出现问题,所以作为字符串输出。

 让贪吃蛇移动起来

接下来,我们来让一条长度为 n 的贪吃蛇移动起来,而且可以用WASD四个键控制移动方向,如下图所示:


其实,移动贪吃蛇并不需要移动所有节点,只需要添加蛇头、删除蛇尾,就会有动态效果,这样会大大提高程序的效率。

我们可以定义一个结构体来表示贪吃蛇一个节点在控制台上的位置(也即所在行和列):

struct POS{
int x; //所在行
int y; //所在列
}


然后再定义一个比贪吃蛇长的数组来保存贪吃蛇的所有节点:

struct POS snakes[n+m];

并设置两个变量 headerIndex、tailIndex,分别用来表示蛇头、蛇尾在数组中的下标坐标,这样每次添加蛇头、删除蛇尾时只需要改变两个变量的值就可以。

headerIndex 和 tailIndex 都向前移动,也就是每次减1。如果 headerIndex=0,也就是指向数组的头部,那么下次移动时 headerIndex = arrayLength - 1,也就是指向数组的尾部,就这样一圈一圈地循环,tailIndex 也是如此。这相当于把数组首尾相连成一个圆圈,贪吃蛇在这个圆圈中不停地转圈。

#include <stdio.h>
#include <conio.h>
#include <windows.h>

//贪吃蛇背景的宽度和高度
#define HEIGHT (30)
#define WIDTH HEIGHT

//背景的中心位置
int xCenter = HEIGHT%2==0 ? HEIGHT/2 : HEIGHT/2+1;
int yCenter = WIDTH%2==0 ? WIDTH/2 : WIDTH/2+1;

//贪吃蛇的长度
int snakesLen = 10;
//贪吃蛇的最大长度
int snakesMaxLen = (HEIGHT-2) * (WIDTH-2);

//一个供贪吃蛇移动的结构体数组
struct{
    int x;
    int y;
}snakes[(HEIGHT-2) * (WIDTH-2)];

//蛇头、蛇尾在snakes数组中的下标(索引)
int headerIndex, tailIndex;

HANDLE hConsole;  //控制台句柄

// 设置光标位置,x为行,y为列
void setPosition(int x, int y){
    COORD coord;
    coord.X = 2*y;
    coord.Y = x;
    SetConsoleCursorPosition(hConsole, coord);
}

// 设置颜色
void setColor(int color){
    SetConsoleTextAttribute(hConsole, color);
}

//初始化
void init(){
    int xCenter = HEIGHT%2==0 ? HEIGHT/2 : HEIGHT/2+1;
    int yCenter = WIDTH%2==0 ? WIDTH/2 : WIDTH/2+1;
    int x, y, i;
    int offset;
    CONSOLE_CURSOR_INFO cci;
   
    headerIndex = 0;
    tailIndex = snakesLen-1;

    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    //隐藏光标
    GetConsoleCursorInfo(hConsole, &cci);
    cci.bVisible = 0;
    SetConsoleCursorInfo(hConsole, &cci);
   
    //设置窗口大小
    system("mode con: cols=64 lines=32");

    //打印背景,按行输出
    for(x=0; x<WIDTH; x++){
        for(y=0; y<HEIGHT; y++){
            if(y==0 || y==WIDTH-1 || x==0 || x==HEIGHT-1){  //输出边框
                setColor(4);
                printf("□");
            }else{  //贪吃蛇活动区域
                setColor(2);
                printf("■");
            }
        }
        printf("\n");
    }

    //输出贪吃蛇并初始化snake数组
    offset = snakesLen%2==0 ? snakesLen/2 : snakesLen/2-1;
    setPosition(xCenter, yCenter-offset);
    setColor(0xe);
   
    for(i=0; i<snakesLen; i++){
        printf("%s", "★");
        snakes[i].x = xCenter;
        snakes[i].y = yCenter-offset+i;
    }
}

// 蛇移动
void move(char direction){
    int headerX = snakes[headerIndex].x;
    int headerY = snakes[headerIndex].y;
    switch(direction){
        case 'w':
            headerX--;
            break;
        case 's':
            headerX++;
            break;
        case 'a':
            headerY--;
            break;
        case 'd':
            headerY++;
            break;
    }

    //输出新蛇头
    setPosition(headerX, headerY);
    setColor(0xe);
    printf("%s", "★");

    //删除蛇尾
    setPosition(snakes[tailIndex].x, snakes[tailIndex].y);
    setColor(2);
    printf("%s", "■");
   
    //设置headerIndex和tailIndex
    headerIndex = headerIndex==0 ? snakesMaxLen-1 : headerIndex-1;
    snakes[headerIndex].x = headerX;
    snakes[headerIndex].y = headerY;
    tailIndex = tailIndex==0 ? snakesMaxLen-1 : tailIndex-1;
}

//下次移动的方向
char nextDirection(char ch, char directionOld){
    int sum = ch+directionOld;
    ch = tolower(ch);
    if( (ch=='w' || ch=='a' || ch=='s' || ch=='d') && sum!=197 && sum!=234 ){
        return ch;
    }else{
        return directionOld;
    }
}

int main(){
    char charInput, direction = 'a';
    init();
   
    charInput = tolower(getch());
    direction = nextDirection(charInput, direction);

    //不停地移动贪吃蛇
    while(1){
        if(kbhit()){
            charInput = tolower(getch());
            direction = nextDirection(charInput, direction);
        }
        move(direction);
        Sleep(500);
    }
    return 0;
}

对代码的说明

1) 贪吃蛇的最大长度为绿色方框的个数,所以我们将容纳贪吃蛇的数组 snakes 的长度定义为(HEIGHT-2) * (WIDTH-2)

2) □、■、★ 占用两个字符的宽度,所以在 setPosition() 中该变光标位置时,光标的X坐标应该是:

coord.X = 2*y;

 

随机生成食物

食物的生成是贪吃蛇游戏的难点,因为食物只能在绿色背景(■)部分生成,它不能占用钻红色边框(□)和贪吃蛇本身(★)的位置。

最容易想到的思路是:随机生成一个坐标,然后检测该坐标是不是绿色背景,如果是,那么成功生成,如果不是,继续生成随机数,继续检测。幸运的话,可以一次生成;不幸的话,可能要循环好几次甚至上百次才能生成,这样带来的后果就是程序卡死一段时间,贪吃蛇不能移动。

这种方案的优点就是思路简单,容易实现,缺点就是贪吃蛇移动不流畅,经常会卡顿。

改进的方案

最好的方案是生成的随机数一定会在绿色背景的范围内,这样一次就能成功生成食物。该如何实现呢?

这里我们提供了一种看起来不容易理解却行之有效的方案。

我们不妨将贪吃蛇的活动范围称为“贪吃蛇地图”,而加上边框就称为“全局地图”。首先定义一个二维的结构体数组,用来保存所有的点(也即全局地图):

 
  1. struct{
  2. char type;
  3. int index;
  4. }globalMap[MAXWIDTH][MAXHEIGHT];

MAXWIDTH 为宽度,也即列数;MAXHEIGHT 为高度,也即行数。成员 type 表示点的类型,它可以是食物、绿色背景、边框和贪吃蛇节点。

直观上讲,应该将 type 定义为int类型,不过int占用四个字节,而节点类型的取值范围非常有限,一个字节就足够了,所以为了节省内存才定义为char类型。

然后再定义一个一维的结构体数组,用来保存贪吃蛇的有效活动范围:

 
  1. struct{
  2. int x;
  3. int y;
  4. } snakeMap[ (MAXWIDTH-2)*(MAXHEIGHT-2) ];

x、y 表示行和列,也就是 globalMap 数组的两个下标。globalMap 数组中的 index 成员就是 snakeMap 数组的下标。

globalMap 表示了所有节点的信息,而 snakeMap 只表示了贪吃蛇的活动区域。通过 snakeMap 可以定位 globalMap 中的元素,反过来通过 globalMap 也可以找到 snakeMap 中的元素。它们之间的对应关系请看下图:


图1:globalMap 和 snakeMap 的初始对应关系


贪吃蛇向左移动时,headerIndex 指向 404,tailIndex指向 406。

在 snakeMap 数组中,贪吃蛇占用一部分元素,剩下的元素都是绿色的背景,可以随机选取这些元素中的一个作为食物,然后通过 x、y 确定食物的坐标。而这个坐标,一定在绿色背景范围内。

需要注意的是,在贪吃蛇移动过程中需要维护 globalMap 和 snakeMap 的对应关系。

这种方案的另外一个优点就是,贪吃蛇移动时很容易知道下一个节点的类型,不用遍历数组就可以知道是否与自身相撞。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <time.h>
#include <windows.h>

//MAXWIDTH、MAXHEIGHT、INITLEN 以字符记
#define MAXWIDTH (30)
#define MAXHEIGHT MAXWIDTH
#define INITLEN (3)  //贪吃蛇的初始长度 

//程序中用到的各种字符,以及它们的颜色和类型(以数字表示)
struct{
    char *ch;
    int color;
    char type;
}
charBorder = {"□", 4, 1},  //边框
charBg = {"■", 2, 2},  //背景
charSnake = {"★", 0xe, 3},  //贪吃蛇节点
charFood = {"●", 0xc, 4};  //食物

//用一个结构体数组保存地图中的各个点
struct{
    char type;
    int index;
}globalMap[MAXWIDTH][MAXHEIGHT];


//贪吃蛇有效活动范围地图的索引
struct{
    int x;
    int y;
} snakeMap[ (MAXWIDTH-2)*(MAXHEIGHT-2) ], scoresPostion;

int scores = 0;  //得分
int snakeMapLen = (MAXWIDTH-2)*(MAXHEIGHT-2);
int headerIndex, tailIndex;  //蛇头蛇尾对应的snakeMap中的索引(下标)
HANDLE hStdin;  //控制台句柄

// 设置光标位置,x为行,y为列
void setPosition(int x, int y){
    COORD coord;
    coord.X = 2*y;
    coord.Y = x;
    SetConsoleCursorPosition(hStdin, coord);
}

// 设置颜色
void setColor(int color){
    SetConsoleTextAttribute(hStdin, color);
}

//创建食物
void createFood(){
    int index, rang, x, y;
    //产生随机数,确定 snakeMap 数组的索引 
    srand((unsigned)time(NULL));
    if(tailIndex<headerIndex){
        rang = headerIndex-tailIndex-1;
        index = rand()%rang + tailIndex + 1;
    }else{
        rang = snakeMapLen - (tailIndex - headerIndex+1);
        index = rand()%rang;
        if(index>=headerIndex){
            index += (tailIndex-headerIndex+1);
        }
    }
    
    x = snakeMap[index].x;
    y = snakeMap[index].y;
    setPosition(x, y);
    setColor(charFood.color);
    printf("%s", charFood.ch);
    globalMap[x][y].type=charFood.type;
}

//死掉
void die(){
    int xCenter = MAXHEIGHT%2==0 ? MAXHEIGHT/2 : MAXHEIGHT/2+1;
    int yCenter = MAXWIDTH%2==0 ? MAXWIDTH/2 : MAXWIDTH/2+1;

    setPosition(xCenter, yCenter-5);
    setColor(0xC);

    printf("You die! Game Over!");
    getch();
    exit(0);
}

// 蛇移动
void move(char direction){
    int newHeaderX, newHeaderY;  //新蛇头的坐标
    int newHeaderPreIndex;  //新蛇头坐标以前对应的索引
    int newHeaderPreX, newHeaderPreY;  //新蛇头的索引以前对应的坐标
    int newHeaderPreType;  //新蛇头以前的类型
    int oldTailX, oldTailY;  //老蛇尾坐标
    // -----------------------------------------------
    //新蛇头的坐标
    switch(direction){
        case 'w':
            newHeaderX = snakeMap[headerIndex].x-1;
            newHeaderY = snakeMap[headerIndex].y;
            break;
        case 's':
            newHeaderX = snakeMap[headerIndex].x+1;
            newHeaderY = snakeMap[headerIndex].y;
            break;
        case 'a':
            newHeaderX = snakeMap[headerIndex].x;
            newHeaderY = snakeMap[headerIndex].y-1;
            break;
        case 'd':
            newHeaderX = snakeMap[headerIndex].x;
            newHeaderY = snakeMap[headerIndex].y+1;
            break;
    }
    //新蛇头的索引
    headerIndex = headerIndex==0 ? snakeMapLen-1 : headerIndex-1;
    // -----------------------------------------------
    //新蛇头坐标以前对应的索引
    newHeaderPreIndex = globalMap[newHeaderX][newHeaderY].index;
    //新蛇头的索引以前对应的坐标
    newHeaderPreX = snakeMap[headerIndex].x;
    newHeaderPreY = snakeMap[headerIndex].y;

    //双向绑定新蛇头索引与坐标的对应关系
    snakeMap[headerIndex].x = newHeaderX;
    snakeMap[headerIndex].y = newHeaderY;
    globalMap[newHeaderX][newHeaderY].index = headerIndex;

    //双向绑定以前的索引与坐标的对应关系
    snakeMap[newHeaderPreIndex].x = newHeaderPreX;
    snakeMap[newHeaderPreIndex].y = newHeaderPreY;
    globalMap[newHeaderPreX][newHeaderPreY].index = newHeaderPreIndex;

    //新蛇头以前的类型
    newHeaderPreType = globalMap[newHeaderX][newHeaderY].type;
    //设置新蛇头类型
    globalMap[newHeaderX][newHeaderY].type = charSnake.type;
    // 判断是否出界或撞到自己
    if(newHeaderPreType==charBorder.type || newHeaderPreType==charSnake.type ){
        die();
    }
    //输出新蛇头
    setPosition(newHeaderX, newHeaderY);
    setColor(charSnake.color);
    printf("%s", charSnake.ch);
    //判断是否吃到食物
    if(newHeaderPreType==charFood.type){  //吃到食物
        createFood();
        //更改分数
        setPosition(scoresPostion.x, scoresPostion.y);
        printf("%d", ++scores);
    }else{
        //老蛇尾坐标
        oldTailX = snakeMap[tailIndex].x;
        oldTailY = snakeMap[tailIndex].y;
        //删除蛇尾
        setPosition(oldTailX, oldTailY);
        setColor(charBg.color);
        printf("%s", charBg.ch);
        globalMap[oldTailX][oldTailY].type = charBg.type;
        tailIndex = (tailIndex==0) ? snakeMapLen-1 : tailIndex-1;
    }
}

//下次移动的方向
char nextDirection(char ch, char directionOld){
    int sum = ch+directionOld;
    ch = tolower(ch);
    if( (ch=='w' || ch=='a' || ch=='s' || ch=='d') && sum!=197 && sum!=234 ){
        return ch;
    }else{
        return directionOld;
    }
}

//暂停
char pause(){
    return getch();
}

// 初始化
void init(){
    // 设置相关变量 
    int x, y, i, index;
    int xCenter = MAXHEIGHT%2==0 ? MAXHEIGHT/2 : MAXHEIGHT/2+1;
    int yCenter = MAXWIDTH%2==0 ? MAXWIDTH/2 : MAXWIDTH/2+1;
    CONSOLE_CURSOR_INFO cci;  //控制台光标信息

    //判断相关设置是否合理
    if(MAXWIDTH<16){
        printf("'MAXWIDTH' is too small!");
        getch();
        exit(0);
    }

    //设置窗口大小 
    system("mode con: cols=96 lines=32");

    //隐藏光标
    hStdin = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleCursorInfo(hStdin, &cci);
    cci.bVisible = 0;
    SetConsoleCursorInfo(hStdin, &cci);
    
    //打印背景 
    for(x=0; x<MAXHEIGHT; x++){
        for(y=0; y<MAXWIDTH; y++){
            if(y==0 || y==MAXWIDTH-1 || x==0 || x==MAXHEIGHT-1){
                globalMap[x][y].type = charBorder.type;
                setColor(charBorder.color);
                printf("%s", charBorder.ch);
            }else{
                index = (x-1)*(MAXWIDTH-2)+(y-1);
                snakeMap[index].x= x;
                snakeMap[index].y= y;
                globalMap[x][y].type = charBg.type;
                globalMap[x][y].index = index;

                setColor(charBg.color);
                printf("%s", charBg.ch);
            }
        }
        printf("\n");
    }
    
    //初始化贪吃蛇
    globalMap[xCenter][yCenter-1].type = globalMap[xCenter][yCenter].type = globalMap[xCenter][yCenter+1].type = charSnake.type;

    headerIndex = (xCenter-1)*(MAXWIDTH-2)+(yCenter-1) - 1;
    tailIndex = headerIndex+2;
    setPosition(xCenter, yCenter-1);
    setColor(charSnake.color);
    for(y = yCenter-1; y<=yCenter+1; y++){
        printf("%s", charSnake.ch);
    }
    //生成食物
    createFood();

    //设置程序信息
    setPosition(xCenter-1, MAXWIDTH+2);
    printf("   Apples : 0");
    setPosition(xCenter, MAXWIDTH+2);
    printf("   Author : xiao p");
    setPosition(xCenter+1, MAXWIDTH+2);
    printf("Copyright : c.biancheng.net");
    scoresPostion.x = xCenter-1;
    scoresPostion.y = MAXWIDTH+8;
}

int main(){
    char charInput, direction = 'a';
    init();
    
    charInput = tolower(getch());
    direction = nextDirection(charInput, direction);

    while(1){
        if(kbhit()){
            charInput = tolower(getch());
            if(charInput == ' '){
                charInput = pause();
            }
            direction = nextDirection(charInput, direction);
        }
        move(direction);
        Sleep(500);
    }
    
    getch();
    return 0;
}

 

标签:蛇头,globalMap,int,C语言,headerIndex,贪吃蛇,终端,snakeMap
来源: https://blog.csdn.net/qq_43629083/article/details/112993424

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有