EasyX绘制透明背景图片与屏幕刷新

环境:VS2022+EasyX
使用show()函数(XOR叠加)的方式绘制背景透明的图片,第一个putimage输入原码图,第二个putimage输入掩码图,运行一开始图片是正常显示的,为什么移动后图片变得很亮,应该如何修改实现游戏画面的刷新?


void Player::show()
{
    putimage(x, y, &img_Player[direction], NOTSRCERASE);
    putimage(x, y, &img_Player[direction + 4], SRCINVERT);
}

void Player::Move()
{
    int step = 5;
    outtextxy(win_width/2, win_height/2, "YES");
    if ((GetAsyncKeyState('A') & 0x8000)) {//a
        x -= step;
    }
    if ((GetAsyncKeyState('D') & 0x8000)) {//d
        x += step;
    }
}

//int main
    Player P;
    P.show();
    P.initStatus();
    while (true) {
        P.Move();
        BeginBatchDraw();
        P.DrawStatus();
        P.show();
        FlushBatchDraw();          // 刷新画面
        Sleep(100);
    }
    EndBatchDraw();

正常显示:

img


移动后显示:

img


原码图和掩码图:

img

答案借鉴c知道。
在你的代码中,使用了EasyX库的putimage函数来绘制背景透明的图片。在show函数中,你使用了XOR叠加的方式来显示图片,这样会导致移动后图片变得很亮。

这是因为每次移动后,你没有清除之前绘制的图片,而是直接叠加新的图片。所以在每次移动前,你需要先清除上一帧绘制的图片。

你可以在Move函数中,在移动之前添加以下代码来清除之前的图片:

putimage(x, y, &img_Player[direction], NOTSRCERASE);
putimage(x, y, &img_Player[direction + 4], SRCINVERT);

同时,在show函数中,你可以将putimage函数的参数调整为SRCINVERTNOTSRCERASE,这样就可以避免图片变得很亮:

void Player::show()
{
    putimage(x, y, &img_Player[direction], SRCINVERT);
    putimage(x, y, &img_Player[direction + 4], NOTSRCERASE);
}

这样,在移动时就会正确地刷新游戏画面,并且背景透明的图片不会变得很亮。

希望这个解决方案对你有帮助!如果你有任何其他问题,请随时问我。

每次移动后要用背景覆盖一次角色位置的内容