91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

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
紫金县| 确山县| 襄樊市| 阿城市| 大悟县| 盘山县| 连云港市| 长垣县| 清新县| 沾化县| 清河县| 桐乡市| 棋牌| 潮安县| 若羌县| 德兴市| 景洪市| 衢州市| 浏阳市| 布尔津县| 桓台县| 砚山县| 和平区| 新沂市| 阜平县| 高碑店市| 高密市| 福泉市| 陇南市| 崇州市| 通道| 百色市| 郑州市| 吉木乃县| 腾冲县| 鹿邑县| 民和| 麻阳| 罗定市| 耿马| 丽水市|