这可能不是最快的方法,但它非常轻量级且易于使用。图像以整数数组返回,包含RGB颜色。
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <Windows.h>
int* screenshot(int& width, int& height) {
HDC hdc = GetDC(NULL);
HDC cdc = CreateCompatibleDC(hdc);
height = (int)GetSystemMetrics(SM_CYVIRTUALSCREEN);
width = 16*height/9;
HBITMAP hbitmap = CreateCompatibleBitmap(hdc, width, height);
SelectObject(cdc, hbitmap);
BITMAPINFOHEADER bmi = { 0 };
bmi.biSize = sizeof(BITMAPINFOHEADER);
bmi.biPlanes = 1;
bmi.biBitCount = 32;
bmi.biWidth = width;
bmi.biHeight = -height;
bmi.biCompression = BI_RGB;
bmi.biSizeImage = 3*width*height;
BitBlt(cdc, 0, 0, width, height, hdc, 0, 0, SRCCOPY);
ReleaseDC(NULL, hdc);
int* image = new int[width*height];
GetDIBits(cdc, hbitmap, 0, height, image, (BITMAPINFO*)&bmi, DIB_RGB_COLORS);
DeleteObject(hbitmap);
DeleteDC(cdc);
return image;
}
以上代码结合了这个答案和这个答案。
如何使用的示例:
int main() {
int width=0, height=0;
int* image = screenshot(width, height);
const int x=0, y=0;
const int color = image[x+y*width];
const int red = (color>>16)&255;
const int green = (color>> 8)&255;
const int blue = color &255;
delete[] image;
}
MovePlayerLeft()
。同时,您还需要记录按键和其他输入的时间和持续时间。然后,当您处于回放模式时,您只需忽略输入,而是读取记录的数据。如果在数据中看到左键按下,则调用MovePlayerLeft()
。 - Benjamin Lindley