跳到正文

闲话

贪吃蛇Game

Clang_239约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月26日
篇幅
约 7 分钟

版本

  1. v1当前

带过me的老师:

@icebin CBin老师(2023/9-2024/2 and 2024/10-2025/2)

@罐头小哥 CJyu老师 (2024/9-2024/10)

@Key_l Mo老师 (2025/3-至今)

以上更新于2025年3月30日。

C++
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;

const int WIDTH = 20;
const int HEIGHT = 20;

int snakeX[100], snakeY[100];
int snakeLength;
int foodX, foodY;
int direction;
bool gameOver;

void Setup() {
    gameOver = false;
    direction = 'd';
    snakeLength = 1;
    snakeX[0] = WIDTH / 2;
    snakeY[0] = HEIGHT / 2;
    foodX = rand() % WIDTH;
    foodY = rand() % HEIGHT;
}

void Draw() {
    system("cls");
    for (int i = 0; i < WIDTH + 2; i++)
        cout << "#";
    cout << endl;

    for (int y = 0; y < HEIGHT; y++) {
        for (int x = 0; x < WIDTH; x++) {
            if (x == 0 || x == WIDTH - 1)
                cout << "#";
            if (x == snakeX[0] && y == snakeY[0])
                cout << "O";
            else if (x == foodX && y == foodY)
                cout << "F";
            else {
                bool isBody = false;
                for (int k = 1; k < snakeLength; k++) {
                    if (x == snakeX[k] && y == snakeY[k]) {
                        cout << "o";
                        isBody = true;
                    }
                }
                if (!isBody)
                    cout << " ";
            }
        }
        cout << endl;
    }
    for (int i = 0; i < WIDTH + 2; i++)
        cout << "#";
    cout << endl;
}


void Input() {
    if (_kbhit())
        switch (_getch()) {
            case 'a':
                if (direction != 'd')
                    direction = 'a';
                break;
            case 'd':
                if (direction != 'a')
                    direction = 'd';
                break;
            case 'w':
                if (direction != 's')
                    direction = 'w';
                break;
            case 's':
                if (direction != 'w')
                    direction = 's';
                break;
            case 'x':
                gameOver = true;
                break;
        }
    }
}

void Logic() {
    for (int i = snakeLength - 1; i > 0; i--) {
        snakeX[i] = snakeX[i - 1];
        snakeY[i] = snakeY[i - 1];
    }
    switch (direction) {
        case 'a':
            snakeX[0]--;
            break;
        case 'd':
            snakeX[0]++;
            break;
        case 'w':
            snakeY[0]--;
            break;
        case 's':
            snakeY[0]++;
            break;
    }

    if (snakeX[0] == foodX && snakeY[0] == foodY) {
        snakeLength++;
        foodX = rand() % WIDTH;
        foodY = rand() % HEIGHT;
    }

    if (snakeX[0] < 0 || snakeX[0] >= WIDTH || snakeY[0] < 0 || snakeY[0] >= HEIGHT)
        gameOver = true;

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

int main() {
    Setup();
    while (!gameOver) {
        Draw();
        Input();
        Logic();
        Sleep(100);
    }
    cout << "Game Over! Your score: " << snakeLength - 1 << endl;
    return 0;
}
原文