跳到正文

cccnm

427678

kkksc03ด้้้้้็้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็✩

在洛谷打开
资料、奖项与版本

资料

编号
427678
抓取

沙盒

主页

我是一个 傻 逼 \color{black}{我是一个}\colorbox{blue}{\color{blue}{ 傻 逼 }}

我是一个 渣 渣 \color{black}{我是一个}\colorbox{purple}{\color{purple}{ 渣 渣 }}

我是一个 菜 鸡 \color{black}{我是一个}\colorbox{pink}{\color{pink}{ 菜 鸡 }}

我是一个 蒟 蒻 \color{black}{我是一个}\colorbox{gray}{\color{gray}{ 蒟 蒻 }}

我是一个 大 佬 \color{black}{我是一个}\colorbox{black}{\color{black}{ 大 佬 }}

你是第Counter次访问我主页的蒟蒻

该内容暂不可见 \colorbox{green}{\color{gray}{该内容暂不可见}}

C
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <cstdlib>
#include <ctime>
#include <queue>
using namespace std;

// 游戏配置
const int WIDTH = 40;
const int HEIGHT = 20;
const int BASE_SPEED = 100;

enum eDir { STOP = 0, LEFT, RIGHT, UP, DOWN };

struct SnakeSegment {
    int x, y;
} snake[100];

// 游戏状态
struct GameState {
    eDir direction;
    int length;
    int foodX, foodY;
    int score;
    bool gameOver;
    bool autoPilot;
} state;

// 初始化游戏
void InitializeGame() {
    state.direction = STOP;
    state.length = 1;
    snake[0].x = WIDTH / 2;
    snake[0].y = HEIGHT / 2;
    state.score = 0;
    state.gameOver = false;
    state.autoPilot = false;
    
    // 生成食物
    do {
        state.foodX = rand() % WIDTH;
        state.foodY = rand() % HEIGHT;
    } while (state.foodX == snake[0].x && state.foodY == snake[0].y);
}

// 绘制游戏界面
void RenderGame() {
    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) cout << "#"; // 左边界
            
            bool isSnake = false;
            if (x == snake[0].x && y == snake[0].y) {
                cout << "O"; // 蛇头
                isSnake = true;
            }
            else if (x == state.foodX && y == state.foodY) {
                cout << "F"; // 食物
            }
            else {
                for (int i = 1; i < state.length; i++) {
                    if (snake[i].x == x && snake[i].y == y) {
                        cout << "o"; // 蛇身
                        isSnake = true;
                        break;
                    }
                }
                if (!isSnake) cout << " ";
            }
            
            if (x == WIDTH-1) cout << "#"; // 右边界
        }
        cout << endl;
    }

    // 下边界
    for (int i = 0; i < WIDTH+2; i++) cout << "#";
    cout << endl;
    
    // 状态信息
    cout << "Score: " << state.score 
         << " | Mode: " << (state.autoPilot ? "AUTO" : "MANUAL")
         << "\nControls: WASD移动 M切换模式 X退出";
}

// 处理玩家输入
void ProcessInput() {
    if (_kbhit()) {
        switch (_getch()) {
            case 'w': if (state.direction != DOWN) state.direction = UP; break;
            case 's': if (state.direction != UP) state.direction = DOWN; break;
            case 'a': if (state.direction != RIGHT) state.direction = LEFT; break;
            case 'd': if (state.direction != LEFT) state.direction = RIGHT; break;
            case 'm': state.autoPilot = !state.autoPilot; break;
            case 'x': state.gameOver = true; break;
        }
    }
}

// 自动导航系统
void AutoNavigate() {
    int targetX = state.foodX;
    int targetY = state.foodY;
    
    // 简单追踪算法
    int dx = targetX - snake[0].x;
    int dy = targetY - snake[0].y;
    
    if (abs(dx) > abs(dy)) {
        if (dx > 0 && state.direction != LEFT) state.direction = RIGHT;
        else if (dx < 0 && state.direction != RIGHT) state.direction = LEFT;
    } else {
        if (dy > 0 && state.direction != UP) state.direction = DOWN;
        else if (dy < 0 && state.direction != DOWN) state.direction = UP;
    }
    
    // 边缘保护
    if (snake[0].x == 0 && state.direction == LEFT) state.direction = UP;
    if (snake[0].x == WIDTH-1 && state.direction == RIGHT) state.direction = DOWN;
    if (snake[0].y == 0 && state.direction == UP) state.direction = LEFT;
    if (snake[0].y == HEIGHT-1 && state.direction == DOWN) state.direction = RIGHT;
}

// 更新游戏逻辑
void UpdateGame() {
    // 保存旧尾部位置
    int prevX = snake[state.length-1].x;
    int prevY = snake[state.length-1].y;
    
    // 移动身体
    for (int i = state.length-1; i > 0; i--) {
        snake[i].x = snake[i-1].x;
        snake[i].y = snake[i-1].y;
    }
    
    // 移动头部
    switch (state.direction) {
        case LEFT:  snake[0].x--; break;
        case RIGHT: snake[0].x++; break;
        case UP:    snake[0].y--; break;
        case DOWN:  snake[0].y++; break;
    }
    
    // 边界检测
    if (snake[0].x < 0 || snake[0].x >= WIDTH ||
        snake[0].y < 0 || snake[0].y >= HEIGHT) {
        state.gameOver = true;
    }
    // 吃食物检测
    if (snake[0].x == state.foodX && snake[0].y == state.foodY) {
        state.score += 10;
        state.length++;
        // 生成新食物
        bool validPosition;
        do {
            validPosition = true;
            state.foodX = rand() % WIDTH;
            state.foodY = rand() % HEIGHT;
            
            // 检查食物是否生成在蛇身上
            for (int i = 0; i < state.length; i++) {
                if (snake[i].x == state.foodX && snake[i].y == state.foodY) {
                    validPosition = false;
                    break;
                }
            }
        } while (!validPosition);
    }
}

int main() {
    srand(time(0));
    InitializeGame();
    
    while (!state.gameOver) {
        RenderGame();
        ProcessInput();
        if (state.autoPilot) AutoNavigate();
        UpdateGame();
        Sleep(BASE_SPEED - (state.length * 2)); // 随长度增加加快速度
    }
    
    system("cls");
    cout << "===================\n"
         << "   游戏结束!\n"
         << "   最终得分: " << state.score << "\n"
         << "===================\n";
    system("pause");
    return 0;
}
共 1 篇
  1. 个人记录

    规则怪谈游戏

    8-105机房规定摘要: 机房开放时间为周一至周五18:30-22:00,周日8:00-22:00,非开放时间禁止进入。机房内仅有机房管理员的黑色键盘和白色键盘,其他机位若出现红色键盘需避开。若未找到白色键盘,可闭眼想象10秒后变白,或等待管理员处理。键盘声正常,但若听到管理员方向有异常响声需低头确认自己键盘颜色。违规时若出现陌生图标,不得删除或关机,需静待消失;若图标自行打开,需默念“我是蒟蒻”并保持闭眼直至管理员提醒。禁止娱乐行为,发现非学习活动需立即报告管理员。所有规则均以“蒟蒻”身份为前提,确保安全与秩序。

    1 赞