版本比较
OI 之我的游戏发展史
正文
增加 44 行删除 44 行
38 行未改动394041424343444545464748495050515253545556575859606061626363646566677 行未改动757677787979808181828384858687878889909192939495959697989910 行未改动11011111211311411411511611711811912012112112212312412517 行未改动14314414514614714714814915015133 行未改动185186187188189189190191192193247 行未改动全部展开44144244344444544544644744844945045145245245345445445545645645745845845946046146246346446446546646746846947047147247247347447547647747847547647747847948048148248348448548548648748748848949049149249249349449549616 行未改动513514515516517518519517518519520521521522523524524525526527528529530531529530531532533534535535536537537538539539540541542541542543544545546552 行未改动全部展开109911001101110211031103110411051106110711081109111011111112111211131114111511166 行未改动112311241125112611271127112811291130113148 行未改动1180118111821183118411841185118611871188104 行未改动全部展开38 行未改动
def monitor_space(): global is_paused while True: keyboard.wait('space') keyboard.wait('space') with pause_lock: is_paused = not is_paused is_paused = not is_paused if is_paused: print("\n[输出已暂停,按空格键继续]", end='', flush=True) else: print("\r ", end='', flush=True) print("\r", end='', flush=True) print("\r", end='', flush=True)
def Print(s, huanhang=1): global is_paused
thread = threading.Thread(target=monitor_space, daemon=True) thread.start()
for i in s: while is_paused: time.sleep(0.1) time.sleep(0.1)
print(i, end='', flush=True) time.sleep(0.02) time.sleep(0.02)
if huanhang == 1: print()
7 行未改动import timeimport keyboardimport threading
current_pos = 0 current_pos = 0is_paused = Falserunning = True running = Truelock = threading.Lock()
def monitor_keys(): global is_paused, current_pos, running while running: event = keyboard.read_event(suppress=False) event = keyboard.read_event(suppress=False) if event.event_type == keyboard.KEY_DOWN: with lock: if event.name == 'space': is_paused = not is_paused if is_paused: print("\n[输出暂停,按空格继续]", end='', flush=True) else: print("\r", end='', flush=True) print("\r", end='', flush=True) elif event.name == 'p': if current_pos > 0: current_pos -= 1 print('\b \b', end='', flush=True)10 行未改动 time.sleep(0.1)
print(char, end='', flush=True) current_pos += 1 time.sleep(0.05) time.sleep(0.05)
if huanhang == 1: print() except KeyboardInterrupt: pass finally: running = False running = False print("\n[输出结束]")```
经过这样的实现,感觉看起来会比原来好很多。17 行未改动 print('\x1bc') if j < 10: print('引擎中······') elif j < 30: print('网络加载中······') print('网络加载中······') elif j < 45: print('资源加载中······') elif j < 75: print('程序加载中······')33 行未改动import random
def guess_number_game(): number = random.randint(1, 100) guess_count = 0 guess_count = 0 print("欢迎来到猜数字小游戏!") 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); SetConsoleTextAttribute(hConsole, 12); cout << "这是红色文字" << endl; SetConsoleTextAttribute(hConsole, 10); SetConsoleTextAttribute(hConsole, 10); cout << "这是绿色文字" << endl; SetConsoleTextAttribute(hConsole, 14); SetConsoleTextAttribute(hConsole, 14); cout << "这是黄色文字" << endl; SetConsoleTextAttribute(hConsole, 15); SetConsoleTextAttribute(hConsole, 15); cout << "这是白色文字(默认)" << endl; return 0; } ```2. Sleep() 函数:
可以停住一段时间。Sleep(1000) 可以暂停一秒。 在程序中很好用。3. CreateWindowEx{} 函数:
可以创建一个窗口,也很好用。
例如:
``` HWND hwnd = CreateWindowEx( 0, "MyGameClass", "我的小游戏", WS_OVERLAPPEDWINDOW, 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); 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)); 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); 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)); 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} {x, y - 100}, {x + 100, y - 150}, {x + 200, y - 100} }; Polygon(hdc, roof, 3);
HBRUSH hDoorBrush = CreateSolidBrush(RGB(100, 60, 30)); HBRUSH hDoorBrush = CreateSolidBrush(RGB(100, 60, 30)); SelectObject(hdc, hDoorBrush); Rectangle(hdc, x + 80, y - 60, x + 120, y); Rectangle(hdc, x + 80, y - 60, x + 120, y);
HBRUSH hWindowBrush = CreateSolidBrush(RGB(255, 255, 150)); 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); 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) { 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) { 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)) { 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); initgraph(640, 480);
int viewWidth = 640; int viewHeight = 480; int depth = 200;104 行未改动全部展开