版本比较
OI 之我的游戏发展史
正文
增加 44 行删除 44 行
38 行未改动39404142434445464748495051525354555657585960616263646566677 行未改动7576777879808182838485868788899091929394959697989910 行未改动11011111211311411511611711811912012112212312412517 行未改动14314414514614714814915015133 行未改动185186187188189190191192193247 行未改动全部展开44144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549616 行未改动513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546552 行未改动全部展开1099110011011102110311041105110611071108110911101111111211131114111511166 行未改动11231124112511261127112811291130113148 行未改动118011811182118311841185118611871188104 行未改动全部展开38 行未改动def monitor_space():global is_pausedwhile True:keyboard.wait('space')with pause_lock:is_paused = not is_pausedif is_paused:print("\n[输出已暂停,按空格键继续]", end='', flush=True)else:print("\r ", end='', flush=True)print("\r", end='', flush=True)def Print(s, huanhang=1):global is_pausedthread = threading.Thread(target=monitor_space, daemon=True)thread.start()for i in s:while is_paused:time.sleep(0.1)print(i, end='', flush=True)time.sleep(0.02)if huanhang == 1:print()7 行未改动import timeimport keyboardimport threadingcurrent_pos = 0is_paused = Falserunning = Truelock = threading.Lock()def monitor_keys():global is_paused, current_pos, runningwhile running:event = keyboard.read_event(suppress=False)if event.event_type == keyboard.KEY_DOWN:with lock:if event.name == 'space':is_paused = not is_pausedif is_paused:print("\n[输出暂停,按空格继续]", end='', flush=True)else:print("\r", end='', flush=True)elif event.name == 'p':if current_pos > 0:current_pos -= 1print('\b \b', end='', flush=True)10 行未改动time.sleep(0.1)print(char, end='', flush=True)current_pos += 1time.sleep(0.05)if huanhang == 1:print()except KeyboardInterrupt:passfinally:running = Falseprint("\n[输出结束]")```经过这样的实现,感觉看起来会比原来好很多。17 行未改动print('\x1bc')if j < 10:print('引擎中······')elif j < 30:print('网络加载中······')elif j < 45:print('资源加载中······')elif j < 75:print('程序加载中······')33 行未改动import randomdef guess_number_game():number = random.randint(1, 100)guess_count = 0print("欢迎来到猜数字小游戏!")print("我已经想好了一个 1 到 100 之间的整数,你来猜猜看吧!")while True:247 行未改动全部展开- 链接 kernel32.lib- 使用 CONSOLE_FONT_INFOEX 结构例如:```cpp#include <iostream>#include <windows.h>using namespace std;int main() {HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleTextAttribute(hConsole, 12);cout << "这是红色文字" << endl;SetConsoleTextAttribute(hConsole, 10);cout << "这是绿色文字" << endl;SetConsoleTextAttribute(hConsole, 14);cout << "这是黄色文字" << endl;SetConsoleTextAttribute(hConsole, 15);cout << "这是白色文字(默认)" << endl;return 0;}```2. Sleep() 函数:可以停住一段时间。Sleep(1000) 可以暂停一秒。在程序中很好用。3. CreateWindowEx{} 函数:可以创建一个窗口,也很好用。例如:```HWND hwnd = CreateWindowEx(0,"MyGameClass","我的小游戏",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT,800, 600,NULL, NULL, hInstance, NULL);```4. GetDC() 和 ReleaseDC() 函数:GetDC() 用于获取指定窗口的 DC,ReleaseDC() 用于释放 DC,两者配合 GDI 函数(如 Rectangle,Ellipse,TextOut)使用。例如:```HDC hdc = GetDC(hwnd);Ellipse(hdc, 100, 100, 200, 200);// 画一个椭圆(这里是圆)ReleaseDC(hwnd, hdc);```另外还有一些画图形的函数,这里不再解释。这些函数可以进行基础的绘画了,可以制作一些 2D 小游戏,例如坦克大战。只是感觉似乎能画的内容不多。16 行未改动GetClientRect(hwnd, &rect);FillRect(hdc, &rect, (HBRUSH) GetStockObject(WHITE_BRUSH));int x = (rect.right - 200) / 2;int y = rect.bottom - 150;HBRUSH hHouseBrush = CreateSolidBrush(RGB(220, 80, 80));SelectObject(hdc, hHouseBrush);Rectangle(hdc, x, y - 100, x + 200, y);HPEN hRoofPen = CreatePen(PS_SOLID, 2, RGB(100, 30, 30));HBRUSH hRoofBrush = CreateSolidBrush(RGB(150, 40, 40));SelectObject(hdc, hRoofPen);SelectObject(hdc, hRoofBrush);POINT roof[3] = {{x, y - 100},{x + 100, y - 150},{x + 200, y - 100}};Polygon(hdc, roof, 3);HBRUSH hDoorBrush = CreateSolidBrush(RGB(100, 60, 30));SelectObject(hdc, hDoorBrush);Rectangle(hdc, x + 80, y - 60, x + 120, y);HBRUSH hWindowBrush = CreateSolidBrush(RGB(255, 255, 150));SelectObject(hdc, hWindowBrush);Rectangle(hdc, x + 30, y - 80, x + 70, y - 40);Rectangle(hdc, x + 130, y - 80, x + 170, y - 40);DeleteObject(hHouseBrush);DeleteObject(hRoofPen);DeleteObject(hRoofBrush);552 行未改动全部展开setfillcolor(YELLOW);solidrectangle(left, top, right, bottom);setfillcolor(WHITE);Sleep(200);if (GetAsyncKeyState('W') && d != 1) {d = 0;}else if (GetAsyncKeyState('S') && d != 0) {d = 1;}else if (GetAsyncKeyState('A') && d != 3) {d = 2;}else if (GetAsyncKeyState('D') && d != 2) {d = 3;}else if (GetAsyncKeyState(VK_UP) && d != 1) {d = 0;6 行未改动}else if (GetAsyncKeyState(VK_RIGHT) && d != 2) {d = 3;}else if (GetAsyncKeyState(VK_SPACE)) {setbkcolor(RGB(rand() % 256 + 1, rand() % 256 + 1, rand() % 256 + 1));cleardevice();}node tail = snakeMove(snake, length, d);48 行未改动}int main(){initgraph(640, 480);int viewWidth = 640;int viewHeight = 480;int depth = 200;104 行未改动全部展开38 行未改动39404142434445464748495051525354555657585960616263646566677 行未改动7576777879808182838485868788899091929394959697989910 行未改动11011111211311411511611711811912012112212312412517 行未改动14314414514614714814915015133 行未改动185186187188189190191192193247 行未改动全部展开44144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549616 行未改动513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546552 行未改动全部展开1099110011011102110311041105110611071108110911101111111211131114111511166 行未改动11231124112511261127112811291130113148 行未改动118011811182118311841185118611871188104 行未改动全部展开38 行未改动def monitor_space():global is_pausedwhile True:keyboard.wait('space')with pause_lock:is_paused = not is_pausedif is_paused:print("\n[输出已暂停,按空格键继续]", end='', flush=True)else:print("\r ", end='', flush=True)print("\r", end='', flush=True)def Print(s, huanhang=1):global is_pausedthread = threading.Thread(target=monitor_space, daemon=True)thread.start()for i in s:while is_paused:time.sleep(0.1)print(i, end='', flush=True)time.sleep(0.02)if huanhang == 1:print()7 行未改动import timeimport keyboardimport threadingcurrent_pos = 0is_paused = Falserunning = Truelock = threading.Lock()def monitor_keys():global is_paused, current_pos, runningwhile running:event = keyboard.read_event(suppress=False)if event.event_type == keyboard.KEY_DOWN:with lock:if event.name == 'space':is_paused = not is_pausedif is_paused:print("\n[输出暂停,按空格继续]", end='', flush=True)else:print("\r", end='', flush=True)elif event.name == 'p':if current_pos > 0:current_pos -= 1print('\b \b', end='', flush=True)10 行未改动time.sleep(0.1)print(char, end='', flush=True)current_pos += 1time.sleep(0.05)if huanhang == 1:print()except KeyboardInterrupt:passfinally:running = Falseprint("\n[输出结束]")```经过这样的实现,感觉看起来会比原来好很多。17 行未改动print('\x1bc')if j < 10:print('引擎中······')elif j < 30:print('网络加载中······')elif j < 45:print('资源加载中······')elif j < 75:print('程序加载中······')33 行未改动import randomdef guess_number_game():number = random.randint(1, 100)guess_count = 0print("欢迎来到猜数字小游戏!")print("我已经想好了一个 1 到 100 之间的整数,你来猜猜看吧!")while True:247 行未改动全部展开- 链接 kernel32.lib- 使用 CONSOLE_FONT_INFOEX 结构例如:```cpp#include <iostream>#include <windows.h>using namespace std;int main() {HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleTextAttribute(hConsole, 12);cout << "这是红色文字" << endl;SetConsoleTextAttribute(hConsole, 10);cout << "这是绿色文字" << endl;SetConsoleTextAttribute(hConsole, 14);cout << "这是黄色文字" << endl;SetConsoleTextAttribute(hConsole, 15);cout << "这是白色文字(默认)" << endl;return 0;}```2. Sleep() 函数:可以停住一段时间。Sleep(1000) 可以暂停一秒。在程序中很好用。3. CreateWindowEx{} 函数:可以创建一个窗口,也很好用。例如:```HWND hwnd = CreateWindowEx(0,"MyGameClass","我的小游戏",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT,800, 600,NULL, NULL, hInstance, NULL);```4. GetDC() 和 ReleaseDC() 函数:GetDC() 用于获取指定窗口的 DC,ReleaseDC() 用于释放 DC,两者配合 GDI 函数(如 Rectangle,Ellipse,TextOut)使用。例如:```HDC hdc = GetDC(hwnd);Ellipse(hdc, 100, 100, 200, 200);// 画一个椭圆(这里是圆)ReleaseDC(hwnd, hdc);```另外还有一些画图形的函数,这里不再解释。这些函数可以进行基础的绘画了,可以制作一些 2D 小游戏,例如坦克大战。只是感觉似乎能画的内容不多。16 行未改动GetClientRect(hwnd, &rect);FillRect(hdc, &rect, (HBRUSH) GetStockObject(WHITE_BRUSH));int x = (rect.right - 200) / 2;int y = rect.bottom - 150;HBRUSH hHouseBrush = CreateSolidBrush(RGB(220, 80, 80));SelectObject(hdc, hHouseBrush);Rectangle(hdc, x, y - 100, x + 200, y);HPEN hRoofPen = CreatePen(PS_SOLID, 2, RGB(100, 30, 30));HBRUSH hRoofBrush = CreateSolidBrush(RGB(150, 40, 40));SelectObject(hdc, hRoofPen);SelectObject(hdc, hRoofBrush);POINT roof[3] = {{x, y - 100},{x + 100, y - 150},{x + 200, y - 100}};Polygon(hdc, roof, 3);HBRUSH hDoorBrush = CreateSolidBrush(RGB(100, 60, 30));SelectObject(hdc, hDoorBrush);Rectangle(hdc, x + 80, y - 60, x + 120, y);HBRUSH hWindowBrush = CreateSolidBrush(RGB(255, 255, 150));SelectObject(hdc, hWindowBrush);Rectangle(hdc, x + 30, y - 80, x + 70, y - 40);Rectangle(hdc, x + 130, y - 80, x + 170, y - 40);DeleteObject(hHouseBrush);DeleteObject(hRoofPen);DeleteObject(hRoofBrush);552 行未改动全部展开setfillcolor(YELLOW);solidrectangle(left, top, right, bottom);setfillcolor(WHITE);Sleep(200);if (GetAsyncKeyState('W') && d != 1) {d = 0;}else if (GetAsyncKeyState('S') && d != 0) {d = 1;}else if (GetAsyncKeyState('A') && d != 3) {d = 2;}else if (GetAsyncKeyState('D') && d != 2) {d = 3;}else if (GetAsyncKeyState(VK_UP) && d != 1) {d = 0;6 行未改动}else if (GetAsyncKeyState(VK_RIGHT) && d != 2) {d = 3;}else if (GetAsyncKeyState(VK_SPACE)) {setbkcolor(RGB(rand() % 256 + 1, rand() % 256 + 1, rand() % 256 + 1));cleardevice();}node tail = snakeMove(snake, length, d);48 行未改动}int main(){initgraph(640, 480);int viewWidth = 640;int viewHeight = 480;int depth = 200;104 行未改动全部展开