c++小游戏
  • 板块灌水区
  • 楼主mozhengyang801
  • 当前回复1
  • 已保存回复1
  • 发布时间2025/1/20 10:13
  • 上次更新2025/1/20 12:17:42
查看原帖
c++小游戏
1491309
mozhengyang801楼主2025/1/20 10:13
#include <iostream>
#include <windows.h>  // 用于控制控制台相关功能,如光标位置等
#include <cstdlib>
#include <ctime>
#include <conio.h>  // 用于获取键盘输入

using namespace std;

// 游戏窗口相关参数
const int WIDTH = 80;
const int HEIGHT = 20;

// 蛇的结构体
struct Snake {
    int x;
    int y;
    Snake* next;
};

Snake* snake;
int foodX, foodY;
int score = 0;
int dir = 77;  // 初始方向为向右,72上,80下,75左,77右(对应ASCII码值)

// 初始化游戏
void initGame() {
    snake = new Snake;
    snake->x = WIDTH / 2;
    snake->y = HEIGHT / 2;
    snake->next = NULL;

    srand(static_cast<unsigned int>(time(NULL)));
    foodX = rand() % (WIDTH - 2) + 1;
    foodY = rand() % (HEIGHT - 2) + 1;
}

// 绘制游戏场景(包括蛇和食物等)
void drawScene() {
    system("cls");  // 清屏
    for (int i = 0; i < HEIGHT; ++i) {
        for (int j = 0; j < WIDTH; ++j) {
            if (i == 0 || i == HEIGHT - 1 || j == 0 || j == WIDTH - 1) {
                cout << "#";
            }
            else {
                bool isSnake = false;
                Snake* current = snake;
                while (current!= NULL) {
                    if (current->x == j && current->y == i) {
                        cout << "O";
                        isSnake = true;
                        break;
                    }
                    current = current->next;
                }
                if (!isSnake) {
                    if (j == foodX && i == foodY) {
                        cout << "*";
                    }
                    else {
                        cout << " ";
                    }
                }
            }
        }
        cout << endl;
    }
    cout << "Score: " << score << endl;
}

// 移动蛇
void moveSnake() {
    Snake* newHead = new Snake;
    switch (dir) {
    case 72:
        newHead->x = snake->x;
        newHead->y = snake->y - 1;
        break;
    case 80:
        newHead->x = snake->x;
        newHead->y = snake->y + 1;
        break;
    case 75:
        newHead->x = snake->x - 1;
        newHead->y = snake->y;
        break;
    case 77:
        newHead->x = snake->x + 1;
        newHead->y = snake->y;
        break;
    }
    newHead->next = snake;
    snake = newHead;

    // 判断是否吃到食物
    if (snake->x == foodX && snake->y == foodY) {
        score++;
        srand(static_cast<unsigned int>(time(NULL)));
        foodX = rand() % (WIDTH - 2) + 1;
        foodY = rand() % (HEIGHT - 2) + 1;
    }
    else {
        Snake* current = snake;
        Snake* prev = NULL;
        while (current->next!= NULL) {
            prev = current;
            current = current->next;
        }
        delete current;
        prev->next = NULL;
    }
}

// 检查游戏是否结束(撞墙或者撞到自己)
bool isGameOver() {
    if (snake->x == 0 || snake->x == WIDTH - 1 || snake->y == 0 || snake->y == HEIGHT - 1) {
        return true;
    }
    Snake* current = snake->next;
    while (current!= NULL) {
        if (current->x == snake->x && current->y == snake->y) {
            return true;
        }
        current = current->next;
    }
    return false;
}

int main() {
    initGame();
    while (true) {
        if (_kbhit()) {  // 检测是否有键盘输入
            char key = _getch();
            if ((key == 'w' || key == 72) && dir!= 80) dir = 72;
            if ((key == 's' || key == 80) && dir!= 72) dir = 80;
            if ((key == 'a' || key == 75) && dir!= 77) dir = 75;
            if ((key == 'b' || key == 77) && dir!= 75) dir = 77;
        }

        drawScene();
        moveSnake();
        if (isGameOver()) {
            system("cls");
            cout << "Game Over! Your score: " << score << endl;
            break;
        }
        Sleep(100);
    }
    return 0;
}
2025/1/20 10:13
加载中...