中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

C語言實現貪吃蛇代碼怎么寫

小億
91
2024-03-01 20:03:27
欄目: 編程語言

這是一個簡單的C語言貪吃蛇游戲的代碼示例:

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

#define WIDTH 20
#define HEIGHT 20

int snakeX[100], snakeY[100];
int snakeLength = 1;
int fruitX, fruitY;
int score = 0;
int gameover = 0;
char direction = 'r';

void setup() {
    snakeX[0] = WIDTH / 2;
    snakeY[0] = HEIGHT / 2;

    fruitX = rand() % WIDTH;
    fruitY = rand() % HEIGHT;
}

void draw() {
    system("cls");

    for (int i = 0; i < WIDTH + 2; i++)
        printf("#");
    printf("\n");

    for (int i = 0; i < HEIGHT; i++) {
        for (int j = 0; j < WIDTH; j++) {
            if (j == 0)
                printf("#");

            if (i == snakeY[0] && j == snakeX[0])
                printf("O");
            else if (i == fruitY && j == fruitX)
                printf("F");
            else {
                int printSnake = 0;
                for (int k = 1; k < snakeLength; k++) {
                    if (i == snakeY[k] && j == snakeX[k]) {
                        printf("o");
                        printSnake = 1;
                    }
                }
                if (!printSnake)
                    printf(" ");
            }

            if (j == WIDTH - 1)
                printf("#");
        }
        printf("\n");
    }

    for (int i = 0; i < WIDTH + 2; i++)
        printf("#");
    printf("\n");

    printf("Score: %d", score);
}

void input() {
    if (_kbhit()) {
        switch (_getch()) {
        case 'a':
            direction = 'l';
            break;
        case 'd':
            direction = 'r';
            break;
        case 'w':
            direction = 'u';
            break;
        case 's':
            direction = 'd';
            break;
        case 'x':
            gameover = 1;
            break;
        }
    }
}

void logic() {
    int prevX = snakeX[0];
    int prevY = snakeY[0];
    int prev2X, prev2Y;
    snakeX[0] = snakeX[0] + 1;
    snakeY[0] = snakeY[0] + 1;
    for (int i = 1; i < snakeLength; i++) {
        prev2X = snakeX[i];
        prev2Y = snakeY[i];
        snakeX[i] = prevX;
        snakeY[i] = prevY;
        prevX = prev2X;
        prevY = prev2Y;
    }

    switch (direction) {
    case 'l':
        snakeX[0]--;
        break;
    case 'r':
        snakeX[0]++;
        break;
    case 'u':
        snakeY[0]--;
        break;
    case 'd':
        snakeY[0]++;
        break;
    }

    if (snakeX[0] == fruitX && snakeY[0] == fruitY) {
        score += 10;
        snakeLength++;
        fruitX = rand() % WIDTH;
        fruitY = rand() % HEIGHT;
    }

    if (snakeX[0] >= WIDTH || snakeX[0] < 0 || snakeY[0] >= HEIGHT || snakeY[0] < 0)
        gameover = 1;

    for (int i = 1; i < snakeLength; i++) {
        if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i])
            gameover = 1;
    }
}

int main() {
    setup();
    while (!gameover) {
        draw();
        input();
        logic();
        Sleep(100);
    }

    return 0;
}

請注意,這只是一個簡單版本的貪吃蛇游戲,可能還需要進一步優化和完善。您可以根據自己的需求進行修改和擴展。

0
陇南市| 高唐县| 龙山县| 汶川县| 德昌县| 吴旗县| 德保县| 巫山县| 舞钢市| 赞皇县| 米林县| 鹿邑县| 贺兰县| 万山特区| 五寨县| 忻州市| 盐边县| 清原| 淳化县| 南宁市| 武冈市| 安乡县| 嫩江县| 高雄县| 汨罗市| 道孚县| 惠州市| 绥德县| 都江堰市| 新和县| 托里县| 友谊县| 宁晋县| 和田市| 封开县| 乌兰浩特市| 库车县| 五华县| 北票市| 和田县| 霸州市|