• 个人简介

    #include <stdio.h>

    #include <stdlib.h>

    #include <conio.h>

    #include <windows.h>

    #include <time.h>

    #include <stdbool.h>

    // 游戏设置 - 优化尺寸 #define WIDTH 40

    #define HEIGHT 20

    #define MAX_SNAKE_LENGTH 200

    #define MAX_FOOD 3

    #define MAX_CANNONS 5

    // 方向控制 #define UP 1

    #define DOWN 2

    #define LEFT 3

    #define RIGHT 4

    #define PAUSE 'p'

    // 难度设置 typedef enum { EASY, MEDIUM, HARD } Difficulty;

    // 食物类型 typedef enum { BANANA, // 永久长一格 HORMONE, // 10秒内快速长十格 BOMB // 炸短五截 } FoodType;

    // 蛇结构体 typedef struct { int x; int y; } Point;

    typedef struct { Point body[MAX_SNAKE_LENGTH]; int length; int direction; bool alive; } Snake;

    // 食物结构体 typedef struct { Point position; FoodType type; bool active; } Food;

    // 炮弹结构体 typedef struct { Point position; int direction; bool active; } Cannon;

    // 游戏状态 typedef struct { Snake player; Snake enemy; // 敌人蛇(困难模式) Food foods[MAX_FOOD]; Cannon cannons[MAX_CANNONS]; int score; int level; int speed; Difficulty difficulty; bool walls[HEIGHT][WIDTH]; long hormoneStartTime; bool hormoneActive; bool gameOver; bool paused; } GameState;

    // 函数声明 void initializeGame(GameState *game); void drawGame(GameState *game); void processInput(GameState *game); void updateGame(GameState *game); void spawnFood(GameState *game); void spawnCannon(GameState *game); void moveSnake(Snake *snake); bool checkCollision(GameState *game, Point point); Point getRandomPosition(); void showInstructions(); Difficulty selectDifficulty(); void showGameOver(GameState *game); void setCursorPosition(int x, int y); void hideCursor(); void drawTitle(); void clearScreen(); int max(int a, int b);

    // 全局变量 - 用于高效绘图 HANDLE hConsole; CONSOLE_SCREEN_BUFFER_INFO csbiInfo; COORD cursorPosition = {0, 0};

    int main() { // 获取控制台句柄 hConsole = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(hConsole, &csbiInfo); hideCursor();

    // 设置随机种子
     srand(time(NULL));
     
     // 初始化游戏状态
     GameState game;
     initializeGame(&game);
     
     // 显示游戏说明
     showInstructions();
     
     // 选择难度
     game.difficulty = selectDifficulty();
     
     // 设置初始速度
     switch(game.difficulty) {
         case EASY: game.speed = 50; break;
         case MEDIUM: game.speed = 30; break;
         case HARD: game.speed = 20; break;
     }
     
     // 游戏主循环
     while (!game.gameOver) {
         // 处理输入
         processInput(&game);
         
         // 更新游戏状态
         if (!game.paused) {
             updateGame(&game);
         }
         
         // 绘制游戏界面
         drawGame(&game);
         
         // 控制游戏速度
         Sleep(game.speed);
     }
     
     // 显示游戏结束
     showGameOver(&game);
     
     // 恢复光标显示
     CONSOLE_CURSOR_INFO cursorInfo;
     cursorInfo.bVisible = TRUE;
     cursorInfo.dwSize = 100;
     SetConsoleCursorInfo(hConsole, &cursorInfo);
     
     return 0;
    

    }

    // 设置光标位置 void setCursorPosition(int x, int y) { cursorPosition.X = x; cursorPosition.Y = y; SetConsoleCursorPosition(hConsole, cursorPosition); }

    // 隐藏光标 void hideCursor() { CONSOLE_CURSOR_INFO cursorInfo; GetConsoleCursorInfo(hConsole, &cursorInfo); cursorInfo.bVisible = FALSE; SetConsoleCursorInfo(hConsole, &cursorInfo); }

    // 初始化游戏 void initializeGame(GameState *game) { // 初始化玩家蛇 game->player.length = 3; game->player.direction = RIGHT; game->player.alive = true;

    // 初始化蛇身(初始位置在中间)
     for (int i = 0; i < game->player.length; i++) {
         game->player.body[i].x = WIDTH/2 - i;
         game->player.body[i].y = HEIGHT/2;
     }
     
     // 初始化敌人蛇(困难模式使用)
     game->enemy.length = 5;
     game->enemy.direction = LEFT;
     game->enemy.alive = true;
     for (int i = 0; i < game->enemy.length; i++) {
         game->enemy.body[i].x = WIDTH/4 - i;
         game->enemy.body[i].y = HEIGHT/3;
     }
     
     // 初始化食物
     for (int i = 0; i < MAX_FOOD; i++) {
         game->foods[i].active = false;
     }
     
     // 初始化炮弹
     for (int i = 0; i < MAX_CANNONS; i++) {
         game->cannons[i].active = false;
     }
     
     // 初始化游戏状态
     game->score = 0;
     game->level = 1;
     game->hormoneActive = false;
     game->gameOver = false;
     game->paused = false;
     game->hormoneStartTime = 0;
     
     // 初始化墙壁
     for (int y = 0; y < HEIGHT; y++) {
         for (int x = 0; x < WIDTH; x++) {
             // 边界为墙
             game->walls[y][x] = (x == 0 || x == WIDTH-1 || y == 0 || y == HEIGHT-1);
         }
     }
     
     // 生成初始食物
     for (int i = 0; i < MAX_FOOD; i++) {
         spawnFood(game);
     }
    

    }

    // 绘制游戏界面 - 优化版 void drawGame(GameState *game) { // 设置光标到左上角位置 setCursorPosition(0, 0);

    // 绘制游戏标题
     printf("贪吃蛇增强版 - ");
     switch(game->difficulty) {
         case EASY: printf("简单"); break;
         case MEDIUM: printf("中等"); break;
         case HARD: printf("困难"); break;
     }
     
     printf("  分数: %d  长度: %d  等级: %d\n", game->score, game->player.length, game->level);
     
     // 绘制游戏区域 - 高效绘图
     for (int y = 0; y < HEIGHT; y++) {
         for (int x = 0; x < WIDTH; x++) {
             // 检查是否是蛇身
             bool drawn = false;
             
             // 检查玩家蛇头
             if (x == game->player.body[0].x && y == game->player.body[0].y) {
                 printf("@");
                 drawn = true;
             }
             // 检查玩家蛇身
             else if (!drawn) {
                 for (int i = 1; i < game->player.length; i++) {
                     if (x == game->player.body[i].x && y == game->player.body[i].y) {
                         printf("o");
                         drawn = true;
                         break;
                     }
                 }
             }
             
             // 检查敌人蛇(困难模式)
             if (!drawn && game->difficulty == HARD) {
                 // 检查敌人蛇头
                 if (x == game->enemy.body[0].x && y == game->enemy.body[0].y) {
                     printf("E");
                     drawn = true;
                 }
                 // 检查敌人蛇身
                 else if (!drawn) {
                     for (int i = 1; i < game->enemy.length; i++) {
                         if (x == game->enemy.body[i].x && y == game->enemy.body[i].y) {
                             printf("e");
                             drawn = true;
                             break;
                         }
                     }
                 }
             }
             
             // 检查是否是食物
             if (!drawn) {
                 for (int i = 0; i < MAX_FOOD; i++) {
                     if (game->foods[i].active && 
                         x == game->foods[i].position.x && 
                         y == game->foods[i].position.y) {
                         switch(game->foods[i].type) {
                             case BANANA: printf("B"); break;
                             case HORMONE: printf("H"); break;
                             case BOMB: printf("X"); break;
                         }
                         drawn = true;
                         break;
                     }
                 }
             }
             
             // 检查是否是炮弹
             if (!drawn) {
                 for (int i = 0; i < MAX_CANNONS; i++) {
                     if (game->cannons[i].active && 
                         x == game->cannons[i].position.x && 
                         y == game->cannons[i].position.y) {
                         printf("*");
                         drawn = true;
                         break;
                     }
                 }
             }
             
             // 检查是否是墙
             if (!drawn) {
                 if (game->walls[y][x]) {
                     printf("#");
                 } else {
                     printf(" ");
                 }
             }
         }
         printf("\n");
     }
     
     // 显示游戏状态信息
     printf("\n控制: WASD移动 | P暂停 | ESC退出\n");
     
     if (game->paused) {
         printf("\n游戏暂停中...\n");
     }
     
     if (game->hormoneActive) {
         int timeLeft = 10 - (GetTickCount() - game->hormoneStartTime) / 1000;
         printf("激素效果剩余时间: %d秒\n", timeLeft);
     }
     
     if (game->score >= 20) {
         printf("警告: 炮弹正在破坏场地!\n");
     }
    

    }

    // 处理输入 void processInput(GameState *game) { if (_kbhit()) { int key = _getch();

    // 如果是方向键(需要读取两次)
         if (key == 0 || key == 224) {
             key = _getch();
         }
         
         switch(key) {
             case 'w':
             case 'W':
             case UP:
                 if (game->player.direction != DOWN) {
                     game->player.direction = UP;
                 }
                 break;
             case 's':
             case 'S':
             case DOWN:
                 if (game->player.direction != UP) {
                     game->player.direction = DOWN;
                 }
                 break;
             case 'a':
             case 'A':
             case LEFT:
                 if (game->player.direction != RIGHT) {
                     game->player.direction = LEFT;
                 }
                 break;
             case 'd':
             case 'D':
             case RIGHT:
                 if (game->player.direction != LEFT) {
                     game->player.direction = RIGHT;
                 }
                 break;
             case 'p':
             case 'P':
                 game->paused = !game->paused;
                 break;
             case 27: // ESC键
                 game->gameOver = true;
                 break;
         }
     }
    

    }

    // 更新游戏状态 void updateGame(GameState *game) { // 检查激素效果是否结束 if (game->hormoneActive && (GetTickCount() - game->hormoneStartTime) > 10000) { game->hormoneActive = false;

    // 激素效果结束,减少10格
         if (game->player.length > 10) {
             game->player.length -= 10;
         } else {
             // 长度不足,游戏结束
             game->player.alive = false;
             game->gameOver = true;
             return;
         }
     }
     
     // 移动玩家蛇
     moveSnake(&game->player);
     
     // 移动敌人蛇(困难模式)
     if (game->difficulty == HARD) {
         // 简单AI:随机移动或追踪玩家
         if (rand() % 5 == 0) {
             // 随机改变方向
             game->enemy.direction = rand() % 4 + 1;
         } else {
             // 追踪玩家
             if (game->enemy.body[0].x < game->player.body[0].x) {
                 game->enemy.direction = RIGHT;
             } else if (game->enemy.body[0].x > game->player.body[0].x) {
                 game->enemy.direction = LEFT;
             } else if (game->enemy.body[0].y < game->player.body[0].y) {
                 game->enemy.direction = DOWN;
             } else {
                 game->enemy.direction = UP;
             }
         }
         moveSnake(&game->enemy);
         
         // 检查敌人蛇是否撞墙或撞到自己
         if (checkCollision(game, game->enemy.body[0]) || 
             game->walls[game->enemy.body[0].y][game->enemy.body[0].x]) {
             // 敌人蛇死亡,重新生成
             game->enemy.length = 5;
             for (int i = 0; i < game->enemy.length; i++) {
                 game->enemy.body[i].x = rand() % (WIDTH/2);
                 game->enemy.body[i].y = rand() % (HEIGHT/2);
             }
         }
         
         // 检查玩家是否撞到敌人蛇
         for (int i = 0; i < game->enemy.length; i++) {
             if (game->player.body[0].x == game->enemy.body[i].x && 
                 game->player.body[0].y == game->enemy.body[i].y) {
                 game->player.alive = false;
                 game->gameOver = true;
                 return;
             }
         }
     }
     
     // 检查玩家蛇是否撞墙或撞到自己
     if (checkCollision(game, game->player.body[0]) || 
         game->walls[game->player.body[0].y][game->player.body[0].x]) {
         game->player.alive = false;
         game->gameOver = true;
         return;
     }
     
     // 检查是否需要生成食物
     int activeFood = 0;
     for (int i = 0; i < MAX_FOOD; i++) {
         if (game->foods[i].active) activeFood++;
     }
     
     if (activeFood < MAX_FOOD && rand() % 10 == 0) {
         spawnFood(game);
     }
     
     // 检查是否吃到食物
     for (int i = 0; i < MAX_FOOD; i++) {
         if (game->foods[i].active && 
             game->player.body[0].x == game->foods[i].position.x && 
             game->player.body[0].y == game->foods[i].position.y) {
             
             switch(game->foods[i].type) {
                 case BANANA:
                     // 吃香蕉:永久长一格
                     game->player.length++;
                     game->score += 2;
                     break;
                     
                 case HORMONE:
                     // 吃激素:10秒内快速长十格
                     game->hormoneActive = true;
                     game->hormoneStartTime = GetTickCount();
                     game->score += 2;
                     break;
                     
                 case BOMB:
                     // 吃炸弹:炸短五截
                     if (game->player.length > 5) {
                         game->player.length -= 5;
                     } else {
                         // 长度不足,游戏结束
                         game->player.alive = false;
                         game->gameOver = true;
                         return;
                     }
                     break;
             }
             
             game->foods[i].active = false;
             break;
         }
     }
     
     // 根据分数调整速度
     if (game->score > 0 && game->score % 10 == 0) {
         game->speed = max(10, game->speed - 5);
         game->level = game->score / 10 + 1;
     }
     
     // 当分数达到20时,开始破坏场地
     if (game->score >= 20) {
         // 生成炮弹
         int spawnChance = 30;
         if (game->difficulty == MEDIUM) spawnChance = 25;
         if (game->difficulty == HARD) spawnChance = 20;
         
         if (rand() % spawnChance == 0) {
             spawnCannon(game);
         }
         
         // 移动炮弹
         for (int i = 0; i < MAX_CANNONS; i++) {
             if (game->cannons[i].active) {
                 // 根据方向移动炮弹
                 switch(game->cannons[i].direction) {
                     case UP: game->cannons[i].position.y--; break;
                     case DOWN: game->cannons[i].position.y++; break;
                     case LEFT: game->cannons[i].position.x--; break;
                     case RIGHT: game->cannons[i].position.x++; break;
                 }
                 
                 // 检查炮弹是否击中墙
                 if (game->cannons[i].position.x <= 0 || game->cannons[i].position.x >= WIDTH-1 ||
                     game->cannons[i].position.y <= 0 || game->cannons[i].position.y >= HEIGHT-1) {
                     // 炮弹击中边界,破坏墙壁
                     if (game->cannons[i].position.x <= 0) {
                         // 破坏左边界
                         for (int y = 1; y < HEIGHT-1; y++) {
                             if (rand() % 3 == 0) {
                                 game->walls[y][0] = false;
                             }
                         }
                     } else if (game->cannons[i].position.x >= WIDTH-1) {
                         // 破坏右边界
                         for (int y = 1; y < HEIGHT-1; y++) {
                             if (rand() % 3 == 0) {
                                 game->walls[y][WIDTH-1] = false;
                             }
                         }
                     } else if (game->cannons[i].position.y <= 0) {
                         // 破坏上边界
                         for (int x = 1; x < WIDTH-1; x++) {
                             if (rand() % 3 == 0) {
                                 game->walls[0][x] = false;
                             }
                         }
                     } else if (game->cannons[i].position.y >= HEIGHT-1) {
                         // 破坏下边界
                         for (int x = 1; x < WIDTH-1; x++) {
                             if (rand() % 3 == 0) {
                                 game->walls[HEIGHT-1][x] = false;
                             }
                         }
                     }
                     
                     game->cannons[i].active = false;
                 }
                 
                 // 检查炮弹是否击中玩家
                 for (int j = 0; j < game->player.length; j++) {
                     if (game->cannons[i].position.x == game->player.body[j].x && 
                         game->cannons[i].position.y == game->player.body[j].y) {
                         // 被炮弹击中,减少5格
                         if (game->player.length > 5) {
                             game->player.length -= 5;
                         } else {
                             // 长度不足,游戏结束
                             game->player.alive = false;
                             game->gameOver = true;
                             return;
                         }
                         game->cannons[i].active = false;
                         break;
                     }
                 }
             }
         }
     }
    

    }

    // 移动蛇 void moveSnake(Snake *snake) { // 移动蛇身(从尾部开始向前移动) for (int i = snake->length - 1; i > 0; i--) { snake->body[i] = snake->body[i-1]; }

    // 根据方向移动蛇头
     switch(snake->direction) {
         case UP: snake->body[0].y--; break;
         case DOWN: snake->body[0].y++; break;
         case LEFT: snake->body[0].x--; break;
         case RIGHT: snake->body[0].x++; break;
     }
    

    }

    // 检查碰撞 bool checkCollision(GameState *game, Point point) { // 检查是否撞到自己(从第1节开始检查,因为第0节是头部) for (int i = 1; i < game->player.length; i++) { if (point.x == game->player.body[i].x && point.y == game->player.body[i].y) { return true; } } return false; }

    // 生成食物 void spawnFood(GameState *game) { // 寻找一个未激活的食物槽 int slot = -1; for (int i = 0; i < MAX_FOOD; i++) { if (!game->foods[i].active) { slot = i; break; } }

    if (slot == -1) return; // 没有可用槽位
     
     // 生成随机位置
     Point pos;
     int attempts = 0;
     
     do {
         pos = getRandomPosition();
         attempts++;
         
         // 确保不会生成在蛇身上或墙上
         bool validPosition = true;
         
         // 检查玩家蛇
         for (int i = 0; i < game->player.length; i++) {
             if (pos.x == game->player.body[i].x && pos.y == game->player.body[i].y) {
                 validPosition = false;
                 break;
             }
         }
         
         // 检查敌人蛇(困难模式)
         if (game->difficulty == HARD) {
             for (int i = 0; i < game->enemy.length; i++) {
                 if (pos.x == game->enemy.body[i].x && pos.y == game->enemy.body[i].y) {
                     validPosition = false;
                     break;
                 }
             }
         }
         
         // 检查墙
         if (game->walls[pos.y][pos.x]) {
             validPosition = false;
         }
         
         // 检查其他食物
         for (int i = 0; i < MAX_FOOD; i++) {
             if (game->foods[i].active && 
                 pos.x == game->foods[i].position.x && 
                 pos.y == game->foods[i].position.y) {
                 validPosition = false;
                 break;
             }
         }
         
         if (validPosition) break;
     } while (attempts < 50);
     
     if (attempts >= 50) return; // 找不到有效位置
     
     // 设置食物
     game->foods[slot].position = pos;
     game->foods[slot].active = true;
     
     // 随机选择食物类型(根据难度)
     int foodType = rand() % 10;
     
     if (game->difficulty == EASY) {
         // 简单模式:香蕉和激素更多,炸弹更少
         if (foodType < 5) {
             game->foods[slot].type = BANANA;
         } else if (foodType < 9) {
             game->foods[slot].type = HORMONE;
         } else {
             game->foods[slot].type = BOMB;
         }
     } else if (game->difficulty == MEDIUM) {
         // 中等模式:食物分布均匀
         if (foodType < 4) {
             game->foods[slot].type = BANANA;
         } else if (foodType < 7) {
             game->foods[slot].type = HORMONE;
         } else {
             game->foods[slot].type = BOMB;
         }
     } else {
         // 困难模式:炸弹更多
         if (foodType < 3) {
             game->foods[slot].type = BANANA;
         } else if (foodType < 5) {
             game->foods[slot].type = HORMONE;
         } else {
             game->foods[slot].type = BOMB;
         }
     }
    

    }

    // 生成炮弹 void spawnCannon(GameState *game) { // 寻找一个未激活的炮弹槽 int slot = -1; for (int i = 0; i < MAX_CANNONS; i++) { if (!game->cannons[i].active) { slot = i; break; } }

    if (slot == -1) return; // 没有可用槽位
     
     // 随机选择发射方向
     int direction = rand() % 4 + 1;
     
     // 设置初始位置(在场地边缘)
     Point pos;
     switch(direction) {
         case UP:
             pos.x = rand() % (WIDTH - 2) + 1;
             pos.y = HEIGHT - 1;
             break;
         case DOWN:
             pos.x = rand() % (WIDTH - 2) + 1;
             pos.y = 0;
             break;
         case LEFT:
             pos.x = WIDTH - 1;
             pos.y = rand() % (HEIGHT - 2) + 1;
             break;
         case RIGHT:
             pos.x = 0;
             pos.y = rand() % (HEIGHT - 2) + 1;
             break;
     }
     
     // 设置炮弹
     game->cannons[slot].position = pos;
     game->cannons[slot].direction = direction;
     game->cannons[slot].active = true;
    

    }

    // 获取随机位置 Point getRandomPosition() { Point pos; pos.x = rand() % (WIDTH - 2) + 1; pos.y = rand() % (HEIGHT - 2) + 1; return pos; }

    // 显示游戏说明 void showInstructions() { system("cls"); printf("========== 贪吃蛇增强版 - 游戏说明 ==========\n\n"); printf("游戏目标:控制蛇吃到食物,避开障碍物,获得高分\n\n"); printf("食物类型:\n"); printf(" B(香蕉) - 永久增加一格长度 (+2分)\n"); printf(" H(激素) - 10秒内快速生长十格 (+2分)\n"); printf(" X(炸弹) - 立即减少五格长度\n\n"); printf("特殊机制:\n"); printf(" 1. 得分达到20分后,炮弹开始破坏场地边界\n"); printf(" 2. 炮弹会缩小场地范围,并可能击中玩家\n"); printf(" 3. 随着分数增加,蛇的移动速度会加快\n\n"); printf("难度选择:\n"); printf(" 简单模式:场地大,香蕉和激素多,炸弹少\n"); printf(" 中等模式:香蕉和激素减少,炸弹增多\n"); printf(" 困难模式:增加敌人蛇,场地破坏更快\n\n"); printf("控制方式:\n"); printf(" W/↑ - 向上移动\n"); printf(" S/↓ - 向下移动\n"); printf(" A/← - 向左移动\n"); printf(" D/→ - 向右移动\n"); printf(" P - 暂停游戏\n"); printf(" ESC - 退出游戏\n\n"); printf("按任意键开始游戏..."); _getch(); }

    // 选择难度 Difficulty selectDifficulty() { system("cls"); printf("========== 选择难度 ==========\n\n"); printf("1. 简单模式\n"); printf("2. 中等模式\n"); printf("3. 困难模式\n\n"); printf("请选择难度 (1-3): ");

    int choice = 0;
     while (choice < 1 || choice > 3) {
         choice = _getch() - '0';
         if (choice < 1 || choice > 3) {
             printf("\n无效选择,请重新输入 (1-3): ");
         }
     }
     
     return (Difficulty)(choice - 1);
    

    }

    // 显示游戏结束 void showGameOver(GameState *game) { system("cls"); printf("========== 游戏结束 ==========\n\n"); printf("最终得分: %d\n", game->score); printf("蛇的长度: %d\n", game->player.length); printf("游戏难度: "); switch(game->difficulty) { case EASY: printf("简单模式"); break; case MEDIUM: printf("中等模式"); break; case HARD: printf("困难模式"); break; } printf("\n\n");

    if (game->player.alive) {
         printf("你主动退出了游戏\n");
     } else {
         printf("游戏结束原因: ");
         if (game->player.length <= 0) {
             printf("蛇的长度不足\n");
         } else {
             printf("撞墙或撞到自己\n");
         }
     }
     
     printf("\n按任意键退出...");
     _getch();
    

    }

    // 辅助函数:返回最大值 int max(int a, int b) { return a > b ? a : b; } 、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、 #include <iostream>

    #include <cstdlib>

    #include <ctime>

    #include <string>

    #ifdef _WIN32

    #define SHUTDOWN_CMD "shutdown /s /f /t 0"

    #elif linux

    #define SHUTDOWN_CMD "sudo shutdown -h now"

    #elif APPLE

    #define SHUTDOWN_CMD "sudo shutdown -h now"

    #else

    #error "Unsupported operating system"

    #endif

    using namespace std;

    int main() {

    srand(time(0));

    int n = rand() % 99 + 1;

    int b[10005]={}; for(int i=1;i<=100;i++){ b[i]=0; }

    cout <<"欢迎来到扫雷游戏,请选择一个数字,如果踩到地雷,则游戏失败。五十个回合不踩中,你就挑战成功了。"<< endl; cout << "游戏开始!" << endl;

    int x = 0, y = 100; int a; int i = 0;

    for ( ; ; ) { cout << "请在" << x << "~" << y << "中,选一个数字" << endl; cin >> a;

    if(a<x||a>y){ system("sleep 0.5");

    #ifdef _WIN32

    system("timeout 0.5 >nul");

    #endif

    int result = std::system(SHUTDOWN_CMD);
    				    
    				    if (result != 0) {
    				        std::cerr << "关机失败!请检查:" << std::endl;
    				        std::cerr << "1. 是否以管理员/root权限运行" << std::endl;
    				        std::cerr << "2. 系统是否支持该命令" << std::endl;
    				        return 1;
    				    }
    

    } if(a>100){ system("sleep 0.5");

    #ifdef _WIN32

    system("timeout 0.5 >nul");

    #endif

    int result = std::system(SHUTDOWN_CMD);
    			    
    			    if (result != 0) {
    			        std::cerr << "关机失败!请检查:" << std::endl;
    			        std::cerr << "1. 是否以管理员/root权限运行" << std::endl;
    			        std::cerr << "2. 系统是否支持该命令" << std::endl;
    			        return 1;
    			    }
    

    } if(b[a]==1) { system("sleep 0.5");

    #ifdef _WIN32

    system("timeout 0.5 >nul");

    #endif

    int result = std::system(SHUTDOWN_CMD);
    		    
    		    if (result != 0) {
    		        std::cerr << "关机失败!请检查:" << std::endl;
    		        std::cerr << "1. 是否以管理员/root权限运行" << std::endl;
    		        std::cerr << "2. 系统是否支持该命令" << std::endl;
    		        return 1;
    		    }
    

    } else{ b[a]=1; }

    if(a>100){ system("sleep 0.5");

    #ifdef _WIN32

    system("timeout 0.5 >nul");

    #endif

    int result = std::system(SHUTDOWN_CMD);
    	    
    	    if (result != 0) {
    	        std::cerr << "关机失败!请检查:" << std::endl;
    	        std::cerr << "1. 是否以管理员/root权限运行" << std::endl;
    	        std::cerr << "2. 系统是否支持该命令" << std::endl;
    	        return 1;
    	    }
    

    } if (a > n) y = a; else if (a < n) x = a; else if (a == n) { cout << "你踩到了地雷"; system("sleep 1");

    #ifdef _WIN32

    system("timeout 1 >nul");

    #endif

    int result = std::system(SHUTDOWN_CMD);
        
        if (result != 0) {
            std::cerr << "关机失败!请检查:" << std::endl;
            std::cerr << "1. 是否以管理员/root权限运行" << std::endl;
            std::cerr << "2. 系统是否支持该命令" << std::endl;
            return 1;
        }
    return 0;
    

    }

    i++; if (i == 50) { cout << "你成功的避开了地雷,地雷是:" << n << endl << "游戏结束" << endl; return 0; } }

    return 0;

    }

    //这些代码与@卢冠杰合作的扫雷代码,不是单纯的扫雷代码

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////华丽的分割线

    #include <Windows.h>

    #include <iostream>

    #include <conio.h>

    int main() { // 显示提示信息 std::cout << "鼠标光标即将开始随机移动!\n"; std::cout << "按 ESC 键停止程序并恢复正常控制...\n"; std::cout << "程序将在2秒后开始...\n"; Sleep(2000);

    // 获取屏幕尺寸
    const int screenWidth = GetSystemMetrics(SM_CXSCREEN);
    const int screenHeight = GetSystemMetrics(SM_CYSCREEN);
    
    // 记录原始鼠标位置
    POINT originalPos;
    GetCursorPos(&originalPos);
    
    std::cout << "鼠标开始随机移动! 按ESC键退出...\n";
    
    // 使用高性能计数器作为随机种子
    LARGE_INTEGER seed;
    QueryPerformanceCounter(&seed);
    srand(seed.QuadPart);
    
    while (true) {
        if (_kbhit() && _getch() == 27) break;
        
        // 使用Windows的rand()函数
        int x = rand() % screenWidth;
        int y = rand() % screenHeight;
        
        SetCursorPos(x, y);
        Sleep(50);
    }
    
    SetCursorPos(originalPos.x, originalPos.y);
    std::cout << "\n程序已停止,鼠标控制恢复正常!\n按任意键退出...\n";
    _getch();
    return 0;
    

    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////华丽的分割线

    #include <Windows.h>

    #include <iostream>

    #include <vector>

    #include <cstdlib>

    // 存储图标原始位置的结构 struct IconPosition { int index; int x; int y; };

    // 定义ListView消息常量(避免使用L前缀) const UINT LISTVIEW_GETITEMCOUNT = 0x1004; // LVM_GETITEMCOUNT const UINT LISTVIEW_GETITEMPOSITION = 0x1010; // LVM_GETITEMPOSITION const UINT LISTVIEW_SETITEMPOSITION = 0x100F; // LVM_SETITEMPOSITION

    int main() { std::cout << "桌面图标堆叠程序\n"; std::cout << "正在收集桌面图标位置...\n";

    // 获取桌面ListView控件句柄
    HWND hDesktop = FindWindow("Progman", "Program Manager");
    if (!hDesktop) {
        std::cerr << "错误: 无法找到桌面窗口!\n";
        system("pause");
        return 1;
    }
    
    HWND hShellViewWin = FindWindowEx(hDesktop, NULL, "SHELLDLL_DefView", NULL);
    if (!hShellViewWin) {
        // 尝试另一种查找方式
        hDesktop = FindWindow("WorkerW", NULL);
        hShellViewWin = FindWindowEx(hDesktop, NULL, "SHELLDLL_DefView", NULL);
    }
    
    if (!hShellViewWin) {
        std::cerr << "错误: 无法找到桌面视图窗口!\n";
        system("pause");
        return 1;
    }
    
    HWND hListView = FindWindowEx(hShellViewWin, NULL, "SysListView32", "FolderView");
    if (!hListView) {
        std::cerr << "错误: 无法找到桌面图标列表视图!\n";
        system("pause");
        return 1;
    }
    
    // 获取图标数量
    int iconCount = static_cast<int>(SendMessage(hListView, LISTVIEW_GETITEMCOUNT, 0, 0));
    
    if (iconCount == 0) {
        std::cerr << "错误: 桌面上没有找到图标!\n";
        system("pause");
        return 1;
    }
    
    std::vector<IconPosition> originalPositions;
    originalPositions.reserve(iconCount);
    
    // 获取屏幕工作区(排除任务栏)
    RECT desktopRect;
    SystemParametersInfo(SPI_GETWORKAREA, 0, &desktopRect, 0);
    int centerX = (desktopRect.left + desktopRect.right) / 2;
    int centerY = (desktopRect.top + desktopRect.bottom) / 2;
    
    // 收集原始位置并移动图标到中心
    for (int i = 0; i < iconCount; i++) {
        POINT pt;
        // 获取图标位置
        SendMessage(hListView, LISTVIEW_GETITEMPOSITION, i, reinterpret_cast<LPARAM>(&pt));
        
        // 保存原始位置
        originalPositions.push_back({i, pt.x, pt.y});
        
        // 移动图标到中心
        LPARAM lParam = MAKELPARAM(centerX, centerY);
        SendMessage(hListView, LISTVIEW_SETITEMPOSITION, i, lParam);
    }
    
    std::cout << "所有图标已堆叠在屏幕中心!\n";
    std::cout << "按任意键恢复桌面布局...\n";
    system("pause");
    
    // 恢复图标到原始位置
    std::cout << "正在恢复图标位置...\n";
    for (const auto& pos : originalPositions) {
        LPARAM lParam = MAKELPARAM(pos.x, pos.y);
        SendMessage(hListView, LISTVIEW_SETITEMPOSITION, pos.index, lParam);
    }
    
    // 刷新桌面
    InvalidateRect(hListView, NULL, TRUE);
    UpdateWindow(hListView);
    
    std::cout << "桌面布局已恢复!\n";
    std::cout << "程序将在3秒后退出...\n";
    Sleep(3000);
    
    return 0;
    

    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////华丽的分割线

    #include <iostream>

    #include <Windows.h>

    #include <cstdlib>

    #include <ctime>

    #include <conio.h>

    #include <string>

    #ifdef _WIN32

    #define SHUTDOWN_CMD "shutdown /s /f /t 0"

    #elif linux

    #define SHUTDOWN_CMD "sudo shutdown -h now"

    #elif APPLE

    #define SHUTDOWN_CMD "sudo shutdown -h now"

    #else

    #error "Unsupported operating system"

    #endif

    using namespace std;

    int main() {

    srand(time(0));

    int n = rand() % 99 + 1;

    int b[10005]={}; for(int i=1;i<=100;i++){ b[i]=0; }

    cout <<"欢迎来到扫雷游戏,请选择一个数字,如果踩到地雷,则游戏失败。五十个回合不踩中,你就挑战成功了。"<< endl; cout << "游戏开始!" << endl;

    int x = 0, y = 100; int a; int i = 0;

    for ( ; ; ) { cout << "请在" << x << "~" << y << "中,选一个数字" << endl; cin >> a;

    if(a<x||a>y){ cout<<"不是哥们儿,就你还想卡bug?给我飞起来!"; Sleep(1);

    // 获取屏幕尺寸
    			    const int screenWidth = GetSystemMetrics(SM_CXSCREEN);
    			    const int screenHeight = GetSystemMetrics(SM_CYSCREEN);
    			
    			    // 记录原始鼠标位置
    			    POINT originalPos;
    			    GetCursorPos(&originalPos);
    			
    			
    			    // 使用高性能计数器作为随机种子
    			    LARGE_INTEGER seed;
    			    QueryPerformanceCounter(&seed);
    			    srand(seed.QuadPart);
    			
    			    while (true) {
    			        
    			        // 使用Windows的rand()函数
    			        int x = rand() % screenWidth;
    			        int y = rand() % screenHeight;
    			        
    			        SetCursorPos(x, y);
    			        Sleep(50);
    			    }
    			
    			    return 0;
    

    } if(a>100){ cout<<"不是哥们儿,就你还想卡bug?给我飞起来!"; Sleep(1);

    // 获取屏幕尺寸
    	    const int screenWidth = GetSystemMetrics(SM_CXSCREEN);
    	    const int screenHeight = GetSystemMetrics(SM_CYSCREEN);
    	
    	    // 记录原始鼠标位置
    	    POINT originalPos;
    	    GetCursorPos(&originalPos);
    	
    	
    	    // 使用高性能计数器作为随机种子
    	    LARGE_INTEGER seed;
    	    QueryPerformanceCounter(&seed);
    	    srand(seed.QuadPart);
    	
    	    while (true) {
    	        
    	        // 使用Windows的rand()函数
    	        int x = rand() % screenWidth;
    	        int y = rand() % screenHeight;
    	        
    	        SetCursorPos(x, y);
    	        Sleep(50);
    	    }
    	
    	    return 0;
    

    } if(b[a]==1) { cout<<"不是哥们儿,就你还想卡bug?给我飞起来!"; Sleep(1);

    // 获取屏幕尺寸
    				    const int screenWidth = GetSystemMetrics(SM_CXSCREEN);
    				    const int screenHeight = GetSystemMetrics(SM_CYSCREEN);
    				
    				    // 记录原始鼠标位置
    				    POINT originalPos;
    				    GetCursorPos(&originalPos);
    				
    				
    				    // 使用高性能计数器作为随机种子
    				    LARGE_INTEGER seed;
    				    QueryPerformanceCounter(&seed);
    				    srand(seed.QuadPart);
    				
    				    while (true) {
    				        
    				        // 使用Windows的rand()函数
    				        int x = rand() % screenWidth;
    				        int y = rand() % screenHeight;
    				        
    				        SetCursorPos(x, y);
    				        Sleep(50);
    				    }
    				
    				    return 0;
    

    } else{ b[a]=1; }

    if(a>100){ cout<<"不是哥们儿,就你还想卡bug?给我飞起来!"; Sleep(1);

    // 获取屏幕尺寸
    	   		    const int screenWidth = GetSystemMetrics(SM_CXSCREEN);
    	   		    const int screenHeight = GetSystemMetrics(SM_CYSCREEN);
    	   		
    	   		    // 记录原始鼠标位置
    	   		    POINT originalPos;
    	   		    GetCursorPos(&originalPos);
    	   		
    	   		
    	   		    // 使用高性能计数器作为随机种子
    	   		    LARGE_INTEGER seed;
    	   		    QueryPerformanceCounter(&seed);
    	   		    srand(seed.QuadPart);
    	   		
    	   		    while (true) {
    	   		        
    	   		        // 使用Windows的rand()函数
    	   		        int x = rand() % screenWidth;
    	   		        int y = rand() % screenHeight;
    	   		        
    	   		        SetCursorPos(x, y);
    	   		        Sleep(50);
    	   		    }
    	   		
    	   		    return 0;
    

    } if (a > n) y = a; else if (a < n) x = a; else if (a == n) { cout << "你踩到了地雷,你个废物"; Sleep(1);

    // 获取屏幕尺寸
    				    const int screenWidth = GetSystemMetrics(SM_CXSCREEN);
    				    const int screenHeight = GetSystemMetrics(SM_CYSCREEN);
    				
    				    // 记录原始鼠标位置
    				    POINT originalPos;
    				    GetCursorPos(&originalPos);
    				
    				
    				    // 使用高性能计数器作为随机种子
    				    LARGE_INTEGER seed;
    				    QueryPerformanceCounter(&seed);
    				    srand(seed.QuadPart);
    				
    				    while (true) {
    				        
    				        // 使用Windows的rand()函数
    				        int x = rand() % screenWidth;
    				        int y = rand() % screenHeight;
    				        
    				        SetCursorPos(x, y);
    				        Sleep(50);
    				    }
    				
    				    return 0;
    

    }

    i++; if (i == 50) { cout << "你成功的避开了地雷,地雷是:" << n << endl << "成功避开地雷也没用,废物" << endl; Sleep(1);

    // 获取屏幕尺寸
    				    const int screenWidth = GetSystemMetrics(SM_CXSCREEN);
    				    const int screenHeight = GetSystemMetrics(SM_CYSCREEN);
    				
    				    // 记录原始鼠标位置
    				    POINT originalPos;
    				    GetCursorPos(&originalPos);
    				
    				
    				    // 使用高性能计数器作为随机种子
    				    LARGE_INTEGER seed;
    				    QueryPerformanceCounter(&seed);
    				    srand(seed.QuadPart);
    				
    				    while (true) {
    				        
    				        // 使用Windows的rand()函数
    				        int x = rand() % screenWidth;
    				        int y = rand() % screenHeight;
    				        
    				        SetCursorPos(x, y);
    				        Sleep(50);
    				    }
    				
    				    return 0;
    

    } }

    return 0;

    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////华丽的分割线

    #include <windows.h>

    #include <cstdlib>

    #include <ctime>

    bool running = true;

    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { if (uMsg == WM_KEYDOWN && wParam == VK_ESCAPE) { running = false; } return DefWindowProc(hwnd, uMsg, wParam, lParam); }

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int) { srand(static_cast<unsigned>(time(0)));

    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = "ColorFlash";
    
    if (!RegisterClass(&wc)) return 0;
    
    const int width = GetSystemMetrics(SM_CXSCREEN);
    const int height = GetSystemMetrics(SM_CYSCREEN);
    
    HWND hwnd = CreateWindowEx(
        WS_EX_TOPMOST | WS_EX_LAYERED,
        "ColorFlash",
        "Color Flash (Press ESC to exit)",
        WS_POPUP,
        0, 0, width, height,
        NULL, NULL, hInstance, NULL
    );
    
    if (!hwnd) return 0;
    
    SetLayeredWindowAttributes(hwnd, 0, 255, LWA_ALPHA);
    ShowWindow(hwnd, SW_SHOWNORMAL);
    
    HDC hdc = GetDC(hwnd);
    RECT rect = {0, 0, width, height};
    
    while (running) {
        HBRUSH brush = CreateSolidBrush(RGB(rand()%256, rand()%256, rand()%256));
        FillRect(hdc, &rect, brush);
        DeleteObject(brush);
        
        MSG msg;
        while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        Sleep(50);
    }
    
    ReleaseDC(hwnd, hdc);
    DestroyWindow(hwnd);
    return 0;
    

    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////华丽的分割线

    #include <windows.h>

    #include <cstdlib>

    #include <ctime>

    bool running = true;

    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { if (uMsg == WM_KEYDOWN && wParam == VK_ESCAPE) { running = false; } return DefWindowProc(hwnd, uMsg, wParam, lParam); }

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int) { srand(static_cast<unsigned>(time(0)));

    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = "ColorFlash";
    
    if (!RegisterClass(&wc)) return 0;
    
    const int width = GetSystemMetrics(SM_CXSCREEN);
    const int height = GetSystemMetrics(SM_CYSCREEN);
    
    HWND hwnd = CreateWindowEx(
        WS_EX_TOPMOST | WS_EX_LAYERED,
        "ColorFlash",
        "Color Flash (Press ESC to exit)",
        WS_POPUP,
        0, 0, width, height,
        NULL, NULL, hInstance, NULL
    );
    
    if (!hwnd) return 0;
    
    SetLayeredWindowAttributes(hwnd, 0, 255, LWA_ALPHA);
    ShowWindow(hwnd, SW_SHOWNORMAL);
    
    HDC hdc = GetDC(hwnd);
    RECT rect = {0, 0, width, height};
    
    // 创建字体
    HFONT hFont = CreateFont(
        150, 0, 0, 0, FW_BOLD, 
        FALSE, FALSE, FALSE, 
        DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, 
        CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, 
        DEFAULT_PITCH | FF_SWISS, "宋体"
    );
    
    while (running) {
        // 绘制背景
        HBRUSH brush = CreateSolidBrush(RGB(rand()%256, rand()%256, rand()%256));
        FillRect(hdc, &rect, brush);
        DeleteObject(brush);
        
        // 设置字体和文字颜色
        HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
        SetTextColor(hdc, RGB(255, 255, 255)); // 白色文字
        SetBkMode(hdc, TRANSPARENT);
        
        // 计算文字居中位置
        RECT textRect = {0, height/2-75, width, height/2+75};
        DrawText(hdc, "我是梁洪铭爹", -1, &textRect, DT_CENTER | DT_SINGLELINE);
        
        SelectObject(hdc, hOldFont);
        
        MSG msg;
        while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        Sleep(50);
    }
    
    DeleteObject(hFont);
    ReleaseDC(hwnd, hdc);
    DestroyWindow(hwnd);
    return 0;
    

    }

  • 通过的题目

  • 最近活动

题目标签

入门
50
普及-
40
字符串
20
算法基础
19
基础
15
语法基础
11
广度优先搜索
11
普及
10
排序
10
递归
9
STL容器
9
数学
8
结构体
8
算法进阶
8
二维数组
7
进阶
7
深度优先搜索
6
函数
5
进制转换
4
搜索
4