-
Bio
#include <iostream> #include <conio.h> // 用于键盘输入 #include <windows.h> // 用于控制台操作 using namespace std; // 游戏区域大小 const int width = 20; const int height = 20; // 蛇的方向 enum Direction { STOP = 0, LEFT, RIGHT, UP, DOWN }; Direction dir; // 游戏状态 bool gameOver; int x, y; // 蛇头的位置 int fruitX, fruitY; // 食物的位置 int tailX[100], tailY[100]; // 蛇尾的位置 int nTail; // 蛇尾的长度 int score; // 初始化游戏 void Setup() { gameOver = false; dir = STOP; x = width / 2; y = height / 2; fruitX = rand() % width; fruitY = rand() % height; score = 0; } void Draw() { system("cls"); // 清屏 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 == y && j == x) cout << "O"; // 绘制食物 else if (i == fruitY && j == fruitX) cout << "F"; else { bool printTail = false; // 绘制蛇尾 for (int k = 0; k < nTail; k++) { if (tailX[k] == j && tailY[k] == i) { cout << "o"; printTail = true; } } if (!printTail) cout << " "; } if (j == width - 1) cout << "#"; } cout << endl; } for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; cout << "Score: " << score << endl;} // 处理键盘输入 void Input() { if (_kbhit()) { // 检测是否有键盘输入 switch (_getch()) { case 'a': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'x': gameOver = true; break; } } } void Logic() { int prevX = tailX[0]; int prevY = tailY[0]; int prev2X, prev2Y; tailX[0] = x; tailY[0] = y; for (int i = 1; i < nTail; i++) { prev2X = tailX[i]; prev2Y = tailY[i]; tailX[i] = prevX; tailY[i] = prevY; prevX = prev2X; prevY = prev2Y; } switch (dir) { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; break; } // 检测是否撞墙 if (x >= width || x < 0 || y >= height || y < 0) gameOver = true; // 检测是否撞到自己 for (int i = 0; i < nTail; i++) { if (tailX[i] == x && tailY[i] == y) gameOver = true; } // 检测是否吃到食物 if (x == fruitX && y == fruitY) { score += 10; fruitX = rand() % width; fruitY = rand() % height; nTail++; } } int main() { Setup(); while (!gameOver) { Draw(); Input(); Logic(); Sleep(100); // 控制游戏速度 } cout << "Game Over! Final Score: " << score << endl; return 0; }
:start
taskkill /f /im "REDAgent.exe" /t
goto start
-
Accepted Problems
-
Recent Activities
- 【2024级】递归算法 Assignment
- 【2024级】基础dfs Assignment
- 【2024-2025-3-21】3月测试 IOI
- 【2024级】简单排序 Assignment
- 【2024级】递推算法 Assignment
- 【2024级】枚举&优化 Assignment
- 【2024级】字符串作业 Assignment
- 【2024级】字符&字符数组作业 Assignment
- 【2024级】语法测试 IOI
- 【2024级】函数作业 Assignment
- 【2024级】二维数组作业 Assignment
- 【2024级】循环结构作业 Assignment
- 【2024级】选择结构作业 Assignment
- 【2024级】顺序结构作业 Assignment
- 【2024级】一维数组作业 Assignment
-
Recent Solutions
This person is lazy and didn't wrote any solution
Problem Tags
- 递推
- 1