跳到正文

FloydWar

838636

我是天才,天生的蠢材

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

资料

编号
838636
抓取

tang

我在你后面来机房,以后走的时候记得退出登录哦,要不然有人会用你的号抄题解之类的事,一定要记住!!!!!!!!嘻嘻

thank you very much

我的U盘

字符画转换器,c++11,无依赖

html

五子棋

躲避球球

rp++

年度报告

345

c++元胞自动机

123

C++
#include <iostream>
#include <vector>
#include <ctime>
#include <conio.h>
#include <windows.h>
#include <sstream> // 引入 stringstream 用于双缓冲

using namespace std;

// --- 游戏常量 ---
const int GAME_WIDTH = 50;
const int GAME_HEIGHT = 20;
const int INITIAL_SNAKE_LENGTH = 3;

// --- 游戏对象和状态 ---
struct SnakeSegment {
    int x, y;
};
vector<SnakeSegment> snake;
SnakeSegment food;

int score = 0;
char currentDirection = 'R'; // 'U', 'D', 'L', 'R'
bool gameOver = false;

// --- 函数声明 ---
void SetupConsole();
void InitializeGame();
void DrawGame();
void HandleInput();
void UpdateGameLogic();
void GameOverScreen();

// --- 辅助函数 ---
void SetCursorPosition(int x, int y) {
    COORD coord = { (short)x, (short)y };
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void HideCursor() {
    CONSOLE_CURSOR_INFO cursorInfo;
    cursorInfo.dwSize = 1;
    cursorInfo.bVisible = FALSE;
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
}

// --- 游戏函数 ---
void SetupConsole() {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    // 缓冲区高度应足以容纳游戏区域、分数和游戏结束信息
    COORD bufferSize = { GAME_WIDTH, GAME_HEIGHT + 5 };
    SetConsoleScreenBufferSize(hConsole, bufferSize);

    SMALL_RECT windowSize = { 0, 0, GAME_WIDTH - 1, GAME_HEIGHT + 2 };
    SetConsoleWindowInfo(hConsole, TRUE, &windowSize);
    
    HideCursor();
}

void InitializeGame() {
    snake.clear();
    score = 0;
    currentDirection = 'R';
    gameOver = false;

    int startX = GAME_WIDTH / 2;
    int startY = GAME_HEIGHT / 2;
    for (int i = 0; i < INITIAL_SNAKE_LENGTH; ++i) {
        snake.push_back({ startX - i, startY });
    }

    srand(static_cast<unsigned int>(time(0)));
    food.x = rand() % (GAME_WIDTH - 2) + 1;
    food.y = rand() % (GAME_HEIGHT - 2) + 1;
}

// 使用双缓冲技术重写 DrawGame
void DrawGame() {
    // 1. 创建一个字符串流作为后台缓冲区
    std::stringstream buffer;

    // 2. 将所有要绘制的内容写入缓冲区
    // 绘制上边界
    buffer << std::string(GAME_WIDTH, '#') << std::endl;

    // 绘制游戏区域
    for (int y = 0; y < GAME_HEIGHT; ++y) {
        buffer << "#"; // 绘制左边界
        for (int x = 1; x < GAME_WIDTH - 1; ++x) { // x从1到WIDTH-2,因为左右边界已经绘制
            bool isSnakeBody = false;
            for (size_t i = 0; i < snake.size(); ++i) {
                if (snake[i].x == x && snake[i].y == y) {
                    buffer << (i == 0 ? "O" : "o");
                    isSnakeBody = true;
                    break;
                }
            }
            if (!isSnakeBody && x == food.x && y == food.y) {
                buffer << "F";
            } else if (!isSnakeBody) {
                buffer << " ";
            }
        }
        buffer << "#" << std::endl; // 绘制右边界并换行
    }

    // 绘制下边界
    buffer << std::string(GAME_WIDTH, '#') << std::endl;

    // 显示分数
    buffer << "Score: " << score << " | Press 'X' to quit." << std::endl;

    // 3. 将光标移动到左上角
    SetCursorPosition(0, 0);

    // 4. 一次性将缓冲区的所有内容输出到控制台
    std::cout << buffer.str();
}


void HandleInput() {
    if (_kbhit()) {
        switch (_getch()) {
            case 'w': case 'W': if (currentDirection != 'D') currentDirection = 'U'; break;
            case 's': case 'S': if (currentDirection != 'U') currentDirection = 'D'; break;
            case 'a': case 'A': if (currentDirection != 'R') currentDirection = 'L'; break;
            case 'd': case 'D': if (currentDirection != 'L') currentDirection = 'R'; break;
            case 'x': case 'X': gameOver = true; break;
        }
    }
}

void UpdateGameLogic() {
    if (gameOver) return;

    SnakeSegment newHead = snake.front();
    switch (currentDirection) {
        case 'U': newHead.y--; break;
        case 'D': newHead.y++; break;
        case 'L': newHead.x--; break;
        case 'R': newHead.x++; break;
    }

    if (newHead.x <= 0 || newHead.x >= GAME_WIDTH - 1 || newHead.y < 0 || newHead.y >= GAME_HEIGHT) {
        gameOver = true;
        return;
    }
    for (size_t i = 0; i < snake.size(); ++i) {
        if (newHead.x == snake[i].x && newHead.y == snake[i].y) {
            gameOver = true;
            return;
        }
    }

    snake.insert(snake.begin(), newHead);

    if (newHead.x == food.x && newHead.y == food.y) {
        score += 10;
        bool foodOnSnake;
        do {
            foodOnSnake = false;
            food.x = rand() % (GAME_WIDTH - 2) + 1;
            food.y = rand() % (GAME_HEIGHT - 2) + 1;
            for (const auto& segment : snake) {
                if (food.x == segment.x && food.y == segment.y) {
                    foodOnSnake = true;
                    break;
                }
            }
        } while (foodOnSnake);
    } else {
        snake.pop_back();
    }
}

void GameOverScreen() {
    // 使用双缓冲来显示游戏结束画面,保持视觉一致性
    std::stringstream buffer;
    buffer << std::string((GAME_WIDTH - 9) / 2, ' ') << "GAME OVER" << std::endl;
    buffer << std::string((GAME_WIDTH - 16) / 2, ' ') << "Final Score: " << score << std::endl;
    
    // 将光标移动到游戏区域中心并输出
    SetCursorPosition(0, GAME_HEIGHT / 2 - 1);
    std::cout << buffer.str();
}

// --- 主函数 ---
int main() {
    SetupConsole();
    InitializeGame();

    while (!gameOver) {
        DrawGame();
        HandleInput();
        UpdateGameLogic();
        Sleep(120); // 控制游戏速度,可以适当调整
    }

    GameOverScreen();
    Sleep(3000);

    return 0;
}

123# 冲刺

虽然我觉得去考IOI的人都是SB
但

AK

倒是挺帅


em~~~~

大事记

2022-10-16来到洛谷
…………
2023-8-15名字颜色变绿
2023-8-15报名了我的第一场月赛
2023-9-2发现月赛没做
2023-9-16开始了人生中第一场csp普及组考试
2023-9-28得知csp考了48分
2025-11-5 n年没上锣鼓

还没有收录这位用户的文章。