贪吃蛇源代码(低配版)

这个代码是刚学完C语言在没看任何别人贪吃蛇代码情况下,纯粹按自己理解写出来的。用了三天时间吧,反正没事就想。用数组搞定,成功运行后自己很开心,觉得自己很牛B,但实际的游戏体验并不好,用cls清屏,屏幕狂闪。后来在网上找到了原因,说用双缓冲能解决。但是看了半天也没理解。倒是误打误撞用一个重新定位光标的方法解决了(这游戏画面不好,只能说有贪吃蛇的最基本功能)。后面去看了别人家的代码,才知道自己要改进的地方太多了。

/**********************************************
*程序名称:贪吃蛇游戏
*作者相关:Neal Caffrey(893399065)
*修改时间:2017-05-14
***********************************************/
#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <conio.h>
/**************宏定义区*****************/
#define HIGH    20
#define WIDTH   40
#define BLANK  32
#define HEAD  1
#define BOODY  2
#define FOOD 3
#define WALL 4
/***************结构体定义区************/
typedef struct INFO {
    int x;
    int y;
    struct INFO *next;
} SNAKE;
void Initialize(char data[HIGH][WIDTH]);                            //初始化
SNAKE *Beign(char data[HIGH][WIDTH], char *direction);              //开始
SNAKE *Find_Tail(SNAKE *head);                                      //找尾巴
SNAKE Food(char data[HIGH][WIDTH]);                                 //随机产生食物
char Option(char direction);                                        //操作
SNAKE *Move(char data[HIGH][WIDTH], SNAKE *head, char direction,SNAKE *food);   //移动
void Display(char data[HIGH][WIDTH]);                               //显示
void Free_Snake(SNAKE *head);                                       //释放链表
int Game();
int main(void) {
    char sign;
    system("color 81");
    Game();
    return 0;
}
int Game() {
    int i, j;
    char data[HIGH][WIDTH];
    char direction;
    SNAKE *head;
    SNAKE food;
    Initialize(data);
    head = Beign(data, &direction);
    food = Food(data);
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD coord = {0,0};
    CONSOLE_CURSOR_INFO cci;
    cci.bVisible=0;
    cci.dwSize=1;
    SetConsoleCursorInfo(handle, &cci);
    while(1) {
        direction = Option(direction);
        if((head = Move(data,  head, direction, &food)) == NULL) {
            getch();
            return 1;
        }
        Display(data);
        SetConsoleCursorPosition(handle, coord);
        Sleep(200);

    }
}
/*初始化*/
void Initialize(char data[HIGH][WIDTH]) {
    int i, j;
    for(i=0; i<HIGH; i++) {
        for(j=0; j<WIDTH-1; j++)
            data[i][j] = WALL;  //墙
        data[i][j] = '\0';
    }
    for(i=1; i<HIGH-1; i++)
        for(j=1; j<WIDTH-2; j++)
            data[i][j] = BLANK; //空白
}
/*开始*/
SNAKE* Beign(char data[HIGH][WIDTH],char *direction) {
    int x, y;
    int i;
    SNAKE *head, *p1, *p2;
    int random;
    head = (SNAKE *)malloc(sizeof(SNAKE));
    srand(time(NULL));
    x = rand()%(HIGH-4)+2;
    y = rand()%(WIDTH-10)+5;
    head->x = x;
    head->y = y;
    data[head->x][head->y] = HEAD;
    p1 = head;
    for(i=1; i<3; i++) {
        p2 = (SNAKE *)malloc(sizeof(SNAKE));
        p2->x = x;
        p2->y = y-i;
        p1->next = p2;
        p1 = p2;
    }
    p2->next = head;
    *direction = 'd';
    return head;
}
SNAKE *Find_Tail(SNAKE *head) {
    SNAKE *p1;
    p1 = head;
    do {
        p1 = p1->next;
    } while(p1->next != head);
    return p1;
}
SNAKE Food(char data[HIGH][WIDTH]) {
    int x, y;
    SNAKE food;
    while(1) {
        srand(time(NULL));
        x = rand()%HIGH;
        y = rand()%WIDTH;
        if(data[x][y] == BLANK) {
            food.x = x;
            food.y = y;
            data[x][y] = FOOD;
            return food;
        }
    }
}

char Option(char direction) {
    char temp ;
    if(kbhit() != 0) {
        while(kbhit() != 0)
            temp = getch();
        switch(temp) {
            case 'w':
                if(direction == 's')
                    return direction;
                direction = temp;
                break;
            case 's':
                if(direction == 'w')
                    return direction;
                direction = temp;
                break;
            case 'a':
                if(direction == 'd')
                    return direction;
                direction = temp;
                break;
            case 'd':
                if(direction == 'a')
                    return direction;
                direction = temp;
                break;
            default:
                break;
        }
    }
    return direction;
}

SNAKE *Move(char data[HIGH][WIDTH], SNAKE *head, char direction,SNAKE *food) {
    SNAKE *tail, *p1;
    tail = Find_Tail(head);
    SNAKE temp;
    switch(direction) {
        case 'w': {
            temp.x = (head)->x-1;
            temp.y = (head)->y;
            break;
        }
        case 's': {
            temp.x = (head)->x+1;
            temp.y = (head)->y;
            break;
        }
        case 'a': {
            temp.x = (head)->x;
            temp.y = (head)->y-1;
            break;
        }
        case 'd': {
            temp.x = (head)->x;
            temp.y = (head)->y+1;
            break;
        }
    }
    if(data[temp.x][temp.y] == BLANK) {
        data[temp.x][temp.y] = HEAD;
        data[head->x][head->y] = BOODY;
        data[tail->x][tail->y] = BLANK;
    } else if(data[temp.x][temp.y] == FOOD) {
        p1 = (SNAKE*)malloc(sizeof(SNAKE));
        p1->x = temp.x;
        p1->y = temp.y;
        *food = Food(data);
        p1->next = head;
        tail->next = p1;
        data[temp.x][temp.y] = HEAD;
        data[head->x][head->y] = BOODY;
        data[tail->x][tail->y] = BOODY;
        return p1;
    } else {
//      system("cls");
        printf("\n\n\n\n\n\n\n\n\n\n\n\n\nYou have died!\n");
        Free_Snake(head);
        return NULL;
    }
    tail->x = temp.x;
    tail->y = temp.y;
    return tail;
}

void Display(char data[HIGH][WIDTH]) {
    int i;
    for(i=0; i<HIGH; i++)
        puts(data[i]);
}
void Free_Snake(SNAKE *head) {
    SNAKE *p1;
    SNAKE *p2;
    p2 = head;
    do {
        p1 = head;
        head = head->next;
        free(p1);
    } while(head != p2);
}

-

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值