CURSORINFO

CURSORINFO Structure

该结构包含了全局光标信息。

语法

typedef struct {
DWORD cbSize;
DWORD flags;
HCURSOR hCursor;
POINT ptScreenPos;
} CURSORINFO, *PCURSORINFO, *LPCURSORINFO;

参数:

cbSize  DWORD
指定结构自身的大小(字节)。主调方必须设置该成员为 sizeof(CURSORINFO).

flags     DWORD


指定光标的状态。该参数可以是下列值之一:

     0    光标处于隐藏状态。

     CURSOR_SHOWING    光标显示状态。

 

hCursor     HCURSOR    光标句柄



ptScreenPos     POINT


一个POINT结构,它用于接收光标的屏幕坐标。


 

要求

Minimum supported client

Windows XP

Minimum supported server

Windows 2000 Server

Header

Winuser.h (include Windo

#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<windows.h> #define High 29 #define Width 50 int moveDirection; int food_x, food_y; int canvas[High][Width] = { 0 }; int snakeLength = 4; int gameStarted = 0; void gotoxy(int x, int y) { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(handle, pos); } void hideCursor() { CONSOLE_CURSOR_INFO cursorInfo; cursorInfo.dwSize = 1; cursorInfo.bVisible = FALSE; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo); } int isPositionOnSnake(int x, int y) { return canvas[x][y] > 0; } void generateFood() { do { food_x = rand() % (High - 2) + 1; food_y = rand() % (Width - 2) + 1; } while (isPositionOnSnake(food_x, food_y)); canvas[food_x][food_y] = -2; } void moveSnakeByDirection() { if (!gameStarted) return; int i, j; for (i = 1; i < High - 1; i++) for (j = 1; j < Width - 1; j++) if (canvas[i][j] > 0) canvas[i][j]++; int oldTail_i, oldTail_j, oldHead_i, oldHead_j; int max = 0; for (i = 1; i < High - 1; i++) for (j = 1; j < Width - 1; j++) if (canvas[i][j] > 0) { if (max < canvas[i][j]) { max = canvas[i][j]; oldTail_i = i; oldTail_j = j; } if (canvas[i][j] == 2) { oldHead_i = i; oldHead_j = j; } } int newHead_i,newHead_j; if (moveDirection == 1) { newHead_i = oldHead_i - 1; newHead_j = oldHead_j; } if (moveDirection == 2) { newHead_i = oldHead_i + 1; newHead_j = oldHead_j; } if (moveDirection == 3) { newHead_i = oldHead_i; newHead_j = oldHead_j - 1; } if (moveDirection == 4) { newHead_i = oldHead_i; newHead_j = oldHead_j + 1; } if (canvas[newHead_i][newHead_j] == -2) { snakeLength++; generateFood(); } else canvas[oldTail_i][oldTail_j] = 0; if (canvas[newHead_i][newHead_j] > 0 || canvas[newHead_i][newHead_j] == -1) { printf("游戏结束!"); Sleep(2000); system("pause"); exit(0); } else canvas[newHead_i][newHead_j] = 1; } void startup() { int i, j; for (i = 0; i < High; i++) { canvas[i][0] = -1; canvas[i][Width - 1] = -1; } for (j = 0; j < Width; j++) { canvas[0][j] = -1; canvas[High - 1][j] = -1; } canvas[High / 2][Width / 2] = 1; for (i = 1; i < 4; i++) canvas[High / 2][Width / 2 - i] = i + 1; moveDirection = 4; generateFood(); } void show() { if (!gameStarted) { gotoxy(Width / 2 - 10, High / 2 - 5); printf("欢迎来到贪吃蛇游戏!"); gotoxy(Width / 2 - 15, High / 2 - 3); printf("使用 W/A/S/D 控制蛇的移动方向"); gotoxy(Width / 2 - 10, High / 2 + 1); printf("按空格键开始游戏"); return; } gotoxy(0, 0); int i, j; for (i = 0; i < High; i++) { for (j = 0; j < Width; j++) { if (canvas[i][j] == 0) printf(" "); else if (canvas[i][j] == -1) printf("#"); else if (canvas[i][j] == 1) printf("@"); else if (canvas[i][j] > 1) printf("*"); else if (canvas[i][j] == -2) printf("F"); } printf("\n"); } printf("得分: %d", snakeLength - 4); Sleep(150); } void updateWithoutInput() { moveSnakeByDirection(); } void updateWihtInput() { char input; if (_kbhit()) { input = _getch(); if (!gameStarted) { if (input == ' ') { gameStarted = 1; system("cls"); } else if (input == 27) { exit(0); } return; } if (input == 'w' && moveDirection != 2) moveDirection = 1; else if (input == 's' && moveDirection != 1) moveDirection = 2; else if (input == 'a' && moveDirection != 4) moveDirection = 3; else if (input == 'd' && moveDirection != 3) moveDirection = 4; } } int main() { hideCursor(); startup(); while (1) { show(); updateWithoutInput(); updateWihtInput(); } return 0; }加入吃球后音效提示
06-14
#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> #include <time.h> #include <string.h> #define WIDTH 60 #define HEIGHT 20 typedef struct Food { int x; int y; } Food; typedef struct Snakenode { int x; int y; struct Snakenode* next; } Snakenode; typedef struct PlayerRecord { char username[50]; int totalScore; int totalTime; int gameCount; } PlayerRecord; // 全局变量 Snakenode* head = NULL; Food food; time_t start; char username[50]; int direction = 1; int playAgain = 0; int snakeBody[WIDTH * HEIGHT][2]; int bodyCount = 0; PlayerRecord records[100]; int recordCount = 0; // 函数声明(修复核心问题) void moveCursor(int x, int y); void sortRecordsByUsername(); void loadAllRecords(); void showSortedRecords(); void init(); void makefood(); void move(); int alive(); void endGame(); int Score(); void saveData(); void showMap(); void viewData(); // 函数实现 void moveCursor(int x, int y) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); CONSOLE_CURSOR_INFO cursorInfo; GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo); cursorInfo.bVisible = FALSE; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo); } void sortRecordsByUsername() { for (int i = 0; i < recordCount - 1; i++) { for (int j = 0; j < recordCount - i - 1; j++) { if (strcmp(records[j].username, records[j + 1].username) > 0) { PlayerRecord temp = records[j]; records[j] = records[j + 1]; records[j + 1] = temp; } } } } void loadAllRecords() { FILE* fp = fopen("score_and_time_and_username.txt", "r"); if (!fp) return; char line[200]; char currentUser[50] = { 0 }; int currentScore = 0; int currentTime = 0; recordCount = 0; while (fgets(line, sizeof(line), fp) != NULL) { if (strstr(line, "用户名: ") == line) { sscanf(line, "用户名: %s", currentUser); } else if (strstr(line, "分数: ") == line) { sscanf(line, "分数: %d", &currentScore); } else if (strstr(line, "时间: ") == line) { sscanf(line, "时间: %d", &currentTime); int found = 0; for (int i = 0; i < recordCount; i++) { if (strcmp(records[i].username, currentUser) == 0) { records[i].totalScore += currentScore; records[i].totalTime += currentTime; records[i].gameCount++; found = 1; break; } } if (!found && recordCount < 100) { strcpy(records[recordCount].username, currentUser); records[recordCount].totalScore = currentScore; records[recordCount].totalTime = currentTime; records[recordCount].gameCount = 1; recordCount++; } } } fclose(fp); } void showSortedRecords() { system("cls"); loadAllRecords(); sortRecordsByUsername(); printf("===== 玩家总战绩(按用户名排序) =====\n"); printf("%-15s %-10s %-10s %-10s\n", "用户名", "总分数", "总时间", "游戏次数"); for (int i = 0; i < recordCount; i++) { printf("%-15s %-10d %-10d %-10d\n", records[i].username, records[i].totalScore, records[i].totalTime, records[i].gameCount); } printf("\n按任意键返回..."); _getch(); } void init() { head = (Snakenode*)malloc(sizeof(Snakenode)); head->x = WIDTH / 2; head->y = HEIGHT / 2; head->next = NULL; makefood(); start = time(NULL); snakeBody[0][0] = head->x; snakeBody[0][1] = head->y; bodyCount = 1; } void makefood() { food.x = rand() % WIDTH; food.y = rand() % HEIGHT; Snakenode* p = head; while (p) { if (p->x == food.x && p->y == food.y) { makefood(); return; } p = p->next; } } void move() { int x = head->x; int y = head->y; switch (direction) { case 0: y--; break; case 1: x++; break; case 2: y++; break; case 3: x--; break; } Snakenode* newhead = NULL; if (x == food.x && y == food.y) { newhead = (Snakenode*)malloc(sizeof(Snakenode)); newhead->x = x; newhead->y = y; newhead->next = head; head = newhead; makefood(); snakeBody[bodyCount][0] = x; snakeBody[bodyCount][1] = y; bodyCount++; } else { newhead = (Snakenode*)malloc(sizeof(Snakenode)); newhead->x = x; newhead->y = y; newhead->next = head; head = newhead; for (int i = bodyCount - 1; i > 0; i--) { snakeBody[i][0] = snakeBody[i - 1][0]; snakeBody[i][1] = snakeBody[i - 1][1]; } snakeBody[0][0] = x; snakeBody[0][1] = y; Snakenode* q = head; while (q->next->next) { q = q->next; } free(q->next); q->next = NULL; } if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT || alive()) { endGame(); } } int alive() { Snakenode* p = head->next; while (p) { if (p->x == head->x && p->y == head->y) { return 1; } p = p->next; } return 0; } void endGame() { system("cls"); time_t end = time(NULL); int gameduration = (int)difftime(end, start); printf("Game Over!\n"); printf("用户名:%s\n", username); printf("分数: %d\n", Score()); printf("时间: %d 秒\n", gameduration); printf("重新开始游戏请选择:\n"); printf("1. 重新开始\n"); printf("2. 查看保存的战绩\n"); saveData(); int choice; scanf("%d", &choice); if (choice == 1) { playAgain = 1; Snakenode* p = head; while (p) { Snakenode* temp = p; p = p->next; free(temp); } head = NULL; head = (Snakenode*)malloc(sizeof(Snakenode)); head->x = WIDTH / 2; head->y = HEIGHT / 2; head->next = NULL; makefood(); start = time(NULL); bodyCount = 0; } else if (choice == 2) { system("cls"); viewData(); printf("是否重新开始?(1. 重新开始,其他键退出)"); scanf("%d", &choice); if (choice == 1) { playAgain = 1; Snakenode* p = head; while (p) { Snakenode* temp = p; p = p->next; free(temp); } head = NULL; head = (Snakenode*)malloc(sizeof(Snakenode)); head->x = WIDTH / 2; head->y = HEIGHT / 2; head->next = NULL; makefood(); start = time(NULL); bodyCount = 0; } else { exit(0); } } else { exit(0); } } int Score() { int score = 0; Snakenode* p = head; while (p) { score++; p = p->next; } return score - 1; } void saveData() { int score = Score(); time_t end = time(NULL); int gameDuration = (int)difftime(end, start); FILE* fp = fopen("score_and_time_and_username.txt", "a"); if (fp) { fprintf(fp, "用户名: %s\n", username); fprintf(fp, "分数: %d\n", score); fprintf(fp, "时间: %d 秒\n", gameDuration); fclose(fp); } } void showMap() { moveCursor(0, 0); for (int i = 0; i < WIDTH + 4; i++) { printf("#"); } printf("\n"); for (int j = 0; j < HEIGHT; j++) { printf("##"); for (int i = 0; i < WIDTH; i++) { if (i == head->x && j == head->y) { printf("@"); } else if (i == food.x && j == food.y) { printf("$"); } else { int isSnake = 0; for (int k = 0; k < bodyCount; k++) { if (i == snakeBody[k][0] && j == snakeBody[k][1]) { printf("@"); isSnake = 1; break; } } if (!isSnake) { printf(" "); } } } printf("##\n"); } for (int i = 0; i < WIDTH + 4; i++) { printf("#"); } moveCursor(WIDTH + 5, 0); printf("用户: %s", username); moveCursor(WIDTH + 5, 1); printf("得分: %d", Score()); moveCursor(WIDTH + 5, 2); printf("时间: %d 秒", (int)difftime(time(NULL), start)); printf("\n"); } void viewData() { FILE* fp = fopen("score_and_time_and_username.txt", "r"); if (fp) { char line[200]; printf("保存的数据如下:\n"); while (fgets(line, sizeof(line), fp) != NULL) { printf("%s", line); } fclose(fp); } else { printf("没有保存的数据可查看。\n"); } printf("\n1. 按用户名排序查看总战绩\n"); printf("2. 返回\n"); int choice; scanf("%d", &choice); if (choice == 1) { showSortedRecords(); } } int main() { char choice; printf("菜单:\n"); printf("x - 进入游戏\n"); printf("c - 退出游戏\n"); scanf(" %c", &choice); if (choice == 'x') { printf("请输入用户名:"); scanf("%s", username); srand((unsigned int)time(NULL)); do { init(); while (1) { showMap(); move(); if (_kbhit()) { char key = _getch(); switch (key) { case 'w': if (direction != 2) direction = 0; break; case 'd': if (direction != 3) direction = 1; break; case 's': if (direction != 0) direction = 2; break; case 'a': if (direction != 1) direction = 3; break; } } Sleep(100); } saveData(); } while (playAgain); } else if (choice == 'c') { printf("已退出游戏。\n"); return 0; } else { printf("无效的选择,请重新运行程序。\n"); return 0; } return 0; } 添加每一行的注释
最新发布
07-11
#include <iostream> #include <conio.h> #include <windows.h> #include <ctime> #include <deque> #include <cstdlib> using namespace std; const int WIDTH = 40; // 游戏区域宽度 const int HEIGHT = 20; // 游戏区域高度 // 定义方向枚举 enum Direction { STOP = 0, LEFT, RIGHT, UP, DOWN }; // 游戏状态结构体 struct GameState { int snakeX, snakeY; // 蛇头位置 int fruitX, fruitY; // 水果位置 int chestX, chestY; // 宝箱位置 int score; // 得分 Direction direction; // 当前方向 bool gameOver; // 游戏结束标志 deque<int> tailX; // 蛇身X坐标队列 deque<int> tailY; // 蛇身Y坐标队列 int tailLength; // 蛇身长度 bool chestActive; // 宝箱是否激活 int chestTimer; // 宝箱刷新计时器 const int CHEST_SPAWN_INTERVAL = 50; // 宝箱刷新间隔 bool mapFull = false; // 标志地图是否被填满 int speedLevel = 0; // 新增:速度等级 (0-初始速度, 1-加快, 2-更快等) }; // 设置控制台光标位置 void SetCursorPosition(short x, short y) { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos = {x, y}; SetConsoleCursorPosition(hConsole, pos); } // 隐藏控制台光标 void HideCursor() { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cursorInfo; GetConsoleCursorInfo(hConsole, &cursorInfo); cursorInfo.bVisible = false; SetConsoleCursorInfo(hConsole, &cursorInfo); } // 检查位置是否被占用 bool IsPositionOccupied(const GameState& state, int x, int y) { // 如果是地图满状态,所有位置都被占用 if (state.mapFull) return true; // 检查蛇头 if (state.snakeX == x && state.snakeY == y) return true; // 检查水果 if (state.fruitX == x && state.fruitY == y) return true; // 检查宝箱 if (state.chestActive && state.chestX == x && state.chestY == y) return true; // 检查蛇身 for (int i = 0; i < state.tailLength; i++) { if (state.tailX[i] == x && state.tailY[i] == y) return true; } return false; } // 检查地图是否被完全占据 bool IsMapFull(const GameState& state) { // 总格子数 int totalCells = WIDTH * HEIGHT; // 如果蛇身长度+1(蛇头)小于总格子数,地图肯定没满 if (state.tailLength + 1 < totalCells) return false; // 创建一个虚拟地图,标记所有占据位置 bool occupied[HEIGHT][WIDTH] = {false}; // 标记蛇头位置 occupied[state.snakeY][state.snakeX] = true; // 标记蛇身位置 for (int i = 0; i < state.tailLength; i++) { occupied[state.tailY[i]][state.tailX[i]] = true; } // 遍历所有格子,如果发现任何空位,返回false for (int y = 0; y < HEIGHT; y++) { for (int x = 0; x < WIDTH; x++) { if (!occupied[y][x]) return false; // 发现空位,地图没满 } } return true; // 所有格子都被占满 } // 初始化游戏状态 void Setup(GameState& state) { state.gameOver = false; state.direction = STOP; state.snakeX = WIDTH / 2; state.snakeY = HEIGHT / 2; // 随机生成水果位置 state.fruitX = rand() % WIDTH; state.fruitY = rand() % HEIGHT; // 初始化宝箱 state.chestActive = false; state.chestTimer = 0; state.score = 0; state.tailLength = 0; state.mapFull = false; // 初始状态地图不满 state.speedLevel = 0; // 初始速度等级 state.tailX.clear(); state.tailY.clear(); } // 生成新水果(带安全机制) void GenerateFruit(GameState& state) { // 如果地图被填满,跳过生成水果 if (state.mapFull) { return; } const int MAX_ATTEMPTS = 100; // 最大尝试次数 int attempts = 0; do { state.fruitX = rand() % WIDTH; state.fruitY = rand() % HEIGHT; attempts++; // 防止无限循环,达到最大尝试次数后停止 if (attempts >= MAX_ATTEMPTS) { // 如果尝试了很多次仍失败,标记地图已满 state.mapFull = IsMapFull(state); break; } } while (IsPositionOccupied(state, state.fruitX, state.fruitY)); } // 生成新宝箱(带安全机制) void GenerateChest(GameState& state) { // 如果地图被填满,跳过生成宝箱 if (state.mapFull) { return; } const int MAX_ATTEMPTS = 100; // 最大尝试次数 int attempts = 0; do { state.chestX = rand() % WIDTH; state.chestY = rand() % HEIGHT; attempts++; // 防止无限循环 if (attempts >= MAX_ATTEMPTS) { state.chestActive = false; // 放弃生成宝箱 state.chestTimer = state.CHEST_SPAWN_INTERVAL / 2; // 设置半程计时器 state.mapFull = IsMapFull(state); // 检查地图是否已满 break; } } while (IsPositionOccupied(state, state.chestX, state.chestY)); if (attempts < MAX_ATTEMPTS) { state.chestActive = true; } } // 绘制游戏界面 void Draw(const GameState& state) { // 设置控制台位置到左上角 SetCursorPosition(0, 0); // 绘制上边框 for (int i = 0; i < WIDTH + 2; i++) cout << "#"; cout << endl; // 绘制游戏区域 for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { // 左边界 if (j == 0) cout << "#"; // 绘制蛇头 if (i == state.snakeY && j == state.snakeX) cout << "O"; // 绘制水果 else if (i == state.fruitY && j == state.fruitX) cout << "@"; // 绘制宝箱 else if (state.chestActive && i == state.chestY && j == state.chestX) cout << "$"; else { bool isTail = false; // 绘制蛇身 for (int k = 0; k < state.tailLength; k++) { if (state.tailX[k] == j && state.tailY[k] == i) { cout << "o"; isTail = true; break; } } if (!isTail) cout << " "; } // 右边界 if (j == WIDTH - 1) cout << "#"; } cout << endl; } // 绘制下边框 for (int i = 0; i < WIDTH + 2; i++) cout << "#"; cout << endl; // 显示游戏信息 cout << "得分: " << state.score << " "; cout << "蛇身长度: " << state.tailLength << " "; // 显示速度等级 cout << "速度: " << state.speedLevel + 1 << "级 "; if (state.mapFull) { cout << "地图已满! "; } else if (state.chestActive) { cout << "宝箱: ($) "; } else { int timeLeft = (state.CHEST_SPAWN_INTERVAL - state.chestTimer) / 5; cout << "下个宝箱: " << max(0, timeLeft) << " "; } cout << "按 'x'退出游戏" << endl; } // 处理用户输入 void ProcessInput(GameState& state) { // 检查是否有按键输入 if (_kbhit()) { switch (_getch()) { case 'a': case 'A': if (state.direction != RIGHT) state.direction = LEFT; break; case 'd': case 'D': if (state.direction != LEFT) state.direction = RIGHT; break; case 'w': case 'W': if (state.direction != DOWN) state.direction = UP; break; case 's': case 'S': if (state.direction != UP) state.direction = DOWN; break; case 'x': case 'X': state.gameOver = true; break; case 'p': case 'P': // 暂停功能 while (true) { if (_kbhit() && _getch() == 'p') break; Sleep(100); } break; } } } // 更新速度等级(基于蛇身长度) void UpdateSpeedLevel(GameState& state) { // 每5个长度提升一个速度等级 int newLevel = state.tailLength / 5; // 仅当等级提升时才显示提示 if (newLevel > state.speedLevel) { state.speedLevel = newLevel; // 在游戏界面显示速度提升提示 SetCursorPosition(0, HEIGHT + 3); cout << "蛇身长度达到 " << (state.speedLevel * 5) + 5 << ",速度提升!"; Sleep(800); // 短暂显示提示 // 清除提示 SetCursorPosition(0, HEIGHT + 3); cout << " "; SetCursorPosition(0, HEIGHT + 3); // 重置位置 } } // 更新游戏逻辑 void Update(GameState& state) { // 如果地图已满,直接游戏胜利 if (state.mapFull) { state.gameOver = true; return; } // 保存蛇头位置 int prevHeadX = state.snakeX; int prevHeadY = state.snakeY; // 更新蛇头位置 switch (state.direction) { case LEFT: state.snakeX--; break; case RIGHT: state.snakeX++; break; case UP: state.snakeY--; break; case DOWN: state.snakeY++; break; } // 处理穿墙效果 if (state.snakeX < 0) state.snakeX = WIDTH - 1; if (state.snakeX >= WIDTH) state.snakeX = 0; if (state.snakeY < 0) state.snakeY = HEIGHT - 1; if (state.snakeY >= HEIGHT) state.snakeY = 0; // 检查是否撞到自己 for (int i = 0; i < state.tailLength; i++) { if (state.tailX[i] == state.snakeX && state.tailY[i] == state.snakeY) state.gameOver = true; } // 如果蛇在移动,更新蛇身位置 if (state.direction != STOP) { // 添加新的头部位置 state.tailX.push_front(prevHeadX); state.tailY.push_front(prevHeadY); // 吃到水果 if (state.snakeX == state.fruitX && state.snakeY == state.fruitY) { // 增加分数 state.score += 10; state.tailLength++; // 检查速度等级是否提升 UpdateSpeedLevel(state); // 检查地图是否即将满 state.mapFull = IsMapFull(state); // 生成新水果位置 GenerateFruit(state); } // 吃到宝箱 else if (state.chestActive && state.snakeX == state.chestX && state.snakeY == state.chestY) { // 增加分数 state.score += 50; // 身体增加两个长度 state.tailLength += 2; // 检查速度等级是否提升 UpdateSpeedLevel(state); // 检查地图是否即将满 state.mapFull = IsMapFull(state); // 重置宝箱状态 state.chestActive = false; state.chestTimer = 0; } // 什么都没吃到,移除尾部 else { if (!state.tailX.empty()) { state.tailX.pop_back(); state.tailY.pop_back(); } } } // 更新宝箱逻辑 if (!state.chestActive) { state.chestTimer++; // 达到刷新时间 if (state.chestTimer >= state.CHEST_SPAWN_INTERVAL) { GenerateChest(state); } } } // 计算游戏速度(基于速度等级) int CalculateGameSpeed(const GameState& state) { // 速度等级对应的延迟毫秒数(越小越快) const int speedLevels[6] = { 150, // 等级0: 基础速度 (0-4长度) 120, // 等级1: 基础+1 (5-9长度) 90, // 等级2: 基础+2 (10-14长度) 70, // 等级3: 基础+3 (15-19长度) 50, // 等级4: 基础+4 (20-24长度) 30 // 等级5: 基础+5 (25+长度) }; // 获取当前速度等级对应的延迟 int levelIndex = min(state.speedLevel, 5); return speedLevels[levelIndex]; } int main() { // 设置随机种子 srand(static_cast<unsigned>(time(0))); // 隐藏光标 HideCursor(); // 设置控制台标题 SetConsoleTitle("贪吃蛇游戏 - 长度等级速度系统"); // 初始化游戏状态 GameState game; Setup(game); // 游戏主循环 while (!game.gameOver) { Draw(game); ProcessInput(game); Update(game); // 根据速度等级计算游戏速度 int speed = CalculateGameSpeed(game); Sleep(speed); } // 游戏结束处理 system("cls"); if (game.mapFull) { cout << "恭喜你通关了!整个地图都被蛇填满了!\n"; } else { cout << "游戏结束!\n"; } cout << "你的最终得分: " << game.score << endl; cout << "蛇身长度: " << game.tailLength << endl; cout << "最终速度等级: " << game.speedLevel + 1 << endl; cout << "按任意键退出..." << endl; _getch(); return 0; }帮我对上述代码每一行做出解释
06-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值