diff --git a/level1/p01_runningLetter/runningLetter.cpp b/level1/p01_runningLetter/runningLetter.cpp new file mode 100644 index 00000000..f2433cd1 --- /dev/null +++ b/level1/p01_runningLetter/runningLetter.cpp @@ -0,0 +1,54 @@ +#include + +#include + +using namespace std; + +void runningLetter() { + //输入字符或者字母 + string s; + cin >> s; + + int x = 0, len = s.size(); // x为坐标 + + //光标位置 + HANDLE hout; + COORD pos; + hout = GetStdHandle(STD_OUTPUT_HANDLE); + pos.X = 0, pos.Y = 0; //设置Y坐标(不变) + + //控制台窗口信息 + CONSOLE_SCREEN_BUFFER_INFO scr; + + //用于判断是否碰到右侧边界 + bool judge = false; + + DWORD result; + + while (true) { + GetConsoleScreenBufferInfo(hout, &scr); //每次刷新获取窗口大小 + if (x + len >= scr.dwSize.X) + judge = true; + else if (x <= 0) + judge = false; + + pos.X = x; + + SetConsoleCursorPosition(hout, pos); //设置光标位置 + cout << s; + + if (judge == false) + ++x; + else if (judge == true) + --x; + + Sleep(50); //设置刷新速度 + system("cls"); + } + CloseHandle(hout); +} + +int main() { + runningLetter(); + return 0; +} \ No newline at end of file diff --git a/level1/p02_isPrime/isPrime.cpp b/level1/p02_isPrime/isPrime.cpp new file mode 100644 index 00000000..63f90675 --- /dev/null +++ b/level1/p02_isPrime/isPrime.cpp @@ -0,0 +1,15 @@ +#include +#include + +using namespace std; + +bool isPrime(unsigned long long n) { + for (int i = 2; i <= sqrt(n); ++i) + if (n % i == 0) return false; + return true; +} + +int main() { + cout << isPrime(49999); + return 0; +} \ No newline at end of file diff --git a/level1/p03_Diophantus/Diophantus.cpp b/level1/p03_Diophantus/Diophantus.cpp new file mode 100644 index 00000000..a52f7b84 --- /dev/null +++ b/level1/p03_Diophantus/Diophantus.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +double Diophantus() { + double aDad, aSon = 35, temp; + + do { + ++aSon; + aDad = 2 * aSon; + temp = aDad * 33 / 84 + 5 + 4 + aSon; + } while (temp != aDad); + + return aDad - 4; +} + +int main() { + cout << Diophantus(); + return 0; +} \ No newline at end of file diff --git a/level1/p04_ narcissus/narcissus.cpp b/level1/p04_ narcissus/narcissus.cpp new file mode 100644 index 00000000..3ec008d8 --- /dev/null +++ b/level1/p04_ narcissus/narcissus.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +void narcissus() { + int x, y, z, n = 100; + while (n < 1000) { + z = n % 10; + y = n / 10 % 10; + x = n / 100; + if (x * x * x + y * y * y + z * z * z == n) cout << n << ' '; + ++n; + } + cout << endl; +} + +int main() { + narcissus(); + return 0; +} \ No newline at end of file diff --git a/level1/p05_allPrimes/allPrimes.cpp b/level1/p05_allPrimes/allPrimes.cpp new file mode 100644 index 00000000..321e995e --- /dev/null +++ b/level1/p05_allPrimes/allPrimes.cpp @@ -0,0 +1,31 @@ +#include +#include + +using namespace std; + +void allPrimes() { + clock_t startTime = clock(); + + int list[170]; + int i, n, cnt = 0; + + for (n = 2; n < 1001; ++n) { + for (i = 0; i < cnt; ++i) + if (n % list[i] == 0) goto end; + + list[cnt++] = n; + cout << n << ' '; + + end: + continue; + } + + clock_t endTime = clock(); + + cout << "\n计算时间: " << endTime - startTime << endl; +} + +int main() { + allPrimes(); + return 0; +} \ No newline at end of file diff --git a/level1/p06_Goldbach/Goldbach.cpp b/level1/p06_Goldbach/Goldbach.cpp new file mode 100644 index 00000000..8b0d88c1 --- /dev/null +++ b/level1/p06_Goldbach/Goldbach.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +bool Goldbach() { + int prime[25] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}; + bool meet = false; + + for (int n = 4; n <= 100; n += 2) { + meet = false; + for (int i = 0; i < 15; ++i) + for (int j = i; j < 25; ++j) { + if (prime[i] + prime[j] == n) { + meet = true; + goto judge; + } else if (prime[i] + prime[j] > n) + break; + } + + judge: + if (meet == true) + continue; + else + return false; + } + + return true; +} + +int main() { + cout << Goldbach(); + return 0; +} \ No newline at end of file diff --git a/level1/p07_encrypt_decrypt/encrypt_decrypt.cpp b/level1/p07_encrypt_decrypt/encrypt_decrypt.cpp new file mode 100644 index 00000000..6e97aaa0 --- /dev/null +++ b/level1/p07_encrypt_decrypt/encrypt_decrypt.cpp @@ -0,0 +1,86 @@ +#include +#include + +using namespace std; + +using ull = unsigned long long; + +const int MAX_N = 700; //不能太大 + +ull k, d, e, n, fn; + +inline ull gcd(ull a, ull b) { return b == 0 ? a : gcd(b, a % b); } //求最大公约数 + +ull powMod(ull base, ull pow, ull mod) { //幂模运算 + ull res = 1; + + while (pow) { + if (pow & 1) res = (res * base) % mod; + pow >>= 1; + base = (base * base) % mod; + } + return res; +} + +void allPrimes(int *list, int &cnt) { //寻找质数 + int i, n; + + for (n = 2; n < MAX_N; ++n) { + for (i = 0; i < cnt; ++i) + if (n % list[i] == 0) goto end; + list[cnt++] = n; + end: + continue; + } +} + +void RSA_Initialize() { //简易RSA算法加密初始化 + int Primes[MAX_N / 5]; + int cnt = 0; + allPrimes(Primes, cnt); + cnt /= 2; + srand((unsigned)time(NULL)); + int p = Primes[rand() % cnt + cnt], q = Primes[rand() % cnt + cnt]; + n = p * q, fn = (p - 1) * (q - 1); + + e = 7, k = 7; + while (gcd(e, fn) != 1) e += 777; + d = k * fn + 1; + + cout << "公开的: n = " << n << " e = " << e << endl; //公开n, e + cout << "不公开的: d = " << d << endl; +} + +void encrypt(wstring ws, int e, int n) { //加密 + for (int i = 0; i < ws.size(); ++i) { + wcout << powMod(ws[i], e, n) << ' '; //输出密文 + ws[i] = powMod(ws[i], e, n); + } + wcout << endl; +} + +void decrypt(wstring ws, int d, int n) { //解密 + for (int i = 0; i < ws.size(); ++i) { + wcout << powMod(ws[i], d, n) << ' '; //输出明文 + ws[i] = powMod(ws[i], d, n); + } + wcout << endl; +} + +int main() { + wstring wstr = L"bilibili.com/video/BV1FX4y1g7u8"; + + for (int i = 0; i < wstr.size(); ++i) wcout << wstr[i]; + cout << endl; + + // RSA + RSA_Initialize(); + encrypt(wstr, e, n); + cout << endl; + decrypt(wstr, d, n); + + for (int i = 0; i < wstr.size(); ++i) wcout << (wchar_t)wstr[i]; + cout << endl; + + return 0; +} \ No newline at end of file diff --git a/level1/p08_hanoi/hanoi.cpp b/level1/p08_hanoi/hanoi.cpp new file mode 100644 index 00000000..e60a3a86 --- /dev/null +++ b/level1/p08_hanoi/hanoi.cpp @@ -0,0 +1,17 @@ +#include + +using namespace std; + +void hanoi(int n, char A, char B, char C) { //将A塔上n块圆盘通过B塔搬运至C塔上 + // A塔为被搬运塔,B塔为中间塔,C塔为目标塔 + if (n > 0) { + hanoi(n - 1, A, C, B); //把n - 1块圆盘从A塔搬运至B塔 + cout << "将顶层圆盘从塔" << A << "移至塔" << C << endl; + hanoi(n - 1, B, A, C); //把剩下n - 1块圆盘从B塔搬运至C塔 + } +} + +int main() { + hanoi(64, 'A', 'B', 'C'); + return 0; +} \ No newline at end of file diff --git a/level1/p09_maze/maze.cpp b/level1/p09_maze/maze.cpp new file mode 100644 index 00000000..f914d560 --- /dev/null +++ b/level1/p09_maze/maze.cpp @@ -0,0 +1,227 @@ +#include //非标准 +#include + +#include +#include +#include + +using namespace std; + +//方形迷宫最外圈固定为墙,坐标原点为左上角,终点为右下角 +#define MAX_L 50 //迷宫最大边界长度--过大需调整窗口大小 +#define WALL -1 //墙 +#define BLANK 1 //空 +#define up 6 //上 +#define down 7 //下 +#define left 8 //左 +#define right 9 //右 + +class pos { //位置类 + public: + pos(int x = 1, int y = 1, int direction = 0) { X = x, Y = y, D = direction; } + void Up() { --X; } + void Down() { ++X; } + void Left() { --Y; } + void Right() { ++Y; } + int X; + int Y; + int D; +}; + +pos player; //玩家,默认初始化位置为(1,1) +char in; //输入wsad进行上下左右移动 +int maze[MAX_L][MAX_L]; //迷宫 +vector block; //墙 +HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); // 获取标准输出设备句柄 +COORD coord; //坐标 + +void HideCursor() { + CONSOLE_CURSOR_INFO CursorInfo; //光标信息 + GetConsoleCursorInfo(hOut, &CursorInfo); //获取控制台光标信息 + CursorInfo.bVisible = false; //隐藏控制台光标 + SetConsoleCursorInfo(hOut, &CursorInfo); //设置控制台光标状态 + + COORD size = {MAX_L * 2 + 10, size.Y = MAX_L + 20}; + int i = SetConsoleScreenBufferSize(hOut, size); + + SMALL_RECT rc = {0, 0, MAX_L * 2 + 10, MAX_L + 20}; + int j = SetConsoleWindowInfo(hOut, true, &rc); +} + +void FindBlock() { //找出与当前位置相邻的墙 + if (player.Y + 1 < MAX_L - 1 && maze[player.X][player.Y + 1] == WALL) { //向右 + block.push_back(pos(player.X, player.Y + 1, right)); + } + if (player.Y - 1 > 0 && maze[player.X][player.Y - 1] == WALL) { //向左 + block.push_back(pos(player.X, player.Y - 1, left)); + } + if (player.X - 1 > 0 && maze[player.X - 1][player.Y] == WALL) { //向上 + block.push_back(pos(player.X - 1, player.Y, up)); + } + if (player.X + 1 < MAX_L - 1 && maze[player.X + 1][player.Y] == WALL) { //向下 + block.push_back(pos(player.X + 1, player.Y, down)); + } +} + +void MazeInitialize() { //初始化生成迷宫 + maze[player.X][player.Y] = BLANK; //设置起点 + FindBlock(); //压入起点下和右的墙 + pos here; //聚焦点 + + //生成迷宫 + while (!block.empty()) { + int rPos = rand() % block.size(); + here = block[rPos]; + player.X = here.X, player.Y = here.Y; //玩家从起点飘到当前位置 + + switch (here.D) { + case up: { + player.Up(); + break; + } + case down: { + if (player.X + 1 < MAX_L - 1) player.Down(); + break; + } + case left: { + player.Left(); + break; + } + case right: { + if (player.Y + 1 < MAX_L - 1) player.Right(); + break; + } + } + if (maze[player.X][player.Y] == WALL) { //当前墙两边块均为墙 + maze[here.X][here.Y] = BLANK; //当前墙置空 + maze[player.X][player.Y] = BLANK; //目标块墙置空 + FindBlock(); + } else { //目标块可以到达 + } + block.erase(block.begin() + rPos); //删除当前墙 + } + //打印迷宫 + for (int i = 0; i < MAX_L; ++i) { + for (int j = 0; j < MAX_L; ++j) { + if (maze[i][j] == WALL) + cout << "■"; + else if (maze[i][j] == BLANK) + cout << " "; + } + cout << endl; + } + //设置终点 + coord.X = (MAX_L - 2) * 2, coord.Y = MAX_L - 2; + SetConsoleCursorPosition(hOut, coord); + cout << "★"; + + //打印玩家 + player.X = 1, player.Y = 1; + coord.X = player.Y * 2, coord.Y = player.X; + SetConsoleCursorPosition(hOut, coord); + cout << "♀"; + SetConsoleCursorPosition(hOut, coord); +} + +void Move() { //控制移动 + clock_t sTime = clock(); + + while (in = getch()) { + switch (in) { + case 'w': + case 'W': { + if (maze[player.X - 1][player.Y] == BLANK) + player.Up(); + else + goto end; + + break; + } + case 's': + case 'S': { + if (maze[player.X + 1][player.Y] == BLANK) + player.Down(); + else + goto end; + + break; + } + case 'a': + case 'A': { + if (maze[player.X][player.Y - 1] == BLANK) + player.Left(); + else + goto end; + + break; + } + case 'd': + case 'D': { + if (maze[player.X][player.Y + 1] == BLANK) + player.Right(); + else + goto end; + break; + } + default: { //无效输入 + break; + } + } + if (player.Y == MAX_L - 2 && player.X == MAX_L - 2) { //判断胜利 + clock_t eTime = clock(); + + coord.Y = MAX_L, coord.X = 0; + SetConsoleCursorPosition(hOut, coord); //光标移动至玩家位置 + cout << "游戏胜利!!!" << endl; + cout << "用时: " << (eTime - sTime) / 1000.0 << "秒" << endl; + return; + } + cout << " "; + coord.X = player.Y * 2, coord.Y = player.X; + SetConsoleCursorPosition(hOut, coord); //光标移动至玩家位置 + cout << "♀"; + SetConsoleCursorPosition(hOut, coord); + Sleep(10); + end: + continue; + } +} + +bool OneMore() { + cout << "要再来一局吗???" << endl; + cout << "是: 1 否: 0, 请输入数字" << endl; + while (in = getch()) { + if (in == '1') { + system("cls"); + player.X = 1, player.Y = 1; //设置玩家初始位置 + return true; + } else if (in == '0') { + system("cls"); + cout << "感谢游玩" << endl; + cout << "再次按0退出程序" << endl; + while (in = getch()) + if (in == '0') exit(0); + } else { + continue; + } + } +} + +void Maze() { //迷宫游戏 +loop: + memset(maze, WALL, sizeof(maze)); //填充墙 + MazeInitialize(); //迷宫出现 + Move(); //游戏中 + + //胜利 + if (OneMore()) goto loop; + //游戏结束 + CloseHandle(hOut); // 关闭标准输出设备句柄 +} + +int main() { + srand((unsigned)time(NULL)); //随机数种子 + HideCursor(); + Maze(); + return 0; +} \ No newline at end of file diff --git a/level1/p10_pushBoxes/pushBoxes.cpp b/level1/p10_pushBoxes/pushBoxes.cpp new file mode 100644 index 00000000..7f6aaf12 --- /dev/null +++ b/level1/p10_pushBoxes/pushBoxes.cpp @@ -0,0 +1,645 @@ +#include +#include + +#include +#include +#include +#include + +using namespace std; + +//字符为空则更换字体。建议使用黑体 +//人--"♀" +#define WALL '1' //墙--"■" +#define BLANK '0' //空--" " +#define BOX '2' //箱子--"※" +#define GOAL '3' //目标点--"□" +#define REACH '4' //箱子推到目标点--"★" +#define up 6 //上 +#define down 7 //下 +#define left 8 //左 +#define right 9 //右 + +class pos { //位置类 + public: + pos(int x = 1, int y = 1, int direction = 0) { + X = x, Y = y; + D = direction; + } + void Up() { --X; } + void Down() { ++X; } + void Left() { --Y; } + void Right() { ++Y; } + int X; + int Y; + int D; +}; + +class Stage { //关卡类 + public: + Stage(vector theMap, pos thePlayer, int theNum = 0, int theStart = 0) { + map = theMap; + player = thePlayer; + num = theNum; + start = theStart; + } + vector map; //关卡地图 + pos player; //玩家起始位置 + int num; //关卡箱子数量 + int start; //关卡起始箱子数量 +}; + +class step { //步骤类 + public: + step(int theD = 0, char thePush = BLANK) { + D = theD; + push = thePush; + } + int D; //方向 + char push; //移动类型 +}; + +vector stages; //所有关卡 +vector scores; //所有关卡所得分数 +int now = 0; //当前关卡 +int num = 0; //关卡数量 +vector zero; //隐藏关卡地图 +pos playerZero; //隐藏关卡玩家起点坐标 +HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); // 获取标准输出设备句柄 +COORD coord; //坐标 + +void HideCursor() { //隐藏光标 + CONSOLE_CURSOR_INFO CursorInfo; //光标信息 + GetConsoleCursorInfo(hOut, &CursorInfo); //获取控制台光标信息 + CursorInfo.bVisible = false; //隐藏控制台光标 + SetConsoleCursorInfo(hOut, &CursorInfo); //设置控制台光标状态 +} + +bool Welcome() { //主菜单 + cout << "推箱子游戏\n建议调大字体,调整合适窗口大小,使用黑体再进行游戏\n" << endl; + cout << "请输入关卡 输入范围: 1 ~ " << num << "\t(输入'0'退出程序)" << endl; + cin >> now; + if (now == 0) return true; + while (now < 1 || now > num) { + cout << "输入为" << now << ",请重新输入" << endl; + cin >> now; + } + system("cls"); + return false; +} + +void StageInitialize() { //文件中读取关卡地图与分数 + stages.push_back(Stage(zero, playerZero)); //关卡0--不可访问 + scores.push_back(0); //关卡0分数--不可访问 + + //从文件中获取关卡地图 + ifstream infile; + infile.open("stages.dat"); + + string line; + vector mapN; + pos playerN; + + while (!infile.eof()) { + getline(infile, line); + if (line[0] == '#') { //读取了一关 + playerN.X = line[1] - '0', playerN.Y = line[2] - '0'; + stages.push_back(Stage(mapN, playerN, line[3] - '0', line[4] - '0')); + ++num; //关卡数量加一 + mapN.clear(); //临时地图清空 + continue; + } + mapN.push_back(line); + } + infile.close(); + + //从文件中获取关卡分数 + infile.open("scores.dat"); + + int score; + int numS = 0; + + while (!infile.eof()) { + infile >> score; + ++numS; + scores.push_back(score); + } + infile.close(); + + //判断关卡地图数量和关卡分数数量是否一致 + if (num != numS) { + cerr << "文件错误,关卡地图数量与关卡分数数量不一致,程序即将退出" << endl; + system("pause"); + exit(-1); + } +} + +void ShowStage() { //打印当前关卡游戏界面 + //打印地图 + Stage stageN = stages[now]; + for (int i = 0; i < stageN.map.size(); ++i) { + for (int j = 0; j < stageN.map[i].size(); ++j) { + if (stageN.map[i][j] == '0') + cout << " "; + else if (stageN.map[i][j] == '1') + cout << "■"; + else if (stageN.map[i][j] == '2') + cout << "※"; + else if (stageN.map[i][j] == '3') + cout << "□"; + else if (stageN.map[i][j] == '4') + cout << "★"; + } + cout << endl; + } + //打印信息 + cout << "关卡:" << now << " \t当前分数:0" + << "\t\t历史最低分:" << scores[now] << endl; + cout << "WADS--上左右下移动 R--重开 Z--回退 Q--返回主菜单" << endl; + //打印人物 + coord.X = stageN.player.Y * 2, coord.Y = stageN.player.X; + SetConsoleCursorPosition(hOut, coord); + cout << "♀"; + SetConsoleCursorPosition(hOut, coord); +} + +bool Move() { //移动等功能实现 + char in, inin; + Stage stageN = stages[now]; + int x = 0, y = 0, score = 0, satisfy = stageN.start; + stack steps; //操作步骤 + + //控制逻辑 + while (in = getch()) { + x = stageN.player.X, y = stageN.player.Y; + switch (in) { + case 'w': + case 'W': { + if (stageN.map[x - 1][y] == BLANK || stageN.map[x - 1][y] == GOAL) { //空 + stageN.player.Up(); + inin = BLANK; + steps.push(step(up, BLANK)); + } else if (stageN.map[x - 1][y] == REACH) { //箱子推出目标点 + if (stageN.map[x - 2][y] == BLANK) { //箱子能移动 + stageN.map[x - 2][y] = BOX; + stageN.map[x - 1][y] = GOAL; + stageN.player.Up(); + inin = BOX; + stageN.player.D = up; //推箱子方向 + --satisfy; //到达目标点箱子减一 + steps.push(step(up, BOX)); + } else if (stageN.map[x - 2][y] == GOAL) { //箱子推到终点 + stageN.map[x - 2][y] = REACH; + stageN.map[x - 1][y] = GOAL; + stageN.player.Up(); + inin = REACH; + stageN.player.D = up; //推箱子方向 + steps.push(step(up, REACH)); + } else + goto end; //箱子无法推动 + } else if (stageN.map[x - 1][y] == BOX) { //箱子 + if (stageN.map[x - 2][y] == BLANK) { //箱子能移动 + stageN.map[x - 2][y] = BOX; + stageN.map[x - 1][y] = BLANK; + stageN.player.Up(); + inin = BOX; + stageN.player.D = up; //推箱子方向 + steps.push(step(up, BOX)); + } else if (stageN.map[x - 2][y] == GOAL) { //箱子推到终点 + stageN.map[x - 2][y] = REACH; + stageN.map[x - 1][y] = BLANK; + stageN.player.Up(); + inin = REACH; + stageN.player.D = up; //推箱子方向 + ++satisfy; //到达目标点箱子加一 + steps.push(step(up, REACH)); + } else + goto end; //箱子无法推动 + } else { //墙 + goto end; + } + break; + } + case 's': + case 'S': { + if (stageN.map[x + 1][y] == BLANK || stageN.map[x + 1][y] == GOAL) { //空 + stageN.player.Down(); + inin = BLANK; + steps.push(step(down, BLANK)); + } else if (stageN.map[x + 1][y] == REACH) { //箱子推出目标点 + if (stageN.map[x + 2][y] == BLANK) { //箱子能移动 + stageN.map[x + 2][y] = BOX; + stageN.map[x + 1][y] = GOAL; + stageN.player.Down(); + inin = BOX; + stageN.player.D = down; //推箱子方向 + --satisfy; //到达目标点箱子减一 + steps.push(step(down, BOX)); + } else if (stageN.map[x + 2][y] == GOAL) { //箱子推到终点 + stageN.map[x + 2][y] = REACH; + stageN.map[x + 1][y] = GOAL; + stageN.player.Down(); + inin = REACH; + stageN.player.D = down; //推箱子方向 + steps.push(step(down, REACH)); + } else + goto end; //箱子无法推动 + } else if (stageN.map[x + 1][y] == BOX) { //箱子 + if (stageN.map[x + 2][y] == BLANK) { //箱子能移动 + stageN.map[x + 2][y] = BOX; + stageN.map[x + 1][y] = BLANK; + stageN.player.Down(); + inin = BOX; + stageN.player.D = down; //推箱子方向 + steps.push(step(down, BOX)); + } else if (stageN.map[x + 2][y] == GOAL) { //箱子推到终点 + stageN.map[x + 2][y] = REACH; + stageN.map[x + 1][y] = BLANK; + stageN.player.Down(); + inin = REACH; + stageN.player.D = down; //推箱子方向 + ++satisfy; //到达目标点箱子加一 + steps.push(step(down, REACH)); + } else + goto end; //箱子无法推动 + + } else { //墙 + goto end; + } + break; + } + case 'a': + case 'A': { + if (stageN.map[x][y - 1] == BLANK || stageN.map[x][y - 1] == GOAL) { //空 + stageN.player.Left(); + inin = BLANK; + steps.push(step(left, BLANK)); + } else if (stageN.map[x][y - 1] == REACH) { //箱子推出目标点 + if (stageN.map[x][y - 2] == BLANK) { //箱子能移动 + stageN.map[x][y - 2] = BOX; + stageN.map[x][y - 1] = GOAL; + stageN.player.Left(); + inin = BOX; + stageN.player.D = left; //推箱子方向 + --satisfy; //到达目标点箱子减一 + steps.push(step(left, BOX)); + } else if (stageN.map[x][y - 2] == GOAL) { //箱子推到终点 + stageN.map[x][y - 2] = REACH; + stageN.map[x][y - 1] = GOAL; + stageN.player.Left(); + inin = REACH; + stageN.player.D = left; //推箱子方向 + steps.push(step(left, REACH)); + } else + goto end; //箱子无法推动 + } else if (stageN.map[x][y - 1] == BOX) { //箱子 + if (stageN.map[x][y - 2] == BLANK) { //箱子能移动 + stageN.map[x][y - 2] = BOX; + stageN.map[x][y - 1] = BLANK; + stageN.player.Left(); + inin = BOX; + stageN.player.D = left; //推箱子方向 + steps.push(step(left, BOX)); + } else if (stageN.map[x][y - 2] == GOAL) { //箱子推到终点 + stageN.map[x][y - 2] = REACH; + stageN.map[x][y - 1] = BLANK; + stageN.player.Left(); + inin = REACH; + stageN.player.D = left; //推箱子方向 + ++satisfy; //到达目标点箱子加一 + steps.push(step(left, REACH)); + } else + goto end; //箱子无法推动 + + } else { //墙 + goto end; + } + break; + } + case 'd': + case 'D': { + if (stageN.map[x][y + 1] == BLANK || stageN.map[x][y + 1] == GOAL) { //空 + stageN.player.Right(); + inin = BLANK; + steps.push(step(right, BLANK)); + } else if (stageN.map[x][y + 1] == REACH) { //箱子推出目标点 + if (stageN.map[x][y + 2] == BLANK) { //箱子能移动 + stageN.map[x][y + 2] = BOX; + stageN.map[x][y + 1] = GOAL; + stageN.player.Right(); + inin = BOX; + stageN.player.D = right; //推箱子方向 + --satisfy; //到达目标点箱子减一 + steps.push(step(right, BOX)); + } else if (stageN.map[x][y + 2] == GOAL) { //箱子推到终点 + stageN.map[x][y + 2] = REACH; + stageN.map[x][y + 1] = GOAL; + stageN.player.Right(); + inin = REACH; + stageN.player.D = right; //推箱子方向 + steps.push(step(right, REACH)); + } else + goto end; //箱子无法推动 + } else if (stageN.map[x][y + 1] == BOX) { //箱子 + if (stageN.map[x][y + 2] == BLANK) { //箱子能移动 + stageN.map[x][y + 2] = BOX; + stageN.map[x][y + 1] = BLANK; + stageN.player.Right(); + inin = BOX; + stageN.player.D = right; //推箱子方向 + steps.push(step(right, BOX)); + } else if (stageN.map[x][y + 2] == GOAL) { //箱子推到终点 + stageN.map[x][y + 2] = REACH; + stageN.map[x][y + 1] = BLANK; + stageN.player.Right(); + inin = REACH; + stageN.player.D = right; //推箱子方向 + ++satisfy; //到达目标点箱子加一 + steps.push(step(right, REACH)); + } else + goto end; //箱子无法推动 + + } else { //墙 + goto end; + } + break; + } + case 'r': + case 'R': { //重新开始该关卡 + coord.X = 0, coord.Y = stageN.map.size() + 2; + SetConsoleCursorPosition(hOut, coord); + char restart = 2; + cout << "是否重新开始本关卡 是--1 否--0"; + while (restart = getch()) { + if (restart == '1') { //继续下一关 + system("cls"); + return true; + } else if (restart == '0') { //不进行操作 + SetConsoleCursorPosition(hOut, coord); + cout << " "; + coord.X = stageN.player.Y * 2, coord.Y = stageN.player.X; + SetConsoleCursorPosition(hOut, coord); + goto end; + } + } + break; + } + case 'z': + case 'Z': { //撤回操作 + if (!steps.empty()) { //回到上一步 + switch (steps.top().D) { + case up: { //向下 + inin = BLANK; + if (steps.top().push == BOX) { + inin = WALL; + stageN.player.D = up; + stageN.map[x - 1][y] = BLANK; + if (stageN.map[x][y] == BLANK) + stageN.map[x][y] = BOX; + else { + stageN.map[x][y] = REACH; + ++satisfy; + } + } else if (steps.top().push == REACH) { + --satisfy; + inin = GOAL; + stageN.player.D = up; + stageN.map[x - 1][y] = GOAL; + stageN.map[x][y] = BOX; + } + stageN.player.Down(); + break; + } + case down: { //向上 + inin = BLANK; + if (steps.top().push == BOX) { + inin = WALL; + stageN.player.D = down; + stageN.map[x + 1][y] = BLANK; + if (stageN.map[x][y] == BLANK) + stageN.map[x][y] = BOX; + else { + stageN.map[x][y] = REACH; + ++satisfy; + } + } else if (steps.top().push == REACH) { + --satisfy; + inin = GOAL; + stageN.player.D = down; + stageN.map[x + 1][y] = GOAL; + stageN.map[x][y] = BOX; + } + stageN.player.Up(); + break; + } + case left: { //向右 + inin = BLANK; + if (steps.top().push == BOX) { + inin = WALL; + stageN.player.D = left; + stageN.map[x][y - 1] = BLANK; + if (stageN.map[x][y] == BLANK) + stageN.map[x][y] = BOX; + else { + stageN.map[x][y] = REACH; + ++satisfy; + } + } else if (steps.top().push == REACH) { + --satisfy; + inin = GOAL; + stageN.player.D = left; + stageN.map[x][y - 1] = GOAL; + stageN.map[x][y] = BOX; + } + stageN.player.Right(); + break; + } + case right: { //向左 + inin = BLANK; + if (steps.top().push == BOX) { + inin = WALL; + stageN.player.D = right; + stageN.map[x][y + 1] = BLANK; + if (stageN.map[x][y] == BLANK) + stageN.map[x][y] = BOX; + else { + stageN.map[x][y] = REACH; + ++satisfy; + } + } else if (steps.top().push == REACH) { + --satisfy; + inin = GOAL; + stageN.player.D = right; + stageN.map[x][y + 1] = GOAL; + stageN.map[x][y] = BOX; + } + stageN.player.Left(); + break; + } + } + steps.pop(); + score -= 2; + } else { //步骤栈为空,无响应 + goto end; + } + break; + } + case 'q': + case 'Q': { + coord.X = 0, coord.Y = stageN.map.size() + 2; + SetConsoleCursorPosition(hOut, coord); + char restart = 2; + cout << "是否返回主菜单 是--1 否--0"; + while (restart = getch()) { + if (restart == '1') { //继续下一关 + system("cls"); + return false; + } else if (restart == '0') { //不进行操作 + SetConsoleCursorPosition(hOut, coord); + cout << " "; + coord.X = stageN.player.Y * 2, coord.Y = stageN.player.X; + SetConsoleCursorPosition(hOut, coord); + goto end; + } + } + break; + } + default: { //无效输入 + goto end; + } + } + + //移动一步加一分 + ++score; + //绘制输出画面 + if (stageN.map[x][y] == BLANK) + cout << " "; //玩家移动前位置置空 + else if (stageN.map[x][y] == GOAL) + cout << "□"; //玩家移动前位置变回目标点 + else if (stageN.map[x][y] == REACH) + cout << "★"; + else if (stageN.map[x][y] == BOX) + cout << "※"; + + coord.X = stageN.player.Y * 2, coord.Y = stageN.player.X; + SetConsoleCursorPosition(hOut, coord); //光标移动至玩家位置 + cout << "♀"; + + if (inin == BOX || inin == REACH || inin == GOAL || inin == WALL) { + switch (stageN.player.D) { //判断箱子方向 + case up: { + if (inin == GOAL || inin == WALL) + coord.Y -= 2; + else + coord.Y -= 1; + break; + } + case down: { + if (inin == GOAL || inin == WALL) + coord.Y += 2; + else + coord.Y += 1; + break; + } + case left: { + if (inin == GOAL || inin == WALL) + coord.X -= 4; + else + coord.X -= 2; + break; + } + case right: { + if (inin == GOAL || inin == WALL) + coord.X += 4; + else + coord.X += 2; + break; + } + } + SetConsoleCursorPosition(hOut, coord); + + if (inin == BOX) + cout << "※"; + else if (inin == REACH) + cout << "★"; + else if (inin == GOAL) + cout << "□"; + else + cout << " "; + } + + //打印分数 + coord.X = 26, coord.Y = stageN.map.size(); + Sleep(30); //时间过小图像打印会出现问题 + SetConsoleCursorPosition(hOut, coord); + cout << score << ' '; + + //光标回到人物位置 + coord.X = stageN.player.Y * 2, coord.Y = stageN.player.X; + SetConsoleCursorPosition(hOut, coord); + + if (satisfy == stageN.num) { //所有箱子推到目标点 + if (score < scores[now]) scores[now] = score; //更新历史最好成绩 + Sleep(300); + system("cls"); + cout << "第" << now << "关通过!" + << " 分数为:" << score << " 历史最低分为:" << scores[now] << endl; + + if (now < num) { //询问是否进行下一关 + cout << "是否进行下一关 是--1 否--0" << endl; + char choose = 2; + while (choose = getch()) { + if (choose == '1') { //继续下一关 + system("cls"); + ++now; + return true; + } else if (choose == '0') { //返回主菜单 + return false; + } + } + } else { //全部通过 + cout << "恭喜你通过了全部关卡!!!" << endl; + system("pause"); + system("cls"); + return false; + } + } + end: + continue; + } + throw exception(); + return false; +} + +void UpdateScores() { //分数保存至文件中 + ofstream outfile; + outfile.open("scores.dat"); + vector::const_iterator it = scores.begin() + 1; + while (it != scores.end() - 1) { + outfile << *it << endl; + ++it; + } + outfile << *it; + outfile.close(); +} + +void pushBoxes() { //推箱子游戏 + StageInitialize(); +wel: + if (Welcome()) goto end; +show: + ShowStage(); + if (Move()) + goto show; + else + goto wel; +end: + UpdateScores(); +} + +int main() { + HideCursor(); + pushBoxes(); + CloseHandle(hOut); + return 0; +} \ No newline at end of file diff --git a/level1/p10_pushBoxes/scores.dat b/level1/p10_pushBoxes/scores.dat new file mode 100644 index 00000000..0f1589e3 --- /dev/null +++ b/level1/p10_pushBoxes/scores.dat @@ -0,0 +1,21 @@ +9999 +9999 +9999 +9999 +9999 +9999 +9999 +9999 +9999 +9999 +9999 +9999 +9999 +9999 +9999 +9999 +9999 +9999 +9999 +9999 +9999 \ No newline at end of file diff --git a/level1/p10_pushBoxes/stages.dat b/level1/p10_pushBoxes/stages.dat new file mode 100644 index 00000000..41ca6bd0 --- /dev/null +++ b/level1/p10_pushBoxes/stages.dat @@ -0,0 +1,158 @@ +0111111 +11000011 +10330001 +10102221 +10001031 +11111111 +#4530 +1111110 +1304011 +1200201 +1000101 +1003001 +1111111 +#1231 +111111 +100331 +1012011 +1010001 +1002101 +1004001 +1111111 +#1131 +11111 +10001 +1010111 +1432001 +1001201 +1000031 +1111111 +#5131 +0011111 +1110001 +1304101 +1000201 +1023011 +111111 +#1531 +1111111 +10000011 +103240011 +101230001 +100000001 +111111111 +#1131 +111111 +100301 +1013011 +1003001 +1022201 +1001001 +1111111 +#5530 +1111 +1001111 +1000431 +1002101 +1003201 +1111111 +#4531 +1111111 +10030011 +10221301 +10101001 +10031001 +11111111 +#3330 +111111 +1000011 +1022201 +1103331 +1000101 +1000001 +1111111 +#1130 +1111 +1001 +10011 +100011 +1202011 +1301201 +1003031 +1111111 +#4430 +111111 +100031 +122201 +130101 +100301 +111111 +#2430 +1111111 +10000011 +10101201 +13024001 +10001031 +11111111 +#4131 +111111 +1000011 +1001401 +1000201 +1002331 +1111111 +#2131 +00011111 +11110301 +10020001 +10004401 +10000111 +111111 +#4232 +01111110 +11003011 +10002201 +10130201 +10031001 +11111111 +#2630 +1111111 +1003001 +10201011 +13120201 +10031001 +10001001 +11111111 +#4530 +11111 +1300111 +1010201 +1004001 +1001101 +1040001 +1111111 +#5532 +1111111 +10000011 +10101001 +13222301 +10001301 +11111111 +#2330 +1111 +1031111 +1220001 +1320101 +1003001 +1111111 +#1130 +111111111 +100010001 +100020001 +110121011 +11010301 +13343421 +10021001 +11100001 +00111111 +#546 \ No newline at end of file diff --git a/level1/p11_linkedList/illegalParameter.h b/level1/p11_linkedList/illegalParameter.h new file mode 100644 index 00000000..3f7e3884 --- /dev/null +++ b/level1/p11_linkedList/illegalParameter.h @@ -0,0 +1,14 @@ +#ifndef illegalParameter_ +#define illegalParameter_ +#include + +class illegalParameter { + public: + illegalParameter() : message("Illegal parameter value") {} + illegalParameter(std::string theMessage) { message = theMessage; } + void outputMessage() { std::cout << message << std::endl; } + + private: + std::string message; +}; +#endif \ No newline at end of file diff --git a/level1/p11_linkedList/linearList.h b/level1/p11_linkedList/linearList.h new file mode 100644 index 00000000..ead45da6 --- /dev/null +++ b/level1/p11_linkedList/linearList.h @@ -0,0 +1,20 @@ +#ifndef linearList_ +#define linearList_ +#include + +template +class linearList { // ADT 线性表 + public: + virtual ~linearList(){}; + virtual bool empty() const = 0; + virtual int size() const = 0; + virtual T& get(int theIndex) const = 0; + virtual int indexOf(const T& theElement, int theIndex) const = 0; + virtual void erase(int theIndex) = 0; + virtual void insert(int theIndex, const T& theElement) = 0; + virtual void output(std::ostream& out) const = 0; + virtual void clear() = 0; + virtual void push_back(const T& theElement) = 0; + virtual void pop_back() = 0; +}; +#endif \ No newline at end of file diff --git a/level1/p11_linkedList/linkedList.cpp b/level1/p11_linkedList/linkedList.cpp new file mode 100644 index 00000000..720c50dc --- /dev/null +++ b/level1/p11_linkedList/linkedList.cpp @@ -0,0 +1,283 @@ +#include + +#include "illegalParameter.h" +#include "linearList.h" +#include "linkedNode.h" + +template +class linkedList : public linearList { //链表 + public: + linkedList(); + linkedList(const linkedList&); + ~linkedList(); + + bool empty() const { return listSize == 0; } + int size() const { return listSize; }; + T& get(int theIndex) const; + int indexOf(const T& theElement, int theIndex = -1) const; + void erase(int theIndex); + void insert(int theIndex, const T& theElement); + void output(std::ostream& out) const; + void clear(); + void push_back(const T& theElement); + void pop_back(); + + void set(int theIndex, const T& theElement); + void resize(int size, const T& theElement); + void reverse(); + + protected: + linkedNode* firstNode; + linkedNode* lastNode; + int listSize; +}; + +template +linkedList::linkedList() { + firstNode = nullptr; + lastNode = nullptr; + listSize = 0; +} + +template +linkedList::linkedList(const linkedList& theList) { + listSize = theList.listSize; + if (listSize == 0) { + linkedList(); + return; + } + linkedNode* sourceNode = theList.firstNode; + firstNode = new linkedNode(sourceNode->element); + linkedNode* currentNode = firstNode; + sourceNode = sourceNode->next; + + while (sourceNode != nullptr) { + currentNode->next = new linkedNode(sourceNode->element); + currentNode = currentNode->next; + sourceNode = sourceNode->next; + } + lastNode = currentNode; + lastNode->next = nullptr; +} + +template +linkedList::~linkedList() { + linkedNode* tempNode; + while (firstNode != nullptr) { + tempNode = firstNode->next; + delete firstNode; + firstNode = tempNode; + } +} + +template +T& linkedList::get(int theIndex) const { + if (theIndex >= listSize || theIndex < 0) { + std::ostringstream s; + s << "获取失败 链表大小为" << listSize << " 输入Index为" << theIndex << std::endl; + throw illegalParameter(s.str()); + } + linkedNode* currentNode = firstNode; + for (int i = 0; i < theIndex; ++i) { + currentNode = currentNode->next; + } + return currentNode->element; +} + +template +int linkedList::indexOf(const T& theElement, int theIndex) const { + if (theIndex >= listSize - 1 || theIndex < -1) { + return -1; + } + + linkedNode* currentNode = firstNode; + int index = theIndex + 1; + for (int i = -1; i < theIndex; ++i) { + currentNode = currentNode->next; + } + while (currentNode != nullptr && currentNode->element != theElement) { + ++index; + currentNode = currentNode->next; + } + if (currentNode == nullptr) { + return -1; + } + return index; +} + +template +void linkedList::erase(int theIndex) { + if (theIndex >= listSize || theIndex < 0) { + std::ostringstream s; + s << "删除失败 链表大小为" << listSize << " 输入Index为" << theIndex << std::endl; + throw illegalParameter(s.str()); + } + linkedNode* deleteNode; + if (theIndex == 0) { + deleteNode = firstNode; + firstNode = firstNode->next; + } else { + linkedNode* preNode = firstNode; + for (int i = 1; i < theIndex; ++i) { + preNode = preNode->next; + } + deleteNode = preNode->next; + preNode->next = deleteNode->next; + if (deleteNode == lastNode) lastNode = preNode; + } + delete deleteNode; + --listSize; +} + +template +void linkedList::insert(int theIndex, const T& theElement) { + if (theIndex > listSize || theIndex < 0) { + std::ostringstream s; + s << "插入失败 链表大小为" << listSize << " 输入Index为" << theIndex << std::endl; + throw illegalParameter(s.str()); + } + if (theIndex == 0) { + firstNode = new linkedNode(theElement, firstNode); + if (listSize == 0) { + lastNode = firstNode; + } + } else { + linkedNode* preNode = firstNode; + for (int i = 1; i < theIndex; ++i) { + preNode = preNode->next; + } + preNode->next = new linkedNode(theElement, preNode->next); + if (listSize == theIndex) { + lastNode = preNode->next; + } + } + ++listSize; +} + +template +void linkedList::output(std::ostream& out) const { + for (linkedNode* currentNode = firstNode; currentNode != nullptr; currentNode = currentNode->next) { + out << currentNode->element << ' '; + } +} + +template +void linkedList::clear() { + linkedNode* tempNode = firstNode; + while (firstNode != nullptr) { + tempNode = firstNode->next; + delete firstNode; + firstNode = tempNode; + } + listSize = 0; +} + +template +void linkedList::push_back(const T& theElement) { + linkedNode* newNode = new linkedNode(theElement, nullptr); + if (listSize == 0) { + firstNode = newNode; + lastNode = newNode; + } else { + lastNode->next = newNode; + lastNode = newNode; + } + ++listSize; +} + +template +void linkedList::pop_back() { + if (listSize == 0) { + std::cerr << "链表为空" << std::endl; + return; + } else if (listSize == 1) { + delete firstNode; + firstNode = nullptr; + lastNode = nullptr; + } else { + linkedNode* preNode = firstNode; + for (int i = 2; i < listSize; ++i) { + preNode = preNode->next; + } + delete lastNode; + preNode->next = nullptr; + lastNode = preNode; + } + --listSize; +} + +template +std::ostream& operator<<(std::ostream& out, const linkedList& l) { + l.output(out); + return out; +} + +template +void linkedList::set(int theIndex, const T& theElement) { + if (theIndex >= listSize || theIndex < 0) { + std::cerr << "更改失败 链表大小为" << listSize << " 输入Index为" << theIndex << std::endl; + return; + } + linkedNode* currentNode = firstNode; + for (int i = 0; i < theIndex; ++i) { + currentNode = currentNode->next; + } + currentNode->element = theElement; +} + +template +void linkedList::resize(int size, const T& theElement) { + if (size < 0) { + std::ostringstream s; + s << "失败 大小为" << listSize << " 输入Size为" << size << std::endl; + throw illegalParameter(s.str()); + } else if (size < listSize) { + while (size < listSize) { + pop_back(); + } + } else if (size > listSize) { + while (size > listSize) { + push_back(theElement); + } + } else { + return; + } +} + +template +void linkedList::reverse() { + lastNode = firstNode; + + linkedNode* tempNode = nullptr; + linkedNode* nextNode = nullptr; + + while (firstNode != nullptr) { + nextNode = firstNode->next; + firstNode->next = tempNode; + tempNode = firstNode; + firstNode = nextNode; + } + firstNode = tempNode; +} + +int main() { + linkedList l; + for (int i = 1; i <= 10; ++i) l.push_back(i); + l.set(9, 5); + + linkedList list(l); + + std::cout << list << std::endl; + list.reverse(); + std::cout << list << std::endl; + + int i1 = list.indexOf(5); + int i2 = list.indexOf(5, i1); + int i3 = list.indexOf(5, i2); + std::cout << "节点序号从0开始" << std::endl; + std::cout << "数字5第一次出现节点序号:" << i1 << std::endl; + std::cout << "数字5第一次出现节点序号:" << i2 << std::endl; + std::cout << "数字5第一次出现节点序号:" << i3 << std::endl; + + return 0; +} \ No newline at end of file diff --git a/level1/p11_linkedList/linkedNode.h b/level1/p11_linkedList/linkedNode.h new file mode 100644 index 00000000..c46a1911 --- /dev/null +++ b/level1/p11_linkedList/linkedNode.h @@ -0,0 +1,16 @@ +#ifndef linkedNode_ +#define linkedNode_ + +template +class linkedNode { //链表节点 + public: + linkedNode() {} + linkedNode(T theElement) { element = theElement; } + linkedNode(T theElement, linkedNode* theNext) { + element = theElement; + next = theNext; + } + T element; + linkedNode* next; +}; +#endif \ No newline at end of file diff --git a/level1/p12_warehouse/goods.dat b/level1/p12_warehouse/goods.dat new file mode 100644 index 00000000..df1caca9 --- /dev/null +++ b/level1/p12_warehouse/goods.dat @@ -0,0 +1,21 @@ +apple H 132 +apricot I 3833 +banana C 14 +blueberry B 532 +cherry J 645 +cranberry C 76 +date V 51 +grape F 423 +grapefruit C 765 +kiwi_fruit C 111 +lemon C 95 +lime E 652 +lychee E 414 +mango A 141 +orange G 511 +peach D 255 +pear C 866 +pineapple D 84 +plum D 72 +strawberry A 978 +watermelon B 83 diff --git a/level1/p12_warehouse/warehouse.cpp b/level1/p12_warehouse/warehouse.cpp new file mode 100644 index 00000000..d9070f29 --- /dev/null +++ b/level1/p12_warehouse/warehouse.cpp @@ -0,0 +1,240 @@ +#include + +#include +#include +#include +#include +#include +#include + +using namespace std; + +class Goods { //货物类 + public: + Goods(string theMod = NULL, int theNum = 0) { mod = theMod, num = theNum; } + string mod; + int num; +}; + +map data; //存放货物数据 +bool alter = false; //是否更改文件 + +void Infile() { // 以读模式打开文件 + ifstream infile; + infile.open("goods.dat"); + + string line, name, mod; + int num; + + while (infile.good()) { + getline(infile, line); + istringstream iss(line); + iss >> name >> mod >> num; + data.insert(make_pair(name, Goods(mod, num))); + } + infile.close(); +} + +void Outfile() { //数据写入文件中 + ofstream outfile; + outfile.open("goods.dat"); + map::const_iterator it = data.begin(); + while (it != data.end()) { + outfile << it->first + ' ' << it->second.mod + ' ' << it->second.num << endl; + ++it; + } + outfile.close(); +} + +void ShowStock() { //显示存货列表 + cout << "存货列表" << endl; + map::const_iterator it = data.begin(); + while (it != data.end()) { + cout << "Name: " << it->first << setw(21) << "\tMod: " << it->second.mod << setw(21) + << "\tNumber: " << it->second.num << endl; + ++it; + } +} + +void Inbound() { //入库 + ShowStock(); + + alter = true; + + string line, name, mod; + int num, i, j; + map::iterator it; + + cout << "\n→入库←" << endl; + cout << "请依次输入货物的 “名称” “型号” “数量” 并按回车结束" << endl; + cout << "注:用空格分隔,输入为空即结束输入" << endl; + while (getline(cin, line)) { + num = 0, i = 0, j = 0; + if (line.empty()) break; + + while (line[i] == ' ') ++i; + while (line[i] != ' ') ++i, ++j; + name.assign(line, i - j, j); + j = 0; + + while (line[i] == ' ') ++i; + while (line[i] != ' ') ++i, ++j; + mod.assign(line, i - j, j); + + while (line[i] == ' ') ++i; + while (line[i] != ' ' && line[i] != '\0') { + num *= 10; + num += line[i] - '0'; + ++i; + } + + if (num < 0) { //入库数量为负值 + cerr << "入库数量不能为负值,请重新输入" << endl; + continue; + } + if (mod.empty() || name.empty()) { + cerr << "输入有误,请重新输入" << endl; + continue; + } + if (mod.size() > 12 || name.size() > 12) { + cerr << "名称或型号长度最大为12,请重新输入" << endl; + continue; + } + + it = data.find(name); + if (it == data.end()) { //无该货物 + data.insert(make_pair(name, Goods(mod, num))); + cout << name << "成功入库" << num << "件,目前存货为" << num << "件" << endl; + } else { //已有货物 + it->second.num += num; + cout << name << "成功入库" << num << "件,目前存货为" << it->second.num << "件" << endl; + } + } + cout << "入库结束! 以下为目前存货表" << endl; + ShowStock(); +} + +void Outbound() { //出库 + ShowStock(); + + alter = true; + + string line, name; + int num, i, j; + map::iterator it; + + cout << "\n←出库→" << endl; + cout << "请依次输入货物名称以及出库数量" << endl; + cout << "注:用空格分隔,输入为空即结束输入" << endl; + + while (getline(cin, line)) { + num = 0, i = 0, j = 0; + if (line.empty()) break; + + while (line[i] == ' ') ++i; + while (line[i] != ' ') ++i, ++j; + name.assign(line, i - j, j); + + while (line[i] == ' ') ++i; + while (line[i] != ' ' && line[i] != '\0') { + num *= 10; + num += line[i] - '0'; + ++i; + } + if (num < 0) { //出库数量为负值 + cerr << "出库数量不能为负值,请重新输入" << endl; + continue; + } + if (name.empty()) { + cerr << "输入有误,请重新输入" << endl; + continue; + } + + it = data.find(name); + if (it == data.end()) { //没有找到货物 + cout << "不存在该货物" << endl; + } else { //找到货物 + if (it->second.num < num) { //出库数量大于存货 + cout << "出库数量大于存货数量,需要全部出库吗?(1--是 0--否)" << endl; + while (cin >> j) { + if (j == 1) { + data.erase(it); + cout << name << "已全部出库" << endl; + break; + } else if (j == 0) { + cout << name << "出库失败" << endl; + break; + } + } + cin.ignore(); + } else if (it->second.num == num) { //出库数量等于存货 + data.erase(it); + cout << name << "已全部出库" << endl; + } else { //出货数量小于存货 + it->second.num -= num; + cout << name << "出库" << num << "件,剩余" << it->second.num << "件" << endl; + } + } + } + cout << "出库结束! 以下为目前存货表" << endl; + ShowStock(); +} + +void ShowMenu() { //功能菜单 + cout << "***************" + << "\n请选择功能" + << "\n1. 显示存货列表" + << "\n2. 入库" + << "\n3. 出库" + << "\n0. 退出程序" + << "\n***************" << endl; +} + +void Choose() { //功能选择 + char in = getch(); + system("cls"); + switch (in) { + case '1': { //显示存货列表 + ShowStock(); + system("pause"); + break; + } + case '2': { //入库 + Inbound(); + system("pause"); + break; + } + case '3': { //出库 + Outbound(); + system("pause"); + break; + } + case '0': { //退出程序 + system("cls"); + if (alter) { //存货改变,重新写入数据 + Outfile(); + } + system("pause"); + exit(0); + } + default: { + break; + } + } +} + +void warehouse() { + while (true) { + ShowMenu(); + Choose(); + system("cls"); + } +} + +int main() { + Infile(); + + warehouse(); + + return -1; //非正常结束 +} \ No newline at end of file diff --git "a/\345\256\236\351\252\214/FlightGame.cpp" "b/\345\256\236\351\252\214/FlightGame.cpp" new file mode 100644 index 00000000..927857fa --- /dev/null +++ "b/\345\256\236\351\252\214/FlightGame.cpp" @@ -0,0 +1,665 @@ +#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup") + +#include "raylib.h" +#include "raymath.h" + +// WASD进行上下左右移动, P键暂停游戏, Esc键退出当前游戏, Enter键进入游戏, 空格键射击(按住持续射击) + +#define WIDTH 1600.0f //设置宽 +#define HEIGHT 900.0f //设置高 +#define MAX_FPS 480.0f //设置FPS最大值(别太高) + +#define MAX_ENEMY 15 //设置敌人数目 + +#define MAX_BULLET 20 //设置飞机最大存在子弹数目 + +#define PLAYER_HEALTH 20 //设置普通模式下面飞机血量 +#define PLAYER_SIZE 20.0f //设置飞机大小 +#define PLAYER_SPEED 3.0f //设置飞机移动速度 +#define PLAYER_ROTATION_SPEED 3.0f //设置飞机转向速度 +#define PLAYER_SHOOT_RATE 4 //设置飞机开火速度 + +class Bullet { + public: + Vector2 position; + Color color; + int attack; + float radius; + float speed; + float rotation; + bool active; +}; + +class Player { + public: + void initialize() { + position = Vector2{WIDTH / 2, HEIGHT - height}; + rotation = 0.0f; + score = 0; + defeat = 0; + health = PLAYER_HEALTH; + color = MAROON; + speed = PLAYER_SPEED * 240.0f / MAX_FPS; + rSpeed = PLAYER_ROTATION_SPEED * 240.0f / MAX_FPS; + height = (PLAYER_SIZE / 2) / tanf(20 * DEG2RAD); + + for (int i = 0; i < MAX_BULLET; ++i) { + bullet[i].attack = 1; + bullet[i].color = RED; + bullet[i].radius = PLAYER_SIZE / 7; + } + } + + void move() { + if (IsKeyDown(KEY_A)) { + rotation -= rSpeed; + } + if (IsKeyDown(KEY_D)) { + rotation += rSpeed; + } + if (IsKeyDown(KEY_W)) { + position.x += speed * sin(rotation * DEG2RAD); + position.y -= speed * cos(rotation * DEG2RAD); + } + if (IsKeyDown(KEY_S)) { + position.x -= speed * sin(rotation * DEG2RAD); + position.y += speed * cos(rotation * DEG2RAD); + } + + if (position.x > WIDTH + height) { + position.x = -height; + } else if (position.x < -height) { + position.x = WIDTH + height; + } + if (position.y > (HEIGHT + height)) { + position.y = -height; + } else if (position.y < -height) { + position.y = HEIGHT + height; + } + } + + void shoot(Sound& shoot) { + ++lag; + + if (IsKeyDown(KEY_SPACE) && lag > (MAX_FPS / PLAYER_SHOOT_RATE)) { + PlaySound(shoot); + for (int i = 0; i < MAX_BULLET; ++i) { + if (!bullet[i].active) { + bullet[i].active = true; + bullet[i].position = Vector2{position.x + (float)sin(rotation * DEG2RAD) * (height / 2.5f), + position.y - (float)cos(rotation * DEG2RAD) * (height / 2.5f)}; + bullet[i].speed = speed * 3.0f; + bullet[i].rotation = rotation; + break; + } + } + lag = 0; + } + + for (int i = 0; i < MAX_BULLET; ++i) { + if (bullet[i].active) { + bullet[i].position.x += bullet[i].speed * sin(bullet[i].rotation * DEG2RAD); + bullet[i].position.y -= bullet[i].speed * cos(bullet[i].rotation * DEG2RAD); + + if (bullet[i].position.x > WIDTH) { + bullet[i].active = false; + } else if (bullet[i].position.x < 0) { + bullet[i].active = false; + } + if (bullet[i].position.y > HEIGHT) { + bullet[i].active = false; + } else if (bullet[i].position.y < 0) { + bullet[i].active = false; + } + } + } + } + + Vector2 position; + float rotation; + int health; + int score; + int defeat; + int lag = 0; + Color color; + + float speed; + float rSpeed; + float height; + + Bullet bullet[MAX_BULLET]; +}; + +class Enemy { + public: + void initialize() { + position.x = (float)GetRandomValue(0, WIDTH); + position.y = (float)GetRandomValue(0, HEIGHT / 2); + radius = (float)GetRandomValue(PLAYER_SIZE, PLAYER_SIZE * 4); + speed = PLAYER_SPEED * PLAYER_SIZE / radius * 60 / MAX_FPS; + health = radius / PLAYER_SIZE; + survive = true; + for (int i = 0; i < MAX_BULLET; ++i) { + //bullet[i].active = false; + bullet[i].attack = health; + bullet[i].color = DARKGRAY; + bullet[i].radius = radius / 7; + } + } + + void initializeU() { + position.x = (float)GetRandomValue(0, WIDTH); + position.y = (float)GetRandomValue(0, HEIGHT / 2); + health = radius / PLAYER_SIZE; + survive = true; + } + + void move() { + if (survive) { + position.x += speed; + position.y += speed; + + if (position.x > WIDTH + radius) { + position.x = -radius; + } else if (position.x < -radius) { + position.x = WIDTH + radius; + } + if (position.y > (HEIGHT + radius)) { + position.y = -radius; + } else if (position.y < -radius) { + position.y = HEIGHT + radius; + } + } + } + + void shoot(Vector2& pPosition) { + ++lag; + if (survive && lag > (MAX_FPS * radius / 50)) { + float rotation = 180 - atan((pPosition.x - position.x) / (pPosition.y - position.y)) * RAD2DEG; + + for (int i = 0; i < MAX_BULLET; ++i) { + if (!bullet[i].active) { + bullet[i].active = true; + bullet[i].position = Vector2{position.x + (float)sin(rotation * DEG2RAD) * radius, + position.y - (float)cos(rotation * DEG2RAD) * radius}; + bullet[i].speed = speed * 2.0f; + bullet[i].rotation = rotation; + break; + } + } + + lag = 0; + } + + for (int i = 0; i < MAX_BULLET; ++i) { + if (bullet[i].active) { + bullet[i].position.x += bullet[i].speed * sin(bullet[i].rotation * DEG2RAD); + bullet[i].position.y -= bullet[i].speed * cos(bullet[i].rotation * DEG2RAD); + + if (bullet[i].position.x > WIDTH) { + bullet[i].active = false; + } else if (bullet[i].position.x < 0) { + bullet[i].active = false; + } + if (bullet[i].position.y > HEIGHT) { + bullet[i].active = false; + } else if (bullet[i].position.y < 0) { + bullet[i].active = false; + } + } + } + } + + Vector2 position; + float radius; + float speed; + bool survive; + int health; + int lag = 0; + Bullet bullet[MAX_BULLET]; +}; + +static Player player; +static Enemy enemy[MAX_ENEMY]; +static Bullet bullet[MAX_BULLET]; +static int framesCounter; +static bool gameOver; +static bool pause; +static int mode = 0; + +static void Welcome(); +static void InitGame(Music& background); +static void UpdateGame(Music& background, Sound& shoot, Sound& defeat); +static void UpdateGameU(Music& background, Sound& shoot, Sound& defeat); +static void DrawGame(Music& background, Sound& win, Sound& lose); +static void DrawGameU(Music& background, Sound& end); +static void UnloadGame(Music& background, Music& backgroundU, Sound& shoot, Sound& defeat, Sound& win, Sound& lose, + Sound& end); + +int main() { + InitWindow(WIDTH, HEIGHT, "Flight Game"); + InitAudioDevice(); + SetExitKey(KEY_NULL); + + Music background_music = LoadMusicStream("resources/background_music.ogg"); + Music background_music_u = LoadMusicStream("resources/background_music_u.ogg"); + Sound shoot_sound = LoadSound("resources/shoot_sound.ogg"); + Sound lose_sound = LoadSound("resources/lose_sound.ogg"); + Sound win_sound = LoadSound("resources/win_sound.ogg"); + Sound defeat_sound = LoadSound("resources/defeat_sound.ogg"); + Sound end_sound = LoadSound("resources/end_sound.ogg"); + + InitGame(background_music); + + while (!WindowShouldClose()) { + switch (mode) { + case 0: { + Welcome(); + if (IsKeyPressed(KEY_ONE)) { + mode = 1; + InitGame(background_music); + } else if (IsKeyPressed(KEY_TWO)) { + mode = 2; + InitGame(background_music_u); + } else if (IsKeyPressed(KEY_ESCAPE)) { + goto exit; + } + break; + } + + case 1: { + UpdateGame(background_music, shoot_sound, defeat_sound); + DrawGame(background_music, win_sound, lose_sound); + UpdateMusicStream(background_music); + break; + } + + case 2: { + UpdateGameU(background_music_u, shoot_sound, defeat_sound); + DrawGameU(background_music_u, end_sound); + UpdateMusicStream(background_music_u); + break; + } + + default: { + break; + } + } + } + +exit: + UnloadGame(background_music, background_music_u, shoot_sound, defeat_sound, win_sound, lose_sound, end_sound); + + return 0; +} + +static void Welcome() { + BeginDrawing(); + + ClearBackground(RAYWHITE); + + DrawText("<<< FLIGHT GAME >>>", WIDTH / 2 - MeasureText("<<< FLIGHT GAME >>>", 70) / 2, HEIGHT / 2 - 100, 70, + SKYBLUE); + DrawText("> NORMAL MODE : ONE", WIDTH / 2 - MeasureText("> NORMAL MODE : ONE", 20) / 2, HEIGHT / 2, 25, DARKPURPLE); + DrawText("> INFINITE MODE : TWO", WIDTH / 2 - MeasureText("> INFINITE MODE : TWO", 20) / 2, HEIGHT / 2 + 40, 25, + DARKPURPLE); + DrawText("> PRESS [ESC] TO EXIT", WIDTH / 2 - MeasureText("> PRESS [ESC] TO EXIT", 20) / 2, HEIGHT / 2 + 80, 25, + DARKPURPLE); + + EndDrawing(); +} + +static void InitGame(Music& background) { + SetTargetFPS(MAX_FPS); + + framesCounter = 0; + pause = false; + gameOver = false; + + for (int i = 0; i < MAX_ENEMY; ++i) { + enemy[i].initialize(); + } + player.initialize(); + + PlayMusicStream(background); + + if (mode == 2) { + player.color = ORANGE; + player.health *= 10; + player.speed *= 1.5f; + player.rSpeed *= 1.2f; + } +} + +static void UpdateGame(Music& background, Sound& shoot, Sound& defeat) { + if (IsKeyPressed(KEY_ESCAPE)) { + StopMusicStream(background); + mode = 0; + } + + if (!gameOver) { + if (IsKeyPressed(KEY_P)) { + pause = !pause; + if (pause) { + PauseMusicStream(background); + } else { + ResumeMusicStream(background); + } + } + + if (!pause) { + ++framesCounter; + + //玩家--移动 + player.move(); + + //玩家--射击 + player.shoot(shoot); + + //玩家&&敌人--碰撞系统 + Vector2 place = {player.position.x + sin(player.rotation * DEG2RAD) * (player.height / 2.5f), + player.position.y - cos(player.rotation * DEG2RAD) * (player.height / 2.5f)}; + for (int i = 0; i < MAX_ENEMY; i++) { + if (enemy[i].survive && + CheckCollisionCircles({place.x, place.y}, PLAYER_SIZE / 3, enemy[i].position, enemy[i].radius)) { + player.health -= enemy[i].health; + ++player.defeat; + enemy[i].survive = false; + PlaySound(defeat); + } + } + + //玩家&&子弹--碰撞系统 + for (int i = 0; i < MAX_ENEMY; ++i) { + for (int j = 0; j < MAX_BULLET; ++j) { + if (enemy[i].bullet[j].active && + CheckCollisionCircles({ place.x, place.y }, PLAYER_SIZE / 3, enemy[i].bullet[j].position, + enemy[i].bullet[j].radius)) { + player.health -= enemy[i].bullet[j].attack; + enemy[i].bullet[j].active = false; + } + } + } + + if (player.health <= 0) { + gameOver = true; + } + + //敌人&&子弹--碰撞系统 + for (int i = 0; i < MAX_BULLET; ++i) { + if (player.bullet[i].active) { + place = {player.bullet[i].position.x, player.bullet[i].position.y}; + for (int j = 0; j < MAX_ENEMY; ++j) { + if (enemy[j].survive && CheckCollisionCircles({place.x, place.y}, player.bullet[i].radius, + enemy[j].position, enemy[j].radius)) { + --enemy[j].health; + ++player.score; + if (enemy[j].health <= 0) { + enemy[j].survive = false; + ++player.defeat; + PlaySound(defeat); + } + player.bullet[i].active = false; + break; + } + } + } + } + + //敌人--移动--射击 + for (int i = 0; i < MAX_ENEMY; i++) { + enemy[i].move(); + enemy[i].shoot(player.position); + } + + if (player.defeat == MAX_ENEMY) { + gameOver = true; + } + } + } else { + if (IsKeyPressed(KEY_ENTER)) { + InitGame(background); + gameOver = false; + } + } +} + +static void UpdateGameU(Music& background, Sound& shoot, Sound& defeat) { + if (IsKeyPressed(KEY_ESCAPE)) { + StopMusicStream(background); + mode = 0; + } + + if (!gameOver) { + if (IsKeyPressed(KEY_P)) { + pause = !pause; + if (pause) { + PauseMusicStream(background); + } else { + ResumeMusicStream(background); + } + } + + if (!pause) { + ++framesCounter; + + //玩家--移动 + player.move(); + + //玩家--射击 + player.shoot(shoot); + + //玩家&&敌人--碰撞系统 + Vector2 place = {player.position.x + sin(player.rotation * DEG2RAD) * (player.height / 2.5f), + player.position.y - cos(player.rotation * DEG2RAD) * (player.height / 2.5f)}; + for (int i = 0; i < MAX_ENEMY; i++) { + if (CheckCollisionCircles({place.x, place.y}, PLAYER_SIZE / 3, enemy[i].position, enemy[i].radius)) { + player.health -= enemy[i].health; + ++player.defeat; + enemy[i].initializeU(); + PlaySound(defeat); + } + } + + //玩家&&子弹--碰撞系统 + for (int i = 0; i < MAX_ENEMY; ++i) { + for (int j = 0; j < MAX_BULLET; ++j) { + if (enemy[i].bullet[j].active && + CheckCollisionCircles({place.x, place.y}, PLAYER_SIZE / 3, enemy[i].bullet[j].position, + enemy[i].bullet[j].radius)) { + player.health -= enemy[i].bullet[j].attack; + enemy[i].bullet[j].active = false; + } + } + } + + if (player.health <= 0) { + gameOver = true; + } + + //敌人&&子弹--碰撞系统 + for (int i = 0; i < MAX_BULLET; ++i) { + if (player.bullet[i].active) { + place = {player.bullet[i].position.x, player.bullet[i].position.y}; + for (int j = 0; j < MAX_ENEMY; ++j) { + if (CheckCollisionCircles({place.x, place.y}, player.bullet[i].radius, enemy[j].position, + enemy[j].radius)) { + --enemy[j].health; + ++player.score; + if (enemy[j].health <= 0) { + enemy[j].initializeU(); + ++player.defeat; + PlaySound(defeat); + } + player.bullet[i].active = false; + break; + } + } + } + } + + //敌人--移动--射击 + for (int i = 0; i < MAX_ENEMY; i++) { + enemy[i].move(); + enemy[i].shoot(player.position); + } + } + } else { + if (IsKeyPressed(KEY_ENTER)) { + InitGame(background); + gameOver = false; + } + } +} + +static void DrawGame(Music& background, Sound& win, Sound& lose) { + BeginDrawing(); + + ClearBackground(RAYWHITE); + + //玩家 + Vector2 v1 = {player.position.x + sinf(player.rotation * DEG2RAD) * (player.height), + player.position.y - cosf(player.rotation * DEG2RAD) * (player.height)}; + Vector2 v2 = {player.position.x - cosf(player.rotation * DEG2RAD) * (PLAYER_SIZE / 2), + player.position.y - sinf(player.rotation * DEG2RAD) * (PLAYER_SIZE / 2)}; + Vector2 v3 = {player.position.x + cosf(player.rotation * DEG2RAD) * (PLAYER_SIZE / 2), + player.position.y + sinf(player.rotation * DEG2RAD) * (PLAYER_SIZE / 2)}; + DrawTriangle(v1, v2, v3, player.color); + + //玩家--子弹 + for (int i = 0; i < MAX_BULLET; ++i) { + if (player.bullet[i].active == true) { + DrawCircleV(player.bullet[i].position, player.bullet[i].radius, player.bullet[i].color); + } + } + + //敌人 + for (int i = 0; i < MAX_ENEMY; i++) { + if (enemy[i].survive) { + DrawCircleV(enemy[i].position, enemy[i].radius, GRAY); + } else { + DrawCircleV(enemy[i].position, enemy[i].radius, Fade(LIGHTGRAY, 0.3f)); + } + //敌人--子弹 + for (int j = 0; j < MAX_BULLET; ++j) { + if (enemy[i].bullet[j].active == true) { + DrawCircleV(enemy[i].bullet[j].position, enemy[i].bullet[j].radius, enemy[i].bullet[j].color); + } + } + } + + //信息 + DrawText(TextFormat("TIME: %.02f", (float)framesCounter / MAX_FPS), 10, 10, 10, BLACK); + DrawText(TextFormat("FPS: %.02f", (float)GetFPS()), MeasureText("TIME: %000000000.02f", 10), 10, 10, BLACK); + + DrawText(TextFormat("HEALTH: %d", player.health), WIDTH - 120, 10, 15, BLACK); + DrawText(TextFormat("SPEED: %.02f", player.speed), WIDTH - 120, 30, 15, BLACK); + DrawText(TextFormat("SCORE: %d", player.score), WIDTH - 120, 50, 15, BLACK); + + //胜利||失败 + if (gameOver && player.defeat == MAX_ENEMY) { + DrawText("--- YOU WIN ---", WIDTH / 2 - MeasureText("--- YOU WIN ---", 50) / 2, HEIGHT / 2 - 60, 50, RED); + DrawText("PRESS [ENTER] TO PLAY AGAIN", WIDTH / 2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20) / 2, + HEIGHT / 2, 20, BLACK); + + if (IsMusicPlaying(background)) StopMusicStream(background); + if (!pause) { + PlaySound(win); + pause = !pause; + } + } else if (gameOver) { + DrawText("--- YOU LOSE ---", WIDTH / 2 - MeasureText("--- YOU LOSE ---", 50) / 2, HEIGHT / 2 - 60, 50, RED); + DrawText("PRESS [ENTER] TO PLAY AGAIN", WIDTH / 2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20) / 2, + HEIGHT / 2, 20, BLACK); + + if (IsMusicPlaying(background)) StopMusicStream(background); + if (!pause) { + PlaySound(lose); + pause = !pause; + } + } + + //暂停 + if (pause && !gameOver) { + DrawText("--- PAUSED ---", WIDTH / 2 - MeasureText("--- PAUSED ---", 50) / 2, HEIGHT / 2 - 60, 50, BLACK); + } + + EndDrawing(); +} + +static void DrawGameU(Music& background, Sound& end) { + BeginDrawing(); + + ClearBackground(RAYWHITE); + + //玩家 + Vector2 v1 = {player.position.x + sinf(player.rotation * DEG2RAD) * (player.height), + player.position.y - cosf(player.rotation * DEG2RAD) * (player.height)}; + Vector2 v2 = {player.position.x - cosf(player.rotation * DEG2RAD) * (PLAYER_SIZE / 2), + player.position.y - sinf(player.rotation * DEG2RAD) * (PLAYER_SIZE / 2)}; + Vector2 v3 = {player.position.x + cosf(player.rotation * DEG2RAD) * (PLAYER_SIZE / 2), + player.position.y + sinf(player.rotation * DEG2RAD) * (PLAYER_SIZE / 2)}; + DrawTriangle(v1, v2, v3, player.color); + + //玩家--子弹 + for (int i = 0; i < MAX_BULLET; ++i) { + if (player.bullet[i].active == true) { + DrawCircleV(player.bullet[i].position, player.bullet[i].radius, player.bullet[i].color); + } + } + + //敌人 + for (int i = 0; i < MAX_ENEMY; i++) { + DrawCircleV(enemy[i].position, enemy[i].radius, GRAY); + + //敌人--子弹 + for (int j = 0; j < MAX_BULLET; ++j) { + if (enemy[i].bullet[j].active == true) { + DrawCircleV(enemy[i].bullet[j].position, enemy[i].bullet[j].radius, enemy[i].bullet[j].color); + } + } + } + + //信息 + DrawText(TextFormat("TIME: %.02f", (float)framesCounter / MAX_FPS), 10, 10, 10, BLACK); + DrawText(TextFormat("FPS: %.02f", (float)GetFPS()), MeasureText("TIME: %000000000.02f", 10), 10, 10, BLACK); + + DrawText(TextFormat("HEALTH: %d", player.health), WIDTH - 120, 10, 15, BLACK); + DrawText(TextFormat("SPEED: %.02f", player.speed), WIDTH - 120, 30, 15, BLACK); + DrawText(TextFormat("SCORE: %d", player.score), WIDTH - 120, 50, 15, BLACK); + DrawText(TextFormat("DEFEAT: %d", player.defeat), WIDTH - 120, 70, 15, BLACK); + + //结束 + if (gameOver) { + DrawText("--- WELL DONE ---", WIDTH / 2 - MeasureText("--- WELL DONE ---", 50) / 2, HEIGHT / 2 - 60, 50, + ORANGE); + DrawText("PRESS [ENTER] TO PLAY AGAIN", WIDTH / 2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20) / 2, + HEIGHT / 2, 20, BLACK); + + if (IsMusicPlaying(background)) StopMusicStream(background); + if (!pause) { + PlaySound(end); + pause = !pause; + } + } + //暂停 + if (pause && !gameOver) { + DrawText("--- PAUSED ---", WIDTH / 2 - MeasureText("--- PAUSED ---", 50) / 2, HEIGHT / 2 - 60, 50, BLACK); + } + + EndDrawing(); +} + +static void UnloadGame(Music& background, Music& backgroundU, Sound& shoot, Sound& defeat, Sound& win, Sound& lose, + Sound& end) { + UnloadMusicStream(background); + UnloadMusicStream(backgroundU); + UnloadSound(shoot); + UnloadSound(lose); + UnloadSound(win); + UnloadSound(end); + UnloadSound(defeat); + + CloseAudioDevice(); + CloseWindow(); +} \ No newline at end of file diff --git "a/\345\256\236\351\252\214/FlightGame.zip" "b/\345\256\236\351\252\214/FlightGame.zip" new file mode 100644 index 00000000..a5c8f04b Binary files /dev/null and "b/\345\256\236\351\252\214/FlightGame.zip" differ