C snake C语言贪吃蛇

C语言 贪吃蛇

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

#define SNAKE_MAX_LENGTH 20
#define SNAKE_HEAD 'H'
#define SNAKE_BODY 'X'
#define SNAKE_FOOD '$'
#define BLANK_CELL ' '
#define WALL_CELL '*'

void snakeMove(int a, int b);
void put_money();
void input();
void output();
void gameover();
void collision();
void creatfood();

char map[12][13]=
{"************",
"*XXXXH *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"************"};

int snakeX[SNAKE_MAX_LENGTH] = {1, 1, 1, 1, 1};
int snakeY[SNAKE_MAX_LENGTH] = {1, 2, 3, 4, 5};
int snakeLength = 4;
int snakeLife = 1;
int food = 0;
int fx, fy;
int snaketailX, snaketailY;        //Use in Snake's growth
char ch;

int main() {
    while (snakeLife) {            //WHILE snake live DO
        _sleep(300);               //each 0.3second refresh the map
        system("cls");
        if (food == 0)
            creatfood();           //IT no food THEN creat food
        input();             
        output();
        collision(); 
    }                              //END WHILE
    getch();
    return 0;
}

void input() {
    if (kbhit() != 0) {            //IF no keyboard input THEN return the latest input 
        while(kbhit() != 0)        //INPUT the last keyboard input
            ch = getch();
    }
    switch (ch) {                  //CASE ch DO
        case 'a':                  //'a': turn left
            snakeMove(0, -1);
            break;
        case 'd':                  //'d': turn right
            snakeMove(0, 1);
            break;
        case 'w':                  //'w': up move
            snakeMove(-1, 0);
            break;
        case 's':                  //'s': down move
            snakeMove(1, 0);
            break;

        default:
            break;
    }                              //END CASE
}

void output() {
    int i, j;
    printf("C little game\n");
    printf("created by MR.GU\n");
    for (i = 1; i < 11; i++) {
        for(j = 1; j < 11; j++) {
            map[i][j] = ' '; 
        }
    }
    map[fx][fy] = '$';
    map[snakeX[snakeLength]][snakeY[snakeLength]] = 'H';
    for (i = 0; i < snakeLength; i++) {
            map[snakeX[i]][snakeY[i]] = 'X';
    }
    for (i = 0; i < 12 ;i++) {
        for (j = 0; j < 12; j++) {
            printf("%c", map[i][j]);
        }
        printf("\n");
    }
    printf("Snake's Length: %d\n", snakeLength + 1);
}

void snakeMove(int a, int b) {
    int i;
    snaketailX = snakeX[0];
    snaketailY = snakeY[0];
    for (i = 0; i < snakeLength; i++) {
        snakeX[i] = snakeX[i + 1];
        snakeY[i] = snakeY[i + 1];
    }
    snakeX[snakeLength] += a;
    snakeY[snakeLength] += b;
}

void creatfood() {
    fx = rand() % 10 + 1; 
    fy = rand() % 10 + 1; 
    if(map[fx][fy] != 'H' && map[fx][fy]!= 'X') {
        food++;
    } else {
        creatfood();
    }
}

void collision() {
    int i;
    if (snakeX[snakeLength] == 11 || snakeX[snakeLength] == 0) {           //Snake | Head on WALL
        snakeLife = 0;                                                     //Snake die
        printf("Gameover!\n");                                             //OUTPUT "Gameover"
    }
    if (snakeY[snakeLength] == 11 || snakeY[snakeLength] == 0) {           //Snake | Head on WALL
        snakeLife = 0;                                                     //Snake die
        printf("Gameover!\n");                                             //OUTPUT "Gameover"
    }
    if (snakeX[snakeLength] == fx && snakeY[snakeLength] == fy) {          //Snake | Head on WALL
        food--;
        snakeLength++;                                                     //Food disappear and Snake grow
        for (i = snakeLength; i > 0; i--) {
            snakeX[i] = snakeX[i - 1];
            snakeY[i] = snakeY[i - 1];
        }
        snakeX[0] = snaketailX;                                            //grow from the tail
        snakeY[0] = snaketailY;
    }
    for (i = 0; i < snakeLength; i++) {                                    //Snake | Head on BODY
        if (snakeX[i] == snakeX[snakeLength] && snakeY[i] == snakeY[snakeLength]) {
            snakeLife = 0;
            printf("hit your body!\n");                                    //Snake die
            printf("Gameover!\n");                                          //OUTPUT "hit your body!","Gameover"
        }
    }
}

你可能感兴趣的:(C snake C语言贪吃蛇)