C语言小游戏---贪吃蛇

 

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

#define High 15
#define Width 35
#define EnemyNum 5

int canves[High][Width] = {0};  //canves dimention,0 blank space, -1 frame, 1 snake head, >1 snake body, -2 food
int snakeMoveDirection;  // control snake move direction
int food_i, food_j;  //food,snake eat food can become long

void HideCursor() { // if cursor
    CONSOLE_CURSOR_INFO cursor_info= {1,0}; //second value is 0 mean hide cursor
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
void gotoxy(int x, int y) { // move mouse to x,y position, similer clear screen
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle, pos);
}
void startup() { // init data
    int i, j;
    // draw frame
    for(i=0; i<High; i++) {
        canves[i][0] = -1;
        canves[i][Width-1] = -1;
    }
    for(j=0; j<Width; j++) {
        canves[0][j] = -1;
        canves[High-1][j] = -1;
    }
    // init snake head position
    canves[High/2][Width/2] = 1;
    // init snake body
    for(i=1; i<=4; i++)
        canves[High/2][Width/2-i] = 1+i;
    // init snake move direction
    snakeMoveDirection = 4;
    // init food position
    food_i = rand() % (High - 5) + 2;
    food_j = rand() % (Width - 5) + 2;
    canves[food_i][food_j] = -2;
}
void show() { // show windows
    gotoxy(0,0);  //move mouse to 0,0 origin point, and redraw screen
    int i, j;
    for(i=0; i<High; i++) {
        for(j=0; j<Width; j++) {
            if(canves[i][j] == -1)
                printf("#");  //show frame #
            else if(canves[i][j] == 1)
                printf("@");  //show snake head @
            else if(canves[i][j] > 1)
                printf("*");  //show snake body *
            else if(canves[i][j] == -2)
                printf("$");  //show food $
            else
                printf(" ");  //show nothing
        }
        printf("\n");
    }
}
void updateWithoutInput() {
    int i, j, max=0, oldTail_i, oldTail_j, oldHead_i, oldHead_j, newHead_i, newHead_j;
    for(i=1; i<High-1; i++)
        for(j=1; j<Width-1; j++) {
            if(canves[i][j]>0) {
                // snake move all body plus 1
                canves[i][j]++;
                // record snake tail position,snake tail has biggest num
                if(max<canves[i][j]) {
                    max = canves[i][j];
                    oldTail_i = i;
                    oldTail_j = j;
                }
                //record old snake head position
                if(canves[i][j] == 2) {
                    oldHead_i = i;
                    oldHead_j = j;
                }
            }
        }
    // record snake head position
    // snake move up
    if(snakeMoveDirection == 1) {
        newHead_i = oldHead_i-1;
        newHead_j = oldHead_j;
    }
    // snake move down
    if(snakeMoveDirection == 2) {
        newHead_i = oldHead_i+1;
        newHead_j = oldHead_j;
    }
    // snake move left
    if(snakeMoveDirection == 3) {
        newHead_i = oldHead_i;
        newHead_j = oldHead_j-1;
    }
    // snake move right
    if(snakeMoveDirection == 4) {
        newHead_i = oldHead_i;
        newHead_j = oldHead_j+1;
    }
    // snake head crash frame or crash itself, game over
    if(canves[newHead_i][newHead_j]==-1||canves[newHead_i][newHead_j]>0) {
        printf("GAME OVER!");
        exit(0);
    }
    // snake eat food
    if(canves[newHead_i][newHead_j]==-2) {
        // new food, tail need stay
        food_i = rand() % (High - 5) + 2;
        food_j = rand() % (Width - 5) + 2;
        canves[food_i][food_j] = -2;
    } else {
        // no food, lose tail
        canves[oldTail_i][oldTail_j] = 0;
    }
    // new snake head
    canves[newHead_i][newHead_j] = 1;
    Sleep(150);
}
void updateWithInput() {
    char input;
    if(kbhit()) { //runing while user push keyboard
        input = getch();
        if(input == 'a') // move left
            snakeMoveDirection = 3;
        if(input == 'd') // move right
            snakeMoveDirection = 4;
        if(input == 'w')  //move up
            snakeMoveDirection = 1;
        if(input == 's')  //move down
            snakeMoveDirection = 2;
    }
}
int main() {
    startup(); // init data
    while(1) { // game loop run
        show(); // show windows
        updateWithoutInput(); //update don't need user
        updateWithInput(); //update need user
    }
    return 0;
}

 

posted @ 2020-01-13 15:54  yangly  阅读(247)  评论(0编辑  收藏  举报