无聊至极,弄了一个控制台游标模块,可用于写贪食蛇游戏
test_Console.exe.zip
(8 KB)
附件为EXE可运行程序,不用解压,修改文件后缀即可。
以下是运行图样:
" border="0" />
[ 本帖最后由 TonyDeng 于 2011-10-7 02:51 编辑 ]
#include "myTools.h" #include "Console.h" #include <conio.h> void Block_Move(const WCHAR* block, COORD& position, USHORT& direction, DWORD time); Console console; void main(void) { const WCHAR block[] = L"■"; const DWORD time(120); COORD position = {0, 0}; USHORT direction(6); console.SetTitle(L"控制台按键测试程序"); console.ClearScreen(BACKGROUND_BLUE); console.SetTextColor(FOREGROUND_WHITE | BACKGROUND_BLUE); console.WriteText(L"←↑→↓控制方块运动方向,PgUp加速,PgDn减速,Esc键结束", console.GetMaxRow(), 0); console.SetTextColor(FOREGROUND_YELLOW | BACKGROUND_BLUE); console.SetCursor(false); Block_Move(block, position, direction, time); } void Block_Move(const WCHAR* block, COORD& position, USHORT& direction, DWORD time) { int keycode; while (true) { if (_kbhit()) { keycode = console.ReadKey(); if (keycode == K_ESC) { break; } switch (keycode) { case K_UP: direction = 8; break; case K_DOWN: direction = 2; break; case K_LEFT: direction = 4; break; case K_RIGHT: direction = 6; break; case K_PGUP: time -= 50; if (time < 0) { time = 0; } break; case K_PGDN: time += 50; if (time > 1000) { time = 1000; } break; default: break; } } console.WriteText(block, position.Y, position.X); Sleep(time); console.WriteText(L" ", position.Y, position.X); switch (direction) { case 2: ++position.Y; if (position.Y > console.GetMaxRow() - 1) { position.Y = 0; } break; case 4: --position.X; if (position.X < 0) { position.X = console.GetMaxCol() - 1; } break; case 6: ++position.X; if (position.X > console.GetMaxCol()) { position.X = 0; } break; case 8: --position.Y; if (position.Y < 0) { position.Y = console.GetMaxRow() - 1; } break; default: break; } console.WriteText(block, position.Y, position.X); } }